Skip to content

Commit

Permalink
test_notes_get passed
Browse files Browse the repository at this point in the history
  • Loading branch information
kota-yata committed Apr 12, 2024
1 parent ec777ad commit c59358d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
21 changes: 20 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down
39 changes: 7 additions & 32 deletions tests/routers/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}&noteIds={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

0 comments on commit c59358d

Please sign in to comment.