Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions app/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
@@ -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()