def test_load_user_failed(self): """ """ twitch = Twitch() twitch._user_profile = Mock(return_value=None) self.assertEqual(None, twitch._load_user("foo11"))
def test_load_json_decode_error(self): """ """ twitch = Twitch() twitch.tcp.get = Mock(side_effect=json.decoder.JSONDecodeError('a', 'b', 10)) # NOQA self.assertEqual(None, twitch._load_json("bar"))
def test_load_json_type_error(self): """ """ twitch = Twitch() twitch.tcp.get = Mock(side_effect=TypeError) self.assertEqual(None, twitch._load_json("baz"))
def test_user_profile(self): """ """ record = { "name": "bazbar", "created_at": "2016-02-26T17:29:16Z", "updated_at": "2016-03-26T17:29:16Z", } twitch = Twitch() mock_response = Mock() mock_response.json.return_value = record twitch.tcp.get = Mock(return_value=mock_response) self.assertEqual(record, twitch._user_profile("randomuser"))
def test_chatters(self): """ """ viewer = self.create_user("viewer") moderator = self.create_user("moderator") chatters = { "chatters": { "viewers": [viewer.name, "foobar"], "moderators": [moderator.name] } } twitch = Twitch() mock_response = Mock() mock_response.json.return_value = chatters twitch.tcp.get = Mock(return_value=mock_response) self.assertEqual(twitch.chatters("foo"), [moderator, viewer])
def test_load_user(self): """ """ record = { "name": "bazbar", "created_at": "2016-02-26T17:29:16Z", "updated_at": "2016-03-26T17:29:16Z", } twitch = Twitch() twitch._user_profile = Mock(return_value=record) twitch._user_follows = Mock(return_value=None) user = twitch._load_user(record["name"]) self.assertEqual(user.name, record["name"]) self.assertEqual(user.created, parse_date(record["created_at"])) self.assertEqual(user.updated, parse_date(record["updated_at"])) self.assertEqual(user.follows, 0)
def chatters(request): """ /{stream} - stream: stream name Returns a list of chatters with their registration date """ channel = request.matchdict["stream"].lower() users = Twitch().chatters(channel) if users is None: raise exc.HTTPInternalServerError( "Error while loading channel details {}".format(channel)) return { 'chatters': users, }
def stream(request): """ /stream/{stream} - stream: stream name Returns basic stream info (viewers, chatters, followers, etc) """ name = request.matchdict["stream"].lower() info = Twitch().stream(name) if info is None: raise exc.HTTPInternalServerError( "Error while loading stream details {}".format(name)) # NOQA if info is False: raise exc.HTTPNotFound("Offline stream {}".format(name)) return { 'streams': [info], }
def directory(request): """ /directory/{directory} (optional) - directory: sub-section of the directory Returns basic stream info (viewers, chatters, followers, etc) for the top 20 streams in a section """ directory = request.matchdict["directory"].lower() info = Twitch().streams(directory) if info is None: raise exc.HTTPInternalServerError( "Error while loading streams details {}".format(directory)) # NOQA if info is False: raise exc.HTTPNotFound("Unknown directory {}".format(directory)) return { 'streams': info, }
def test_chatters_bogus(self, _load_json): """ """ twitch = Twitch() self.assertEqual(None, twitch.chatters("boo"))
def test_chatters_failed(self, _load_json): """ """ twitch = Twitch() self.assertEqual(None, twitch.chatters("bar"))
def test_user_follows_bogus(self, _load_json): """ """ twitch = Twitch() self.assertEqual(None, twitch._user_follows("bar43"))
def test_user_follows_failed(self, _load_json): """ """ twitch = Twitch() self.assertEqual(None, twitch._user_follows("bar45"))
def test_user_follows(self, _load_json): """ """ twitch = Twitch() self.assertEqual(420, twitch._user_follows("randomuser34"))
def test_user_profile_bogus(self, _load_json): """ """ twitch = Twitch() self.assertEqual(None, twitch._user_profile("bar23"))