85 lines
2.3 KiB
Python
85 lines
2.3 KiB
Python
import os
|
|
import pytest
|
|
|
|
os.environ["DB_TYPE"] = "sqlite"
|
|
os.environ["DB_PATH"] = ":memory:"
|
|
|
|
|
|
def test_sqlite_connection():
|
|
"""Test SQLite database works."""
|
|
from sqlalchemy import create_engine, text
|
|
from app.models import Base
|
|
|
|
engine = create_engine("sqlite:///:memory:", echo=False)
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
with engine.connect() as conn:
|
|
conn.execute(text("INSERT INTO parents (openid, nickname, status) VALUES ('test', 'Test', 1)"))
|
|
conn.commit()
|
|
result = conn.execute(text("SELECT * FROM parents")).fetchall()
|
|
assert len(result) == 1
|
|
|
|
|
|
def test_parent_dao():
|
|
"""Test ParentDAO."""
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.models import Base
|
|
from app.dao.parent import ParentDAO
|
|
|
|
engine = create_engine("sqlite:///:memory:", echo=False)
|
|
Base.metadata.create_all(bind=engine)
|
|
Session = sessionmaker(bind=engine)
|
|
db = Session()
|
|
|
|
dao = ParentDAO(db)
|
|
user_id = dao.create("test_openid", None, "Test User")
|
|
assert user_id > 0
|
|
|
|
parent = dao.get_by_id(user_id)
|
|
assert parent["openid"] == "test_openid"
|
|
|
|
|
|
def test_child_dao():
|
|
"""Test ChildDAO."""
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.models import Base
|
|
from app.dao.child import ChildDAO
|
|
|
|
engine = create_engine("sqlite:///:memory:", echo=False)
|
|
Base.metadata.create_all(bind=engine)
|
|
Session = sessionmaker(bind=engine)
|
|
db = Session()
|
|
|
|
dao = ChildDAO(db)
|
|
child_id = dao.create(1, "Test Child")
|
|
assert child_id > 0
|
|
|
|
child = dao.get_by_id(child_id)
|
|
assert child["child_name"] == "Test Child"
|
|
|
|
|
|
def test_binding_dao():
|
|
"""Test BindingDAO."""
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.models import Base
|
|
from app.dao.binding import BindingDAO
|
|
|
|
engine = create_engine("sqlite:///:memory:", echo=False)
|
|
Base.metadata.create_all(bind=engine)
|
|
Session = sessionmaker(bind=engine)
|
|
db = Session()
|
|
|
|
dao = BindingDAO(db)
|
|
token = dao.start_bind(1, "device_123", 1)
|
|
assert len(token) == 36
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_sqlite_connection()
|
|
test_parent_dao()
|
|
test_child_dao()
|
|
test_binding_dao()
|
|
print("All DAO tests passed!") |