@@ -282,18 +282,19 @@ Sample below is simplified session fixture from
282282
283283.. code-block :: python
284284
285+ from psycopg import Connection
285286 from sqlalchemy import create_engine
286- from sqlalchemy.orm import scoped_session, sessionmaker
287+ from sqlalchemy.orm import scoped_session, sessionmaker, Session
287288 from sqlalchemy.pool import NullPool
288289 from zope.sqlalchemy import register
289290
290291
291292 @pytest.fixture
292- def db_session (postgresql ) :
293+ def db_session (postgresql : Connection) -> Iterator[Session] :
293294 """ Session for SQLAlchemy."""
294295 from pyramid_fullauth.models import Base
295296
296- connection = f ' postgresql+psycopg2 :// { postgresql.info.user} :@ { postgresql.info.host} : { postgresql.info.port} / { postgresql.info.dbname} '
297+ connection = f ' postgresql+psycopg :// { postgresql.info.user} :@ { postgresql.info.host} : { postgresql.info.port} / { postgresql.info.dbname} '
297298
298299 engine = create_engine(connection, echo = False , poolclass = NullPool)
299300 pyramid_basemodel.Session = scoped_session(sessionmaker(extension = ZopeTransactionExtension()))
@@ -307,7 +308,7 @@ Sample below is simplified session fixture from
307308
308309
309310 @pytest.fixture
310- def user (db_session ) :
311+ def user (db_session : Session) -> User :
311312 """ Test user fixture."""
312313 from pyramid_fullauth.models import User
313314 from tests.tools import DEFAULT_USER
@@ -318,7 +319,7 @@ Sample below is simplified session fixture from
318319 return new_user
319320
320321
321- def test_remove_last_admin (db_session , user ) :
322+ def test_remove_last_admin (db_session : pyramid_basemodel.Session , user : User) -> None :
322323 """
323324 Sample test checks internal login, but shows usage in tests with SQLAlchemy
324325 """
@@ -346,11 +347,16 @@ For this import DatabaseJanitor and use its init and drop methods:
346347
347348.. code-block :: python
348349
350+ from typing import Iterator
351+
352+ import psycopg
353+ from psycopg import Connection
349354 import pytest
350355 from pytest_postgresql.janitor import DatabaseJanitor
356+ from pytest_postgresql.executor import PostgreSQLExecutor
351357
352358 @pytest.fixture
353- def database (postgresql_proc ) :
359+ def database (postgresql_proc : PostgreSQLExecutor) -> Iterator[Connection] :
354360 # variable definition
355361
356362 janitor = DatabaseJanitor(
@@ -362,7 +368,7 @@ For this import DatabaseJanitor and use its init and drop methods:
362368 password = " secret_password" ,
363369 )
364370 janitor.init()
365- yield psycopg2 .connect(
371+ yield psycopg .connect(
366372 dbname = " my_test_database" ,
367373 user = postgresql_proc.user,
368374 password = " secret_password" ,
@@ -375,11 +381,16 @@ or use it as a context manager:
375381
376382.. code-block :: python
377383
384+ from typing import Iterator
385+
386+ import psycopg
387+ from psycopg import Connection
378388 import pytest
379389 from pytest_postgresql.janitor import DatabaseJanitor
390+ from pytest_postgresql.executor import PostgreSQLExecutor
380391
381392 @pytest.fixture
382- def database (postgresql_proc ) :
393+ def database (postgresql_proc : PostgreSQLExecutor) -> Iterator[Connection] :
383394 # variable definition
384395
385396 with DatabaseJanitor(
@@ -390,7 +401,7 @@ or use it as a context manager:
390401 version = postgresql_proc.version,
391402 password = " secret_password" ,
392403 ):
393- yield psycopg2 .connect(
404+ yield psycopg .connect(
394405 dbname = " my_test_database" ,
395406 user = postgresql_proc.user,
396407 password = " secret_password" ,
@@ -425,14 +436,14 @@ In tests, make sure that all your tests are using **postgresql_noproc** fixture
425436
426437.. code-block :: python
427438
439+ from psycopg import Connection
428440 from pytest_postgresql import factories
429441
430-
431442 postgresql_in_docker = factories.postgresql_noproc()
432443 postgresql = factories.postgresql(" postgresql_in_docker" , dbname = " test" )
433444
434445
435- def test_postgres_docker (postgresql ) :
446+ def test_postgres_docker (postgresql : Connection) -> None :
436447 """ Run test."""
437448 cur = postgresql.cursor()
438449 cur.execute(" CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);" )
@@ -453,9 +464,10 @@ your custom postgresql process fixture:
453464
454465.. code-block :: python
455466
467+ import psycopg
456468 import pytest_postgresql.factories
457- def load_database (** kwargs ) :
458- db_connection: connection = psycopg2 .connect(** kwargs)
469+ def load_database (** kwargs : str ) -> None :
470+ db_connection: psycopg.Connection = psycopg .connect(** kwargs)
459471 with db_connection.cursor() as cur:
460472 cur.execute(" CREATE TABLE stories (id serial PRIMARY KEY, name varchar);" )
461473 cur.execute(
@@ -486,8 +498,14 @@ How to use SQLAlchemy for common initialisation:
486498
487499.. code-block :: python
488500
489- def load_database (** kwargs ):
490- connection = f " postgresql+psycopg2:// { kwargs[' user' ]} :@ { kwargs[' host' ]} : { kwargs[' port' ]} / { kwargs[' dbname' ]} "
501+ from typing import Iterator
502+
503+ import psycopg
504+ from sqlalchemy.orm import Session
505+
506+ def load_database (** kwargs : str ) -> None :
507+ from your_package import Base
508+ connection = f " postgresql+psycopg:// { kwargs[' user' ]} :@ { kwargs[' host' ]} : { kwargs[' port' ]} / { kwargs[' dbname' ]} "
491509 engine = create_engine(connection)
492510 Base.metadata.create_all(engine)
493511 session = scoped_session(sessionmaker(bind = engine))
@@ -499,8 +517,8 @@ How to use SQLAlchemy for common initialisation:
499517 postgresql = factories.postgresql(' postgresql_proc' ) # still need to check if this is actually needed or not
500518
501519 @pytest.fixture
502- def dbsession (postgresql ) :
503- connection = f ' postgresql+psycopg2 :// { postgresql.info.user} :@ { postgresql.info.host} : { postgresql.info.port} / { postgresql.info.dbname} '
520+ def dbsession (postgresql : psycopg.Connection) -> Iterator[Session] :
521+ connection = f ' postgresql+psycopg :// { postgresql.info.user} :@ { postgresql.info.host} : { postgresql.info.port} / { postgresql.info.dbname} '
504522 engine = create_engine(connection)
505523
506524 session = scoped_session(sessionmaker(bind = engine))
0 commit comments