Tristan Nolde 92d14b5824
Ingest schedule (#391)
Includes a new "app" running the ingestion jobs (aka fetch_news and
find_missing_companies + enrich_company_financials) on a schedule.

This also fixes an issue with the previous schedule implementation by
persisting the schedule in a file that survives new deployment and
continues where it left off.
2023-12-04 19:05:52 +01:00

92 lines
3.4 KiB
Python

"""Testing apps/fetch_news.py."""
from unittest.mock import Mock, patch
from aki_prj23_transparenzregister.apps import fetch_news
from aki_prj23_transparenzregister.models.news import News
def test_import() -> None:
assert fetch_news is not None
@patch("aki_prj23_transparenzregister.apps.fetch_news.MongoNewsService")
@patch("aki_prj23_transparenzregister.apps.fetch_news.MongoConnector")
@patch("aki_prj23_transparenzregister.apps.fetch_news.HandelsblattRSS")
@patch("aki_prj23_transparenzregister.apps.fetch_news.TagesschauAPI")
def test_schedule(
mock_tagesschau_api: Mock,
mock_handelsblatt_rss: Mock,
mock_mongo_connector: Mock,
mock_mongo_news_service: Mock,
) -> None:
mock_mongo_connector.return_value = Mock()
mock_mongo_news_service.return_value = Mock(
get_by_id=Mock(return_value=None), insert=Mock(return_value=Mock)
)
mock_news_handelsblatt = [
News(
id="test",
title="The oldest and strongest emotion of mankind is fear, and the oldest and strongest kind of fear is fear of the unknown",
date="2023-11-10T09:10:27+0100",
source_url="https://www.handelsblatt.com/test",
text="",
),
News(
id="test",
title="That is not dead which can eternal lie, And with strange aeons even death may die.",
date="2023-11-10T09:10:27+0100",
source_url="https://www.handelsblatt.com/test",
text="",
),
]
mock_news_tagesschau = [
News(
id="test",
title="I know always that I am an outsider; a stranger in this century and among those who are still men.",
date="2023-11-10T09:10:27+0100",
source_url="https://www.tagesschau.de/test",
text="",
),
News(
id="test",
title="Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn.",
date="2023-11-10T09:10:27+0100",
source_url="https://www.tagesschau.de/test",
text="",
),
]
mock_tagesschau_api.return_value = Mock(
get_news_for_category=Mock(return_value=mock_news_tagesschau)
)
mock_handelsblatt_rss.return_value = Mock(
get_news_for_category=Mock(return_value=mock_news_handelsblatt)
)
assert fetch_news.main(Mock()) == len(mock_news_handelsblatt + mock_news_tagesschau)
@patch("aki_prj23_transparenzregister.apps.fetch_news.MongoNewsService")
@patch("aki_prj23_transparenzregister.apps.fetch_news.MongoConnector")
@patch("aki_prj23_transparenzregister.apps.fetch_news.HandelsblattRSS")
@patch("aki_prj23_transparenzregister.apps.fetch_news.TagesschauAPI")
def test_schedule_error(
mock_tagesschau_api: Mock,
mock_handelsblatt_rss: Mock,
mock_mongo_connector: Mock,
mock_mongo_news_service: Mock,
) -> None:
mock_mongo_connector.return_value = Mock()
mock_mongo_news_service.return_value = Mock(
get_by_id=Mock(return_value=None), insert=Mock(return_value=Mock)
)
mock_news_handelsblatt = None
mock_news_tagesschau = None
mock_tagesschau_api.return_value = Mock(
get_news_for_category=Mock(return_value=mock_news_tagesschau)
)
mock_handelsblatt_rss.return_value = Mock(
get_news_for_category=Mock(return_value=mock_news_handelsblatt)
)
assert fetch_news.main(Mock()) == 0