pytest sqlalchemy plugin for mock
Project description
pytest-sqlalchemy-mock 👋
This plugin provides pytest fixtures to create an in-memory DB instance on tests and dump your raw test data.
Installation
pip install pytest-sqlalchemy-mock
Usage
Let's assume you have a SQLAlchemy declarative base and some models with it.
models.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
name = Column(String)
Firstly SQLAlchemy base class which is used for declare models should be passed with sqlalchemy_declarative_base
fixture in conftest.py
conftest.py
@pytest.fixture(scope="function")
def sqlalchemy_declarative_base():
return Base
Then in test functions you can use mocked_session
fixture to make query in mocked DB.
test_user_table.py
def test_mocked_session_user_table(mocked_session):
user_data = mocked_session.execute("SELECT * from user;").all()
assert user_data == []
Also, you can dump your mock data to DB before start testing via sqlalchemy_mock_config
fixture like following.
conftest.py
@pytest.fixture(scope="function")
def sqlalchemy_mock_config():
return [("user", [
{
"id": 1,
"name": "Kevin"
},
{
"id": 2,
"name": "Dwight"
}
])]
test_user_table.py
def test_mocked_session_user_class(mocked_session):
user = mocked_session.query(User).filter_by(id=2).first()
assert user.name == "Dwight"
Upcoming Features
- Mock with decorator for specific DB states for specific cases.
- Support to load data from
.json
and.csv
- Async SQLAlchemy support
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for pytest-sqlalchemy-mock-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c52d7ca75a56480bda69cc84dadbfec084b9d32a3c0c5bf8f3a8866c791971c |
|
MD5 | 3be6d5cd028488eb15c80dfe218b9db8 |
|
BLAKE2b-256 | 871a4d47992c82f3d2169ae044354fecc3adbaa11917fb7a723526e6ae109332 |
Close
Hashes for pytest_sqlalchemy_mock-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22235bdd887aac67f0dbdfbe202ecb3d6b71d7e83b2b745d0eb1c6ff9dac589f |
|
MD5 | 752d72a8c396199e42f29ed44da08d38 |
|
BLAKE2b-256 | 86b3a55135b3bcbc276e2e4b24da21b24a795f1f95a6080e8e8a1804bb7b338f |