|
| 1 | +import configparser |
| 2 | +import time |
| 3 | +from pathlib import Path |
| 4 | +from pprint import pprint |
| 5 | + |
| 6 | +import feedparser |
| 7 | +import planet |
| 8 | +import pytest |
| 9 | +from planet.cache import utf8 |
| 10 | + |
| 11 | +# Ensure the `tests/fixtures/` directory exists and feeds are stored there. |
| 12 | +FIXTURES_DIR = Path(__file__).parent / "fixtures" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(name="channel_cache") |
| 16 | +def channel_cache(rss_channel): |
| 17 | + try: |
| 18 | + yield rss_channel._cache |
| 19 | + finally: |
| 20 | + pprint(dict(rss_channel._cache)) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module", name="rss_feed") |
| 24 | +def load_rss_feed(): |
| 25 | + """Load and parse the sample RSS feed fixture.""" |
| 26 | + with open(FIXTURES_DIR / "sample_rss.xml", encoding="utf-8") as rss_file: |
| 27 | + feed_data = rss_file.read() |
| 28 | + return feedparser.parse(feed_data) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope="module", name="atom_feed") |
| 32 | +def load_atom_feed(): |
| 33 | + """Load and parse the sample Atom feed fixture.""" |
| 34 | + with open(FIXTURES_DIR / "sample_atom.xml", encoding="utf-8") as atom_file: |
| 35 | + feed_data = atom_file.read() |
| 36 | + return feedparser.parse(feed_data) |
| 37 | + |
| 38 | + |
| 39 | +def test_newsitem_from_rss(rss_feed, rss_channel): |
| 40 | + """Test that we can create a NewsItem from an RSS feed item.""" |
| 41 | + item = rss_feed.entries[0] |
| 42 | + newsitem = planet.NewsItem(rss_channel, rss_feed.entries[0]["id"]) |
| 43 | + newsitem.update(item) |
| 44 | + assert newsitem.title == "Example Item 1" |
| 45 | + assert newsitem.link == "https://example.com/item1" |
| 46 | + assert newsitem.date[0] == 2021 |
| 47 | + assert newsitem.author == "author@example.com (John Doe)" |
| 48 | + assert newsitem.content == "This is a description of item 1" |
| 49 | + assert newsitem.summary == "This is a description of item 1" |
| 50 | + |
| 51 | + |
| 52 | +def test_newsitem_from_atom(atom_feed, atom_channel): |
| 53 | + """Test that we can create a NewsItem from an RSS feed item.""" |
| 54 | + item = atom_feed.entries[0] |
| 55 | + newsitem = planet.NewsItem(atom_channel, atom_feed.entries[0]["id"]) |
| 56 | + newsitem.update(item) |
| 57 | + assert newsitem.title == "Example Entry 1" |
| 58 | + assert newsitem.link == "https://example.com/entry1" |
| 59 | + # parse the iso timestamp into a time tuple |
| 60 | + assert newsitem.date[0] == 2021 |
| 61 | + assert newsitem.content == "This is a summary of entry 1" |
| 62 | + assert newsitem.summary == "This is a summary of entry 1" |
| 63 | + |
| 64 | + |
| 65 | +def test_caching_newsitem(rss_feed, rss_channel): |
| 66 | + """Test that we can create a NewsItem from an RSS feed item.""" |
| 67 | + item = rss_feed.entries[0] |
| 68 | + newsitem = planet.NewsItem(rss_channel, rss_feed.entries[0]["id"]) |
| 69 | + newsitem.update(item) |
| 70 | + newsitem.cache_write() |
| 71 | + |
| 72 | + # now try read the newsitem, but with the cache; we should be able to |
| 73 | + # get the values before updating |
| 74 | + newsitem = planet.NewsItem(rss_channel, rss_feed.entries[0]["id"]) |
| 75 | + assert newsitem.title == "Example Item 1" |
| 76 | + assert newsitem.link == "https://example.com/item1" |
| 77 | + assert newsitem.date[0] == 2021 |
| 78 | + assert newsitem.author == "author@example.com (John Doe)" |
| 79 | + assert newsitem.content == "This is a description of item 1" |
| 80 | + assert newsitem.summary == "This is a description of item 1" |
| 81 | + |
| 82 | + |
| 83 | +# These tests are aimed at testing the specifications of the cache; we are looking at key structures |
| 84 | +# and internals, so that we can have some sense of implementation consistency. |
| 85 | + |
| 86 | + |
| 87 | +@pytest.fixture(name="news_item") |
| 88 | +def news_item( |
| 89 | + rss_channel, |
| 90 | + rss_feed, |
| 91 | +): |
| 92 | + return planet.NewsItem(rss_channel, rss_feed.entries[0]["id"]) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.fixture(name="sample_entry") |
| 96 | +def sample_entry(rss_feed): |
| 97 | + return rss_feed.entries[0] |
| 98 | + |
| 99 | + |
| 100 | +def test_cache_write_and_read(news_item, sample_entry, channel_cache): |
| 101 | + # First, update the news item using the sample_entry |
| 102 | + news_item.update(sample_entry) |
| 103 | + news_item.cache_write(sync=True) |
| 104 | + |
| 105 | + # Now, inspect the cache to see if keys have been stored correctly |
| 106 | + assert f"{news_item.id} title" in channel_cache |
| 107 | + assert f"{news_item.id} link" in channel_cache |
| 108 | + assert channel_cache[f"{news_item.id} title"] == utf8(sample_entry["title"]) |
| 109 | + assert channel_cache[f"{news_item.id} link"] == utf8(sample_entry["link"]) |
| 110 | + |
| 111 | + # Date value stored as a string representation of the time tuple |
| 112 | + assert f"{news_item.id} updated" in channel_cache |
| 113 | + assert channel_cache[f"{news_item.id} updated"] == " ".join( |
| 114 | + map(str, sample_entry["updated_parsed"]) |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def test_cache_clear(news_item, sample_entry, channel_cache): |
| 119 | + # Update and save to cache |
| 120 | + news_item.update(sample_entry) |
| 121 | + news_item.cache_write(sync=True) |
| 122 | + |
| 123 | + # Ensure keys are there |
| 124 | + assert f"{news_item.id} title" in channel_cache |
| 125 | + |
| 126 | + # Now clear the cache for the news_item |
| 127 | + news_item.cache_clear(sync=True) |
| 128 | + |
| 129 | + # Ensure keys are removed from the cache |
| 130 | + assert f"{news_item.id} title" not in channel_cache |
| 131 | + assert f"{news_item.id} link" not in channel_cache |
| 132 | + assert f"{news_item.id} updated" not in channel_cache |
| 133 | + |
| 134 | + |
| 135 | +def test_cache_key_type(news_item, sample_entry, channel_cache): |
| 136 | + # Update and save to cache |
| 137 | + news_item.update(sample_entry) |
| 138 | + news_item.cache_write(sync=True) |
| 139 | + |
| 140 | + # Ensure keys and types are correct |
| 141 | + assert channel_cache[f"{news_item.id} title"] == "Example Item 1" |
| 142 | + assert channel_cache[f"{news_item.id} title type"] == "string" |
| 143 | + assert channel_cache[f"{news_item.id} updated type"] == "date" |
| 144 | + |
| 145 | + |
| 146 | +def test_cache_reload(news_item, sample_entry, rss_channel): |
| 147 | + # Update and save to cache |
| 148 | + news_item.update(sample_entry) |
| 149 | + news_item.cache_write(sync=True) |
| 150 | + |
| 151 | + # Create a new NewsItem instance with the same cache, and reload |
| 152 | + new_item = planet.NewsItem(rss_channel, f"{news_item.id}") |
| 153 | + new_item.cache_read() |
| 154 | + |
| 155 | + # Check that the data is retrieved as expected |
| 156 | + assert new_item.get("title") == "Example Item 1" |
| 157 | + assert new_item.get("link") == "https://example.com/item1" |
| 158 | + assert new_item.get("date") == sample_entry["date_parsed"] |
| 159 | + |
| 160 | + |
| 161 | +def test_cache_date_field(news_item, sample_entry, rss_channel, channel_cache): |
| 162 | + # Ensure the date field gets cached properly |
| 163 | + news_item.update(sample_entry) |
| 164 | + news_item.cache_write(sync=True) |
| 165 | + |
| 166 | + # Check that the date type is correctly saved as dates |
| 167 | + assert f"{news_item.id} updated" in channel_cache |
| 168 | + assert f"{news_item.id} updated type" in channel_cache |
| 169 | + assert channel_cache[f"{news_item.id} updated type"] == "date" |
| 170 | + |
| 171 | + # Reload item and ensure the date value is parsed correctly |
| 172 | + new_item = planet.NewsItem(rss_channel, f"{news_item.id}") |
| 173 | + new_item.cache_read() |
| 174 | + |
| 175 | + # Verify that the date field is properly restored as date tuple |
| 176 | + assert new_item.get("date") == sample_entry["date_parsed"] |
| 177 | + |
| 178 | + |
| 179 | +def test_delete_key_from_cache(news_item, sample_entry, channel_cache): |
| 180 | + # Update and save to cache |
| 181 | + news_item.update(sample_entry) |
| 182 | + news_item.cache_write(sync=True) |
| 183 | + |
| 184 | + # Delete 'title' key using NewsItem's del_key method |
| 185 | + news_item.del_key("title") |
| 186 | + news_item.cache_write(sync=True) |
| 187 | + |
| 188 | + # Ensure 'title' key is deleted from cache |
| 189 | + assert f"{news_item.id} title" not in channel_cache |
0 commit comments