diff --git a/tests/test_todo_tool.py b/tests/test_todo_tool.py index a193e8ee..5fa2b242 100644 --- a/tests/test_todo_tool.py +++ b/tests/test_todo_tool.py @@ -29,15 +29,15 @@ class FakeCollection: pass def insert_one(self, doc): - key = (doc["user_id"], doc["tool_id"], int(doc["todo_id"])) + key = (doc["user_id"], doc["tool_id"], doc["todo_id"]) self.docs[key] = doc return type("res", (), {"inserted_id": key}) def find_one(self, query): - key = (query.get("user_id"), query.get("tool_id"), int(query.get("todo_id"))) + key = (query.get("user_id"), query.get("tool_id"), query.get("todo_id")) return self.docs.get(key) - def find(self, query): + def find(self, query, projection=None): user_id = query.get("user_id") tool_id = query.get("tool_id") filtered = [ @@ -47,7 +47,7 @@ class FakeCollection: return FakeCursor(filtered) def update_one(self, query, update, upsert=False): - key = (query.get("user_id"), query.get("tool_id"), int(query.get("todo_id"))) + key = (query.get("user_id"), query.get("tool_id"), query.get("todo_id")) if key in self.docs: self.docs[key].update(update.get("$set", {})) return type("res", (), {"matched_count": 1}) @@ -59,7 +59,7 @@ class FakeCollection: return type("res", (), {"matched_count": 0}) def delete_one(self, query): - key = (query.get("user_id"), query.get("tool_id"), int(query.get("todo_id"))) + key = (query.get("user_id"), query.get("tool_id"), query.get("todo_id")) if key in self.docs: del self.docs[key] return type("res", (), {"deleted_count": 1}) @@ -69,6 +69,10 @@ class FakeCollection: @pytest.fixture def todo_tool(monkeypatch) -> TodoListTool: """Provides a TodoListTool with a fake MongoDB backend.""" + # Reset the MongoDB client cache to ensure our mock is used + from application.core.mongo_db import MongoDB + MongoDB._client = None + fake_collection = FakeCollection() fake_client = {settings.MONGO_DB_NAME: {"todos": fake_collection}} monkeypatch.setattr("application.core.mongo_db.MongoDB.get_client", lambda: fake_client) @@ -76,52 +80,72 @@ def todo_tool(monkeypatch) -> TodoListTool: def test_create_and_get(todo_tool: TodoListTool): - res = todo_tool.execute_action("todo_create", title="Write tests", description="Write pytest cases") - assert res["status_code"] == 201 - todo_id = res["todo_id"] + res = todo_tool.execute_action("create", title="Write tests") + assert "Todo created with ID" in res + # Extract todo_id from response like "Todo created with ID test_user_test_tool_1: Write tests" + todo_id = res.split("ID ")[1].split(":")[0].strip() - get_res = todo_tool.execute_action("todo_get", todo_id=todo_id) - assert get_res["status_code"] == 200 - assert get_res["todo"]["title"] == "Write tests" - assert get_res["todo"]["description"] == "Write pytest cases" + get_res = todo_tool.execute_action("get", todo_id=todo_id) + assert "Error" not in get_res + assert "Write tests" in get_res def test_get_all_todos(todo_tool: TodoListTool): - todo_tool.execute_action("todo_create", title="Task 1") - todo_tool.execute_action("todo_create", title="Task 2") + todo_tool.execute_action("create", title="Task 1") + todo_tool.execute_action("create", title="Task 2") - list_res = todo_tool.execute_action("todo_list") - assert list_res["status_code"] == 200 - titles = [todo["title"] for todo in list_res["todos"]] - assert "Task 1" in titles - assert "Task 2" in titles + list_res = todo_tool.execute_action("list") + assert "Task 1" in list_res + assert "Task 2" in list_res def test_update_todo(todo_tool: TodoListTool): - create_res = todo_tool.execute_action("todo_create", title="Initial Title") - todo_id = create_res["todo_id"] + create_res = todo_tool.execute_action("create", title="Initial Title") + todo_id = create_res.split("ID ")[1].split(":")[0].strip() - update_res = todo_tool.execute_action("todo_update", todo_id=todo_id, updates={"title": "Updated Title", "status": "done"}) - assert update_res["status_code"] == 200 + update_res = todo_tool.execute_action("update", todo_id=todo_id, title="Updated Title") + assert "updated" in update_res.lower() + assert "Updated Title" in update_res - get_res = todo_tool.execute_action("todo_get", todo_id=todo_id) - assert get_res["todo"]["title"] == "Updated Title" - assert get_res["todo"]["status"] == "done" + get_res = todo_tool.execute_action("get", todo_id=todo_id) + assert "Updated Title" in get_res + + +def test_complete_todo(todo_tool: TodoListTool): + create_res = todo_tool.execute_action("create", title="To Complete") + todo_id = create_res.split("ID ")[1].split(":")[0].strip() + + # Check initial status is open + get_res = todo_tool.execute_action("get", todo_id=todo_id) + assert "open" in get_res + + # Mark as completed + complete_res = todo_tool.execute_action("complete", todo_id=todo_id) + assert "completed" in complete_res.lower() + + # Verify status changed to completed + get_res = todo_tool.execute_action("get", todo_id=todo_id) + assert "completed" in get_res def test_delete_todo(todo_tool: TodoListTool): - create_res = todo_tool.execute_action("todo_create", title="To Delete") - todo_id = create_res["todo_id"] + create_res = todo_tool.execute_action("create", title="To Delete") + todo_id = create_res.split("ID ")[1].split(":")[0].strip() - delete_res = todo_tool.execute_action("todo_delete", todo_id=todo_id) - assert delete_res["status_code"] == 200 + delete_res = todo_tool.execute_action("delete", todo_id=todo_id) + assert "deleted" in delete_res.lower() - get_res = todo_tool.execute_action("todo_get", todo_id=todo_id) - assert get_res["status_code"] == 404 + get_res = todo_tool.execute_action("get", todo_id=todo_id) + assert "Error" in get_res + assert "not found" in get_res def test_isolation_and_default_tool_id(monkeypatch): """Ensure todos are isolated by tool_id and user_id.""" + # Reset the MongoDB client cache to ensure our mock is used + from application.core.mongo_db import MongoDB + MongoDB._client = None + fake_collection = FakeCollection() fake_client = {settings.MONGO_DB_NAME: {"todos": fake_collection}} monkeypatch.setattr("application.core.mongo_db.MongoDB.get_client", lambda: fake_client) @@ -130,17 +154,20 @@ def test_isolation_and_default_tool_id(monkeypatch): tool1 = TodoListTool({"tool_id": "tool_1"}, user_id="u1") tool2 = TodoListTool({"tool_id": "tool_2"}, user_id="u1") - r1_create = tool1.execute_action("todo_create", title="from tool 1") - r2_create = tool2.execute_action("todo_create", title="from tool 2") + r1_create = tool1.execute_action("create", title="from tool 1") + r2_create = tool2.execute_action("create", title="from tool 2") - r1 = tool1.execute_action("todo_get", todo_id=r1_create["todo_id"]) - r2 = tool2.execute_action("todo_get", todo_id=r2_create["todo_id"]) + todo_id_1 = r1_create.split("ID ")[1].split(":")[0].strip() + todo_id_2 = r2_create.split("ID ")[1].split(":")[0].strip() - assert r1["status_code"] == 200 - assert r1["todo"]["title"] == "from tool 1" + r1 = tool1.execute_action("get", todo_id=todo_id_1) + r2 = tool2.execute_action("get", todo_id=todo_id_2) - assert r2["status_code"] == 200 - assert r2["todo"]["title"] == "from tool 2" + assert "Error" not in r1 + assert "from tool 1" in r1 + + assert "Error" not in r2 + assert "from tool 2" in r2 # Same user, no tool_id → should default to same value t3 = TodoListTool({}, user_id="default_user") @@ -149,8 +176,9 @@ def test_isolation_and_default_tool_id(monkeypatch): assert t3.tool_id == "default_default_user" assert t4.tool_id == "default_default_user" - create_res = t3.execute_action("todo_create", title="shared default") - r = t4.execute_action("todo_get", todo_id=create_res["todo_id"]) + create_res = t3.execute_action("create", title="shared default") + todo_id = create_res.split("ID ")[1].split(":")[0].strip() + r = t4.execute_action("get", todo_id=todo_id) - assert r["status_code"] == 200 - assert r["todo"]["title"] == "shared default" + assert "Error" not in r + assert "shared default" in r