Skip to content

Commit

Permalink
tests(fulltext): remove weird fulltext tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpomerenke committed Aug 7, 2024
1 parent 69b0278 commit 1d9bf09
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 71 deletions.
2 changes: 1 addition & 1 deletion backend-python/media_impact_monitor/fulltext_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
# @cache
# @backoff.on_exception(backoff.expo, [RateLimitError1, RateLimitError2], max_time=120)
async def code_fulltext(text: str) -> dict | None:
if len(text) < 50:
if len(text) < 20:
return None
messages = [
{"role": "system", "content": system_prompt},
Expand Down
70 changes: 0 additions & 70 deletions backend-python/media_impact_monitor/fulltext_coding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ async def test_code_fulltext():
assert result["science"] <= 2 # Should not be very much about science
assert result["activism_sentiment"] is not None
assert result["policy_sentiment"] is not None
assert "demand" in result["activism_reasoning"].lower()
assert "action" in result["policy_reasoning"].lower()
assert len(result["topics"]) <= 10 # Should not exceed 10 topics


Expand All @@ -46,20 +44,14 @@ def test_code_many_fulltexts():
assert len(results) == 3

# Check first text (protest)
assert "protest" in " ".join(results[0]["topics"]).lower()
assert results[0]["activism"] >= 3
assert results[0]["activism_sentiment"] is not None

# Check second text (policy)
assert "policy" in " ".join(results[1]["topics"]).lower()
assert results[1]["policy"] >= 3
assert results[1]["policy_sentiment"] is not None

# Check third text (science)
assert (
"study" in " ".join(results[2]["topics"]).lower()
or "science" in " ".join(results[2]["topics"]).lower()
)
assert results[2]["science"] >= 3


Expand All @@ -74,70 +66,8 @@ async def test_code_fulltext_complex_text():
"""
result = await code_fulltext(text)
assert result is not None
assert all(
topic in result["topics"]
for topic in [
"IPCC report",
"climate action",
"protests",
"emissions regulations",
"carbon tax",
]
)
assert result["activism"] >= 2
assert result["policy"] >= 3
assert result["science"] >= 2
assert result["activism_sentiment"] is not None
assert result["policy_sentiment"] is not None
assert result["science_sentiment"] is not None


@pytest.mark.asyncio
async def test_rate_limiting():
texts = ["Climate text " + str(i) for i in range(5)]
start_time = asyncio.get_event_loop().time()
await asyncio.gather(*[code_fulltext(text) for text in texts])
end_time = asyncio.get_event_loop().time()
duration = end_time - start_time
assert duration >= 0.25, "Rate limiting should prevent too rapid requests"


def test_get_aspect_sentiment():
text = "The climate protest was peaceful and raised important awareness."
result = get_aspect_sentiment(text, "protest")
assert isinstance(result, dict)
assert "sentiment" in result
assert isinstance(result["sentiment"], (int, float))
assert result["sentiment"] > 0 # Should be positive sentiment


def test_get_aspect_sentiment_negative():
text = "The climate policy changes are insufficient and disappointing."
result = get_aspect_sentiment(text, "climate policy")
assert isinstance(result, dict)
assert "sentiment" in result
assert isinstance(result["sentiment"], (int, float))
assert result["sentiment"] < 0 # Should be negative sentiment


def test_get_aspect_sentiment_invalid_aspect():
with pytest.raises(AssertionError):
get_aspect_sentiment("Some text", "invalid_aspect")


@pytest.mark.asyncio
async def test_code_fulltext_error_handling():
# Simulate a BadRequestError
from unittest.mock import patch
from litellm import BadRequestError

with patch(
"media_impact_monitor.fulltext_coding.acompletion",
side_effect=BadRequestError("Test error"),
):
result = await code_fulltext("Some text that would cause an error")
assert result is None


if __name__ == "__main__":
pytest.main(["-v"])

0 comments on commit 1d9bf09

Please sign in to comment.