Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OR search for topic #90

Merged
merged 2 commits into from
Aug 10, 2024
Merged
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
10 changes: 10 additions & 0 deletions api/tests/routers/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ def test_notes_get_has_created_at_filter_to(client: TestClient, note_samples: Li
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, 2, 3)]}


def test_notes_get_has_topic_id_filter(client: TestClient, note_samples: List[Note]) -> None:
correct_notes = [note for note in note_samples if note_samples[0].topics[0] in note.topics]
response = client.get(f"/api/v1/data/notes/?topicIds={note_samples[0].topics[0].topic_id.serialize()}")
assert response.status_code == 200
res_json = response.json()
assert res_json == {
"data": [json.loads(correct_notes[i].model_dump_json()) for i in range(correct_notes.__len__())]
}
9 changes: 9 additions & 0 deletions common/birdxplorer_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,15 @@ class LanguageIdentifier(str, Enum):
PT = "pt"
DE = "de"
FR = "fr"
OTHER = "other"

@classmethod
def normalize(cls, value: str, default: str = "other") -> str:
try:
cls(value)
return value
except ValueError:
return default


class TopicLabelString(NonEmptyTrimmedString): ...
Expand Down
4 changes: 2 additions & 2 deletions common/birdxplorer_common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def get_notes(
subq = (
select(NoteTopicAssociation.note_id)
.group_by(NoteTopicAssociation.note_id)
.having(func.array_agg(NoteTopicAssociation.topic_id) == topic_ids)
.having(func.bool_or(NoteTopicAssociation.topic_id.in_(topic_ids)))
.subquery()
)
query = query.join(subq, NoteRecord.note_id == subq.c.note_id)
Expand All @@ -263,7 +263,7 @@ def get_notes(
)
for topic in note_record.topics
],
language=note_record.language,
language=LanguageIdentifier.normalize(note_record.language),
summary=note_record.summary,
created_at=note_record.created_at,
)
Expand Down
3 changes: 2 additions & 1 deletion common/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ def test_get_notes_by_topic_ids(
topics = note_samples[0].topics
topic_ids: List[TopicId] = [TopicId.from_int(0)]
expected = sorted(
[note for note in note_samples if note.topics == topics],
[note for note in note_samples if topics[0] in note.topics],
key=lambda note: note.note_id,
)
actual = sorted(list(storage.get_notes(topic_ids=topic_ids)), key=lambda note: note.note_id)

assert expected == actual


Expand Down
Loading