class TestTwitchEngine: TWITCH_REST = 'https://api.twitch.tv/kraken' def setUp(self): self.twitch = TwitchEngine() def test_get_followed_channels(self): # twitch user should be following more than 25 users before this test followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_(len(followed_channels) > 25, 'twitch user should be following more than' '25 users before running this test') # to make sure we have tested multiget ok_(len(set(followed_channels)) == len(followed_channels)) for ch in followed_channels: expect_url = 'http://www.twitch.tv/'+ch.lower() ok_(followed_channels[ch]['url'] == expect_url, '{} <-> {}'.format(followed_channels[ch]['url'], expect_url)) # test user not found returns None followed_channels = self.twitch.get_followed_channels(self.twitch.USER[:-2]) ok_(followed_channels is None) def test_get_live_channels(self): # This is a tricky one, not sure how to properly test it.. test_channel_count = 10 live_channels = self.twitch.get_live_channels() error_count = 0 test_count = 0 for ch in live_channels: ret_json = requests.get(self.TWITCH_REST+'/streams/'+ch).json() try: ok_(ret_json['stream']['channel']['display_name'] == ch) except KeyError: error_count += 1 test_count += 1 if test_count >= test_channel_count: break # there is time difference between get live and check live # it is possible that channel went offline between these 2 api calls # so we just expect 80% of tested channels are really live on the # 2nd api call ok_((float(error_count) / test_count) < 0.20, 'test:{}, error:{}'.format(test_count, error_count)) def test_follow_unfollow_channel(self): self.twitch.unfollow_channel('kaydada') followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_('KayDaDa' not in followed_channels) self.twitch.follow_channel('kaydada') followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_('KayDaDa' in followed_channels) ret = self.twitch.unfollow_channel('kaydada') ok_(ret is True) followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_('KayDaDa' not in followed_channels) name, ret = self.twitch.follow_channel('kaydada2') ok_(ret is False) ret = self.twitch.unfollow_channel('kaydada2') ok_(ret is False) name, ret = self.twitch.follow_channel('kaydada') ok_(ret is True) def test_get_channel_info(self): info = self.twitch.get_channel_info('kaydada') ok_(info['display_name'] == 'KayDaDa') info = self.twitch.get_channel_info('kaydada_no_this_guy') ok_(info is None, info)
class TestTwitchEngine: TWITCH_REST = 'https://api.twitch.tv/kraken' def setUp(self): self.twitch = TwitchEngine() def test_get_followed_channels(self): # twitch user should be following more than 25 users before this test followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_( len(followed_channels) > 25, 'twitch user should be following more than' '25 users before running this test' ) # to make sure we have tested multiget ok_(len(set(followed_channels)) == len(followed_channels)) for ch in followed_channels: expect_url = 'http://www.twitch.tv/' + ch.lower() ok_(followed_channels[ch]['url'] == expect_url, '{} <-> {}'.format(followed_channels[ch]['url'], expect_url)) # test user not found returns None followed_channels = self.twitch.get_followed_channels( self.twitch.USER[:-2]) ok_(followed_channels is None) def test_get_live_channels(self): # This is a tricky one, not sure how to properly test it.. test_channel_count = 10 live_channels = self.twitch.get_live_channels() error_count = 0 test_count = 0 for ch in live_channels: ret_json = requests.get(self.TWITCH_REST + '/streams/' + ch).json() try: ok_(ret_json['stream']['channel']['display_name'] == ch) except KeyError: error_count += 1 test_count += 1 if test_count >= test_channel_count: break # there is time difference between get live and check live # it is possible that channel went offline between these 2 api calls # so we just expect 80% of tested channels are really live on the # 2nd api call ok_((float(error_count) / test_count) < 0.20, 'test:{}, error:{}'.format(test_count, error_count)) def test_follow_unfollow_channel(self): self.twitch.unfollow_channel('kaydada') followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_('KayDaDa' not in followed_channels) self.twitch.follow_channel('kaydada') followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_('KayDaDa' in followed_channels) ret = self.twitch.unfollow_channel('kaydada') ok_(ret is True) followed_channels = self.twitch.get_followed_channels(self.twitch.USER) ok_('KayDaDa' not in followed_channels) name, ret = self.twitch.follow_channel('kaydada2') ok_(ret is False) ret = self.twitch.unfollow_channel('kaydada2') ok_(ret is False) name, ret = self.twitch.follow_channel('kaydada') ok_(ret is True) def test_get_channel_info(self): info = self.twitch.get_channel_info('kaydada') ok_(info['display_name'] == 'KayDaDa') info = self.twitch.get_channel_info('kaydada_no_this_guy') ok_(info is None, info)