Metadata-Version: 2.1
Name: pytest-sqlalchemy-mock
Version: 0.1.7
Summary: pytest sqlalchemy plugin for mock
Author-email: Resul Yurttakalan <resulyrt93@gmail.com>
License: MIT License
        
        Copyright (c) 2022 Resul Yurttakalan
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/resulyrt93/pytest-sqlalchemy-mock
Classifier: Framework :: Pytest
Classifier: Development Status :: 3 - Alpha
Classifier: Topic :: Software Development :: Testing
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pytest>=7.0.0
Requires-Dist: sqlalchemy>=2.0.6
Provides-Extra: dev
Requires-Dist: black>=23.12.1; extra == "dev"
Requires-Dist: build>=1.0.3; extra == "dev"
Requires-Dist: flake8>=7.0.0; extra == "dev"
Requires-Dist: isort>=5.13.2; extra == "dev"
Requires-Dist: pre-commit>=3.6.0; extra == "dev"
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"

# pytest-sqlalchemy-mock

[![PyPI version](https://badge.fury.io/py/pytest-sqlalchemy-mock.svg)](https://badge.fury.io/py/pytest-sqlalchemy-mock)
[![codecov](https://codecov.io/gh/resulyrt93/pytest-sqlalchemy-mock/branch/dev/graph/badge.svg?token=RUQ4DN3CH9)](https://codecov.io/gh/resulyrt93/pytest-sqlalchemy-mock)
[![CI](https://github.com/resulyrt93/pytest-sqlalchemy-mock/actions/workflows/tests.yaml/badge.svg?branch=dev)](https://github.com/resulyrt93/pytest-sqlalchemy-mock/actions/workflows/tests.yaml)
[![Supported Python Version](https://img.shields.io/pypi/pyversions/pytest-sqlalchemy-mock)](https://github.com/resulyrt93/pytest-sqlalchemy-mock)
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>

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

```python
pip install pytest-sqlalchemy-mock
```

### Building from source

At the top direcotry,

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

or

```sh
python3 -m pip install .
```

## Uninstall

```sh
python3 -m pip uninstall pytest_sqlalchemy_mock
```

## Usage

Let's assume you have a SQLAlchemy declarative base and some models with it.

### models.py

```python
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

```python
@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

```python
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

```python
@pytest.fixture(scope="function")
def sqlalchemy_mock_config():
    return [("user", [
        {
            "id": 1,
            "name": "Kevin"
        },
        {
            "id": 2,
            "name": "Dwight"
        }
    ])]
```

### test_user_table.py

```python
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
