refactor(tests): update todo tool tests to use simplified action names and improve key handling

This commit is contained in:
Alex
2025-10-28 10:39:35 +00:00
parent ab4ebf9a9d
commit 94f70e6de5

View File

@@ -29,15 +29,15 @@ class FakeCollection:
pass pass
def insert_one(self, doc): 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 self.docs[key] = doc
return type("res", (), {"inserted_id": key}) return type("res", (), {"inserted_id": key})
def find_one(self, query): 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) return self.docs.get(key)
def find(self, query): def find(self, query, projection=None):
user_id = query.get("user_id") user_id = query.get("user_id")
tool_id = query.get("tool_id") tool_id = query.get("tool_id")
filtered = [ filtered = [
@@ -47,7 +47,7 @@ class FakeCollection:
return FakeCursor(filtered) return FakeCursor(filtered)
def update_one(self, query, update, upsert=False): 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: if key in self.docs:
self.docs[key].update(update.get("$set", {})) self.docs[key].update(update.get("$set", {}))
return type("res", (), {"matched_count": 1}) return type("res", (), {"matched_count": 1})
@@ -59,7 +59,7 @@ class FakeCollection:
return type("res", (), {"matched_count": 0}) return type("res", (), {"matched_count": 0})
def delete_one(self, query): 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: if key in self.docs:
del self.docs[key] del self.docs[key]
return type("res", (), {"deleted_count": 1}) return type("res", (), {"deleted_count": 1})
@@ -69,6 +69,10 @@ class FakeCollection:
@pytest.fixture @pytest.fixture
def todo_tool(monkeypatch) -> TodoListTool: def todo_tool(monkeypatch) -> TodoListTool:
"""Provides a TodoListTool with a fake MongoDB backend.""" """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_collection = FakeCollection()
fake_client = {settings.MONGO_DB_NAME: {"todos": fake_collection}} fake_client = {settings.MONGO_DB_NAME: {"todos": fake_collection}}
monkeypatch.setattr("application.core.mongo_db.MongoDB.get_client", lambda: fake_client) 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): def test_create_and_get(todo_tool: TodoListTool):
res = todo_tool.execute_action("todo_create", title="Write tests", description="Write pytest cases") res = todo_tool.execute_action("create", title="Write tests")
assert res["status_code"] == 201 assert "Todo created with ID" in res
todo_id = res["todo_id"] # 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) get_res = todo_tool.execute_action("get", todo_id=todo_id)
assert get_res["status_code"] == 200 assert "Error" not in get_res
assert get_res["todo"]["title"] == "Write tests" assert "Write tests" in get_res
assert get_res["todo"]["description"] == "Write pytest cases"
def test_get_all_todos(todo_tool: TodoListTool): def test_get_all_todos(todo_tool: TodoListTool):
todo_tool.execute_action("todo_create", title="Task 1") todo_tool.execute_action("create", title="Task 1")
todo_tool.execute_action("todo_create", title="Task 2") todo_tool.execute_action("create", title="Task 2")
list_res = todo_tool.execute_action("todo_list") list_res = todo_tool.execute_action("list")
assert list_res["status_code"] == 200 assert "Task 1" in list_res
titles = [todo["title"] for todo in list_res["todos"]] assert "Task 2" in list_res
assert "Task 1" in titles
assert "Task 2" in titles
def test_update_todo(todo_tool: TodoListTool): def test_update_todo(todo_tool: TodoListTool):
create_res = todo_tool.execute_action("todo_create", title="Initial Title") create_res = todo_tool.execute_action("create", title="Initial Title")
todo_id = create_res["todo_id"] 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"}) update_res = todo_tool.execute_action("update", todo_id=todo_id, title="Updated Title")
assert update_res["status_code"] == 200 assert "updated" in update_res.lower()
assert "Updated Title" in update_res
get_res = todo_tool.execute_action("todo_get", todo_id=todo_id) get_res = todo_tool.execute_action("get", todo_id=todo_id)
assert get_res["todo"]["title"] == "Updated Title" assert "Updated Title" in get_res
assert get_res["todo"]["status"] == "done"
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): def test_delete_todo(todo_tool: TodoListTool):
create_res = todo_tool.execute_action("todo_create", title="To Delete") create_res = todo_tool.execute_action("create", title="To Delete")
todo_id = create_res["todo_id"] todo_id = create_res.split("ID ")[1].split(":")[0].strip()
delete_res = todo_tool.execute_action("todo_delete", todo_id=todo_id) delete_res = todo_tool.execute_action("delete", todo_id=todo_id)
assert delete_res["status_code"] == 200 assert "deleted" in delete_res.lower()
get_res = todo_tool.execute_action("todo_get", todo_id=todo_id) get_res = todo_tool.execute_action("get", todo_id=todo_id)
assert get_res["status_code"] == 404 assert "Error" in get_res
assert "not found" in get_res
def test_isolation_and_default_tool_id(monkeypatch): def test_isolation_and_default_tool_id(monkeypatch):
"""Ensure todos are isolated by tool_id and user_id.""" """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_collection = FakeCollection()
fake_client = {settings.MONGO_DB_NAME: {"todos": fake_collection}} fake_client = {settings.MONGO_DB_NAME: {"todos": fake_collection}}
monkeypatch.setattr("application.core.mongo_db.MongoDB.get_client", lambda: fake_client) 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") tool1 = TodoListTool({"tool_id": "tool_1"}, user_id="u1")
tool2 = TodoListTool({"tool_id": "tool_2"}, user_id="u1") tool2 = TodoListTool({"tool_id": "tool_2"}, user_id="u1")
r1_create = tool1.execute_action("todo_create", title="from tool 1") r1_create = tool1.execute_action("create", title="from tool 1")
r2_create = tool2.execute_action("todo_create", title="from tool 2") r2_create = tool2.execute_action("create", title="from tool 2")
r1 = tool1.execute_action("todo_get", todo_id=r1_create["todo_id"]) todo_id_1 = r1_create.split("ID ")[1].split(":")[0].strip()
r2 = tool2.execute_action("todo_get", todo_id=r2_create["todo_id"]) todo_id_2 = r2_create.split("ID ")[1].split(":")[0].strip()
assert r1["status_code"] == 200 r1 = tool1.execute_action("get", todo_id=todo_id_1)
assert r1["todo"]["title"] == "from tool 1" r2 = tool2.execute_action("get", todo_id=todo_id_2)
assert r2["status_code"] == 200 assert "Error" not in r1
assert r2["todo"]["title"] == "from tool 2" 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 # Same user, no tool_id → should default to same value
t3 = TodoListTool({}, user_id="default_user") 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 t3.tool_id == "default_default_user"
assert t4.tool_id == "default_default_user" assert t4.tool_id == "default_default_user"
create_res = t3.execute_action("todo_create", title="shared default") create_res = t3.execute_action("create", title="shared default")
r = t4.execute_action("todo_get", todo_id=create_res["todo_id"]) 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 "Error" not in r
assert r["todo"]["title"] == "shared default" assert "shared default" in r