Skip to main content

pytest sqlalchemy plugin for mock

Project description

pytest-sqlalchemy-mock

PyPI version

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 get 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 decoratorsss
  • 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

pytest-sqlalchemy-mock-0.1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_sqlalchemy_mock-0.1.1-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-sqlalchemy-mock-0.1.1.tar.gz.

File metadata

  • Download URL: pytest-sqlalchemy-mock-0.1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for pytest-sqlalchemy-mock-0.1.1.tar.gz
Algorithm Hash digest
SHA256 779b57aaeb6617859f3ef357311fd97c7e25cf9de9f3c99d4835653b39256e89
MD5 3e9a317d4050a8daa66c6cc570fd3459
BLAKE2b-256 13d8c2adce3d182ed05782a28030cd37aa19034acd23a5e3ace3f3649c0ff52f

See more details on using hashes here.

File details

Details for the file pytest_sqlalchemy_mock-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_sqlalchemy_mock-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4c93d831fd89998438637a823d11d5bfb27893ee6224ef25f51ae355d62a1c56
MD5 ff66d34b0cf706378cc8bd5d694b00bf
BLAKE2b-256 f3bbeef7d568158685032e0d419e75efe9fa44555fd0de2d934de36f6bd367fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page