1111
1212
1313@pytest .fixture (scope = "session" )
14- def engine (request : FixtureRequest , sqlalchemy_connect_url : Optional [str ], app_config : Optional [Dict [str , str ]]) -> Engine :
14+ def engine (
15+ request : FixtureRequest ,
16+ sqlalchemy_connect_url : Optional [str ],
17+ app_config : Optional [Dict [str , str ]],
18+ ) -> Engine :
1519 """Engine configuration.
1620 See http://docs.sqlalchemy.org/en/latest/core/engines.html
1721 for more details.
1822
1923 :sqlalchemy_connect_url: Connection URL to the database. E.g
20- postgresql://scott:tiger@localhost:5432/mydatabase
24+ postgresql://scott:tiger@localhost:5432/mydatabase
2125 :app_config: Path to a ini config file containing the sqlalchemy.url
2226 config variable in the DEFAULT section.
2327 :returns: Engine instance
@@ -38,15 +42,17 @@ def engine(request: FixtureRequest, sqlalchemy_connect_url: Optional[str], app_c
3842 engine = create_engine (url ) # override engine
3943
4044 def fin () -> None :
41- print ("Disposing engine" )
45+ print ("Disposing engine" )
4246 engine .dispose ()
4347
4448 request .addfinalizer (fin )
4549 return engine
4650
4751
4852@pytest .fixture (scope = "session" )
49- def db_schema (request : FixtureRequest , engine : Engine , sqlalchemy_manage_db : bool , sqlalchemy_keep_db : bool ) -> Optional [None ]:
53+ def db_schema (
54+ request : FixtureRequest , engine : Engine , sqlalchemy_manage_db : bool , sqlalchemy_keep_db : bool
55+ ) -> Optional [None ]:
5056 if not sqlalchemy_manage_db :
5157 return
5258
@@ -58,8 +64,9 @@ def db_schema(request: FixtureRequest, engine: Engine, sqlalchemy_manage_db: boo
5864 sqlalchemy_utils .functions .create_database (engine .url )
5965
6066 if not sqlalchemy_keep_db :
67+
6168 def fin () -> None :
62- print ("Tearing down DB" )
69+ print ("Tearing down DB" )
6370 sqlalchemy_utils .functions .drop_database (engine .url )
6471
6572 request .addfinalizer (fin )
@@ -70,7 +77,7 @@ def connection(request: FixtureRequest, engine: Engine, db_schema: Optional[None
7077 connection = engine .connect ()
7178
7279 def fin () -> None :
73- print ("Closing connection" )
80+ print ("Closing connection" )
7481 connection .close ()
7582
7683 request .addfinalizer (fin )
@@ -84,7 +91,7 @@ def transaction(request: FixtureRequest, connection: Connection) -> Connection:
8491 transaction = connection .begin ()
8592
8693 def fin () -> None :
87- print ("Rollback" )
94+ print ("Rollback" )
8895 transaction .rollback ()
8996
9097 request .addfinalizer (fin )
@@ -94,6 +101,7 @@ def fin() -> None:
94101@pytest .fixture ()
95102def dbsession (request : FixtureRequest , connection : Connection ) -> Any :
96103 from sqlalchemy .orm import sessionmaker
104+
97105 return sessionmaker ()(bind = connection )
98106
99107
@@ -136,22 +144,33 @@ def app_config(request: FixtureRequest) -> Optional[Mapping[str, str]]:
136144
137145
138146def pytest_addoption (parser : Parser ) -> None :
139- parser .addoption ("--sqlalchemy-connect-url" , action = "store" ,
140- default = None ,
141- help = "Name of the database to connect to" )
142-
143- parser .addoption ("--sqlalchemy-config-file" , action = "store" ,
144- default = None ,
145- help = "Path to a config file containing the "
146- "'sqlalchemy.url' variable in the DEFAULT section "
147- "of a ini file to define the connect "
148- "url." )
149-
150- parser .addoption ("--sqlalchemy-manage-db" , action = "store_true" ,
151- default = None ,
152- help = "Automatically creates and drops database" )
153-
154- parser .addoption ("--sqlalchemy-keep-db" , action = "store_true" ,
155- default = None ,
156- help = "Do not delete database after test suite, "
157- "allowing for its reuse." )
147+ parser .addoption (
148+ "--sqlalchemy-connect-url" ,
149+ action = "store" ,
150+ default = None ,
151+ help = "Name of the database to connect to" ,
152+ )
153+
154+ parser .addoption (
155+ "--sqlalchemy-config-file" ,
156+ action = "store" ,
157+ default = None ,
158+ help = "Path to a config file containing the "
159+ "'sqlalchemy.url' variable in the DEFAULT section "
160+ "of a ini file to define the connect "
161+ "url." ,
162+ )
163+
164+ parser .addoption (
165+ "--sqlalchemy-manage-db" ,
166+ action = "store_true" ,
167+ default = None ,
168+ help = "Automatically creates and drops database" ,
169+ )
170+
171+ parser .addoption (
172+ "--sqlalchemy-keep-db" ,
173+ action = "store_true" ,
174+ default = None ,
175+ help = "Do not delete database after test suite, allowing for its reuse." ,
176+ )
0 commit comments