Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

67 linhas
2.3 KiB

  1. import unittest
  2. from unittest.mock import patch, MagicMock
  3. from services.ssr import extract_headlines
  4. from services.headlines import prepare_headlines
  5. HTML = """<html><head>
  6. <script type="application/ld+json">
  7. {
  8. "@context": "https://schema.org",
  9. "@graph": [
  10. {"@type": "NewsArticle", "headline": "Court rules on landmark case", "datePublished": "2026-09-14T12:00:00Z"},
  11. {"@type": "Organization", "name": "Example News"}
  12. ]
  13. }
  14. </script>
  15. </head><body>
  16. <script id="__NEXT_DATA__" type="application/json">
  17. {"props": {"pageProps": {"channel": {"items": [
  18. {"title": "Senate passes infrastructure bill", "url": "https://example.com/a/1"},
  19. {"title": "Markets", "url": "https://example.com/markets"}
  20. ]}}}}
  21. </script>
  22. </body></html>"""
  23. class TestExtractHeadlines(unittest.TestCase):
  24. def test_json_ld_and_next_data(self):
  25. items = extract_headlines(HTML)
  26. headlines = [h for h, _ in items]
  27. # schema.org headline and Next.js title+url are both recovered.
  28. self.assertIn("Court rules on landmark case", headlines)
  29. self.assertIn("Senate passes infrastructure bill", headlines)
  30. # Organization `name` without an article signal is not treated as a headline.
  31. self.assertNotIn("Example News", headlines)
  32. # Date is parsed from JSON-LD datePublished.
  33. by_title = {h: d for h, d in items}
  34. self.assertIsNotNone(by_title["Court rules on landmark case"])
  35. class TestPrepareHeadlinesSSR(unittest.TestCase):
  36. def setUp(self):
  37. self.stopwords = {"the", "a", "an", "in", "on", "and", "of", "to"}
  38. @patch("services.headlines.requests.get")
  39. def test_ssr_source_is_parsed(self, mock_get):
  40. mock_response = MagicMock()
  41. mock_response.status_code = 200
  42. mock_response.content = HTML.encode("utf-8")
  43. mock_response.text = HTML
  44. mock_get.return_value = mock_response
  45. headlines = prepare_headlines(["https://www.example.com/business/"], self.stopwords)
  46. titles = [h.display_text for h in headlines]
  47. # "Markets" is filtered out by the minimum-word headline check downstream.
  48. self.assertIn("Court rules on landmark case", titles)
  49. self.assertIn("Senate passes infrastructure bill", titles)
  50. self.assertNotIn("Markets", titles)
  51. if __name__ == "__main__":
  52. unittest.main()