fixture pytest db sqlite in mem

This commit is contained in:
2025-08-25 17:00:57 +02:00
parent 2b6d6893f3
commit f492b12479

View File

@@ -1,10 +1,18 @@
import pytest import pytest
from models import Base, Credentials, Collaborator, Customer, Contract, Event from models import Base, Credentials, Collaborator, Customer, Contract, Event
from db import engine from sqlalchemy import create_engine
from sqlalchemy.orm import Session from sqlalchemy.orm import sessionmaker
from passlib.hash import argon2 from passlib.hash import argon2
@pytest.fixture(scope="function")
DB_URL = "sqlite:///:memory:"
engine = create_engine(DB_URL, echo=False)
SessionLocal = sessionmaker(bind=engine)
cust1 = ("Cust1", "aa", 11, "Cust1CO")
cust2 =
@pytest.fixture
def session(): def session():
if not engine.url.get_backend_name() == "sqlite": if not engine.url.get_backend_name() == "sqlite":
raise RuntimeError("Use SQLite backend to run tests\n" raise RuntimeError("Use SQLite backend to run tests\n"
@@ -13,41 +21,55 @@ def session():
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
try: try:
with Session() as session: with SessionLocal() as session:
yield session yield session
finally: finally:
Base.metadata.drop_all(engine) Base.metadata.drop_all(engine)
@pytest.fixture(scope="function") @pytest.fixture
def seed(session): def seed(session):
session.add_all( session.add_all(
[ [
Collaborator(name="Com", email="a", phone=1, team_id=1), Customer(cust1),
Collaborator(name="Man", email="b", phone=2, team_id=2),
Collaborator(name="Sup", email="c", phone=3, team_id=3),
Customer(name="Cust1", email="aa", phone=11, company="Cust1CO"),
Customer(name="Cust2", email="bb", phone=22, company="Cust2CO"), Customer(name="Cust2", email="bb", phone=22, company="Cust2CO"),
] ]
) )
session.commit() session.commit()
session.add_all( session.add_all(
[ [
Credentials(collaborator_id=1, Collaborator(name="Col1", email="aa", phone=1, team_id=1),
password_hash=argon2.hash("test")), Collaborator(name="Col2", email="bb", phone=2, team_id=2),
Credentials(collaborator_id=2,
password_hash=argon2.hash("test")),
Credentials(collaborator_id=3,
password_hash=argon2.hash("test")),
Contract(signed=0, amount=200000, customer_id=1, commercial_id=1),
] ]
) )
session.commit() # session.add_all(
session.add_all( # [
[ # Collaborator(name="Com", email="a", phone=1, team_id=1),
Event(name="Event1", customer_contact="Test", # Collaborator(name="Man", email="b", phone=2, team_id=2),
date_start="01.01.01", date_end="02.01.01", # Collaborator(name="Sup", email="c", phone=3, team_id=3),
location=".",contract_id=1, customer_id=1) # Customer(name="Cust1", email="aa", phone=11, company="Cust1CO"),
] # Customer(name="Cust2", email="bb", phone=22, company="Cust2CO"),
) # ]
session.commit() # )
# session.commit()
# session.add_all(
# [
# Credentials(collaborator_id=1,
# password_hash=argon2.hash("test")),
# Credentials(collaborator_id=2,
# password_hash=argon2.hash("test")),
# Credentials(collaborator_id=3,
# password_hash=argon2.hash("test")),
# Contract(signed=0, amount=200000, customer_id=1, commercial_id=1),
# ]
# )
# session.commit()
# session.add_all(
# [
# Event(name="Event1", customer_contact="Test",
# date_start="01.01.01", date_end="02.01.01",
# location=".",contract_id=1, customer_id=1),
# ]
# )
# session.commit()