Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pytest-sqlalchemy-mock

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pytest-sqlalchemy-mock

pytest sqlalchemy plugin for mock

  • 0.1.7
  • PyPI
  • Socket score

Maintainers
1

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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc