Skip to content

Commit

Permalink
fix 500 error for empty params
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Jan 26, 2025
1 parent 90754f3 commit e4fce05
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
57 changes: 57 additions & 0 deletions api/tests/routers/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,63 @@ def test_search_pagination(client: TestClient, mock_storage: MagicMock) -> None:
assert data["meta"]["prev"] is not None # Should have prev page


def test_search_empty_parameters(client: TestClient, mock_storage: MagicMock) -> None:
# Mock data
timestamp = TwitterTimestamp.from_int(int(datetime(2023, 1, 1, tzinfo=timezone.utc).timestamp() * 1000))

note = Note(
note_id="1234567890123456789",
post_id="2234567890123456789",
language="ja",
topics=[Topic(topic_id=1, label={"ja": "テスト", "en": "test"}, reference_count=1)],
summary="Test summary",
current_status="NEEDS_MORE_RATINGS",
created_at=timestamp,
)

post = Post(
post_id="2234567890123456789",
x_user_id="9876543210123456789",
x_user=XUser(
user_id="9876543210123456789",
name="test_user",
profile_image="http://example.com/image.jpg",
followers_count=100,
following_count=50,
),
text="Test post",
media_details=[],
created_at=timestamp,
like_count=10,
repost_count=5,
impression_count=100,
links=[],
link="http://x.com/test_user/status/2234567890123456789",
)

# Mock storage response for empty parameters
mock_storage.search_notes_with_posts.return_value = [(note, post)]
mock_storage.count_search_results.return_value = 1

# Test search with no parameters
response = client.get("/api/v1/data/search")
assert response.status_code == 200

data = response.json()
assert "data" in data
assert "meta" in data
assert len(data["data"]) == 1

# Verify response structure
result = data["data"][0]
assert result["noteId"] == "1234567890123456789"
assert result["postId"] == "2234567890123456789"
assert result["language"] == "ja"
assert result["summary"] == "Test summary"
assert result["currentStatus"] == "NEEDS_MORE_RATINGS"
assert result["post"]["postId"] == "2234567890123456789"


def test_search_parameters(client: TestClient, mock_storage: MagicMock) -> None:
mock_storage.search_notes_with_posts.return_value = []
mock_storage.count_search_results.return_value = 0
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 @@ -623,8 +623,8 @@ def count_search_results(
with Session(self.engine) as sess:
query = (
sess.query(NoteRecord)
.join(PostRecord, NoteRecord.post_id == PostRecord.post_id)
.join(XUserRecord, PostRecord.user_id == XUserRecord.user_id)
.outerjoin(PostRecord, NoteRecord.post_id == PostRecord.post_id)
.outerjoin(XUserRecord, PostRecord.user_id == XUserRecord.user_id)
)

# Apply note filters
Expand Down

0 comments on commit e4fce05

Please sign in to comment.