From ee3420ddd9aeb81500055edc8f0508b76dd2fc73 Mon Sep 17 00:00:00 2001 From: kota-yata Date: Fri, 12 Apr 2024 14:52:16 +0900 Subject: [PATCH] test_notes_get passed --- tests/conftest.py | 24 ++++++++++++++++++++++- tests/routers/test_data.py | 39 +++++++------------------------------- 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index fe3ece0..2474fb1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -114,7 +114,10 @@ def user_enrollment_samples( @fixture def mock_storage( - user_enrollment_samples: List[UserEnrollment], topic_samples: List[Topic], post_samples: List[Post] + user_enrollment_samples: List[UserEnrollment], + topic_samples: List[Topic], + post_samples: List[Post], + note_samples: List[Note], ) -> Generator[MagicMock, None, None]: mock = MagicMock(spec=Storage) @@ -129,7 +132,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 def _get_posts() -> Generator[Post, None, None]: yield from post_samples diff --git a/tests/routers/test_data.py b/tests/routers/test_data.py index 96f42c7..47ce0e5 100644 --- a/tests/routers/test_data.py +++ b/tests/routers/test_data.py @@ -91,56 +91,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") + 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 (1, 2)]} + assert res_json == {"data": [json.loads(note_samples[i].model_dump_json()) for i in (0, 1, 2, 3)]} -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") - 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_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