65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
import pytest
|
|
from sqlmodel import Session, select
|
|
from app.services.database import create_db_and_tables, get_session, add_and_refresh
|
|
from app.model.models import UserDB, GroupDB, Card
|
|
|
|
|
|
def test_create_db_and_tables():
|
|
"""Test database and tables creation."""
|
|
# This is primarily an integration test
|
|
from sqlalchemy import inspect
|
|
from app.services.database import engine
|
|
|
|
create_db_and_tables()
|
|
inspector = inspect(engine)
|
|
|
|
# Check that tables exist
|
|
tables = inspector.get_table_names()
|
|
assert "userdb" in tables
|
|
assert "groupdb" in tables
|
|
assert "card" in tables
|
|
assert "accessauthorizationdb" in tables
|
|
assert "timetable" in tables
|
|
assert "aagrouplink" in tables
|
|
|
|
|
|
def test_get_session(db_session):
|
|
"""Test database session generator."""
|
|
# Test that we can get a session
|
|
session_gen = get_session()
|
|
session = next(session_gen)
|
|
|
|
assert isinstance(session, Session)
|
|
|
|
# Test that session works
|
|
user = UserDB(name="Test", passwordhash="hash")
|
|
session.add(user)
|
|
session.commit()
|
|
|
|
retrieved_user = session.get(UserDB, user.id)
|
|
assert retrieved_user is not None
|
|
assert retrieved_user.name == "Test"
|
|
|
|
# Clean up generator
|
|
try:
|
|
next(session_gen)
|
|
except StopIteration:
|
|
pass
|
|
|
|
|
|
def test_add_and_refresh(db_session):
|
|
"""Test add_and_refresh helper function."""
|
|
user = UserDB(name="Test User", passwordhash="hashed")
|
|
|
|
# Add user
|
|
result = add_and_refresh(db_session, user)
|
|
|
|
# Assert that user is now in database with ID
|
|
assert result.id is not None
|
|
assert result.name == "Test User"
|
|
|
|
# Verify in database
|
|
db_user = db_session.get(UserDB, result.id)
|
|
assert db_user is not None
|
|
assert db_user.name == "Test User"
|