RSS feeds from TSV, CSV, JSON or Python lists, for any Python web framework or a static site.

0 downloads

dbbasic-rss/tests/test_generators.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-rss.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-rss.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-rss.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Initial release of dbbasic-rss v1.0.0 bbf4a01 1 year, 1 month ago History
test_generators.py · 4.1 KB · 161 lines
1"""Tests for data source generators"""
2
3import pytest
4from pathlib import Path
5import dbbasic_rss as rss
6
7
8FIXTURES_DIR = Path(__file__).parent / 'fixtures'
9
10
11def test_from_csv():
12 """Test generating feed from CSV file"""
13 csv_file = FIXTURES_DIR / 'sample.csv'
14
15 feed = rss.from_csv(
16 str(csv_file),
17 title='CSV Blog',
18 link='https://example.com',
19 description='From CSV'
20 )
21
22 assert feed.count() == 2
23 assert feed.items[0].title == 'First Post'
24 assert feed.items[0].link == 'https://example.com/first'
25
26
27def test_from_json():
28 """Test generating feed from JSON file"""
29 json_file = FIXTURES_DIR / 'sample.json'
30
31 feed = rss.from_json(
32 str(json_file),
33 title='JSON Blog',
34 link='https://example.com',
35 description='From JSON'
36 )
37
38 assert feed.count() == 2
39 assert feed.items[0].title == 'JSON Post 1'
40 assert feed.items[0].author == 'Test Author'
41
42
43def test_from_tsv():
44 """Test generating feed from TSV file"""
45 tsv_file = FIXTURES_DIR / 'sample.tsv'
46
47 feed = rss.from_tsv(
48 str(tsv_file),
49 title='TSV Blog',
50 link='https://example.com',
51 description='From TSV',
52 url_pattern='https://example.com/{slug}/',
53 categories_field='tags'
54 )
55
56 assert feed.count() == 3
57 assert feed.items[0].title == 'Getting Started with Python'
58 assert feed.items[0].link == 'https://example.com/getting-started-python/'
59 assert feed.items[0].author == 'Dan Quellhorst'
60 assert 'python' in feed.items[0].categories
61 assert 'tutorial' in feed.items[0].categories
62
63
64def test_generate_auto_detect_tsv(tmp_path):
65 """Test generate() auto-detecting TSV format"""
66 tsv_file = FIXTURES_DIR / 'sample.tsv'
67 output_file = tmp_path / 'output.xml'
68
69 rss.generate(
70 str(tsv_file),
71 str(output_file),
72 title='Auto-detected',
73 link='https://example.com',
74 url_pattern='https://example.com/{slug}/'
75 )
76
77 assert output_file.exists()
78 content = output_file.read_text()
79 assert 'Getting Started with Python' in content
80 assert '<rss version="2.0"' in content
81
82
83def test_generate_auto_detect_csv(tmp_path):
84 """Test generate() auto-detecting CSV format"""
85 csv_file = FIXTURES_DIR / 'sample.csv'
86 output_file = tmp_path / 'output.xml'
87
88 rss.generate(
89 str(csv_file),
90 str(output_file),
91 title='CSV Auto',
92 link='https://example.com'
93 )
94
95 assert output_file.exists()
96 content = output_file.read_text()
97 assert 'First Post' in content
98
99
100def test_generate_auto_detect_json(tmp_path):
101 """Test generate() auto-detecting JSON format"""
102 json_file = FIXTURES_DIR / 'sample.json'
103 output_file = tmp_path / 'output.xml'
104
105 rss.generate(
106 str(json_file),
107 str(output_file),
108 title='JSON Auto',
109 link='https://example.com'
110 )
111
112 assert output_file.exists()
113 content = output_file.read_text()
114 assert 'JSON Post 1' in content
115
116
117def test_generate_unsupported_format():
118 """Test that unsupported formats raise error"""
119 with pytest.raises(ValueError, match="Unsupported file format"):
120 rss.generate('file.txt', 'output.xml')
121
122
123def test_url_pattern_substitution():
124 """Test URL pattern field substitution"""
125 posts = [
126 {'title': 'Test', 'id': '123', 'slug': 'test-post', 'content': 'Content'}
127 ]
128
129 feed = rss.from_posts(
130 posts,
131 url_pattern='https://example.com/posts/{id}/{slug}/'
132 )
133
134 assert feed.items[0].link == 'https://example.com/posts/123/test-post/'
135
136
137def test_custom_field_mapping():
138 """Test custom field name mapping"""
139 posts = [
140 {
141 'headline': 'Custom Title',
142 'published_date': '2025-10-19',
143 'body': 'Post body',
144 'permalink': 'https://example.com/custom'
145 }
146 ]
147
148 feed = rss.from_posts(
149 posts,
150 title_field='headline',
151 date_field='published_date',
152 content_field='body',
153 url_field='permalink'
154 )
155
156 assert feed.items[0].title == 'Custom Title'
157 assert feed.items[0].link == 'https://example.com/custom'
158
159
160if __name__ == '__main__':
161 pytest.main([__file__, '-v'])