Goblenus /
WallhavenApi
| 1 | import unittest |
||
| 2 | from wallhavenapi import WallhavenApiV1, Category, Purity, Sorting, Order, TopRange, Color, Type, RequestsLimitError |
||
| 3 | import os |
||
| 4 | import datetime |
||
| 5 | import time |
||
| 6 | |||
| 7 | |||
| 8 | def request_limit_decorator(test_method): |
||
| 9 | def wrapper(self, *args, **kwargs): |
||
| 10 | for _ in range(5): |
||
| 11 | try: |
||
| 12 | result = test_method(self, *args, **kwargs) |
||
| 13 | break |
||
| 14 | except RequestsLimitError: |
||
| 15 | time.sleep(10) |
||
| 16 | |||
| 17 | return result |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 18 | return wrapper |
||
| 19 | |||
| 20 | |||
| 21 | class TestWallhavenApiV1(unittest.TestCase): |
||
| 22 | @classmethod |
||
| 23 | def setUpClass(self): |
||
| 24 | self.wallhaven_api = WallhavenApiV1(api_key=os.getenv('APIKEY', None), verify_connection=False, |
||
| 25 | base_url="http://wallhaven.cc/api/v1") |
||
| 26 | |||
| 27 | def test_search(self): |
||
| 28 | search_data = self.wallhaven_api.search() |
||
| 29 | |||
| 30 | self.assertIn("data", search_data) |
||
| 31 | self.assertIn("meta", search_data) |
||
| 32 | |||
| 33 | @request_limit_decorator |
||
| 34 | def test_search_categories(self): |
||
| 35 | for category in list(Category): |
||
| 36 | search_data = self.wallhaven_api.search(categories=category) |
||
| 37 | |||
| 38 | for wallpaper_data in search_data["data"]: |
||
| 39 | self.assertEqual(category.value, wallpaper_data["category"]) |
||
| 40 | |||
| 41 | @request_limit_decorator |
||
| 42 | def test_search_purities(self): |
||
| 43 | for purity in list(Purity): |
||
| 44 | search_data = self.wallhaven_api.search(purities=purity) |
||
| 45 | |||
| 46 | for wallpaper_data in search_data["data"]: |
||
| 47 | self.assertEqual(purity.value, wallpaper_data["purity"]) |
||
| 48 | |||
| 49 | @request_limit_decorator |
||
| 50 | def test_search_sorting(self): |
||
| 51 | for sorting_method in list(Sorting): |
||
| 52 | search_data = self.wallhaven_api.search(sorting=sorting_method) |
||
| 53 | self.assertIn("data", search_data) |
||
| 54 | |||
| 55 | def search_sorting_dated_added(self, order): |
||
| 56 | def parse_datetime(datetime_string): |
||
| 57 | # Format: 2018-10-31 01:23:10.000000 |
||
| 58 | return datetime.datetime.strptime(datetime_string, "%Y-%m-%d %H:%M:%S") |
||
| 59 | |||
| 60 | search_data = self.wallhaven_api.search(sorting=Sorting.date_added, order=order) |
||
| 61 | for i in range(len(search_data["data"]) - 1): |
||
| 62 | datetime_1 = parse_datetime(search_data["data"][i]["created_at"]) |
||
| 63 | datetime_2 = parse_datetime(search_data["data"][i + 1]["created_at"]) |
||
| 64 | self.assertGreaterEqual(datetime_1, datetime_2) \ |
||
| 65 | if order == Order.desc else self.assertLessEqual(datetime_1, datetime_2) |
||
| 66 | |||
| 67 | @request_limit_decorator |
||
| 68 | def test_search_sorting_dated_added_asc(self): |
||
| 69 | self.search_sorting_dated_added(Order.asc) |
||
| 70 | |||
| 71 | @request_limit_decorator |
||
| 72 | def test_search_sorting_dated_added_desc(self): |
||
| 73 | self.search_sorting_dated_added(Order.desc) |
||
| 74 | |||
| 75 | @request_limit_decorator |
||
| 76 | def search_sorting_views(self, order): |
||
| 77 | search_data = self.wallhaven_api.search(sorting=Sorting.views, order=order) |
||
| 78 | for i in range(len(search_data["data"]) - 1): |
||
| 79 | views_1 = int(search_data["data"][i]["views"]) |
||
| 80 | views_2 = int(search_data["data"][i + 1]["views"]) |
||
| 81 | self.assertGreaterEqual(views_1, views_2) \ |
||
| 82 | if order == Order.desc else self.assertLessEqual(views_1, views_2) |
||
| 83 | |||
| 84 | @request_limit_decorator |
||
| 85 | @unittest.skip("May content unsorted results. Need help") |
||
| 86 | def test_search_sorting_views_asc(self): |
||
| 87 | self.search_sorting_views(Order.asc) |
||
| 88 | |||
| 89 | @request_limit_decorator |
||
| 90 | @unittest.skip("May content unsorted results. Need help") |
||
| 91 | def test_search_sorting_views_desc(self): |
||
| 92 | self.search_sorting_views(Order.desc) |
||
| 93 | |||
| 94 | def search_sorting_favorites(self, order): |
||
| 95 | search_data = self.wallhaven_api.search(sorting=Sorting.favorites, order=order) |
||
| 96 | for i in range(len(search_data["data"]) - 1): |
||
| 97 | favorites_1 = int(search_data["data"][i]["favorites"]) |
||
| 98 | favorites_2 = int(search_data["data"][i + 1]["favorites"]) |
||
| 99 | self.assertGreaterEqual(favorites_1, favorites_2) \ |
||
| 100 | if order == Order.desc else self.assertLessEqual(favorites_1, favorites_2) |
||
| 101 | |||
| 102 | @request_limit_decorator |
||
| 103 | @unittest.skip("May content unsorted results. Need help") |
||
| 104 | def test_search_sorting_favorites_asc(self): |
||
| 105 | self.search_sorting_favorites(Order.asc) |
||
| 106 | |||
| 107 | @request_limit_decorator |
||
| 108 | @unittest.skip("May content unsorted results. Need help") |
||
| 109 | def test_search_sorting_favorites_desc(self): |
||
| 110 | self.search_sorting_favorites(Order.desc) |
||
| 111 | |||
| 112 | @request_limit_decorator |
||
| 113 | @unittest.skip("May content unsorted results. Need help") |
||
| 114 | def test_search_top_range(self): |
||
| 115 | for top_range in list(TopRange): |
||
| 116 | search_data = self.wallhaven_api.search(top_range=top_range, sorting=Sorting.toplist) |
||
| 117 | self.assertIn("data", search_data) |
||
| 118 | |||
| 119 | if str(top_range.value[-1]).endswith("d"): |
||
| 120 | timedelta = datetime.timedelta(days=int(top_range.value[:-1])) |
||
| 121 | elif str(top_range.value[-1]).endswith("w"): |
||
| 122 | timedelta = datetime.timedelta(days=(int(top_range.value[:-1]) * 7)) |
||
| 123 | elif str(top_range.value[-1]).endswith("M"): |
||
| 124 | timedelta = datetime.timedelta(days=(int(top_range.value[:-1]) * 30)) |
||
| 125 | else: |
||
| 126 | timedelta = datetime.timedelta(days=(int(top_range.value[:-1]) * 365)) |
||
| 127 | |||
| 128 | for wallpaper in search_data["data"]: |
||
| 129 | created_at = datetime.datetime.strptime(wallpaper["created_at"], "%Y-%m-%d %H:%M:%S") |
||
| 130 | |||
| 131 | self.assertGreaterEqual(created_at, datetime.datetime.now() - timedelta) |
||
| 132 | |||
| 133 | @request_limit_decorator |
||
| 134 | def test_search_atleast(self): |
||
| 135 | search_data = self.wallhaven_api.search(atleast=(1920, 1080)) |
||
| 136 | for wallpaper in search_data["data"]: |
||
| 137 | self.assertGreaterEqual(int(wallpaper["dimension_x"]), 1920) |
||
| 138 | self.assertGreaterEqual(int(wallpaper["dimension_y"]), 1080) |
||
| 139 | |||
| 140 | @request_limit_decorator |
||
| 141 | def test_search_resolutions(self): |
||
| 142 | search_data = self.wallhaven_api.search(resolutions=(1920, 1080)) |
||
| 143 | for wallpaper in search_data["data"]: |
||
| 144 | self.assertEqual(int(wallpaper["dimension_x"]), 1920) |
||
| 145 | self.assertEqual(int(wallpaper["dimension_y"]), 1080) |
||
| 146 | |||
| 147 | @request_limit_decorator |
||
| 148 | def test_search_ratios(self): |
||
| 149 | search_data = self.wallhaven_api.search(ratios=(16, 10)) |
||
| 150 | for wallpaper in search_data["data"]: |
||
| 151 | self.assertEqual(float(wallpaper["ratio"]), 1.6) |
||
| 152 | |||
| 153 | @request_limit_decorator |
||
| 154 | def test_search_colors(self): |
||
| 155 | for color in list(Color): |
||
| 156 | search_data = self.wallhaven_api.search(colors=color) |
||
| 157 | for wallpaper in search_data["data"]: |
||
| 158 | self.assertIn("colors", wallpaper) |
||
| 159 | self.assertIn("#{}".format(color.value), wallpaper["colors"]) |
||
| 160 | |||
| 161 | @request_limit_decorator |
||
| 162 | def test_search_page(self): |
||
| 163 | search_data = self.wallhaven_api.search() |
||
| 164 | |||
| 165 | self.assertIn("last_page", search_data["meta"]) |
||
| 166 | |||
| 167 | if int(search_data["meta"]["last_page"]) > 1: |
||
| 168 | search_data = self.wallhaven_api.search(page=2) |
||
| 169 | |||
| 170 | self.assertIn("data", search_data) |
||
| 171 | self.assertIn("meta", search_data) |
||
| 172 | self.assertIn("current_page", search_data["meta"]) |
||
| 173 | self.assertEqual(2, int(search_data["meta"]["current_page"])) |
||
| 174 | |||
| 175 | @request_limit_decorator |
||
| 176 | def test_wallpaper(self): |
||
| 177 | search_data = self.wallhaven_api.search() |
||
| 178 | |||
| 179 | if not len(search_data["data"]): |
||
| 180 | return |
||
| 181 | |||
| 182 | for wallpaper_temp in search_data["data"]: |
||
| 183 | self.assertIn("id", wallpaper_temp) |
||
| 184 | |||
| 185 | wallpaper = self.wallhaven_api.wallpaper(search_data["data"][0]["id"]) |
||
| 186 | self.assertIn("data", wallpaper) |
||
| 187 | |||
| 188 | @request_limit_decorator |
||
| 189 | def test_tag(self): |
||
| 190 | search_data = self.wallhaven_api.search() |
||
| 191 | |||
| 192 | for wallpaper_temp in search_data["data"]: |
||
| 193 | self.assertIn("id", wallpaper_temp) |
||
| 194 | wallpaper = self.wallhaven_api.wallpaper(wallpaper_temp["id"]) |
||
| 195 | self.assertIn("data", wallpaper) |
||
| 196 | self.assertIn("tags", wallpaper["data"]) |
||
| 197 | |||
| 198 | if not len(wallpaper["data"]["tags"]): |
||
| 199 | continue |
||
| 200 | |||
| 201 | for tag_temp in wallpaper["data"]["tags"]: |
||
| 202 | self.assertIn("id", tag_temp) |
||
| 203 | |||
| 204 | tag = self.wallhaven_api.tag(wallpaper["data"]["tags"][0]['id']) |
||
| 205 | |||
| 206 | self.assertIn("data", tag) |
||
| 207 | |||
| 208 | break |
||
| 209 | |||
| 210 | @request_limit_decorator |
||
| 211 | def test_search_query_uploader(self): |
||
| 212 | search_data = self.wallhaven_api.search() |
||
| 213 | |||
| 214 | self.assertIn("data", search_data) |
||
| 215 | |||
| 216 | if not len(search_data["data"]): |
||
| 217 | return None |
||
| 218 | |||
| 219 | wallpaper = self.wallhaven_api.wallpaper(search_data["data"][0]["id"]) |
||
| 220 | |||
| 221 | self.assertIn("data", wallpaper) |
||
| 222 | self.assertIn("uploader", wallpaper["data"]) |
||
| 223 | self.assertIn("username", wallpaper["data"]["uploader"]) |
||
| 224 | |||
| 225 | search_data = self.wallhaven_api.search(q="@{}".format(wallpaper["data"]["uploader"]["username"])) |
||
| 226 | |||
| 227 | self.assertIn("data", search_data) |
||
| 228 | |||
| 229 | self.assertGreater(len(search_data["data"]), 0) |
||
| 230 | |||
| 231 | @request_limit_decorator |
||
| 232 | def test_search_query_id(self): |
||
| 233 | search_data = self.wallhaven_api.search() |
||
| 234 | |||
| 235 | for wallpaper_temp in search_data["data"]: |
||
| 236 | self.assertIn("id", wallpaper_temp) |
||
| 237 | wallpaper = self.wallhaven_api.wallpaper(wallpaper_temp["id"]) |
||
| 238 | self.assertIn("data", wallpaper) |
||
| 239 | self.assertIn("tags", wallpaper["data"]) |
||
| 240 | |||
| 241 | if not len(wallpaper["data"]["tags"]): |
||
| 242 | continue |
||
| 243 | |||
| 244 | for tag_temp in wallpaper["data"]["tags"]: |
||
| 245 | self.assertIn("id", tag_temp) |
||
| 246 | |||
| 247 | search_data = self.wallhaven_api.search(q="id:{}".format(wallpaper["data"]["tags"][0]['id'])) |
||
| 248 | |||
| 249 | self.assertIn("data", search_data) |
||
| 250 | self.assertGreater(len(search_data["data"]), 0) |
||
| 251 | break |
||
| 252 | |||
| 253 | @request_limit_decorator |
||
| 254 | def test_search_query_like(self): |
||
| 255 | search_data = self.wallhaven_api.search() |
||
| 256 | |||
| 257 | if not len(search_data): |
||
| 258 | return |
||
| 259 | |||
| 260 | search_data = self.wallhaven_api.search(q="like:{}".format(search_data["data"][0]['id'])) |
||
| 261 | |||
| 262 | self.assertIn("data", search_data) |
||
| 263 | self.assertIn("meta", search_data) |
||
| 264 | |||
| 265 | @request_limit_decorator |
||
| 266 | def test_search_query_type(self): |
||
| 267 | for image_type in Type: |
||
| 268 | search_data = self.wallhaven_api.search(q="type:{}".format(image_type.value)) |
||
| 269 | |||
| 270 | self.assertIn("data", search_data) |
||
| 271 | self.assertIn("meta", search_data) |
||
| 272 | |||
| 273 | for wallpaper in search_data["data"]: |
||
| 274 | self.assertIn("file_type", wallpaper) |
||
| 275 | if image_type in [Type.jpeg, Type.jpg]: |
||
| 276 | self.assertTrue(str(wallpaper["file_type"]).endswith(Type.jpeg.value) \ |
||
| 277 | or str(wallpaper["file_type"]).endswith(Type.jpg.value)) |
||
| 278 | else: |
||
| 279 | self.assertTrue(str(wallpaper["file_type"]).endswith(image_type.value)) |
||
| 280 | |||
| 281 | @request_limit_decorator |
||
| 282 | def test_settings(self): |
||
| 283 | result = self.wallhaven_api.settings() |
||
| 284 | |||
| 285 | assert result is None if self.wallhaven_api.api_key is None else result is not None |
||
| 286 | |||
| 287 | @request_limit_decorator |
||
| 288 | def test_collections(self): |
||
| 289 | # Just a random user, who has a collection with wallpapers |
||
| 290 | result = self.wallhaven_api.collections("ThorRagnarok") |
||
| 291 | |||
| 292 | assert "data" in result |
||
| 293 | assert len(result["data"]) |
||
| 294 | assert "id" in result["data"][0] |
||
| 295 | |||
| 296 | return result |
||
| 297 | |||
| 298 | @request_limit_decorator |
||
| 299 | def test_collection_wallpapers(self, page=None): |
||
| 300 | collections = self.test_collections() |
||
| 301 | |||
| 302 | collections_wallpapers = self.wallhaven_api.collection_wallpapers("ThorRagnarok", |
||
| 303 | collections["data"][0]["id"], page=page) |
||
| 304 | |||
| 305 | assert "data" in collections_wallpapers |
||
| 306 | assert "meta" in collections_wallpapers |
||
| 307 | assert len(collections_wallpapers["data"]) |
||
| 308 | |||
| 309 | @request_limit_decorator |
||
| 310 | def test_collection_wallpapers_page(self): |
||
| 311 | self.test_collection_wallpapers(2) |
||
| 312 | |||
| 313 | @request_limit_decorator |
||
| 314 | def test_my_collections(self): |
||
| 315 | result = self.wallhaven_api.my_collections() |
||
| 316 | |||
| 317 | assert "data" in result |
||
| 318 | |||
| 319 | |||
| 320 | if __name__ == '__main__': |
||
| 321 | unittest.main() |