Skip to content

Commit c460aa1

Browse files
committed
In docs, presume a more traditional Flask-SQLAlchemy app structure
1 parent 4da7ed5 commit c460aa1

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

README.md

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,22 @@ allowing any method from the codebase to run inside a test with the assurance
7676
that 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

8989
db = flask_sqlalchemy.SQLAlchemy()
9090
engine = sqlalchemy.create_engine('db_connection_string')
9191
```
9292

9393
```python
94-
# api/models.py
94+
# In models.py
9595

9696
class 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

110110
def 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)
121121
to **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
127127
def test_db_update():
@@ -279,20 +279,30 @@ any database updates performed by the objects get rolled back at the end of
279279
the test.
280280

281281
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**.
282+
of standard Python import paths, like `database.engine`. This property is **optional**.
283283

284284
Example:
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

291299
To 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
304314
the test.
305315

306316
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**.
317+
of standard Python import paths, like `database.db.session`. This property is **optional**.
308318

309319
Example:
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

316334
To 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

325345
The `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),
328348
replacing 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

331353
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**.
354+
of standard Python import paths, like `database.WorkerSessionmaker`. This property is **optional**.
333355

334356
Example:
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

341369
To 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.
430458
Example:
431459

432460
```python
433-
from api.database import db
461+
from database import db
434462

435463
@pytest.mark.transactional
436464
def test_db_update():

transactions/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def db_session(_engine, _session, _transaction):
170170
can interact with the database in the tests.
171171
172172
Use this fixture in tests when you would like to use the SQLAlchemy ORM
173-
API, just as you might use the `api.database.db.session` object.
173+
API, just as you might use a SQLAlchemy Session object.
174174
'''
175175
return _session
176176

@@ -183,6 +183,6 @@ def db_engine(_engine, _session, _transaction):
183183
transactional Engine object that can interact with the database in the tests.
184184
185185
Use this fixture in tests when you would like to run raw SQL queries using the
186-
SQLAlchemy Engine API, just as you might use the `api.database.engine` object.
186+
SQLAlchemy Engine API.
187187
'''
188188
return _engine

0 commit comments

Comments
 (0)