def test_episode_arg_validation(): client = mk_mock_client({r".*checkin.*": [CHECKIN_EPISODE, 201]}, user=None) with pytest.raises(NotAuthenticated): client.checkin.check_into() with pytest.raises(NotAuthenticated): client.checkin.check_into_episode(episode="eid") client.set_user(USER) with pytest.raises(TypeError): client.checkin.check_into_episode() with pytest.raises(ArgumentError): client.checkin.check_into_episode(episode="eid", sharing=True) client.checkin.check_into_episode(episode="eid", sharing={"twitter": True}) client.checkin.check_into_episode(episode="eid", sharing=Sharing(medium=True, twitter=False)) with pytest.raises(ArgumentError): client.checkin.check_into_episode(episode="eid", show=True) client.checkin.check_into_episode(episode="eid", show={"ids": { "trakt": "123" }}) episode, show = parse_tree(EPISODE, Episode), parse_tree(SHOW, Show) client.checkin.check_into_episode(episode=episode, show=show)
def test_start_scrobble_episode(): client = mk_mock_client({".*scrobble.*": [RESP_EPISODE, 201]}) episode = parse_tree(EPISODE, Episode) show = parse_tree(SHOW, Show) resp = client.scrobble.start_scrobble(episode=episode, show=show, progress=5) assert resp.show.title == SHOW["title"]
def test_defaults(): data = {"a": "b"} tree_struct = {"a": "c", "d": "e"} parsed = json_parser.parse_tree(data, tree_struct) assert parsed["a"] == "b" assert parsed["d"] == "e" assert json_parser.parse_tree(data, {}) == {}
def test_basic_list_dict(): data = ["abc", "xyz"] tree_struct = [str] parsed = json_parser.parse_tree(data, tree_struct) assert parsed == ["abc", "xyz"] data = {"name": "Poland", "code": "pl"} tree_struct = {"name": str, "code": str} parsed = json_parser.parse_tree(data, tree_struct) assert parsed == data
def test_start_scrobble_movie(): client = mk_mock_client({".*scrobble.*": [RESP_MOVIE, 201]}) movie = parse_tree(MOVIE1, Movie) episode = parse_tree(EPISODE, Episode) with pytest.raises(ArgumentError): client.scrobble.start_scrobble(progress=5) with pytest.raises(ArgumentError): client.scrobble.start_scrobble(progress=5, episode=episode, movie=movie) resp = client.scrobble.start_scrobble(movie=movie, progress=5) assert resp.movie.title == MOVIE1["title"]
def exec_path_call(self, path: Path, extra_quargs: Optional[Dict[str, str]] = None, **kwargs: Any) -> ApiResponse: caching_enabled = self._should_use_cache(path, kwargs.get("no_cache", False)) api_path, query_args = path.get_path_and_qargs() query_args.update(extra_quargs or {}) api_response = self.client.http.request( api_path, method=path.method, query_args=query_args, data=kwargs.get("data"), use_cache=caching_enabled, **kwargs, ) api_response.parsed = json_parser.parse_tree(api_response.json, path.response_structure) if caching_enabled: # only runs if there were no errors last_request = cast(FrozenRequest, self.client.http.last_request) self.client.cache.set(last_request) return api_response
def test_movie_arg_validation(): client = mk_mock_client({r".*checkin.*": [CHECKIN_MOVIE, 201]}, user=None) with pytest.raises(NotAuthenticated): client.checkin.check_into() with pytest.raises(NotAuthenticated): client.checkin.check_into_movie(movie="mid") client.set_user(USER) with pytest.raises(TypeError): client.checkin.check_into_movie() with pytest.raises(ArgumentError): client.checkin.check_into_movie(movie="eid", sharing=True) client.checkin.check_into_movie(movie="eid", sharing={"twitter": True}) client.checkin.check_into_movie(movie="eid", sharing=Sharing(medium=True, twitter=False)) with pytest.raises(ArgumentError): client.checkin.check_into_movie(movie="eid", message=55) movie = parse_tree(MOVIE1, Movie) client.checkin.check_into_movie(movie=movie)
def test_dataclass(): data = {"name": "xyz"} tree_struct = MockClassName parsed = json_parser.parse_tree(data, tree_struct) assert parsed.__class__ == MockClassName assert parsed.name == "xyz" data = [{"name": "xyz"}, {"name": "abc"}] tree_struct = [MockClassName] parsed = json_parser.parse_tree(data, tree_struct) assert parsed.__class__ == list assert parsed[1].__class__ == MockClassName assert parsed[1].name == "abc"
def test_get_person(): client = mk_mock_client({r".*people.*": [PERSON, 200]}) person = parse_tree(PERSON, Person) with pytest.raises(ArgumentError): client.people.get_person(person=0.5) assert client.people.get_person(person=person.ids.trakt).name == PERSON["name"] assert client.people.get_person(person=person).name == PERSON["name"]
def test_wildcards(): data = {"a": 100, "c": "d", "e": "f", True: "g", 0.5: "y", 0.7: 10} tree_struct = {"a": int, str: str, float: Any} parsed = json_parser.parse_tree(data, tree_struct) assert parsed["a"] == 100 assert parsed["c"] == "d" assert parsed["e"] == "f" assert True not in parsed assert parsed[0.5] == "y" assert parsed[0.7] == 10
def test_parser_nofail(): ep = json_parser.parse_tree(EPISODE, Episode) epex = json_parser.parse_tree(EXTENDED_EPISODE, Episode) sh = json_parser.parse_tree(SHOW, Show) shex = json_parser.parse_tree(EXTENDED_SHOW, Show) l = json_parser.parse_tree(LIST, TraktList) tl = json_parser.parse_tree(TRENDING_LISTS, [ListResponse]) u = json_parser.parse_tree(USER, User)
def test_hide_show(): m_id = SHOW["ids"]["trakt"] client = mk_mock_client({rf".*shows/{m_id}": [{}, 204]}, user=None) show = parse_tree(SHOW, tree_structure=Show) with pytest.raises(NotAuthenticated): client.recommendations.hide_show(show=show) client.set_user(USER) client.recommendations.hide_show(show=show) assert get_last_req(client.http)["method"] == "DELETE"
def test_hide_movie(): m_id = MOVIE1["ids"]["trakt"] client = mk_mock_client({rf".*movies/{m_id}": [{}, 204]}, user=None) movie = parse_tree(MOVIE1, tree_structure=Movie) with pytest.raises(NotAuthenticated): client.recommendations.hide_movie(movie=movie) client.set_user(USER) client.recommendations.hide_movie(movie=movie) client.recommendations.hide_movie(movie=movie.ids.trakt) assert get_last_req(client.http)["method"] == "DELETE"
def test_mixed_structure(): data = { "count": 2, "items": [ { "info": "m-1", "obj": { "date": 2018, "data": { "name": "xxi" } } }, { "info": "m-2", "obj": { "date": 1410, "data": { "name": "xv" } } }, ], } tree_struct = { "count": int, "items": [{ "info": str, "obj": MockClassDateData }] } parsed = json_parser.parse_tree(data, tree_struct) assert parsed.__class__ == dict assert "count" in parsed and parsed["count"].__class__ == int assert "items" in parsed and parsed["items"][0].__class__ == dict item = parsed["items"][1] assert "info" in item and item["info"] == "m-2" assert "obj" in item and item["obj"].__class__ == MockClassDateData assert item["obj"].data.__class__ == MockClassName
def test_parser_datetime(): show = json_parser.parse_tree(EXTENDED_SHOW, Show) assert isinstance(show.first_aired, datetime) assert show.first_aired == datetime.strptime( EXTENDED_SHOW["first_aired"][:-5] + "Z", "%Y-%m-%dT%H:%M:%S%z")
def test_empty_resp(): data_list = json_parser.parse_tree([], []) data_dict = json_parser.parse_tree({}, {}) assert data_dict == {} assert data_list == []
def test_parser_invalid_structure(): with pytest.raises(TraktResponseError): json_parser.parse_tree([{"a": "b"}], Show)
def test_get_comment(): client = mk_mock_client({".*comments.*": [COMMENT, 200]}) comment = parse_tree(COMMENT, Comment) comment = client.comments.get_comment(id=comment) assert comment.user.name == COMMENT["user"]["name"]
def test_parser_default_none(): show = json_parser.parse_tree(EXTENDED_SHOW, Show) assert show.country == "gb" assert show.trailer is None