Skip to content

Commit

Permalink
Add recommendations tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolinski committed Apr 2, 2019
1 parent 052034a commit 59b47fd
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 16 deletions.
Empty file added -popracuje
Empty file.
Empty file added -zawyżył
Empty file.
Empty file added Jeśli
Empty file.
Empty file added Podsumowując
Empty file.
68 changes: 60 additions & 8 deletions tests/inferfaces_tests/test_recommendations.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from datetime import date

import pytest
from tests.test_data.calendars import MOVIE_PREMIERES, SEASON_PREMIERES, SHOWS
from tests.test_data.movies import MOVIE1, MOVIE2, MOVIES
from tests.test_data.movies import MOVIE1, MOVIES
from tests.utils import USER, mk_mock_client
from trakt.core.exceptions import ArgumentError, NotAuthenticated
from trakt.core.json_parser import parse_tree
from trakt.core.models import Movie, Show
from tests.test_data.shows import SHOW


"""
def test_recommendations_movies():
client = mk_mock_client(
{r".*movies?ignore_collected=true": [MOVIES, 200]}, user=None
{r".*movies\?ignore_collected=true": [MOVIES, 200]}, user=None
)

with pytest.raises(NotAuthenticated):
Expand All @@ -22,5 +22,57 @@ def test_recommendations_movies():

movies = client.recommendations.get_movie_recommendations(ignore_collected=True)

print(movies)
"""
assert len(movies) == 2


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"])

reqs = list(client.http._requests.req_map.items())[0]
req = reqs[1][1] # [(path, data)] -> data

assert req["method"] == "DELETE"


def test_recommendations_shows():
client = mk_mock_client(
{r".*shows\?ignore_collected=true": [[SHOW], 200]}, user=None
)

with pytest.raises(NotAuthenticated):
client.recommendations.get_show_recommendations()

client.set_user(USER)
shows = client.recommendations.get_show_recommendations(ignore_collected=True)

assert len(shows) == 1


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)

reqs = list(client.http._requests.req_map.items())[0]
req = reqs[1][0] # [(path, data)] -> data

assert req["method"] == "DELETE"
6 changes: 1 addition & 5 deletions tests/test_http_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,4 @@ def test_add_quargs():

req = client.http._requests.req_map["a"][0]

url: str = req["path"]

print(url)

assert url.endswith(r"/a?arg=abc")
assert req["path"].endswith(r"/a?arg=abc")
8 changes: 8 additions & 0 deletions tests/test_json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ def test_parser_default_none():
def test_parser_invalid_structure():
with pytest.raises(TraktResponseError):
json_parser.parse_tree([{"a": "b"}], Show)


def test_empty_resp():
data_list = json_parser.parse_tree([], [])
data_dict = json_parser.parse_tree({}, {})

assert data_dict == {}
assert data_list == []
13 changes: 10 additions & 3 deletions trakt/core/json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _parse_tree(data: Any, tree_structure: Any) -> Any:
return jsons.load(data, tree_structure)

if level_type == list:
return _parse_list(data, single_item_type=tree_structure[0])
return _parse_list(data, tree_structure)

if level_type == dict:
return _parse_dict(data, tree_structure)
Expand All @@ -60,16 +60,23 @@ def _is_arbitrary_value(x: Any) -> bool:
return x.__class__ not in (ITERABLES | {type})


def _parse_list(data: List[Any], single_item_type: Any) -> List[Any]:
def _parse_list(data: List[Any], tree_structure: List[Any]) -> List[Any]:
if not tree_structure:
return tree_structure

single_item_type = tree_structure[0]
if single_item_type is Any:
return data
if data is None:
if data is None or not data:
return []

return [_parse_tree(e, single_item_type) for e in data]


def _parse_dict(data: Dict[Any, Any], tree_structure: Dict[Any, Any]) -> Dict[Any, Any]:
if not tree_structure:
return tree_structure

wildcards = { # eg {str: str} / {str: Any}
k: v for k, v in tree_structure.items() if isinstance(k, type)
}
Expand Down
8 changes: 8 additions & 0 deletions trakt/core/paths/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,16 @@ def get_path_and_qargs(self) -> Tuple[str, Dict[str, Any]]:

qargs["extended"] = self.__bound_kwargs["extended"]

qargs = {k: self._stringify_param(v) for k, v in qargs.items()}

return "/".join(parts), qargs

@staticmethod
def _stringify_param(v: Any) -> str:
if isinstance(v, bool):
return "true" if v else "false"
return str(v)

def _get_parsed_filters(self) -> Dict[str, str]:
m = {}

Expand Down

0 comments on commit 59b47fd

Please sign in to comment.