Skip to main content

pytest-sqlalchemy-mock

PyPI version codecov CI Supported Python Version Code style: black

This plugin provides pytest fixtures to create an in-memory DB instance on tests and dump your raw test data.

Supported Python versions

Python 3.12 or later highly recommended but also might work on Python 3.11.

Installation

Download from PyPI

pip install pytest-sqlalchemy-mock

Building from source

At the top direcotry,

python3 -m build
python3 -m pip install dist/pytest_sqlalchemy_mock-*.whl

or

python3 -m pip install .

Uninstall

python3 -m pip uninstall 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

Metadata

Release files for pytest-sqlalchemy-mock 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-sqlalchemy-mock 0.1.7
File Size Uploaded
pytest_sqlalchemy_mock-0.1.7.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-sqlalchemy-mock 0.1.7
File Interpreter ABI Platform
pytest_sqlalchemy_mock-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / pytest_sqlalchemy_mock-0.1.7.tar.gz

Download URL pytest_sqlalchemy_mock-0.1.7.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
516637fcdaf7c88687ea21a8c1f3649da50da7e32f6743e10dbc13cb9fb5e46e
BLAKE2b-256 checksum
How to use checksums
f7bc5e9dde88932fcd6f84ca2c9dbfcc5c5d24749c109e5d27f7fff43d7f00e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / pytest_sqlalchemy_mock-0.1.7-py3-none-any.whl

Download URL pytest_sqlalchemy_mock-0.1.7-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1173d3149f21ce779aac10e2de0e8b7a8fe4b065ea4196463e2f88b75a6a0558
BLAKE2b-256 checksum
How to use checksums
80be1de595c8a9fb4307012c5ee26118f8d5fdb1004345443d232ebadabb7512
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page