Skip to content

Commit

Permalink
Merge pull request #4 from resulyrt93/release/0.1.2
Browse files Browse the repository at this point in the history
Release/0.1.2
  • Loading branch information
resulyrt93 authored Sep 25, 2022
2 parents fd687e6 + 6c72954 commit 657052e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Publish Python 🐍 distributions 📦 to PyPI

name: Deploy
on:
push:
tags:
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
name: Python package
name: CI

on: [ pull_request ]

jobs:
build:

test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10" ]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pytest-sqlalchemy-mock
[![PyPI version](https://badge.fury.io/py/pytest-sqlalchemy-mock.svg)](https://badge.fury.io/py/pytest-sqlalchemy-mock)
![CI](https://github.com/resulyrt93/pytest-sqlalchemy-mock/actions/workflows/tests.yml/badge.svg?branch=dev)
<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.

Expand Down
Empty file.
31 changes: 3 additions & 28 deletions pytest_sqlalchemy_mock.py → pytest_sqlalchemy_mock/base.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
from typing import TYPE_CHECKING, List, Tuple
from typing import TYPE_CHECKING

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from .model_mocker import ModelMocker

if TYPE_CHECKING:
from sqlalchemy.orm import Session


class ModelMocker:
# TODO make type annotations
_base = None
_mock_config: List[Tuple[str, List]] = None

def __init__(self, session, base, mock_config):
self._session: Session = session
self._base = base
self._mock_config = mock_config

def get_model_class_with_table_name(self, table_name: str):
for mapper in self._base.registry.mappers:
cls = mapper.class_
if cls.__tablename__ == table_name:
return cls

def create_all(self):
for model_config in self._mock_config:
table_name, data = model_config
model_class = self.get_model_class_with_table_name(table_name)
if model_class:
for datum in data:
instance = model_class(**datum)
self._session.add(instance)
self._session.commit()


@pytest.fixture(scope="session")
def connection_url():
return "sqlite:///:memory:"
Expand Down
36 changes: 36 additions & 0 deletions pytest_sqlalchemy_mock/model_mocker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import List, Tuple

from sqlalchemy.orm import Session

MOCK_CONFIG_TYPE = List[Tuple[str, List]]


class ModelMocker:
_base = None
_mock_config: MOCK_CONFIG_TYPE = None

def __init__(self, session: Session, base, mock_config: MOCK_CONFIG_TYPE):
self._session: Session = session
self._base = base
self._mock_config = mock_config

def get_model_class_with_table_name(self, table_name: str):
for mapper in self._base.registry.mappers:
cls = mapper.class_
if cls.__tablename__ == table_name:
return cls

def create_all(self):
for model_config in self._mock_config:
table_name, data = model_config
model_class = self.get_model_class_with_table_name(table_name)
if model_class:
for datum in data:
instance = model_class(**datum)
self._session.add(instance)
self._session.commit()
5 changes: 2 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from pytest_sqlalchemy_mock import *
# TODO use pytester for testing
from pytest_sqlalchemy_mock.base import *
from tests.data import MockData
from tests.db import Base

Expand Down

0 comments on commit 657052e

Please sign in to comment.