mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-04-22 07:22:54 +02:00
117 lines
3.7 KiB
Python
117 lines
3.7 KiB
Python
"""Global configurations and definitions for pytest."""
|
|
import datetime
|
|
import os
|
|
from collections.abc import Generator
|
|
from inspect import getmembers, isfunction
|
|
|
|
import pytest
|
|
from sqlalchemy.engine import Engine
|
|
from sqlalchemy.orm import Session
|
|
|
|
from aki_prj23_transparenzregister.utils import data_transfer
|
|
from aki_prj23_transparenzregister.utils.sql import entities
|
|
from aki_prj23_transparenzregister.utils.sql.connector import get_session, init_db
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clear_caches() -> Generator[None, None, None]:
|
|
"""A function that clears all caches after each test.
|
|
|
|
All the modules containing the cached functions need to be listed in the modules tuple.
|
|
"""
|
|
yield
|
|
# https://stackoverflow.com/a/139198/11003343
|
|
modules = (data_transfer,)
|
|
functions = [
|
|
function
|
|
for module in modules
|
|
for name, function in getmembers(module, isfunction)
|
|
if function.__dict__.get("cache") is not None
|
|
]
|
|
# https://cachetools.readthedocs.io/en/stable/?highlight=clear#memoizing-decorators
|
|
for function in functions:
|
|
function.cache.clear() # type: ignore
|
|
|
|
|
|
@pytest.fixture()
|
|
def empty_db() -> Generator[Session, None, None]:
|
|
"""Generates a db Session to a sql_lite db."""
|
|
if os.path.exists("test-db.db"):
|
|
os.remove("test-db.db")
|
|
db = get_session("sqlite:///test-db.db")
|
|
init_db(db)
|
|
yield db
|
|
db.close()
|
|
bind = db.bind
|
|
assert isinstance(bind, Engine)
|
|
bind.dispose()
|
|
os.remove("test-db.db")
|
|
|
|
|
|
@pytest.fixture()
|
|
def full_db(empty_db: Session) -> Session:
|
|
"""Fills a db with some test data."""
|
|
empty_db.add_all(
|
|
[
|
|
entities.DistrictCourt(name="Amtsgericht Bochum", city="Bochum"),
|
|
entities.DistrictCourt(name="Amtsgericht Dortmund", city="Dortmund"),
|
|
entities.Person(
|
|
name="Max",
|
|
surname="Mustermann",
|
|
date_of_birth=datetime.date(2023, 1, 1),
|
|
),
|
|
entities.Person(
|
|
name="Sabine",
|
|
surname="Mustermann",
|
|
date_of_birth=datetime.date(2023, 1, 1),
|
|
),
|
|
entities.Person(
|
|
name="Some Firstname",
|
|
surname="Some Surname",
|
|
date_of_birth=datetime.date(2023, 1, 1),
|
|
),
|
|
entities.Person(
|
|
name="Some Firstname",
|
|
surname="Some Surname",
|
|
date_of_birth=datetime.date(2023, 1, 2),
|
|
),
|
|
entities.Person(
|
|
name="Other Firstname",
|
|
surname="Other Surname",
|
|
date_of_birth=datetime.date(2023, 1, 2),
|
|
),
|
|
]
|
|
)
|
|
empty_db.commit()
|
|
empty_db.add_all(
|
|
[
|
|
entities.Company(
|
|
hr="HRB 123",
|
|
court_id=2,
|
|
name="Some Company GmbH",
|
|
street="Sesamstr.",
|
|
zip_code="12345",
|
|
city="TV City",
|
|
last_update=datetime.date.fromisoformat("2023-01-01"),
|
|
),
|
|
entities.Company(
|
|
hr="HRB 123",
|
|
court_id=1,
|
|
name="Other Company GmbH",
|
|
street="Sesamstr.",
|
|
zip_code="12345",
|
|
city="TV City",
|
|
last_update=datetime.date.fromisoformat("2023-01-01"),
|
|
),
|
|
entities.Company(
|
|
hr="HRB 12",
|
|
court_id=2,
|
|
name="Third Company GmbH",
|
|
last_update=datetime.date.fromisoformat("2023-01-01"),
|
|
),
|
|
]
|
|
)
|
|
empty_db.commit()
|
|
# print(pd.read_sql_table("company", empty_db.bind).to_string())
|
|
return empty_db
|