def test_promoted(self, promoted: bool, promoted_qs: str) -> None: # act api_uri = Pr0gramm._get_api_uri(flags=0, promoted=promoted) (_, _, _, query_string, _) = urlsplit(api_uri) query = parse_qs(query_string) # assert self.assertEqual([promoted_qs], query['promoted'])
def test__check_missing_value(self) -> None: # arrange config_in = dict() # type: Dict[Any, Any] # act config_out = Pr0gramm.check_config(config_in) # assert self.assertIsNone(config_out['tags'])
def test_flags(self, flags: int, flags_qs: str) -> None: # act api_uri = Pr0gramm._get_api_uri(flags=flags, promoted=False) (_, _, _, query_string, _) = urlsplit(api_uri) query = parse_qs(query_string) # assert self.assertEqual([flags_qs], query['flags'])
def test__check_value(self, tags: str) -> None: # arrange config_in = dict(tags=tags) # act config_out = Pr0gramm.check_config(config_in) # assert self.assertEqual(tags, config_out['tags'])
def test_description_config(self) -> None: # act description = Pr0gramm.info() # assert assert isinstance(description.config, dict) self.assertIn('promoted', description.config) self.assertIn('tags', description.config)
def test__check_missing_value_defaults_to_true(self) -> None: # arrange config_in = dict() # type: Dict[Any, Any] # act config_out = Pr0gramm.check_config(config_in) # assert self.assertTrue(config_out['promoted'])
def test__check_value(self, promoted: bool) -> None: # arrange config_in = dict(promoted=promoted) # act config_out = Pr0gramm.check_config(config_in) # assert self.assertEqual(promoted, config_out['promoted'])
def test_older(self, older: Optional[int], older_qs: str) -> None: # act api_uri = Pr0gramm._get_api_uri(flags=0, promoted=False, older=older) (_, _, _, query_string, _) = urlsplit(api_uri) query = parse_qs(query_string) # assert self.assertEqual([older_qs], query.get('older', [None]))
def test_tags(self, tags: Optional[str], tags_qs: str) -> None: # act api_uri = Pr0gramm._get_api_uri(flags=0, promoted=False, tags=tags) (_, _, _, query_string, _) = urlsplit(api_uri) query = parse_qs(query_string) # assert self.assertEqual([tags_qs], query['tags'])
def test_base(self) -> None: # act api_uri = Pr0gramm._get_api_uri(flags=0, promoted=False) (scheme, netloc, path, _, _) = urlsplit(api_uri) # assert self.assertEqual('https', scheme) self.assertEqual('pr0gramm.com', netloc) self.assertEqual('/api/items/get', path)
def test_reset_done(self) -> None: # arrange crawler = Pr0gramm() crawler._older = 1337 # act crawler._reset() # assert self.assertIsNone(crawler._older)
def test_no_reset_before_end(self) -> None: # arrange crawler = Pr0gramm(flags=1, promoted=True, tags='!s:1000') crawler._remote_fetcher = _FILE_FETCHER crawler._reset_before_next_crawl = False # act crawler.crawl() # assert self.assertFalse(crawler._reset_before_next_crawl)
def test_crawl_cursor(self, promoted: bool, expected_cursor: Optional[int]) -> None: # arrange crawler = Pr0gramm(flags=1, promoted=promoted, tags='!s:1000') crawler._remote_fetcher = _FILE_FETCHER crawler._older = None # act crawler._crawl() # assert self.assertEqual(crawler._older, expected_cursor, msg='promoted={!r}'.format(promoted))
def test_crawl_images(self) -> None: # arrange crawler = Pr0gramm(flags=1, promoted=True, tags='!s:15000') crawler._remote_fetcher = _FILE_FETCHER expected_images = ImageCollection() expected_images.add( Image( uri='https://img.pr0gramm.com/2018/10/11/ac41a1fbcc3abf09.png', source='https://pr0gramm.com/new/2782197', )) expected_images.add( Image( uri='https://img.pr0gramm.com/2015/07/05/5624d30ec6e743b2.png', source='https://pr0gramm.com/new/879293', )) # act images = crawler._crawl() # assert self.assertSetEqual(images, expected_images) for expected_image in expected_images: for image in images: if image == expected_image: # sources are irrelevant for equality, need to be checked manually self.assertEqual(image.source, expected_image.source)
def test__check_wrong_type(self) -> None: wrong_types = [True, 23, 4.2, [], (), {}, self] for wrong_type in wrong_types: # assert with self.assertRaises(TypeError, msg=repr(wrong_type)): Pr0gramm.check_config(dict(tags=wrong_type))
def test__check_wrong_tag_value(self) -> None: wrong_values = ['', ' \n\t', ' !', '! \n\t'] for wrong_value in wrong_values: # assert with self.assertRaises(ValueError, msg=repr(wrong_value)): Pr0gramm.check_config(dict(tags=wrong_value))