def test_user_discovery_from_user_profile_pages(self, mock_requests): mal_scraper.discover_users(use_web=False) # Empty cache mock_requests.always_mock( 'http://myanimelist.net/profile/SparkleBunnies', 'user_test_page') mal_scraper.get_user_stats('SparkleBunnies') # Populate cache assert mal_scraper.discover_users(use_web=False) == { 'ChannelOrange', 'SparkleBunnies', 'Woodenspoon', 'Brandon', 'Sacchie', 'Teddy_Bear56', 'ThisNameSucks', 'Z6890', 'Zeally', 'Daedalus', 'IIDarkII', 'Exmortus420', 'stonemask', 'HaXXspetten', 'Padgit', 'Ichigo_Shiba', 'BlackFIFA19', 'AkitoKazuki', 'Speeku', 'no_good_name', 'Kagami', 'BKZekken', }
def test_user_does_not_exist(self, mock_requests): mock_requests.always_mock( 'http://myanimelist.net/profile/asdghiuhunircg', 'user_does_not_exist', status=404, ) with pytest.raises(mal_scraper.RequestError) as err: mal_scraper.get_user_stats('asdghiuhunircg') assert err.value.code == mal_scraper.RequestError.Code.does_not_exist
def test_user_discovery_from_user_profile_pages(self, mock_requests): mal_scraper.discover_users(use_web=False) # Empty cache mock_requests.always_mock('http://myanimelist.net/profile/SparkleBunnies', 'user_test_page') mal_scraper.get_user_stats('SparkleBunnies') # Populate cache assert mal_scraper.discover_users(use_web=False) == { 'ChannelOrange', 'SparkleBunnies', 'Woodenspoon', 'Brandon', 'Sacchie', 'Teddy_Bear56', 'ThisNameSucks', 'Z6890', 'Zeally', 'Daedalus', 'IIDarkII', 'Exmortus420', 'stonemask', 'HaXXspetten', 'Padgit', 'Ichigo_Shiba', 'BlackFIFA19', 'AkitoKazuki', 'Speeku', 'no_good_name', 'Kagami', 'BKZekken', }
def test_user_last_online_date(self, mock_requests): # May 4, 8:09 AM mock_requests.always_mock(self.TEST_LAST_ONLINE_DATE_PAGE, 'user_last_online_date') _, info = mal_scraper.get_user_stats(self.TEST_LAST_ONLINE_DATE_USER) this_year = datetime.utcnow().year assert datetime(year=this_year, month=5, day=4, hour=8, minute=9) == info['last_online']
def test_user_last_online_now(self, mock_requests): mock_requests.always_mock(self.TEST_LAST_ONLINE_NOW_PAGE, 'user_last_online_now') data = mal_scraper.get_user_stats(self.TEST_LAST_ONLINE_NOW_USER).data last_online = data['last_online'] assert datetime.utcnow() - last_online < timedelta(seconds=10)
def test_user_last_online_minutes(self, mock_requests): # 23 minutes ago mock_requests.always_mock(self.TEST_LAST_ONLINE_MINS_PAGE, 'user_last_online_mins') data = mal_scraper.get_user_stats(self.TEST_LAST_ONLINE_MINS_USER).data last_online = data['last_online'] assert (datetime.utcnow() - timedelta(minutes=23)) - last_online < timedelta(minutes=1)
def test_user_stats(self, mock_requests): """Do we retrieve the right stats about a user?""" # Always mock this because the user will change it himself mock_requests.always_mock(self.TEST_USER_PAGE, 'user_test_page') meta, data = mal_scraper.get_user_stats(self.TEST_USER) # Fuzzy match datetime assert datetime.utcnow() - meta['when'] < timedelta(seconds=30) # Assert meta contained by ... assert meta.items() >= { 'user_id': self.TEST_USER, }.items() # Fuzzy match datetime - last online was "10 hours ago" last_online = data['last_online'] assert datetime.utcnow() - last_online < timedelta(hours=11) assert data == { 'name': self.TEST_USER, 'joined': date(year=2014, month=1, day=6), 'last_online': last_online, # Already checked 'num_anime_watching': 14, 'num_anime_completed': 129, 'num_anime_on_hold': 9, 'num_anime_dropped': 4, 'num_anime_plan_to_watch': 16, }
def test_user_last_online_hours(self, mock_requests): mock_requests.always_mock(self.TEST_LAST_ONLINE_HOURS_PAGE, 'user_last_online_hours') data = mal_scraper.get_user_stats( self.TEST_LAST_ONLINE_HOURS_USER).data last_online = data['last_online'] # '6 hours ago' assert (datetime.utcnow() - timedelta(hours=6)) - last_online < timedelta(seconds=10)
def test_user_last_online_yesterday(self, mock_requests): # Yesterday, 9:01 AM mock_requests.always_mock( self.TEST_LAST_ONLINE_YESTERDAY_PAGE, 'user_last_online_yesterday', ) data = mal_scraper.get_user_stats(self.TEST_LAST_ONLINE_YESTERDAY_USER).data yesterday = datetime.utcnow() - timedelta(days=1) expected_date = yesterday.replace(hour=9, minute=1, second=0, microsecond=0) assert data['last_online'] == expected_date
def test_user_discovery_on_user_profile_page(self, mock_requests): mock_requests.always_mock( 'http://myanimelist.net/profile/SparkleBunnies', 'user_test_page') meta = mal_scraper.get_user_stats('SparkleBunnies').meta html = meta['response'].text usernames = list( mal_scraper.user_discovery.discover_users_from_html(html)) assert usernames == [ 'SparkleBunnies', 'SparkleBunnies', 'SparkleBunnies', 'SparkleBunnies', 'AkitoKazuki', 'Exmortus420', 'ChannelOrange', 'Brandon', 'Zeally', 'Daedalus', 'HaXXspetten', 'Kagami', 'no_good_name', 'BlackFIFA19', 'Ichigo_Shiba', 'Ichigo_Shiba', 'Sacchie', 'Sacchie', 'Woodenspoon', 'Woodenspoon', 'Teddy_Bear56', 'Teddy_Bear56', 'Speeku', 'Speeku', 'stonemask', 'stonemask', 'IIDarkII', 'IIDarkII', 'ThisNameSucks', 'ThisNameSucks', 'Z6890', 'Z6890', 'BKZekken', 'BKZekken', 'Woodenspoon', 'Woodenspoon', 'ChannelOrange', 'ChannelOrange', 'Padgit', 'Padgit', ]
def test_user_last_online_yesterday(self, mock_requests): # Yesterday, 9:01 AM mock_requests.always_mock( self.TEST_LAST_ONLINE_YESTERDAY_PAGE, 'user_last_online_yesterday', ) data = mal_scraper.get_user_stats( self.TEST_LAST_ONLINE_YESTERDAY_USER).data yesterday = datetime.utcnow() - timedelta(days=1) expected_date = yesterday.replace(hour=9, minute=1, second=0, microsecond=0) assert data['last_online'] == expected_date
def test_user_discovery_on_user_profile_page(self, mock_requests): mock_requests.always_mock('http://myanimelist.net/profile/SparkleBunnies', 'user_test_page') meta = mal_scraper.get_user_stats('SparkleBunnies').meta html = meta['response'].text usernames = list(mal_scraper.user_discovery.discover_users_from_html(html)) assert usernames == [ 'SparkleBunnies', 'SparkleBunnies', 'SparkleBunnies', 'SparkleBunnies', 'AkitoKazuki', 'Exmortus420', 'ChannelOrange', 'Brandon', 'Zeally', 'Daedalus', 'HaXXspetten', 'Kagami', 'no_good_name', 'BlackFIFA19', 'Ichigo_Shiba', 'Ichigo_Shiba', 'Sacchie', 'Sacchie', 'Woodenspoon', 'Woodenspoon', 'Teddy_Bear56', 'Teddy_Bear56', 'Speeku', 'Speeku', 'stonemask', 'stonemask', 'IIDarkII', 'IIDarkII', 'ThisNameSucks', 'ThisNameSucks', 'Z6890', 'Z6890', 'BKZekken', 'BKZekken', 'Woodenspoon', 'Woodenspoon', 'ChannelOrange', 'ChannelOrange', 'Padgit', 'Padgit', ]
def test_user_last_online_hours(self, mock_requests): mock_requests.always_mock(self.TEST_LAST_ONLINE_HOURS_PAGE, 'user_last_online_hours') data = mal_scraper.get_user_stats(self.TEST_LAST_ONLINE_HOURS_USER).data last_online = data['last_online'] # '6 hours ago' assert (datetime.utcnow() - timedelta(hours=6)) - last_online < timedelta(seconds=10)
def test_detect_bad_download(self, mock_requests): mock_requests.always_mock(self.TEST_USER_PAGE, 'garbled_user_page') with pytest.raises(mal_scraper.ParseError): mal_scraper.get_user_stats(self.TEST_USER)