-
Notifications
You must be signed in to change notification settings - Fork 9
/
tests.py
282 lines (206 loc) · 11 KB
/
tests.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
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
import unittest
from wallhavenapi import WallhavenApiV1, Category, Purity, Sorting, Order, TopRange, Color, Type, RequestsLimitError
import os
import datetime
import time
class TestWallhavenApiV1(unittest.TestCase):
@classmethod
def setUpClass(self):
self.wallhaven_api = WallhavenApiV1(api_key=os.getenv('APIKEY', None), verify_connection=False,
base_url="http://wallhaven.cc/api/v1",
requestslimit_timeout=(15, 5))
def test_search(self):
search_data = self.wallhaven_api.search()
self.assertIn("data", search_data)
self.assertIn("meta", search_data)
def test_search_categories(self):
for category in list(Category):
search_data = self.wallhaven_api.search(categories=category)
for wallpaper_data in search_data["data"]:
self.assertEqual(category.value, wallpaper_data["category"])
def test_search_purities(self):
for purity in list(Purity):
search_data = self.wallhaven_api.search(purities=purity)
for wallpaper_data in search_data["data"]:
self.assertEqual(purity.value, wallpaper_data["purity"])
def test_search_sorting(self):
for sorting_method in list(Sorting):
search_data = self.wallhaven_api.search(sorting=sorting_method)
self.assertIn("data", search_data)
def search_sorting_dated_added(self, order):
def parse_datetime(datetime_string):
# Format: 2018-10-31 01:23:10.000000
return datetime.datetime.strptime(datetime_string, "%Y-%m-%d %H:%M:%S")
search_data = self.wallhaven_api.search(sorting=Sorting.date_added, order=order)
for i in range(len(search_data["data"]) - 1):
datetime_1 = parse_datetime(search_data["data"][i]["created_at"])
datetime_2 = parse_datetime(search_data["data"][i + 1]["created_at"])
self.assertGreaterEqual(datetime_1, datetime_2) \
if order == Order.desc else self.assertLessEqual(datetime_1, datetime_2)
def test_search_sorting_dated_added_asc(self):
self.search_sorting_dated_added(Order.asc)
def test_search_sorting_dated_added_desc(self):
self.search_sorting_dated_added(Order.desc)
def search_sorting_views(self, order):
search_data = self.wallhaven_api.search(sorting=Sorting.views, order=order)
for i in range(len(search_data["data"]) - 1):
views_1 = int(search_data["data"][i]["views"])
views_2 = int(search_data["data"][i + 1]["views"])
self.assertGreaterEqual(views_1, views_2) \
if order == Order.desc else self.assertLessEqual(views_1, views_2)
@unittest.skip("May content unsorted results. Need help")
def test_search_sorting_views_asc(self):
self.search_sorting_views(Order.asc)
@unittest.skip("May content unsorted results. Need help")
def test_search_sorting_views_desc(self):
self.search_sorting_views(Order.desc)
def search_sorting_favorites(self, order):
search_data = self.wallhaven_api.search(sorting=Sorting.favorites, order=order)
for i in range(len(search_data["data"]) - 1):
favorites_1 = int(search_data["data"][i]["favorites"])
favorites_2 = int(search_data["data"][i + 1]["favorites"])
self.assertGreaterEqual(favorites_1, favorites_2) \
if order == Order.desc else self.assertLessEqual(favorites_1, favorites_2)
@unittest.skip("May content unsorted results. Need help")
def test_search_sorting_favorites_asc(self):
self.search_sorting_favorites(Order.asc)
@unittest.skip("May content unsorted results. Need help")
def test_search_sorting_favorites_desc(self):
self.search_sorting_favorites(Order.desc)
@unittest.skip("May content unsorted results. Need help")
def test_search_top_range(self):
for top_range in list(TopRange):
search_data = self.wallhaven_api.search(top_range=top_range, sorting=Sorting.toplist)
self.assertIn("data", search_data)
if str(top_range.value[-1]).endswith("d"):
timedelta = datetime.timedelta(days=int(top_range.value[:-1]))
elif str(top_range.value[-1]).endswith("w"):
timedelta = datetime.timedelta(days=(int(top_range.value[:-1]) * 7))
elif str(top_range.value[-1]).endswith("M"):
timedelta = datetime.timedelta(days=(int(top_range.value[:-1]) * 30))
else:
timedelta = datetime.timedelta(days=(int(top_range.value[:-1]) * 365))
for wallpaper in search_data["data"]:
created_at = datetime.datetime.strptime(wallpaper["created_at"], "%Y-%m-%d %H:%M:%S")
self.assertGreaterEqual(created_at, datetime.datetime.now() - timedelta)
def test_search_atleast(self):
search_data = self.wallhaven_api.search(atleast=(1920, 1080))
for wallpaper in search_data["data"]:
self.assertGreaterEqual(int(wallpaper["dimension_x"]), 1920)
self.assertGreaterEqual(int(wallpaper["dimension_y"]), 1080)
def test_search_resolutions(self):
search_data = self.wallhaven_api.search(resolutions=(1920, 1080))
for wallpaper in search_data["data"]:
self.assertEqual(int(wallpaper["dimension_x"]), 1920)
self.assertEqual(int(wallpaper["dimension_y"]), 1080)
def test_search_ratios(self):
search_data = self.wallhaven_api.search(ratios=(16, 10))
for wallpaper in search_data["data"]:
self.assertEqual(float(wallpaper["ratio"]), 1.6)
def test_search_colors(self):
for color in list(Color):
search_data = self.wallhaven_api.search(colors=color)
for wallpaper in search_data["data"]:
self.assertIn("colors", wallpaper)
self.assertIn("#{}".format(color.value), wallpaper["colors"])
def test_search_page(self):
search_data = self.wallhaven_api.search()
self.assertIn("last_page", search_data["meta"])
if int(search_data["meta"]["last_page"]) > 1:
search_data = self.wallhaven_api.search(page=2)
self.assertIn("data", search_data)
self.assertIn("meta", search_data)
self.assertIn("current_page", search_data["meta"])
self.assertEqual(2, int(search_data["meta"]["current_page"]))
def test_wallpaper(self):
search_data = self.wallhaven_api.search()
if not len(search_data["data"]):
return
for wallpaper_temp in search_data["data"]:
self.assertIn("id", wallpaper_temp)
wallpaper = self.wallhaven_api.wallpaper(search_data["data"][0]["id"])
self.assertIn("data", wallpaper)
def test_tag(self):
search_data = self.wallhaven_api.search()
for wallpaper_temp in search_data["data"]:
self.assertIn("id", wallpaper_temp)
wallpaper = self.wallhaven_api.wallpaper(wallpaper_temp["id"])
self.assertIn("data", wallpaper)
self.assertIn("tags", wallpaper["data"])
if not len(wallpaper["data"]["tags"]):
continue
for tag_temp in wallpaper["data"]["tags"]:
self.assertIn("id", tag_temp)
tag = self.wallhaven_api.tag(wallpaper["data"]["tags"][0]['id'])
self.assertIn("data", tag)
break
def test_search_query_uploader(self):
search_data = self.wallhaven_api.search()
self.assertIn("data", search_data)
if not len(search_data["data"]):
return None
wallpaper = self.wallhaven_api.wallpaper(search_data["data"][0]["id"])
self.assertIn("data", wallpaper)
self.assertIn("uploader", wallpaper["data"])
self.assertIn("username", wallpaper["data"]["uploader"])
search_data = self.wallhaven_api.search(q="@{}".format(wallpaper["data"]["uploader"]["username"]))
self.assertIn("data", search_data)
self.assertGreater(len(search_data["data"]), 0)
def test_search_query_id(self):
search_data = self.wallhaven_api.search()
for wallpaper_temp in search_data["data"]:
self.assertIn("id", wallpaper_temp)
wallpaper = self.wallhaven_api.wallpaper(wallpaper_temp["id"])
self.assertIn("data", wallpaper)
self.assertIn("tags", wallpaper["data"])
if not len(wallpaper["data"]["tags"]):
continue
for tag_temp in wallpaper["data"]["tags"]:
self.assertIn("id", tag_temp)
search_data = self.wallhaven_api.search(q="id:{}".format(wallpaper["data"]["tags"][0]['id']))
self.assertIn("data", search_data)
self.assertGreater(len(search_data["data"]), 0)
break
def test_search_query_like(self):
search_data = self.wallhaven_api.search()
if not len(search_data):
return
search_data = self.wallhaven_api.search(q="like:{}".format(search_data["data"][0]['id']))
self.assertIn("data", search_data)
self.assertIn("meta", search_data)
def test_search_query_type(self):
for image_type in Type:
search_data = self.wallhaven_api.search(q="type:{}".format(image_type.value))
self.assertIn("data", search_data)
self.assertIn("meta", search_data)
for wallpaper in search_data["data"]:
self.assertIn("file_type", wallpaper)
if image_type in [Type.jpeg, Type.jpg]:
self.assertTrue(str(wallpaper["file_type"]).endswith(Type.jpeg.value) \
or str(wallpaper["file_type"]).endswith(Type.jpg.value))
else:
self.assertTrue(str(wallpaper["file_type"]).endswith(image_type.value))
def test_settings(self):
result = self.wallhaven_api.settings()
assert result is None if self.wallhaven_api.api_key is None else result is not None
def test_collections(self):
# Just a random user, who has a collection with wallpapers
result = self.wallhaven_api.collections("ThorRagnarok")
assert "data" in result
assert len(result["data"])
assert "id" in result["data"][0]
return result
def test_collection_wallpapers(self, page=None):
collections = self.test_collections()
collections_wallpapers = self.wallhaven_api.collection_wallpapers("ThorRagnarok",
collections["data"][0]["id"], page=page)
assert "data" in collections_wallpapers
assert "meta" in collections_wallpapers
assert len(collections_wallpapers["data"])
def test_collection_wallpapers_page(self):
self.test_collection_wallpapers(2)
def test_my_collections(self):
result = self.wallhaven_api.my_collections()
assert "data" in result
if __name__ == '__main__':
unittest.main()