-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_feed.py
More file actions
307 lines (272 loc) · 8.87 KB
/
Copy pathtest_feed.py
File metadata and controls
307 lines (272 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
import tempfile
import pytest
from iterable.datatypes import FeedIterable
from iterable.helpers.detect import open_iterable
try:
import feedparser # noqa: F401
HAS_FEEDPARSER = True
except ImportError:
HAS_FEEDPARSER = False
@pytest.mark.skipif(not HAS_FEEDPARSER, reason="Feed support requires 'feedparser' package")
class TestFeed:
def test_id(self):
datatype_id = FeedIterable.id()
assert datatype_id == "feed"
def test_flatonly(self):
flag = FeedIterable.is_flatonly()
assert not flag
def test_openclose(self):
"""Test basic open/close"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<description>Test Description</description>
<item>
<title>Test Item</title>
<link>http://example.com/item1</link>
<description>Test Description</description>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_read_one(self):
"""Test reading single entry"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Test Item</title>
<link>http://example.com/item1</link>
<description>Test Description</description>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
record = iterable.read()
assert isinstance(record, dict)
assert "title" in record
assert record["title"] == "Test Item"
assert "feed_title" in record
assert record["feed_title"] == "Test Feed"
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_read_all(self):
"""Test reading all entries"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Item 1</title>
<link>http://example.com/item1</link>
</item>
<item>
<title>Item 2</title>
<link>http://example.com/item2</link>
</item>
<item>
<title>Item 3</title>
<link>http://example.com/item3</link>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
records = list(iterable)
assert len(records) == 3
assert records[0]["title"] == "Item 1"
assert records[1]["title"] == "Item 2"
assert records[2]["title"] == "Item 3"
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_read_bulk(self):
"""Test bulk reading"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Item 1</title>
<link>http://example.com/item1</link>
</item>
<item>
<title>Item 2</title>
<link>http://example.com/item2</link>
</item>
<item>
<title>Item 3</title>
<link>http://example.com/item3</link>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
rows = iterable.read_bulk(2)
assert len(rows) == 2
assert rows[0]["title"] == "Item 1"
assert rows[1]["title"] == "Item 2"
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_has_totals(self):
"""Test totals method"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Item 1</title>
<link>http://example.com/item1</link>
</item>
<item>
<title>Item 2</title>
<link>http://example.com/item2</link>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
assert FeedIterable.has_totals()
total = iterable.totals()
assert total == 2
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_automatic_detection_rss(self):
"""Test automatic format detection for RSS via open_iterable"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Test Item</title>
<link>http://example.com/item1</link>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
with open_iterable(temp_file) as source:
assert isinstance(source, FeedIterable)
row = source.read()
assert "title" in row
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_automatic_detection_atom(self):
"""Test automatic format detection for Atom via open_iterable"""
atom_content = """<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Test Feed</title>
<link href="http://example.com"/>
<entry>
<title>Test Entry</title>
<link href="http://example.com/entry1"/>
</entry>
</feed>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".atom", delete=False) as f:
f.write(atom_content)
temp_file = f.name
try:
with open_iterable(temp_file) as source:
assert isinstance(source, FeedIterable)
row = source.read()
assert "title" in row
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_reset(self):
"""Test reset functionality"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Test Item</title>
<link>http://example.com/item1</link>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
row1 = iterable.read()
iterable.reset()
row2 = iterable.read()
assert row1 == row2
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_write_not_supported(self):
"""Test that writing raises NotImplementedError"""
rss_content = """<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Test Feed</title>
<link>http://example.com</link>
<item>
<title>Test Item</title>
<link>http://example.com/item1</link>
</item>
</channel>
</rss>"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".rss", delete=False) as f:
f.write(rss_content)
temp_file = f.name
try:
iterable = FeedIterable(temp_file)
with pytest.raises(NotImplementedError):
iterable.write({"title": "New Item"})
with pytest.raises(NotImplementedError):
iterable.write_bulk([{"title": "New Item"}])
iterable.close()
finally:
if os.path.exists(temp_file):
os.unlink(temp_file)
def test_missing_dependency(self):
"""Test that missing feedparser raises helpful error"""
# This test would need to mock the import, but we can at least verify
# the error message format is correct by checking the code
pass