Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,19 @@ Sample below is simplified session fixture from

.. code-block:: python

from psycopg import Connection
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.orm import scoped_session, sessionmaker, Session
from sqlalchemy.pool import NullPool
from zope.sqlalchemy import register


@pytest.fixture
def db_session(postgresql):
def db_session(postgresql: Connection) -> Iterator[Session]:
"""Session for SQLAlchemy."""
from pyramid_fullauth.models import Base

connection = f'postgresql+psycopg2://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}'
connection = f'postgresql+psycopg://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}'

engine = create_engine(connection, echo=False, poolclass=NullPool)
pyramid_basemodel.Session = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Expand All @@ -307,7 +308,7 @@ Sample below is simplified session fixture from


@pytest.fixture
def user(db_session):
def user(db_session: Session) -> User:
"""Test user fixture."""
from pyramid_fullauth.models import User
from tests.tools import DEFAULT_USER
Expand All @@ -318,7 +319,7 @@ Sample below is simplified session fixture from
return new_user


def test_remove_last_admin(db_session, user):
def test_remove_last_admin(db_session: pyramid_basemodel.Session, user: User) -> None:
"""
Sample test checks internal login, but shows usage in tests with SQLAlchemy
"""
Expand Down Expand Up @@ -346,11 +347,16 @@ For this import DatabaseJanitor and use its init and drop methods:

.. code-block:: python

from typing import Iterator

import psycopg
from psycopg import Connection
import pytest
from pytest_postgresql.janitor import DatabaseJanitor
from pytest_postgresql.executor import PostgreSQLExecutor

@pytest.fixture
def database(postgresql_proc):
def database(postgresql_proc: PostgreSQLExecutor) -> Iterator[Connection]:
# variable definition

janitor = DatabaseJanitor(
Expand All @@ -362,7 +368,7 @@ For this import DatabaseJanitor and use its init and drop methods:
password="secret_password",
)
janitor.init()
yield psycopg2.connect(
yield psycopg.connect(
dbname="my_test_database",
user=postgresql_proc.user,
password="secret_password",
Expand All @@ -375,11 +381,16 @@ or use it as a context manager:

.. code-block:: python

from typing import Iterator

import psycopg
from psycopg import Connection
import pytest
from pytest_postgresql.janitor import DatabaseJanitor
from pytest_postgresql.executor import PostgreSQLExecutor

@pytest.fixture
def database(postgresql_proc):
def database(postgresql_proc: PostgreSQLExecutor) -> Iterator[Connection]:
# variable definition

with DatabaseJanitor(
Expand All @@ -390,7 +401,7 @@ or use it as a context manager:
version=postgresql_proc.version,
password="secret_password",
):
yield psycopg2.connect(
yield psycopg.connect(
dbname="my_test_database",
user=postgresql_proc.user,
password="secret_password",
Expand Down Expand Up @@ -425,14 +436,14 @@ In tests, make sure that all your tests are using **postgresql_noproc** fixture

.. code-block:: python

from psycopg import Connection
from pytest_postgresql import factories


postgresql_in_docker = factories.postgresql_noproc()
postgresql = factories.postgresql("postgresql_in_docker", dbname="test")


def test_postgres_docker(postgresql):
def test_postgres_docker(postgresql: Connection) -> None:
"""Run test."""
cur = postgresql.cursor()
cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
Expand All @@ -453,9 +464,10 @@ your custom postgresql process fixture:

.. code-block:: python

import psycopg
import pytest_postgresql.factories
def load_database(**kwargs):
db_connection: connection = psycopg2.connect(**kwargs)
def load_database(**kwargs: str) -> None:
db_connection: psycopg.Connection = psycopg.connect(**kwargs)
with db_connection.cursor() as cur:
cur.execute("CREATE TABLE stories (id serial PRIMARY KEY, name varchar);")
cur.execute(
Expand Down Expand Up @@ -486,8 +498,14 @@ How to use SQLAlchemy for common initialisation:

.. code-block:: python

def load_database(**kwargs):
connection = f"postgresql+psycopg2://{kwargs['user']}:@{kwargs['host']}:{kwargs['port']}/{kwargs['dbname']}"
from typing import Iterator

import psycopg
from sqlalchemy.orm import Session

def load_database(**kwargs: str) -> None:
from your_package import Base
connection = f"postgresql+psycopg://{kwargs['user']}:@{kwargs['host']}:{kwargs['port']}/{kwargs['dbname']}"
engine = create_engine(connection)
Base.metadata.create_all(engine)
session = scoped_session(sessionmaker(bind=engine))
Expand All @@ -499,8 +517,8 @@ How to use SQLAlchemy for common initialisation:
postgresql = factories.postgresql('postgresql_proc') # still need to check if this is actually needed or not

@pytest.fixture
def dbsession(postgresql):
connection = f'postgresql+psycopg2://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}'
def dbsession(postgresql: psycopg.Connection) -> Iterator[Session]:
connection = f'postgresql+psycopg://{postgresql.info.user}:@{postgresql.info.host}:{postgresql.info.port}/{postgresql.info.dbname}'
engine = create_engine(connection)

session = scoped_session(sessionmaker(bind=engine))
Expand Down
1 change: 1 addition & 0 deletions newsfragments/1233.docs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve code examples by adding types
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ showcontent = true
name = "Bugfixes"
showcontent = true

[tool.towncrier.fragment.docs]
name = "Documentation"
showcontent = true

[tool.towncrier.fragment.deprecate]
name = "Deprecations"
showcontent = true
Expand Down
Loading