askrobots / dbbasic-rss
Public
RSS feeds from TSV, CSV, JSON or Python lists, for any Python web framework or a static site.
test_basic.py · 6.7 KB · 242 lines
| 1 | """Basic functionality tests for dbbasic-rss""" |
| 2 | |
| 3 | import pytest |
| 4 | from datetime import datetime |
| 5 | from pathlib import Path |
| 6 | import dbbasic_rss as rss |
| 7 | |
| 8 | |
| 9 | def test_create_empty_feed(): |
| 10 | """Test creating an empty feed""" |
| 11 | feed = rss.Feed( |
| 12 | title="Test Feed", |
| 13 | link="https://example.com", |
| 14 | description="A test feed" |
| 15 | ) |
| 16 | assert feed.title == "Test Feed" |
| 17 | assert feed.link == "https://example.com" |
| 18 | assert feed.description == "A test feed" |
| 19 | assert feed.count() == 0 |
| 20 | |
| 21 | |
| 22 | def test_add_post_to_feed(): |
| 23 | """Test adding posts to feed""" |
| 24 | feed = rss.Feed(title="Test", link="https://example.com", description="Test") |
| 25 | |
| 26 | feed.add_post( |
| 27 | title="First Post", |
| 28 | link="https://example.com/first", |
| 29 | description="This is the first post", |
| 30 | pub_date="2025-10-19" |
| 31 | ) |
| 32 | |
| 33 | assert feed.count() == 1 |
| 34 | assert feed.items[0].title == "First Post" |
| 35 | assert feed.items[0].link == "https://example.com/first" |
| 36 | |
| 37 | |
| 38 | def test_add_post_with_categories(): |
| 39 | """Test adding post with categories""" |
| 40 | feed = rss.Feed(title="Test", link="https://example.com", description="Test") |
| 41 | |
| 42 | feed.add_post( |
| 43 | title="Tagged Post", |
| 44 | link="https://example.com/tagged", |
| 45 | description="A tagged post", |
| 46 | categories=["python", "tutorial"] |
| 47 | ) |
| 48 | |
| 49 | assert feed.items[0].categories == ["python", "tutorial"] |
| 50 | |
| 51 | |
| 52 | def test_generate_xml(): |
| 53 | """Test XML generation""" |
| 54 | feed = rss.Feed( |
| 55 | title="Test Feed", |
| 56 | link="https://example.com", |
| 57 | description="Test Description" |
| 58 | ) |
| 59 | |
| 60 | feed.add_post( |
| 61 | title="Test Post", |
| 62 | link="https://example.com/post", |
| 63 | description="Test post description" |
| 64 | ) |
| 65 | |
| 66 | xml = feed.to_xml() |
| 67 | |
| 68 | # Check for required RSS elements |
| 69 | assert '<?xml version="1.0"' in xml |
| 70 | assert '<rss version="2.0"' in xml |
| 71 | assert '<channel>' in xml |
| 72 | assert '<title>Test Feed</title>' in xml |
| 73 | assert '<link>https://example.com</link>' in xml |
| 74 | assert '<description>Test Description</description>' in xml |
| 75 | assert '<item>' in xml |
| 76 | assert '<title>Test Post</title>' in xml |
| 77 | |
| 78 | |
| 79 | def test_validate_feed(): |
| 80 | """Test feed validation""" |
| 81 | # Valid feed |
| 82 | feed = rss.Feed(title="Test", link="https://example.com", description="Test") |
| 83 | feed.add_post(title="Post", link="https://example.com/post", description="Desc") |
| 84 | warnings = feed.validate() |
| 85 | assert len(warnings) == 0 |
| 86 | |
| 87 | # Invalid feed - missing title |
| 88 | feed2 = rss.Feed(title="", link="https://example.com", description="Test") |
| 89 | warnings2 = feed2.validate() |
| 90 | assert len(warnings2) > 0 |
| 91 | assert any("title" in w.lower() for w in warnings2) |
| 92 | |
| 93 | |
| 94 | def test_feed_item_date_parsing(): |
| 95 | """Test date parsing in FeedItem""" |
| 96 | from dbbasic_rss.feed import FeedItem |
| 97 | |
| 98 | # Test ISO format |
| 99 | item1 = FeedItem( |
| 100 | title="Test", |
| 101 | link="https://example.com", |
| 102 | pub_date="2025-10-19T12:00:00" |
| 103 | ) |
| 104 | assert item1.pub_date is not None |
| 105 | assert "2025" in item1.pub_date |
| 106 | |
| 107 | # Test datetime object |
| 108 | dt = datetime(2025, 10, 19, 12, 0, 0) |
| 109 | item2 = FeedItem( |
| 110 | title="Test", |
| 111 | link="https://example.com", |
| 112 | pub_date=dt |
| 113 | ) |
| 114 | assert item2.pub_date is not None |
| 115 | |
| 116 | |
| 117 | def test_xml_escaping(): |
| 118 | """Test that special characters are properly escaped""" |
| 119 | feed = rss.Feed( |
| 120 | title="Test & Special <Characters>", |
| 121 | link="https://example.com", |
| 122 | description="Test 'quotes' and \"double quotes\"" |
| 123 | ) |
| 124 | |
| 125 | feed.add_post( |
| 126 | title="Post with <tags> & ampersands", |
| 127 | link="https://example.com/post", |
| 128 | description="Description with 'quotes'" |
| 129 | ) |
| 130 | |
| 131 | xml = feed.to_xml() |
| 132 | |
| 133 | # Should escape special characters |
| 134 | assert '&' in xml or '&' in xml # Properly escaped or in CDATA |
| 135 | assert '<tags>' not in xml # Should be escaped |
| 136 | |
| 137 | |
| 138 | def test_feed_stats(): |
| 139 | """Test feed statistics methods""" |
| 140 | feed = rss.Feed(title="Test", link="https://example.com", description="Test") |
| 141 | |
| 142 | feed.add_post(title="Old Post", link="https://example.com/1", |
| 143 | description="", pub_date="2025-10-15") |
| 144 | feed.add_post(title="New Post", link="https://example.com/2", |
| 145 | description="", pub_date="2025-10-20") |
| 146 | feed.add_post(title="Mid Post", link="https://example.com/3", |
| 147 | description="", pub_date="2025-10-17") |
| 148 | |
| 149 | assert feed.count() == 3 |
| 150 | # oldest() and newest() should work |
| 151 | oldest = feed.oldest() |
| 152 | newest = feed.newest() |
| 153 | assert oldest is not None |
| 154 | assert newest is not None |
| 155 | |
| 156 | |
| 157 | def test_from_posts(): |
| 158 | """Test generating feed from list of dicts""" |
| 159 | posts = [ |
| 160 | { |
| 161 | 'title': 'Post 1', |
| 162 | 'date': '2025-10-19', |
| 163 | 'content': 'Content 1', |
| 164 | 'slug': 'post-1', |
| 165 | 'author': 'Dan' |
| 166 | }, |
| 167 | { |
| 168 | 'title': 'Post 2', |
| 169 | 'date': '2025-10-18', |
| 170 | 'content': 'Content 2', |
| 171 | 'slug': 'post-2' |
| 172 | } |
| 173 | ] |
| 174 | |
| 175 | feed = rss.from_posts( |
| 176 | posts, |
| 177 | title='Test Blog', |
| 178 | link='https://example.com', |
| 179 | description='A test blog', |
| 180 | url_pattern='https://example.com/{slug}/' |
| 181 | ) |
| 182 | |
| 183 | assert feed.count() == 2 |
| 184 | assert feed.items[0].title == 'Post 1' |
| 185 | assert feed.items[0].link == 'https://example.com/post-1/' |
| 186 | assert feed.items[0].author == 'Dan' |
| 187 | assert feed.items[1].link == 'https://example.com/post-2/' |
| 188 | |
| 189 | |
| 190 | def test_from_posts_with_categories(): |
| 191 | """Test from_posts with category handling""" |
| 192 | posts = [ |
| 193 | { |
| 194 | 'title': 'Tagged Post', |
| 195 | 'url': 'https://example.com/tagged', |
| 196 | 'content': 'Content', |
| 197 | 'tags': 'python,tutorial,web' |
| 198 | } |
| 199 | ] |
| 200 | |
| 201 | feed = rss.from_posts( |
| 202 | posts, |
| 203 | categories_field='tags' |
| 204 | ) |
| 205 | |
| 206 | assert feed.items[0].categories == ['python', 'tutorial', 'web'] |
| 207 | |
| 208 | |
| 209 | def test_write_feed_to_file(tmp_path): |
| 210 | """Test writing feed to file""" |
| 211 | feed = rss.Feed(title="Test", link="https://example.com", description="Test") |
| 212 | feed.add_post(title="Post", link="https://example.com/post", description="Desc") |
| 213 | |
| 214 | output_file = tmp_path / "feed.xml" |
| 215 | feed.write(str(output_file)) |
| 216 | |
| 217 | assert output_file.exists() |
| 218 | content = output_file.read_text() |
| 219 | assert '<?xml version' in content |
| 220 | assert '<rss version="2.0"' in content |
| 221 | assert 'Test' in content |
| 222 | |
| 223 | |
| 224 | def test_content_with_html(): |
| 225 | """Test that HTML content is preserved in CDATA""" |
| 226 | feed = rss.Feed(title="Test", link="https://example.com", description="Test") |
| 227 | |
| 228 | html_content = "<p>This is <strong>HTML</strong> content</p>" |
| 229 | feed.add_post( |
| 230 | title="HTML Post", |
| 231 | link="https://example.com/html", |
| 232 | description="Plain text description", |
| 233 | content=html_content |
| 234 | ) |
| 235 | |
| 236 | xml = feed.to_xml() |
| 237 | assert '<![CDATA[' in xml |
| 238 | assert html_content in xml |
| 239 | |
| 240 | |
| 241 | if __name__ == '__main__': |
| 242 | pytest.main([__file__, '-v']) |