From 1d9bf097f9892b10cec9cd27e5159d66ca141b53 Mon Sep 17 00:00:00 2001 From: David Pomerenke <46022183+davidpomerenke@users.noreply.github.com> Date: Wed, 7 Aug 2024 19:32:55 +0200 Subject: [PATCH] tests(fulltext): remove weird fulltext tests --- .../media_impact_monitor/fulltext_coding.py | 2 +- .../fulltext_coding_test.py | 70 ------------------- 2 files changed, 1 insertion(+), 71 deletions(-) diff --git a/backend-python/media_impact_monitor/fulltext_coding.py b/backend-python/media_impact_monitor/fulltext_coding.py index 7a0dce9c..518f2626 100644 --- a/backend-python/media_impact_monitor/fulltext_coding.py +++ b/backend-python/media_impact_monitor/fulltext_coding.py @@ -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}, diff --git a/backend-python/media_impact_monitor/fulltext_coding_test.py b/backend-python/media_impact_monitor/fulltext_coding_test.py index 12975dd2..4cfbe421 100644 --- a/backend-python/media_impact_monitor/fulltext_coding_test.py +++ b/backend-python/media_impact_monitor/fulltext_coding_test.py @@ -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 @@ -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 @@ -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"])