fake_session_maker
The fake_session_maker is a SQLAlchemy and Pytest-based package designed to facilitate
database testing by replacing a classic SQLAlchemy SessionMaker context manager.
Features
- Replaces the SQLAlchemy
SessionMakercontext manager with a "read-only" session during tests. - Rollbacks database state at the end of each test, ensuring isolation between tests.
- Simple fixture-based usage integrates smoothly with your Pytest suite.
Drawbacks
Code that plan to be tested using fake_session_maker have the following limitations:
- Prevent the use of factory_boy automated build and bulk. Each object needs to be created and added to the session manually.
Installation
pip install fake_session_maker
Usage
Define the fixture
Below is an example of how to use fake_session_maker in a pytest fixture:
import pytest
from fake_session_maker import fsm
# Assuming Namespace is where the session_maker is defined
@pytest.fixture
def fake_session_maker():
with fsm(
db_url="sqlite:///tests/test.sqlite",
namespace=Namespace,
symbol_name="session_maker",
) as fake_session_maker_:
yield fake_session_maker_
# Now, you can use fake_session_maker in your tests
Use the fixture
Below is an example of how to use fake_session_maker fixture in a test:
# Each test will have a fresh database, empty of any data
@pytest.mark.parametrize("name", ["jane", "joe"])
def test_create_example(fake_session_maker, name):
result = create_example('test')
assert result == 'success'
with fake_session_maker() as session:
# Each time we check, only the data created in this test will be present
assert session.query(models.User).count() == 1
See the tests.test_fsm.py directory for a full example.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for more details.
Testing
If you want to run the tests locally, you can follow instructions here: CONTRIBUTING.md #testing.
License
Distributed under the MIT License. See LICENSE for more information.
Metadata
Release files for fake-session-maker 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fake_session_maker-0.3.0.tar.gz | 338.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fake_session_maker-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 344.0 kB
Release files / fake_session_maker-0.3.0.tar.gz
| Download URL | fake_session_maker-0.3.0.tar.gz |
|---|---|
| Size | 338.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bf0afd5eb4083481278037a29b9434fd2ea309272e1b19906ad4c2ef60d9d914
|
|
BLAKE2b-256 checksum How to use checksums |
aff7ab7a8da10d904d2f0884a271be99c1aae82a2609fa37cd95ee35ecfc9d3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / fake_session_maker-0.3.0-py3-none-any.whl
| Download URL | fake_session_maker-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
756dc26c00f549f232a29107593216e59946fe7fe9758c4c66dbfa65d60b21ee
|
|
BLAKE2b-256 checksum How to use checksums |
85af6affc74006484bc6095fad2e153b29284bc50db852b4eb761bdef3a9136b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|