新增小程序后端代码,包括数据库、路由、服务层等。
小程序前后端打通,包括登录、注册、绑定设备、查询绑定信息,修改小朋友名称等功能。
This commit is contained in:
0
mini-program/tests/__init__.py
Normal file
0
mini-program/tests/__init__.py
Normal file
85
mini-program/tests/test_dao.py
Normal file
85
mini-program/tests/test_dao.py
Normal file
@@ -0,0 +1,85 @@
|
||||
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!")
|
||||
Reference in New Issue
Block a user