From da25abffd045cb4fb875dd59f33102c2035f5ac7 Mon Sep 17 00:00:00 2001 From: "Jose Luis Mejia Rojas (WORK)" Date: Mon, 13 Jul 2026 10:51:27 -0500 Subject: [PATCH] Fix task bugs and add search filter --- app/service.py | 17 +++++++++++----- tests/test_service.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/test_service.py diff --git a/app/service.py b/app/service.py index 8022b93..20fb305 100644 --- a/app/service.py +++ b/app/service.py @@ -12,13 +12,20 @@ def list_tasks(status: str | None = None, q: str | None = None) -> list[dict[str filtered: list[dict[str, Any]] = [] for task in tasks: - # Instructor note: intentional bug for the lab. - # This uses the literal string "status" instead of the query parameter value. - if status and task["status"] != "status": + if status is not None and task["status"] != status: continue - # Instructor note: partial feature for the lab. - # The route already accepts `q`, but search is not implemented yet. + if q is not None: + query = q.lower() + haystack = " ".join( + [ + str(task.get("title", "")).lower(), + str(task.get("description", "")).lower(), + ] + ) + if query not in haystack: + continue + filtered.append(task) return filtered diff --git a/tests/test_service.py b/tests/test_service.py new file mode 100644 index 0000000..b65ced0 --- /dev/null +++ b/tests/test_service.py @@ -0,0 +1,45 @@ +import unittest +from unittest.mock import patch + +from app import service + + +class ListTasksTests(unittest.TestCase): + def test_filters_tasks_by_status(self) -> None: + fake_tasks = [ + {"id": 1, "title": "Open task", "status": "open"}, + {"id": 2, "title": "Done task", "status": "done"}, + ] + + with patch("app.service.load_tasks", return_value=fake_tasks): + result = service.list_tasks(status="open") + + self.assertEqual(result, [fake_tasks[0]]) + + def test_filters_tasks_by_query_case_insensitively(self) -> None: + fake_tasks = [ + {"id": 1, "title": "Launch recap", "description": "", "status": "open"}, + {"id": 2, "title": "Fix login", "description": "Handle launch bug", "status": "done"}, + {"id": 3, "title": "Plan workshop", "description": "Discuss launch timeline", "status": "open"}, + ] + + with patch("app.service.load_tasks", return_value=fake_tasks): + result = service.list_tasks(q="LAUNCH") + + self.assertEqual(result, [fake_tasks[0], fake_tasks[1], fake_tasks[2]]) + + def test_filters_tasks_by_status_and_query_together(self) -> None: + fake_tasks = [ + {"id": 1, "title": "Launch recap", "description": "", "status": "open"}, + {"id": 2, "title": "Plan workshop", "description": "Prepare launch materials", "status": "done"}, + {"id": 3, "title": "Plan workshop", "description": "Prepare launch materials", "status": "open"}, + ] + + with patch("app.service.load_tasks", return_value=fake_tasks): + result = service.list_tasks(status="open", q="plan") + + self.assertEqual(result, [fake_tasks[2]]) + + +if __name__ == "__main__": + unittest.main()