@@ -76,22 +76,22 @@ allowing any method from the codebase to run inside a test with the assurance
7676that any database changes made will be rolled back at the end of the test:
7777
7878``` ini
79- # tests/tox.ini
79+ # In setup.cfg
8080
8181[pytest]
82- mocked-sessions =api. database.db.session
83- mocked-engines =api. database.engine
82+ mocked-sessions =database.db.session
83+ mocked-engines =database.engine
8484```
8585
8686``` python
87- # api/ database.py
87+ # In database.py
8888
8989db = flask_sqlalchemy.SQLAlchemy()
9090engine = sqlalchemy.create_engine(' db_connection_string' )
9191```
9292
9393``` python
94- # api/ models.py
94+ # In models.py
9595
9696class Table (db .Model ):
9797 __tablename__ = ' table'
@@ -105,7 +105,7 @@ class Table(db.Model):
105105```
106106
107107``` python
108- # tests/test_api .py
108+ # In tests/test_set_name .py
109109
110110def test_set_name (db_session ):
111111 row = db_session.query(Table).get(1 )
@@ -121,7 +121,7 @@ Use the [`@pytest.mark.transactional` mark](#using-the-transactional-mark)
121121to ** enforce that a test gets run inside a transaction** :
122122
123123``` python
124- from api. database import db
124+ from database import db
125125
126126@pytest.mark.transactional
127127def test_db_update ():
@@ -279,20 +279,30 @@ any database updates performed by the objects get rolled back at the end of
279279the test.
280280
281281The value for this property should be formatted as a whitespace-separated list
282- of standard Python import paths, like ` api. database.engine` . This property is ** optional** .
282+ of standard Python import paths, like ` database.engine ` . This property is ** optional** .
283283
284284Example:
285285
286+ ``` python
287+ # In database.py
288+
289+ engine = sqlalchemy.create_engine(DATABASE_URI )
290+ ```
291+
286292``` ini
293+ # In setup.cfg
294+
287295[pytest]
288- mocked-engines =api. database.engine
296+ mocked-engines =database.engine
289297```
290298
291299To patch multiple objects at once, separate the paths with a whitespace:
292300
293301``` ini
302+ # In setup.cfg
303+
294304[pytest]
295- mocked-engines =api. database.engine api. database.second_engine
305+ mocked-engines =database.engine database.second_engine
296306```
297307
298308#### ` mocked-sessions `
@@ -304,45 +314,63 @@ any database updates performed by the objects get rolled back at the end of
304314the test.
305315
306316The value for this property should be formatted as a whitespace-separated list
307- of standard Python import paths, like ` api. database.db.session` . This property is ** optional** .
317+ of standard Python import paths, like ` database.db.session ` . This property is ** optional** .
308318
309319Example:
310320
321+ ``` python
322+ # In database.py
323+
324+ db = SQLAlchemy()
325+ ```
326+
311327``` ini
328+ # In setup.cfg
329+
312330[pytest]
313- mocked-sessions =api. database.db.session
331+ mocked-sessions =database.db.session
314332```
315333
316334To patch multiple objects at once, separate the paths with a whitespace:
317335
318336``` ini
337+ # In setup.cfg
338+
319339[pytest]
320- mocked-sessions =api. database.db.session api. database.second_db.session
340+ mocked-sessions =database.db.session database.second_db.session
321341```
322342
323343#### ` mocked-sessionmakers `
324344
325345The ` mocked-sessionmakers ` property directs the plugin to [ patch] ( https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch )
326- objects in your codebase, typically [ SQLAlchemy ` sessionmaker `
327- factories ] ( http://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=sessionmaker#sqlalchemy.orm.session.sessionmaker ) ,
346+ objects in your codebase, typically instances of [ SQLAlchemy's ` sessionmaker `
347+ factory ] ( http://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=sessionmaker#sqlalchemy.orm.session.sessionmaker ) ,
328348replacing them with a mocked class that will return the transactional
329- [ ` db_session ` ] ( #db_session ) fixture.
349+ [ ` db_session ` ] ( #db_session ) fixture. This can be useful if you have
350+ pre-configured instances of sessionmaker objects that you import in the code
351+ to spin up sessions on the fly.
330352
331353The value for this property should be formatted as a whitespace-separated list
332- of standard Python import paths, like ` api. database.WorkerSessionmaker` . This property is ** optional** .
354+ of standard Python import paths, like ` database.WorkerSessionmaker ` . This property is ** optional** .
333355
334356Example:
335357
358+ ``` python
359+ # In database.py
360+
361+ WorkerSessionmaker = sessionmaker()
362+ ```
363+
336364``` ini
337365[pytest]
338- mocked-sessionmakers =api. database.WorkerSessionmaker
366+ mocked-sessionmakers =database.WorkerSessionmaker
339367```
340368
341369To patch multiple objects at once, separate the paths with a whitespace.
342370
343371``` ini
344372[pytest]
345- mocked-sessionmakers =api. database.WorkerSessionmaker api. database.SecondWorkerSessionmaker
373+ mocked-sessionmakers =database.WorkerSessionmaker database.SecondWorkerSessionmaker
346374```
347375
348376## Fixtures
@@ -430,7 +458,7 @@ themselves are necessary to create the transactional context that you expect.
430458Example:
431459
432460``` python
433- from api. database import db
461+ from database import db
434462
435463@pytest.mark.transactional
436464def test_db_update ():
0 commit comments