-
Notifications
You must be signed in to change notification settings - Fork 15
/
feedlist_test.py
253 lines (201 loc) · 7.34 KB
/
feedlist_test.py
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
from datetime import datetime
from pytest import raises
from libearth.feedlist import (AlreadyExistException, Feed, FeedCategory,
FeedList, OpmlDoc)
from libearth.schema import read
from libearth.tz import utc
def test_count_empty_list():
feeds = FeedList()
assert len(feeds) == 0
def test_count_duplicated_url():
feeds = FeedList()
feeds.add_feed('type', 'title', 'url')
with raises(AlreadyExistException):
feeds.add_feed('type', 'title', 'url')
assert len(feeds) == 1
def test_count_after_remove():
feeds = FeedList()
feeds.add_feed('type', 'title', 'url')
del feeds[0]
assert len(feeds) == 0
XML = """<?xml version="1.0" encoding="ISO-8859-1"?>
<opml version="2.0">
<head>
<title>EarthReader.opml</title>
<dateCreated>Sat, 18 Jun 2005 12:11:52 +0000</dateCreated>
<ownerName>libearth</ownerName>
<ownerEmail>earthreader@librelist.com</ownerEmail>
<expansionState>a,b,c,d</expansionState>
<vertScrollState>1</vertScrollState>
<windowTop>12</windowTop>
<windowLeft>34</windowLeft>
<windowBottom>56</windowBottom>
<windowRight>78</windowRight>
</head>
<body>
<outline text="CNET News.com" type="rss" version="RSS2"
xmlUrl="http://news.com/2547-1_3-0-5.xml"/>
<outline text="test.com" type="rss" xmlUrl="http://test.com"/>
</body>
</opml>"""
XML_CATEGORY = """<?xml version="1.0" encoding="ISO-8859-1"?>
<opml version="2.0">
<head>
<title>EarthReader.opml</title>
<dateCreated>Sat, 18 Jun 2005 12:11:52 +0000</dateCreated>
<ownerName>libearth</ownerName>
<ownerEmail>earthreader@librelist.com</ownerEmail>
<expansionState>a,b,c,d</expansionState>
<vertScrollState>1</vertScrollState>
<windowTop>12</windowTop>
<windowLeft>34</windowLeft>
<windowBottom>56</windowBottom>
<windowRight>78</windowRight>
</head>
<body>
<outline text="Game" title="Game" type="category">
<outline text="valve" title="valve" xmlUrl="http://valve.com/" />
<outline text="nintendo" title="nintendo"
xmlUrl="http://nintendo.com/" />
</outline>
<outline text="Music" title="Music" type="category">
<outline text="capsule" title="capsule"
xmlUrl="http://www.capsule-web.com/" />
</outline>
</body>
</opml>"""
XML_DUPLICAED = """<?xml version="1.0" encoding="utf-8"?><opml version="1.1">
<head>
<title>feeds for test</title>
<dateCreated>Tue, 13 Jul 2013 21:34:05 +0900</dateCreated>
<ownerName>Kjwon15</ownerName>
</head>
<body>
<outline type="category" text="sub1">
<outline type="rss" text="cake" xmlUrl="http://kjwon15.tistory.com/" />
</outline>
<outline type="category" text="sub2">
<outline type="rss" text="cake" xmlUrl="http://kjwon15.tistory.com/" />
</outline>
</body>
</opml>
"""
def test_OpmlDocment():
doc = read(OpmlDoc, XML)
expected_datetime = datetime(2005, 6, 18, 12, 11, 52, tzinfo=utc)
assert doc.head.title == "EarthReader.opml"
assert doc.head.date_created == expected_datetime
assert doc.head.date_modified is None
assert doc.head.owner_name == "libearth"
assert doc.head.expansion_state == ['a', 'b', 'c', 'd']
assert doc.head.window_top == 12
assert len(doc.body.outline) == 2
assert doc.body.outline[0].text == "CNET News.com"
def test_file_not_found():
with raises(IOError):
FeedList('this_file_must_be_not_found.ext')
def test_path_as_string():
feeds = FeedList(XML, is_xml_string=True)
assert feeds.title == "EarthReader.opml"
assert len(feeds) == 2
def test_feed_as_iterator():
feeds = FeedList(XML, is_xml_string=True)
expected = set(['CNET News.com', 'test.com'])
assert set(f.title for f in feeds) == expected
def test_feed_contains_category():
feeds = FeedList(XML_CATEGORY, is_xml_string=True)
expected = {
'Game': ['valve', 'nintendo'],
'Music': ['capsule'],
}
for feed in feeds:
print(feed.title)
assert feed.type == 'category'
for child_feed in feed:
print(child_feed.title)
expected[feed.title].remove(child_feed.title)
assert not expected[feed.title]
expected.pop(feed.title)
assert not expected.keys()
def test_save_as_file(tmpdir):
filename = tmpdir.join('feeds.opml').strpath
print(filename)
feeds = FeedList(XML, is_xml_string=True)
feeds.title = "changed_title"
newfeed = Feed('rss2', 'newfeed', 'http://earthreader.com/rss')
feeds.append(newfeed)
feeds.save_file(filename)
feeds_another = FeedList(filename)
assert feeds_another.title == "changed_title"
assert feeds_another.expansion_state == ['a', 'b', 'c', 'd']
assert feeds_another[2].title == "newfeed"
assert feeds_another.window_top == 12
assert feeds_another.window_left == 34
assert feeds_another.window_bottom == 56
assert feeds_another.window_right == 78
def test_same_feed_on_multi_category():
feeds = FeedList(XML_DUPLICAED, is_xml_string=True)
feeds[0][0].xml_url = "changed"
assert feeds[1][0].xml_url == "changed"
def test_feedTree_in_category():
feed1 = Feed('rss2', 'newfeed', 'http://some.url/rss2')
feed2 = Feed('rss2', 'otherfeed', 'http://different.url/rss2')
feed3 = Feed('rss2', 'anotherfeed', 'http://another.url/rss2')
root = FeedCategory('root')
sub = FeedCategory('sub')
cate3 = FeedCategory('outer')
sub.append(feed2)
root.append(feed1)
root.append(sub)
assert feed1 in root
assert feed1 not in sub
assert feed2 in sub
assert feed2 in root
assert feed3 not in sub
assert feed3 not in sub
assert sub in root
assert cate3 not in root
def test_circular_reference(tmpdir):
cate = FeedCategory('Root category')
subcate = FeedCategory('Sub category')
lastcate = FeedCategory('last category')
cate.append(subcate)
subcate.append(lastcate)
with raises(AlreadyExistException):
subcate.append(cate)
with raises(AlreadyExistException):
lastcate.append(cate)
with raises(AlreadyExistException):
subcate.append(subcate)
feeds = FeedList(XML, is_xml_string=True)
feeds.append(cate)
filename = tmpdir.join('feeds.opml').strpath
print(filename)
feeds.save_file(filename)
def test_feedCategory():
feedCategory = FeedCategory('title')
#'Created' must be datetime.
with raises(TypeError):
feedCategory = FeedCategory('title', created='not datetime')
#Or valid rfc822 string.
feedCategory = FeedCategory('title', 'Mon, 23 Sep 2013 11:49:28 +0900')
def test_get_all_feeds():
feed1 = Feed('rss2', 'feed1', 'http://xmlurl1.com/rss')
feed2 = Feed('rss2', 'feed2', 'http://xmlurl2.com/rss')
feed3 = Feed('rss2', 'feed3', 'http://xmlurl3.com/rss')
cate1 = FeedCategory('category1')
cate2 = FeedCategory('category2')
#feeds can be placed on multi category
cate1.append(feed1)
cate1.append(feed2)
cate2.append(feed2)
cate2.append(feed3)
feedlist = FeedList()
feedlist.append(cate1)
feedlist.append(cate2)
feedlist.append(feed3)
all_feeds = feedlist.get_all_feeds()
assert feed1 in all_feeds
assert feed2 in all_feeds
assert feed3 in all_feeds
assert len(all_feeds) == 3