-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles_test.py
233 lines (178 loc) · 5.97 KB
/
articles_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
'''
Unit tests for articles modules
'''
import unittest
from testfixtures import compare
from collections import OrderedDict, namedtuple
from articles import Article, Articles
class TestArticlesStorage(unittest.TestCase):
'''
Tests for Articles module.
'''
def setup_test(self):
'''
Make ourselves a way to quickly setup articles storage.
'''
self.article_0 = Article(
0,
"Some title 0",
"2016-09-22",
"Some body text 0",
['tag0', 'tag1', 'tag2', 'tag3']
)
self.article_1 = Article(
1,
"Some title 1",
"2016-09-22",
"Some body text 1",
['tag0', 'tag4', 'tag5', 'tag6']
)
self.article_2 = Article(
2,
"Some title 2",
"2016-09-23",
"Some body text 2",
['tag0', 'tag1', 'tag2', 'tag3']
)
self.article_3 = Article(
3,
"Some title 3",
"2016-09-23",
"Some body text 3",
['tag0', 'tag1', 'tag2', 'tag3']
)
self.article_4 = Article(
4,
"Some title 4",
"2016-09-23",
"Some body text 4",
['tag0', 'tag1', 'tag2', 'tag3']
)
self.articles = Articles()
def teardown_test(self):
self.articles = None
def test_article_add(self):
'''
Test function Articles.add() when it successfully adds a new article.
'''
# Setup test
self.setup_test()
result = self.articles.add(self.article_0)
compare(self.article_0, result)
# Teardown test
self.teardown_test()
def test_article_add_existing(self):
'''
Test function Articles.add() throws exception when it fails to add a new article when article with such id already exists.
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
with self.assertRaises(Exception):
self.articles.add(self.article_0)
# Teardown test
self.teardown_test()
def test_get_all(self):
'''
Test function Articles.get_all
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
self.articles.add(self.article_1)
expected = OrderedDict()
expected[0] = self.article_0
expected[1] = self.article_1
result = self.articles.get_all()
compare(expected, result)
# Teardown test
self.teardown_test()
def test_get(self):
'''
Test that fucntion Articles.get() resturns correct item
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
result = self.articles.get(0)
compare(result, self.article_0)
# Teardown test
self.teardown_test()
def test_get_non_existing(self):
'''
Test that function Articles.get() throws exception when requested item is not there
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
with self.assertRaises(Exception):
self.articles.get(1)
# Teardown test
self.teardown_test()
def test_get_article_ids(self):
'''
Test that function Articles.get_article_ids(date, n) returns correct list of article ids for a given date.
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
self.articles.add(self.article_1)
self.articles.add(self.article_2)
self.articles.add(self.article_3)
self.articles.add(self.article_4)
result = self.articles.get_article_ids('2016-09-23')
expected = [2, 3, 4]
self.assertEqual(result, expected)
# Teardown test
self.teardown_test()
def test_get_count(self):
'''
Test that function Articles.get_count(tag_name, date) returns the number of occurrences of the given tag_name across all articles submitted on the given date.
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
self.articles.add(self.article_1)
self.articles.add(self.article_2)
self.articles.add(self.article_3)
self.articles.add(self.article_4)
result = self.articles.get_count('tag0', '2016-09-23')
self.assertEqual(result, 3)
# Teardown test
self.teardown_test()
def test_get_last_article_ids(self):
'''
Test that function Articles.get_last_article_ids(tag_name, date, n) returns the last n article ids that have tag_name on the given date.
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
self.articles.add(self.article_1)
self.articles.add(self.article_2)
self.articles.add(self.article_3)
self.articles.add(self.article_4)
result = self.articles.get_last_article_ids('tag0', '2016-09-23', 2)
expected = [3, 4]
self.assertEqual(result, expected)
# Teardown test
self.teardown_test()
def test_get_related_tags(self):
'''
Test that function Articles.get_related_tags(tag_name, date) returns the list of tags that are on the articles that the current tag is on for the same day.
'''
# Setup test
self.setup_test()
self.articles.add(self.article_0)
self.articles.add(self.article_1)
self.articles.add(self.article_2)
self.articles.add(self.article_3)
self.articles.add(self.article_4)
result = self.articles.get_related_tags('tag0', '2016-09-22')
result.sort()
expected = ['tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6']
expected.sort()
self.assertEqual(result, expected)
# Teardown test
self.teardown_test()
if __name__ == '__main__':
unittest.main()