Skip to content

Commit 188a66f

Browse files
committed
Purge transactional mark from the docs and codebase
1 parent 4acdbb1 commit 188a66f

File tree

5 files changed

+12
-128
lines changed

5 files changed

+12
-128
lines changed

README.md

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ transactions using [Flask-SQLAlchemy](http://flask-sqlalchemy.pocoo.org/latest/)
2222
- [Fixtures](#fixtures)
2323
- [`db_session`](#db_session)
2424
- [`db_engine`](#db_engine)
25-
- [Using the `transactional` mark](#using-the-transactional-mark)
2625
- [**Development**](#development)
2726
- [Running the tests](#running-the-tests)
2827
- [Acknowledgements](#acknowledgements)
@@ -117,25 +116,6 @@ def test_transaction_doesnt_persist(db_session):
117116
assert row.name != 'testing'
118117
```
119118

120-
Use the [`@pytest.mark.transactional` mark](#using-the-transactional-mark)
121-
to **enforce that a test gets run inside a transaction**:
122-
123-
```python
124-
from database import db
125-
126-
@pytest.mark.transactional
127-
def test_db_update():
128-
row = db.session.query(Table).get(1)
129-
row.name = 'testing'
130-
db.session.add(row)
131-
db.session.commit()
132-
133-
@pytest.mark.transactional
134-
def test_db_update_doesnt_persist():
135-
row = db.session.query(Table).get(1)
136-
assert row.name != 'testing'
137-
```
138-
139119
# Usage
140120

141121
## Installation
@@ -262,13 +242,9 @@ rolled back when the test exits. Using these patches, you can call methods from
262242
your codebase that alter database state with the knowledge that no changes will persist
263243
beyond the body of the test.
264244

265-
The configured patches are applied in tests where either one of two conditions
266-
are true:
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.
245+
The configured patches are only applied in tests where a transactional fixture
246+
(either [`db_session`](#db_session) or [`db_engine`](#db_engine)) is included
247+
in the test function arguments.
272248

273249
#### `mocked-engines`
274250

@@ -386,9 +362,10 @@ The `db_session` fixture allows you to perform direct updates that will be
386362
rolled back when the test exits. It exposes the same API as [SQLAlchemy's
387363
`scoped_session` object](http://docs.sqlalchemy.org/en/latest/orm/contextual.html#sqlalchemy.orm.scoping.scoped_session).
388364

389-
Listing this fixture as a dependency will activate any mocks that are specified
365+
Including this fixture as a function argument of a test will activate any mocks that are defined
390366
by the configuration properties [`mocked-engines`](#mocked-engines), [`mocked-sessions`](#mocked-sessions),
391-
or [`mocked-sessionmakers`](#mocked-sessionmakers) in a configuration file.
367+
or [`mocked-sessionmakers`](#mocked-sessionmakers) in the test configuration file for
368+
the duration of that test.
392369

393370
Example:
394371

@@ -423,10 +400,10 @@ Since `db_engine` is an instance of `MagicMock` with an `Engine` spec, other
423400
methods of the `Engine` API can be called, but they will not perform any useful
424401
work.
425402

426-
Listing this fixture as a dependency will activate any mocks that are specified
427-
by the configuration properties [`mocked-engines`](#mocked-engines),
428-
[`mocked-sessions`](#mocked-sessions), or [`mocked-sessionmakers`](#mocked-sessionmakers)
429-
in a configuration file.
403+
Including this fixture as a function argument of a test will activate any mocks that are defined
404+
by the configuration properties [`mocked-engines`](#mocked-engines), [`mocked-sessions`](#mocked-sessions),
405+
or [`mocked-sessionmakers`](#mocked-sessionmakers) in the test configuration file for
406+
the duration of that test.
430407

431408
Example:
432409

@@ -440,39 +417,6 @@ def test_transaction_doesnt_persist(db_engine):
440417
assert row_name != 'testing'
441418
```
442419

443-
## Using the `transactional` mark
444-
445-
If you want to enforce transactional context but you don't need to use either
446-
of the built-in transactional fixtures ([`db_session`](#db_session) or [`db_engine`](#db_engine)),
447-
you can use the **`@pytest.mark.transactional`** decorator to mark that a test should
448-
be run inside a transaction. For basic background on marks, see the [pytest
449-
documentation](https://docs.pytest.org/en/latest/mark.html).
450-
451-
Note that since this approach assumes that you'll be performing database
452-
updates using connections defined in your app, you **must** mock the
453-
appropriate connections using the configuration properties [`mocked-sessions`](#mocked-sessions),
454-
[`mocked-engines`](#mocked-engines), or [`mocked-sessionmakers`](#mocked-sessionmakers).
455-
The `transactional` mark is, in essence, a way of triggering these mocks, so the mocks
456-
themselves are necessary to create the transactional context that you expect.
457-
458-
Example:
459-
460-
```python
461-
from database import db
462-
463-
@pytest.mark.transactional
464-
def test_db_update():
465-
row = db.session.query(Table).get(1)
466-
row.name = 'testing'
467-
db.session.add(row)
468-
db.session.commit()
469-
470-
@pytest.mark.transactional
471-
def test_db_update_doesnt_persist():
472-
row = db.session.query(Table).get(1)
473-
assert row.name != 'testing'
474-
```
475-
476420
# Development
477421

478422
## Running the tests

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def readme():
1818
url='https://github.com/jeancochrane/pytest-flask-sqlalchemy-transactions',
1919
description='Run tests in transactions using pytest, Flask, and SQLalchemy.',
2020
long_description=readme(),
21+
long_description_content_type='text/markdown',
2122
license='MIT',
2223
version='0.0.1',
2324
packages=['transactions'],

tests/test_marks.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

transactions/hooks.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

transactions/plugin.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .fixtures import _db, _transaction, _engine, _session, db_session, db_engine
2-
from .hooks import pytest_collection_modifyitems
2+
33

44
def pytest_addoption(parser):
55
'''
@@ -31,17 +31,3 @@ def pytest_configure(config):
3131
config._mocked_engines = config.getini('mocked-engines')
3232
config._mocked_sessions = config.getini('mocked-sessions')
3333
config._mocked_sessionmakers = config.getini('mocked-sessionmakers')
34-
35-
36-
def pytest_load_initial_conftests(early_config, parser, args):
37-
'''
38-
Register marks.
39-
'''
40-
early_config.addinivalue_line(
41-
'markers',
42-
'transactional: Mark the test to be run inside a nested transaction, with '
43-
'any database changes rolled back at the end of the test. Note that '
44-
"running tests in transactions requires you to configure the 'mocked-sessions', "
45-
"'mocked-engines', and 'mocked-sessionmakers' properties in your test "
46-
'config file so that any objects you are using to update the database are '
47-
'appropriately replaced with their transactional equivalent.')

0 commit comments

Comments
 (0)