|
1 | 1 | from datetime import datetime, timezone |
| 2 | +from types import SimpleNamespace |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
5 | 6 | from video_rss_aggregator.application.ports import FetchedFeed, FetchedFeedEntry |
| 7 | +from video_rss_aggregator.infrastructure import feed_source as feed_source_module |
6 | 8 | from video_rss_aggregator.infrastructure.feed_source import HttpFeedSource |
7 | 9 |
|
8 | 10 |
|
@@ -71,3 +73,75 @@ async def test_http_feed_source_fetches_and_maps_entries() -> None: |
71 | 73 | ), |
72 | 74 | ), |
73 | 75 | ) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.anyio |
| 79 | +async def test_http_feed_source_falls_back_to_updated_timestamp() -> None: |
| 80 | + client = FakeAsyncClient( |
| 81 | + FakeResponse( |
| 82 | + """ |
| 83 | + <feed xmlns="http://www.w3.org/2005/Atom"> |
| 84 | + <title>Example atom feed</title> |
| 85 | + <link href="https://example.com" /> |
| 86 | + <entry> |
| 87 | + <title>First</title> |
| 88 | + <id>first-guid</id> |
| 89 | + <updated>2024-01-02T03:04:05Z</updated> |
| 90 | + <link href="https://example.com/watch?v=1" /> |
| 91 | + </entry> |
| 92 | + </feed> |
| 93 | + """ |
| 94 | + ) |
| 95 | + ) |
| 96 | + adapter = HttpFeedSource(client) |
| 97 | + |
| 98 | + feed = await adapter.fetch("https://example.com/feed.xml") |
| 99 | + |
| 100 | + assert feed == FetchedFeed( |
| 101 | + title="Example atom feed", |
| 102 | + site_url="https://example.com", |
| 103 | + entries=( |
| 104 | + FetchedFeedEntry( |
| 105 | + source_url="https://example.com/watch?v=1", |
| 106 | + title="First", |
| 107 | + guid="first-guid", |
| 108 | + published_at=datetime(2024, 1, 2, 3, 4, 5, tzinfo=timezone.utc), |
| 109 | + ), |
| 110 | + ), |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.anyio |
| 115 | +async def test_http_feed_source_ignores_malformed_parsed_dates(monkeypatch) -> None: |
| 116 | + client = FakeAsyncClient(FakeResponse("ignored")) |
| 117 | + adapter = HttpFeedSource(client) |
| 118 | + |
| 119 | + def fake_parse(_text: str) -> SimpleNamespace: |
| 120 | + return SimpleNamespace( |
| 121 | + feed={"title": "Example feed", "link": "https://example.com"}, |
| 122 | + entries=[ |
| 123 | + { |
| 124 | + "title": "First", |
| 125 | + "id": "first-guid", |
| 126 | + "link": "https://example.com/watch?v=1", |
| 127 | + "published_parsed": (2024, 1), |
| 128 | + } |
| 129 | + ], |
| 130 | + ) |
| 131 | + |
| 132 | + monkeypatch.setattr(feed_source_module.feedparser, "parse", fake_parse) |
| 133 | + |
| 134 | + feed = await adapter.fetch("https://example.com/feed.xml") |
| 135 | + |
| 136 | + assert feed == FetchedFeed( |
| 137 | + title="Example feed", |
| 138 | + site_url="https://example.com", |
| 139 | + entries=( |
| 140 | + FetchedFeedEntry( |
| 141 | + source_url="https://example.com/watch?v=1", |
| 142 | + title="First", |
| 143 | + guid="first-guid", |
| 144 | + published_at=None, |
| 145 | + ), |
| 146 | + ), |
| 147 | + ) |
0 commit comments