Newer
Older
[](https://lab.frogg.it/dorianturba/fake_session_maker/-/releases)
[](https://lab.frogg.it/dorianturba/fake_session_maker/-/pipelines)
[](https://pypi.org/project/fake_session_maker)
[](https://pycqa.github.io/isort/)
[](https://pypi.org/project/fake_session_maker)
[](https://lab.frogg.it/dorianturba/fake_session_maker/-/blob/main/LICENSE)
[](https://github.com/psf/black)
[](https://github.com/jackdewinter/pymarkdown)
The `fake_session_maker` is a SQLAlchemy and Pytest-based package designed to facilitate
database testing by replacing a classic SQLAlchemy `SessionMaker` context manager.
- Replaces the SQLAlchemy `SessionMaker` context 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.
### Define the fixture
Below is an example of how to use fake_session_maker in a pytest fixture:
from fake_session_maker import fsm
# Assuming Namespace is where the session_maker is defined
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:
```python
# 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](https://lab.frogg.it/dorianturba/fake_session_maker/-/blob/main/tests/test_fsm.py)
directory for a full example.
Contributions are welcome! Please
see [CONTRIBUTING.md](https://lab.frogg.it/dorianturba/fake_session_maker/-/blob/main/CONTRIBUTING.md)
for more details.
If you want to run the tests locally, you can follow instructions here:
[CONTRIBUTING.md #testing](https://lab.frogg.it/dorianturba/fake_session_maker/-/blob/main/CONTRIBUTING.md#testing).
Distributed under the MIT License.
See [LICENSE](Lhttps://lab.frogg.it/dorianturba/fake_session_maker/-/blob/main/LICENSE)