待办:项目待办模型 + /projects/{id}/todos 接口 + 控制台待办页
This commit is contained in:
1 parent
c10fda4e4a
commit
b3b3b35042
8 files changed
+962
-23
No files matched your search
@@ -0,0 +1,138 @@
|
||||
"""项目待办模块单测(cloud/todos.py)。
|
||||
|
||||
不跑真实模型、不联网:临时 SQLite 文件 + 模块函数直调。
|
||||
覆盖:建表幂等、创建校验与默认值、状态五态与完成时间、
|
||||
PATCH 只改给定字段、过滤(状态/q)、排序追加、硬删除、
|
||||
跨项目隔离(模块层按 project_id 查,服务端层再做 404)。
|
||||
"""
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
from cloud.todos import (
|
||||
create_todo,
|
||||
delete_todo,
|
||||
ensure_todos_table,
|
||||
get_todo,
|
||||
list_todos,
|
||||
update_todo,
|
||||
)
|
||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
from cloud.todos import (
|
||||
create_todo,
|
||||
delete_todo,
|
||||
ensure_todos_table,
|
||||
get_todo,
|
||||
list_todos,
|
||||
update_todo,
|
||||
)
|
||||
|
||||
|
||||
class TodosTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.db_path = os.path.join(self.tmp.name, "control.db")
|
||||
self.conn = sqlite3.connect(self.db_path)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
ensure_todos_table(self.conn)
|
||||
self.conn.commit()
|
||||
self.now = 1700000000.0
|
||||
self.project = "proj-1"
|
||||
|
||||
def tearDown(self):
|
||||
self.conn.close()
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _make(self, title="写文档", **kw):
|
||||
kw.setdefault("project_id", self.project)
|
||||
return create_todo(
|
||||
self.conn, kw.pop("project_id"), "user-1", title,
|
||||
now=self.now, **kw)
|
||||
|
||||
def test_ensure_is_idempotent(self):
|
||||
ensure_todos_table(self.conn)
|
||||
ensure_todos_table(self.conn)
|
||||
self.conn.commit()
|
||||
rows = self.conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table'"
|
||||
" AND name = 'todos'").fetchall()
|
||||
self.assertEqual(len(rows), 1)
|
||||
|
||||
def test_create_defaults_and_validation(self):
|
||||
t = self._make()
|
||||
self.assertEqual(t["status"], "pending")
|
||||
self.assertEqual(t["priority"], "medium")
|
||||
self.assertEqual(t["description"], "")
|
||||
self.assertIsNone(t["task_id"])
|
||||
self.assertIsNone(t["due_at"])
|
||||
self.assertIsNone(t["completed_at"])
|
||||
self.assertEqual(t["sort_order"], 0)
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(title=" ")
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(status="done")
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(priority="urgent")
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(title="x" * 501)
|
||||
|
||||
def test_sort_order_appends(self):
|
||||
a = self._make("第一")
|
||||
b = self._make("第二")
|
||||
c = self._make("第三", sort_order=10)
|
||||
self.assertEqual((a["sort_order"], b["sort_order"],
|
||||
c["sort_order"]), (0, 1, 10))
|
||||
titles = [t["title"] for t in list_todos(self.conn, self.project)]
|
||||
self.assertEqual(titles, ["第一", "第二", "第三"])
|
||||
|
||||
def test_status_completed_sets_and_clears_completed_at(self):
|
||||
t = self._make()
|
||||
done = update_todo(self.conn, t["id"], {"status": "completed"})
|
||||
self.assertIsNotNone(done["completed_at"])
|
||||
back = update_todo(self.conn, t["id"], {"status": "in_progress"})
|
||||
self.assertIsNone(back["completed_at"])
|
||||
self.assertEqual(back["status"], "in_progress")
|
||||
|
||||
def test_update_only_given_fields(self):
|
||||
t = self._make("原标题", description="原说明",
|
||||
priority="low", task_id="task-1")
|
||||
u = update_todo(self.conn, t["id"], {"title": "新标题"})
|
||||
self.assertEqual(u["title"], "新标题")
|
||||
self.assertEqual(u["description"], "原说明")
|
||||
self.assertEqual(u["priority"], "low")
|
||||
self.assertEqual(u["task_id"], "task-1")
|
||||
self.assertIsNone(update_todo(self.conn, "nope", {"title": "x"}))
|
||||
|
||||
def test_due_at_ms_tolerance(self):
|
||||
t = self._make(due_at=1700000000000)
|
||||
self.assertEqual(t["due_at"], 1700000000.0)
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(due_at="明天")
|
||||
|
||||
def test_list_filters(self):
|
||||
self._make("写文档", status="completed")
|
||||
self._make("修 bug", status="in_progress")
|
||||
self._make("写测试", status="pending")
|
||||
done = list_todos(self.conn, self.project, status="completed")
|
||||
self.assertEqual([t["title"] for t in done], ["写文档"])
|
||||
q = list_todos(self.conn, self.project, q="写")
|
||||
self.assertEqual(sorted(t["title"] for t in q), ["写文档", "写测试"])
|
||||
other = list_todos(self.conn, "proj-2")
|
||||
self.assertEqual(other, [])
|
||||
|
||||
def test_delete(self):
|
||||
t = self._make()
|
||||
deleted = delete_todo(self.conn, t["id"])
|
||||
self.assertEqual(deleted["id"], t["id"])
|
||||
self.assertIsNone(get_todo(self.conn, t["id"]))
|
||||
self.assertIsNone(delete_todo(self.conn, t["id"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user