68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
# 小狮落库 + 三色/收尾页的数据侧回归
|
|
import os
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from tests._tmpdir import workdir
|
|
|
|
import backend.db as dbm
|
|
import backend.services as svc
|
|
import backend.seed as seed
|
|
|
|
|
|
class PetStageTest(unittest.TestCase):
|
|
def test_stage_thresholds(self):
|
|
self.assertEqual(svc.pet_stage_of(0), 0)
|
|
self.assertEqual(svc.pet_stage_of(11), 0)
|
|
self.assertEqual(svc.pet_stage_of(12), 1)
|
|
self.assertEqual(svc.pet_stage_of(40), 2)
|
|
self.assertEqual(svc.pet_stage_of(100), 3)
|
|
|
|
|
|
class PetSyncTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.db_path = str(workdir("pet") / (self._testMethodName + ".db"))
|
|
if os.path.exists(self.db_path):
|
|
os.unlink(self.db_path)
|
|
self.conn = dbm.connect(self.db_path)
|
|
dbm.init_db(self.conn)
|
|
self.gid = svc.login(self.conn, "demo")["guardian_id"]
|
|
svc.give_consent(self.conn, self.gid, "v1")
|
|
self.cid = svc.create_child(self.conn, self.gid, "皮皮", 2)["id"]
|
|
|
|
def tearDown(self):
|
|
self.conn.close()
|
|
|
|
def test_columns_exist(self):
|
|
cols = {r["name"] for r in self.conn.execute("PRAGMA table_info(children)")}
|
|
self.assertIn("pet_stage", cols)
|
|
self.assertIn("pet_fed_at", cols)
|
|
|
|
def test_new_child_is_unfed(self):
|
|
pet = svc.get_child(self.conn, self.gid, self.cid)["pet"]
|
|
self.assertEqual(pet["stage"], 0)
|
|
self.assertEqual(pet["graduated"], 0)
|
|
self.assertFalse(pet["fed_today"])
|
|
|
|
def test_not_fed_while_work_left(self):
|
|
# 刚入库的题 next_due 是明天(当天不做),要模拟「今天还有活」
|
|
# 得把它挪成今天到期——否则 left_today 天然是 0。
|
|
r = svc.add_manual_mistake(self.conn, self.gid, self.cid, "math", "36 + 47 =", "83")
|
|
mid = r["created"][0]["mistake_id"]
|
|
self.conn.execute("UPDATE mistakes SET next_due=? WHERE id=?", (svc.today(), mid))
|
|
pet = svc.get_child(self.conn, self.gid, self.cid)["pet"]
|
|
self.assertGreaterEqual(pet["left_today"], 1)
|
|
self.assertFalse(pet["fed_today"])
|
|
|
|
def test_list_children_carries_pet(self):
|
|
items = svc.list_children(self.conn, self.gid)
|
|
self.assertTrue(items)
|
|
self.assertIn("pet", items[0])
|
|
self.assertIn("fed_today", items[0]["pet"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|