@@ -16,14 +16,12 @@ transactions using Flask and SQLAlchemy.
1616 - [ Configuration] ( #configuration )
1717 - [ Conftest setup] ( #conftest-setup )
1818 - [ Test configuration (.ini or .cfg file)] ( #test-configuration-ini-or-cfg-file )
19- - [ ` db-connection-string ` ] ( #db-connection-string )
2019 - [ ` mocked-engines ` ] ( #mocked-engines )
2120 - [ ` mocked-sessions ` ] ( #mocked-sessions )
2221 - [ ` mocked-sessionmakers ` ] ( #mocked-sessionmakers )
2322 - [ Fixtures] ( #fixtures )
2423 - [ ` db_session ` ] ( #db_session )
2524 - [ ` db_engine ` ] ( #db_engine )
26- - [ ` module_engine ` ] ( #module_engine )
2725 - [ Using the ` transactional ` mark] ( #using-the-transactional-mark )
2826- [ ** Development** ] ( #development )
2927 - [ Running the tests] ( #running-the-tests )
@@ -250,32 +248,38 @@ def _db(database):
250248
251249### Test configuration (.ini or .cfg file)
252250
253- This plugin requires that you set up a test configuration file with a few
254- specific properties under the ` [pytest] ` section. For basic background on pytest
255- configuration, see the [ pytest docs] ( https://docs.pytest.org/en/latest/customize.html#adding-default-options ) .
251+ This plugin allows you to configure a few different properties in the test
252+ configuration file in order to handle the specific database connection needs
253+ of an app. For basic background on setting up pytest configuration files, see
254+ the [ pytest docs] ( https://docs.pytest.org/en/latest/customize.html#adding-default-options ) .
256255
257- #### ` db-connection-string `
256+ All three configuration properties ([ ` mocked-engines ` ] ( #mocked-engines ) ,
257+ [ ` mocked-sessions ` ] ( #mocked-sessions ) , and [ ` mocked-sessionmakers ` ] ( #mocked-sessionmakers ) )
258+ work by ** [ patching] ( https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch )
259+ one or more specified objects during a test** , replacing them with equivalent objects whose
260+ database interactions will run inside of a transaction and ultimately be
261+ rolled back when the test exits. Using these patches, you can call methods from
262+ your codebase that alter database state with the knowledge that no changes will persist
263+ beyond the body of the test.
258264
259- The ` db-connection-string ` property allows the plugin to access a test
260- database. ** This property is required. **
265+ The configured patches are applied in tests where either one of two conditions
266+ are true:
261267
262- Example:
263-
264- ``` ini
265- [pytest]
266- db-connection-string =postgresql://postgres@localhost:5432/pytest_test
267- ```
268+ 1 . a transactional fixture ([ ` db_session ` ] ( #db_session ) or [ ` db_engine ` ] ( #db_engine ) )
269+ is listed as a dependency, or
270+ 2 . the [ ` @pytest.mark.transactional ` ] ( #using-the-transactional-mark )
271+ mark is active.
268272
269273#### ` mocked-engines `
270274
271275The ` mocked-engines ` property directs the plugin to [ patch] ( https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch )
272276objects in your codebase, typically SQLAlchemy [ Engine] ( http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Engine )
273- instances, replacing them with the [ ` db_engine ` fixture] ( #db_engine ) in tests where either
274- of the transactional fixtures ([ ` db_session ` ] ( #db_session ) or [ ` db_engine ` ] ( #db_engine ) )
275- are listed as dependencies. The values for this property should be formatted as standard
276- Python import paths, like ` api.database.engine ` .
277+ instances, replacing them with the [ ` db_engine ` fixture] ( #db_engine ) such that
278+ any database updates performed by the objects get rolled back at the end of
279+ the test.
277280
278- This property is optional.
281+ The 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** .
279283
280284Example:
281285
@@ -295,12 +299,12 @@ mocked-engines=api.database.engine api.database.second_engine
295299
296300The ` mocked-sessions ` property directs the plugin to [ patch] ( https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch )
297301objects in your codebase, typically SQLAlchemy [ Session] ( http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Engine )
298- instances, replacing them with the [ ` db_session ` ] ( #db_session ) fixture in tests where either
299- of the transactional fixtures ([ ` db_session ` ] ( #db_session ) or [ ` db_engine ` ] ( #db_engine ) ) are
300- listed as dependencies. Values for this property should be formatted as standard Python import
301- paths, like ` api.database.db.session ` .
302+ instances, replacing them with the [ ` db_session ` ] ( #db_session ) fixture such that
303+ any database updates performed by the objects get rolled back at the end of
304+ the test.
302305
303- This property is optional.
306+ The 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** .
304308
305309Example:
306310
@@ -319,13 +323,13 @@ mocked-sessions=api.database.db.session api.database.second_db.session
319323#### ` mocked-sessionmakers `
320324
321325The ` mocked-sessionmakers ` property directs the plugin to [ patch] ( https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch )
322- objects in your codebase, typically SQLAlchemy [ sessionmaker] ( http://docs.sqlalchemy.org/en/latest/orm/session_api.html?highlight=sessionmaker#sqlalchemy.orm.session.sessionmaker )
323- factories, replacing them with a mocked class that will return the [ ` db_session ` ] ( #db_session ) fixture
324- in tests where either of the transactional fixtures ([ ` db_session ` ] ( #db_session ) or [ ` db_engine ` ] ( #db_engine ) )
325- are listed as dependencies. Values for this property should be formatted as standard Python import paths,
326- like ` api.database.WorkerSessionmaker ` .
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 ) ,
328+ replacing them with a mocked class that will return the transactional
329+ [ ` db_session ` ] ( #db_session ) fixture.
327330
328- This property is optional.
331+ The 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** .
329333
330334Example:
331335
@@ -345,9 +349,8 @@ mocked-sessionmakers=api.database.WorkerSessionmaker api.database.SecondWorkerSe
345349
346350This plugin provides two fixtures for performing database updates inside nested
347351transactions that get rolled back at the end of a test: [ ` db_session ` ] ( #db_session ) and
348- [ ` db_engine ` ] ( #db_engine ) . In addition, the plugin provides a fixture for direct database
349- changes that will not get rolled back: [ ` module_engine ` ] ( #module_engine ) . This fixture can be
350- useful for setting up module- or session-scoped state.
352+ [ ` db_engine ` ] ( #db_engine ) . The fixtures provide similar functionality, but
353+ with different APIs.
351354
352355### ` db_session `
353356
@@ -409,30 +412,6 @@ def test_transaction_doesnt_persist(db_engine):
409412 assert row_name != ' testing'
410413```
411414
412- ### ` module_engine `
413-
414- In contrast to [ ` db_session ` ] ( #db_session ) and [ ` db_engine ` ] ( #db_engine ) ,
415- the ` module_engine ` fixture does not wrap its test in a database transaction.
416- Instead, this fixture returns a SQLAlchemy ` Engine ` object that can be used to
417- set up persistent state in tests or fixtures. Its API is identical to the
418- [ SQLAlchemy ` Engine ` API] ( http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Engine ) .
419-
420- Listing this fixture as a dependency ** will not** activate any mocks that are specified
421- by the configuration properties [ ` mocked-engines ` ] ( #mocked-engines ) , [ ` mocked-sessions ` ] ( #mocked-sessions ) ,
422- or [ ` mocked-sessionmakers ` ] ( #mocked-sessionmakers ) in a configuration file.
423-
424- Example:
425-
426- ``` python
427- def test_module_engine (module_engine ):
428- with module_engine.begin() as conn:
429- row = conn.execute(''' UPDATE table SET name = 'testing' WHERE id = 1''' )
430-
431- def test_module_engine_changes_persist (db_engine ):
432- row_name = db_engine.execute(''' SELECT name FROM table WHERE id = 1''' ).fetchone()[0 ]
433- assert row_name == ' testing'
434- ```
435-
436415## Using the ` transactional ` mark
437416
438417If you want to enforce transactional context but you don't need to use either
0 commit comments