diff --git a/tests/conftest.py b/tests/conftest.py index f13eeeb..e2976ce 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -99,7 +99,7 @@ def user_enrollment_samples( @fixture def mock_storage( - user_enrollment_samples: List[UserEnrollment], topic_samples: List[Topic] + user_enrollment_samples: List[UserEnrollment], topic_samples: List[Topic], note_samples: List[Note] ) -> Generator[MagicMock, None, None]: mock = MagicMock(spec=Storage) @@ -114,7 +114,26 @@ def _get_user_enrollment_by_participant_id(participant_id: ParticipantId) -> Use def _get_topics() -> Generator[Topic, None, None]: yield from topic_samples + def _get_notes( + note_ids=None, created_at_from=None, created_at_to=None, topic_ids=None, post_ids=None, language=None + ) -> Generator[Note, None, None]: + for note in note_samples: + if note_ids is not None and note.note_id not in note_ids: + continue + if created_at_from is not None and note.created_at < created_at_from: + continue + if created_at_to is not None and note.created_at > created_at_to: + continue + if topic_ids is not None and not set(topic_ids).issubset({topic.topic_id for topic in note.topics}): + continue + if post_ids is not None and note.post_id not in post_ids: + continue + if language is not None and note.language != language: + continue + yield note + mock.get_topics.side_effect = _get_topics + mock.get_notes.side_effect = _get_notes yield mock diff --git a/tests/routers/test_data.py b/tests/routers/test_data.py index 46b31f3..cfe21f6 100644 --- a/tests/routers/test_data.py +++ b/tests/routers/test_data.py @@ -28,56 +28,31 @@ def test_notes_get(client: TestClient, note_samples: List[Note]) -> None: def test_notes_get_has_note_id_filter(client: TestClient, note_samples: List[Note]) -> None: - response = client.get(f"/api/v1/data/notes/?noteId={note_samples[0].note_id},{note_samples[2].note_id}") + response = client.get(f"/api/v1/data/notes/?noteIds={note_samples[0].note_id}¬eIds={note_samples[2].note_id}") assert response.status_code == 200 res_json = response.json() assert res_json == { "data": [json.loads(note_samples[0].model_dump_json()), json.loads(note_samples[2].model_dump_json())] } - def test_notes_get_has_created_at_filter_from_and_to(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtFrom=2006-7-25 00:00:00&createdAtTo=2006-7-30 23:59:59") + response = client.get("/api/v1/data/notes/?createdAtFrom=1152921601000&createdAtTo=1152921603000") assert response.status_code == 200 res_json = response.json() - assert res_json == {"data": [json.loads(note_samples[1].model_dump_json())]} + assert res_json == {"data": [json.loads(note_samples[1].model_dump_json()) for i in (1, 2, 3)]} def test_notes_get_has_created_at_filter_from(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtFrom=2006-7-25 00:00:00") + response = client.get("/api/v1/data/notes/?createdAtFrom=1152921601000") assert response.status_code == 200 res_json = response.json() - assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (1, 2)]} + assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (1, 2, 3, 4)]} def test_notes_get_has_created_at_filter_to(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtTo=2006-7-30 00:00:00") - assert response.status_code == 200 - res_json = response.json() - assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (0, 1)]} - - -def test_notes_get_created_at_range_filter_accepts_integer(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtFrom=1153921700000&createdAtTo=1154921800000") - assert response.status_code == 200 - res_json = response.json() - assert res_json == {"data": [json.loads(note_samples[1].model_dump_json())]} - - -def test_notes_get_created_at_from_filter_accepts_integer(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtFrom=1153921700000") - assert response.status_code == 200 - res_json = response.json() - assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (1, 2)]} - - -def test_notes_get_created_at_to_filter_accepts_integer(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtTo=1154921800000") + response = client.get("/api/v1/data/notes/?createdAtTo=1152921603000") assert response.status_code == 200 res_json = response.json() - assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (0, 1)]} + assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (0, 1, 2, 3)]} -def test_notes_get_timestamp_out_of_range(client: TestClient, note_samples: List[Note]) -> None: - response = client.get("/api/v1/data/notes/?createdAtFrom=1153921700&createdAtTo=1153921700") - assert response.status_code == 422