Пример #1
0
 def test_paged_search_no_given_seed(self):
     seed = "abc123"
     for i in range(1, 4):
         with open(
                 get_resource_file(
                     os.path.join("test_paged_search",
                                  f"with_seed_page_{i}.json")), 'r') as fp:
             if i == 1:
                 responses.add(
                     responses.GET,
                     f"https://wallhaven.cc/api/v1/search?page={i}",
                     status=200,
                     json=json.load(fp))
             else:
                 responses.add(
                     responses.GET,
                     f"https://wallhaven.cc/api/v1/search?page={i}&seed={seed}",
                     status=200,
                     json=json.load(fp))
     w = Wallhaven()
     wallpaper_list = []
     pages = w.get_search_pages()
     for wallpapers, meta in pages:
         self.assertIsInstance(meta, Meta)
         self.assertEqual(meta.seed, seed)
         for w in wallpapers:
             self.assertIsInstance(w, Wallpaper)
         wallpaper_list.extend(wallpapers)
     self.assertEqual(len(wallpaper_list), 72)
Пример #2
0
 def test_connection_error(self):
     w = Wallhaven()
     responses.add(responses.GET,
                   'https://wallhaven.cc/api/v1/search',
                   body=requests.exceptions.ConnectionError())
     with self.assertRaises(requests.exceptions.ConnectionError):
         w.search()
Пример #3
0
 def test_error_code_response(self):
     w = Wallhaven()
     responses.add(responses.GET,
                   'https://wallhaven.cc/api/v1/search',
                   status=429)
     with self.assertRaises(RateLimitError):
         w.search()
Пример #4
0
 def test_error_code_response(self):
     w = Wallhaven()
     for e in [201, 400, 404, 422, 500, 502, 503]:
         responses.add(responses.GET,
                       'https://wallhaven.cc/api/v1/search',
                       status=e)
         with self.assertRaises(requests.exceptions.RequestException):
             w.search()
Пример #5
0
 def test_invalid_json(self):
     responses.add(responses.GET,
                   'https://wallhaven.cc/api/v1/search',
                   status=200,
                   body='{}}')
     w = Wallhaven()
     with self.assertRaises(json.JSONDecodeError):
         w.search()
Пример #6
0
 def test_paged_collection_invalid_parameters(self):
     w = Wallhaven()
     with self.assertRaises(KeyError):
         next(w.get_search_pages(test_parameter='1111'))
     with self.assertRaises(ValueError):
         next(w.get_search_pages(purity='1111'))
     with self.assertRaises(AttributeError):
         next(w.get_search_pages(page='-1'))
Пример #7
0
 def test_get_endpoint(self):
     url = "https://wallhaven.cc/api/v1/search"
     with open(get_resource_file("test_search.json"), 'r') as fp:
         responses.add(responses.GET,
                       f"https://wallhaven.cc/api/v1/search",
                       status=200,
                       json=json.load(fp))
     w = Wallhaven()
     return_dict = w.get_endpoint(url)
     self.assertIsInstance(return_dict, dict)
Пример #8
0
 def test_valid_user_settings_json(self):
     with open(get_resource_file("test_user_settings.json"), 'r') as fp:
         mock_json = json.load(fp)
         responses.add(responses.GET,
                       'https://wallhaven.cc/api/v1/settings',
                       status=200,
                       json=mock_json)
         w = Wallhaven(api_key='testkeyisinvalid')
         settings = w.get_user_settings()
         self.assertIsInstance(settings, UserSettings)
         self.assertEqual(dataclasses.asdict(settings), mock_json['data'])
Пример #9
0
 def test_valid_collections_json(self):
     with open(get_resource_file("test_collections.json"), 'r') as fp:
         mock_json = json.load(fp)
         responses.add(responses.GET,
                       'https://wallhaven.cc/api/v1/collections',
                       status=200,
                       json=mock_json)
         w = Wallhaven(api_key='testkeyisinvalid')
         collections = w.get_collections()
         self.assertIsInstance(collections, list)
         for c in collections:
             self.assertIsInstance(c, Collection)
Пример #10
0
 def test_valid_tag_json(self):
     tag_id = 4
     with open(get_resource_file("test_tag.json"), 'r') as fp:
         mock_json = json.load(fp)
         responses.add(responses.GET,
                       'https://wallhaven.cc/api/v1/tag/{}'.format(
                           str(tag_id)),
                       status=200,
                       json=mock_json)
         w = Wallhaven()
         tag = w.get_tag(tag_id)
         self.assertIsInstance(tag, Tag)
         self.assertEqual(dataclasses.asdict(tag), mock_json['data'])
Пример #11
0
 def test_valid_wallpaper_json(self):
     wallpaper_id = '6k3oox'
     with open(get_resource_file("test_wallpaper.json"), 'r') as fp:
         mock_json = json.load(fp)
         responses.add(
             responses.GET,
             'https://wallhaven.cc/api/v1/w/{}'.format(wallpaper_id),
             status=200,
             json=mock_json)
         w = Wallhaven()
         wallpaper = w.get_wallpaper(wallpaper_id)
         self.assertIsInstance(wallpaper, Wallpaper)
         self.assertEqual(dataclasses.asdict(wallpaper), mock_json['data'])
Пример #12
0
 def test_paged_collection_invalid_parameters(self):
     w = Wallhaven()
     username = '******'
     collection_id = 1
     with self.assertRaises(KeyError):
         next(
             w.get_collection_pages(username,
                                    collection_id,
                                    test_parameter='1111'))
     with self.assertRaises(ValueError):
         next(w.get_collection_pages(username, collection_id,
                                     purity='1111'))
     with self.assertRaises(AttributeError):
         next(w.get_collection_pages(username, collection_id, page='-1'))
Пример #13
0
 def test_valid_user_collections_json(self):
     with open(get_resource_file("test_user_collections.json"), 'r') as fp:
         mock_json = json.load(fp)
         username = '******'
         responses.add(
             responses.GET,
             'https://wallhaven.cc/api/v1/collections/{}'.format(username),
             status=200,
             json=mock_json)
         w = Wallhaven()
         collections = w.get_collections(username=username)
         self.assertIsInstance(collections, list)
         for c in collections:
             self.assertIsInstance(c, Collection)
Пример #14
0
 def test_invalid_parameters(self):
     w = Wallhaven()
     with self.assertRaises(ValueError):
         w.search(purity='1111')
     with self.assertRaises(KeyError):
         w.search(test_parameter='1111')
     with self.assertRaises(ValueError):
         w.search(q='id:r4e5tg')
Пример #15
0
 def test_valid_search_json_with_page_in_kwargs(self):
     with open(get_resource_file("test_search.json"), 'r') as fp:
         mock_json = json.load(fp)
         responses.add(responses.GET,
                       'https://wallhaven.cc/api/v1/search',
                       status=200,
                       json=mock_json)
         w = Wallhaven()
         result_wallpapers, result_meta = w.search(**{'page': 1})
         self.assertEqual(dataclasses.asdict(result_meta),
                          mock_json['meta'])
         self.assertIsInstance(result_wallpapers, list)
         for w in result_wallpapers:
             self.assertIsInstance(w, Wallpaper)
         self.assertIsInstance(result_meta, Meta)
Пример #16
0
 def test_valid_user_collection_json(self):
     with open(get_resource_file("test_user_collection.json"), 'r') as fp:
         mock_json = json.load(fp)
         username = '******'
         collection_id = 1
         responses.add(
             responses.GET,
             'https://wallhaven.cc/api/v1/collections/{}/{}'.format(
                 username, str(collection_id)),
             status=200,
             json=mock_json)
         w = Wallhaven()
         collection, meta = w.get_collection(username, collection_id)
         self.assertIsInstance(collection, list),
         for w in collection:
             self.assertIsInstance(w, Wallpaper)
         self.assertIsInstance(meta, Meta)
Пример #17
0
 def test_invalid_parameters(self):
     w = Wallhaven()
     username = '******'
     collection_id = 1
     with self.assertRaises(KeyError):
         w.get_collection(username, collection_id, test_parameter='1111')
     with self.assertRaises(ValueError):
         w.get_collection(username, collection_id, purity='1111')
     with self.assertRaises(ValueError):
         # noinspection PyTypeChecker
         w.get_collection(username, collection_id, page='-1')
Пример #18
0
 def test_paged_collection(self):
     username = '******'
     collection_id = 1
     for i in range(1, 4):
         with open(
                 get_resource_file(
                     os.path.join("test_paged_collection",
                                  f"page{i}.json")), 'r') as fp:
             responses.add(
                 responses.GET,
                 f"https://wallhaven.cc/api/v1/collections/{username}/{collection_id}?page={i}",
                 status=200,
                 json=json.load(fp))
     w = Wallhaven()
     wallpaper_list = []
     collection_pages = w.get_collection_pages(username, collection_id)
     for wallpapers, meta in collection_pages:
         self.assertIsInstance(meta, Meta)
         for w in wallpapers:
             self.assertIsInstance(w, Wallpaper)
         wallpaper_list.extend(wallpapers)
     self.assertEqual(len(wallpaper_list), 72)
Пример #19
0
 def test_init_no_api_key(self):
     w = Wallhaven()
     self.assertIsInstance(w, Wallhaven)
Пример #20
0
 def test_without_api_key(self):
     w = Wallhaven()
     with self.assertRaises(AttributeError):
         w.get_user_settings()
Пример #21
0
 def test_without_api_key(self):
     w = Wallhaven()
     with self.assertRaises(AttributeError):
         w.get_collections()
Пример #22
0
 def test_init_with_api_key(self):
     api_key = 'testkeyisinvalid'
     w = Wallhaven(api_key=api_key)
     self.assertIsInstance(w, Wallhaven)
     self.assertEqual(getattr(w, '_Wallhaven__api_key'), api_key)