Skip to content

Commit

Permalink
Merge pull request #10 from FomalhautWeisszwerg/feature/adapt_to_py312
Browse files Browse the repository at this point in the history
Support Python 3.11 and 3.12.
  • Loading branch information
resulyrt93 authored May 21, 2024
2 parents 2374aec + 5add52c commit 30dd807
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 94 deletions.
9 changes: 9 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[flake8]
max-line-length = 120
exclude =
.git,
.github,
.venv,
build,
dist,
tests/conftest.py
14 changes: 7 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- dev
- 'feature/**'
pull_request:
branches:
- dev
Expand All @@ -13,24 +14,23 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.8", "3.9", "3.10" ]
python-version: [ "3.9", "3.10", "3.11", "3.12" ]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: '**/requirements-dev.txt'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install --no-cache-dir ".[dev]"
pip uninstall -y pytest-sqlalchemy-mock
- name: Run pre commit hooks
run: |
pre-commit run --all-files --show-diff-on-failure
- name: Test with pytest
run: |
pytest --cov pytest_sqlalchemy_mock --cov-report=term-missing
pytest --cov pytest_sqlalchemy_mock --cov-report=term-missing -s -vv tests/
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
13 changes: 6 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
# pre-commit install-hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
exclude: '.*\.pth$'
- id: debug-statements
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
rev: 7.0.0
hooks:
- id: flake8
- repo: https://github.com/asottile/pyupgrade
rev: v2.32.1
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: pyupgrade
args: [ --py36-plus ]
- id: black
54 changes: 47 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pytest-sqlalchemy-mock 👋
# 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)
Expand All @@ -7,15 +8,45 @@

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**
### models.py

```python
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base
Expand All @@ -29,25 +60,31 @@ class User(Base):
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**
### 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**
### 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**
### conftest.py

```python
@pytest.fixture(scope="function")
def sqlalchemy_mock_config():
Expand All @@ -62,14 +99,17 @@ def sqlalchemy_mock_config():
}
])]
```
**test_user_table.py**

### 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
70 changes: 70 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[build-system]
requires = ["setuptools>=69.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "pytest-sqlalchemy-mock"
version = "0.1.6"
license.file = "LICENSE"
description = "pytest sqlalchemy plugin for mock"
authors = [
{ name="Resul Yurttakalan", email="[email protected]" },
]
requires-python = ">=3.9"
readme = "README.md"
classifiers = [
"Framework :: Pytest",
"Development Status :: 3 - Alpha",
"Topic :: Software Development :: Testing",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
]

dependencies = [
"pytest>=7.0.0",
"sqlalchemy>=2.0.6",
]

[project.optional-dependencies]
dev = [
"black>=23.12.1",
"build>=1.0.3",
"flake8>=7.0.0",
"isort>=5.13.2",
"pre-commit>=3.6.0",
"pytest-cov>=4.1.0",
]

[project.urls]
Homepage = "https://github.com/resulyrt93/pytest-sqlalchemy-mock"

[tool.black]
target-version = ["py312"]
line-length = 120

[tool.isort]
force_grid_wrap = 2
profile = "black"
py_version = 312
src_paths = ["src"]
skip_glob = ["tests/conftest.py", "build/*", "dist/*",]

[tool.pytest.ini_options]
pythonpath = [
"src",
]

[tool.pytest]
norecursedirs = [
"dist",
"build",
".git",
".tox",
".eggs",
"venv",
]
5 changes: 0 additions & 5 deletions requirements-dev.txt

This file was deleted.

15 changes: 0 additions & 15 deletions setup.cfg

This file was deleted.

35 changes: 0 additions & 35 deletions setup.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ def session(connection):


@pytest.fixture(scope="function")
def mocked_session(
connection, sqlalchemy_declarative_base, sqlalchemy_mock_config
):
def mocked_session(connection, sqlalchemy_declarative_base, sqlalchemy_mock_config):
session: Session = sessionmaker()(bind=connection)

if sqlalchemy_declarative_base and sqlalchemy_mock_config:
ModelMocker(
session, sqlalchemy_declarative_base, sqlalchemy_mock_config
).create_all()
ModelMocker(session, sqlalchemy_declarative_base, sqlalchemy_mock_config).create_all()

yield session
session.close()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import List, Tuple
from typing import (
List,
Tuple,
)

from sqlalchemy import Table
from sqlalchemy.orm import Session
Expand Down
21 changes: 15 additions & 6 deletions tests/db.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
from typing import List

from sqlalchemy import (Boolean, Column, DateTime, ForeignKey, Integer, String,
func)
from sqlalchemy.orm import Mapped, declarative_base, relationship
from sqlalchemy import (
Boolean,
Column,
DateTime,
ForeignKey,
Integer,
String,
func,
)
from sqlalchemy.orm import (
Mapped,
declarative_base,
relationship,
)
from sqlalchemy.testing.schema import Table

Base = declarative_base()
Expand All @@ -12,9 +23,7 @@
"user_department",
Base.metadata,
Column("user_id", Integer, ForeignKey("user.id"), primary_key=True),
Column(
"department_id", Integer, ForeignKey("department.id"), primary_key=True
),
Column("department_id", Integer, ForeignKey("department.id"), primary_key=True),
)


Expand Down
Loading

0 comments on commit 30dd807

Please sign in to comment.