Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from viewing_party.party import *
from tests.test_constants import *

@pytest.mark.skip()
# @pytest.mark.skip()
def test_create_successful_movie():
# Arrange
movie_title = MOVIE_TITLE_1
Expand All @@ -19,7 +19,7 @@ def test_create_successful_movie():
assert new_movie["genre"] == GENRE_1
assert new_movie["rating"] == pytest.approx(RATING_1)

@pytest.mark.skip()
# @pytest.mark.skip()
def test_create_no_title_movie():
# Arrange
movie_title = None
Expand All @@ -32,7 +32,7 @@ def test_create_no_title_movie():
# Assert
assert new_movie is None

@pytest.mark.skip()
# @pytest.mark.skip()
def test_create_no_genre_movie():
# Arrange
movie_title = "Title A"
Expand All @@ -45,7 +45,7 @@ def test_create_no_genre_movie():
# Assert
assert new_movie is None

@pytest.mark.skip()
# @pytest.mark.skip()
def test_create_no_rating_movie():
# Arrange
movie_title = "Title A"
Expand All @@ -58,7 +58,7 @@ def test_create_no_rating_movie():
# Assert
assert new_movie is None

@pytest.mark.skip()
# @pytest.mark.skip()
def test_adds_movie_to_user_watched():
# Arrange
movie = {
Expand All @@ -79,7 +79,7 @@ def test_adds_movie_to_user_watched():
assert updated_data["watched"][0]["genre"] == GENRE_1
assert updated_data["watched"][0]["rating"] == RATING_1

@pytest.mark.skip()
# @pytest.mark.skip()
def test_adds_movie_to_user_watchlist():
# Arrange
movie = {
Expand All @@ -100,7 +100,7 @@ def test_adds_movie_to_user_watchlist():
assert updated_data["watchlist"][0]["genre"] == GENRE_1
assert updated_data["watchlist"][0]["rating"] == RATING_1

@pytest.mark.skip()
# @pytest.mark.skip()
def test_moves_movie_from_watchlist_to_empty_watched():
# Arrange
janes_data = {
Expand All @@ -118,13 +118,18 @@ def test_moves_movie_from_watchlist_to_empty_watched():
# Assert
assert len(updated_data["watchlist"]) == 0
assert len(updated_data["watched"]) == 1

assert updated_data["watched"][0]["title"] == MOVIE_TITLE_1


raise Exception("Test needs to be completed.")
# raise Exception("Test needs to be completed.")

# *******************************************************************************************
# ****** Add assertions here to test that the correct movie was added to "watched" **********
# *******************************************************************************************
#


@pytest.mark.skip()
# @pytest.mark.skip()
def test_moves_movie_from_watchlist_to_watched():
# Arrange
movie_to_watch = HORROR_1
Expand All @@ -142,13 +147,17 @@ def test_moves_movie_from_watchlist_to_watched():
# Assert
assert len(updated_data["watchlist"]) == 1
assert len(updated_data["watched"]) == 2

assert updated_data["watched"][0] == FANTASY_2


# raise Exception("Test needs to be completed.")

raise Exception("Test needs to be completed.")
# *******************************************************************************************
# ****** Add assertions here to test that the correct movie was added to "watched" **********
# *******************************************************************************************

@pytest.mark.skip()
# @pytest.mark.skip()
def test_does_nothing_if_movie_not_in_watchlist():
# Arrange
movie_to_watch = HORROR_1
Expand Down
8 changes: 4 additions & 4 deletions tests/test_wave_02.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from viewing_party.party import *
from tests.test_constants import *

@pytest.mark.skip()
# @pytest.mark.skip()
def test_calculates_watched_average_rating():
# Arrange
janes_data = clean_wave_2_data()
Expand All @@ -14,7 +14,7 @@ def test_calculates_watched_average_rating():
assert average == pytest.approx(3.58333)
assert janes_data == clean_wave_2_data()

@pytest.mark.skip()
# @pytest.mark.skip()
def test_empty_watched_average_rating_is_zero():
# Arrange
janes_data = {
Expand All @@ -27,7 +27,7 @@ def test_empty_watched_average_rating_is_zero():
# Assert
assert average == pytest.approx(0.0)

@pytest.mark.skip()
# @pytest.mark.skip()
def test_most_watched_genre():
# Arrange
janes_data = clean_wave_2_data()
Expand All @@ -39,7 +39,7 @@ def test_most_watched_genre():
assert popular_genre == "Fantasy"
assert janes_data == clean_wave_2_data()

@pytest.mark.skip()
# @pytest.mark.skip()
def test_genre_is_None_if_empty_watched():
# Arrange
janes_data = {
Expand Down
15 changes: 9 additions & 6 deletions tests/test_wave_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from viewing_party.party import *
from tests.test_constants import *

@pytest.mark.skip()
# @pytest.mark.skip()
def test_my_unique_movies():
# Arrange
amandas_data = clean_wave_3_data()
Expand All @@ -16,7 +16,7 @@ def test_my_unique_movies():
assert INTRIGUE_2 in amandas_unique_movies
assert amandas_data == clean_wave_3_data()

@pytest.mark.skip()
# @pytest.mark.skip()
def test_my_not_unique_movies():
# Arrange
amandas_data = clean_wave_3_data()
Expand All @@ -28,7 +28,7 @@ def test_my_not_unique_movies():
# Assert
assert len(amandas_unique_movies) == 0

@pytest.mark.skip()
# @pytest.mark.skip()
def test_friends_unique_movies():
# Arrange
amandas_data = clean_wave_3_data()
Expand All @@ -43,7 +43,7 @@ def test_friends_unique_movies():
assert FANTASY_4 in friends_unique_movies
assert amandas_data == clean_wave_3_data()

@pytest.mark.skip()
# @pytest.mark.skip()
def test_friends_unique_movies_not_duplicated():
# Arrange
amandas_data = clean_wave_3_data()
Expand All @@ -54,13 +54,16 @@ def test_friends_unique_movies_not_duplicated():

# Assert
assert len(friends_unique_movies) == 3
assert INTRIGUE_3 in friends_unique_movies
assert HORROR_1 in friends_unique_movies
assert FANTASY_4 in friends_unique_movies

raise Exception("Test needs to be completed.")
# raise Exception("Test needs to be completed.")
# *************************************************************************************************
# ****** Add assertions here to test that the correct movies are in friends_unique_movies **********
# **************************************************************************************************

@pytest.mark.skip()
# @pytest.mark.skip()
def test_friends_not_unique_movies():
# Arrange
amandas_data = {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_wave_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from viewing_party.party import *
from tests.test_constants import *

@pytest.mark.skip()
# @pytest.mark.skip()
def test_get_available_friend_rec():
# Arrange
amandas_data = clean_wave_4_data()
Expand All @@ -16,7 +16,7 @@ def test_get_available_friend_rec():
assert FANTASY_4b in recommendations
assert amandas_data == clean_wave_4_data()

@pytest.mark.skip()
# @pytest.mark.skip()
def test_no_available_friend_recs():
# Arrange
amandas_data = {
Expand All @@ -38,7 +38,7 @@ def test_no_available_friend_recs():
# Assert
assert len(recommendations) == 0

@pytest.mark.skip()
# @pytest.mark.skip()
def test_no_available_friend_recs_watched_all():
# Arrange
amandas_data = {
Expand Down
106 changes: 105 additions & 1 deletion viewing_party/party.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,125 @@
# -----------------------------------------
# ------------- WAVE 1 --------------------
# -----------------------------------------

def create_movie(title, genre, rating):
pass
movie_dict = {}
if title and genre and rating:
movie_dict.update({"title": title})
movie_dict.update({"genre": genre})
movie_dict.update({"rating": rating})
return movie_dict
else:
return None
Comment on lines +6 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest flipping the sense of the condition around to handle the special case (that something is falsy) quickly and get out, rather than having that dangling else at the end. This arrangement is referred to as a guard clause or a guard condition.

Notice that it lets us move the main focus of the function (creating the new dict) out of an indented block, letting it stand out more clearly.

def create_movie(movie_title, genre, rating):
    if not movie_title or not genre or not rating:
        return None

    movie_dict = {'title':movie_title, 'genre':genre, 'rating':rating}
    return movie_dict


def add_to_watched(user_data, movie):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

user_data["watched"].append(movie)
return user_data


def add_to_watchlist(user_data, movie):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

user_data["watchlist"].append(movie)
return user_data


def watch_movie(user_data, title):
for movie_dict in user_data["watchlist"]:
if movie_dict["title"] == title:
user_data["watched"].append(movie_dict)
user_data["watchlist"].remove(movie_dict)
return user_data
return user_data
Comment on lines +30 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we only need one return statement. But which one? Well, it depends! Do we want to immediately end the function once we've appended and removed one movie? Is there a possibility there could be duplicates of the same movie inside "watched" and "watchlist"? Maybe!

Either one of these works in this context, but do pick only one



# -----------------------------------------
# ------------- WAVE 2 --------------------
# -----------------------------------------
def get_watched_avg_rating(user_data):
rating_list = []
rating_total = 0

for movie in user_data["watched"]:
if len(user_data["watched"]) == 0:
movie["rating"] = 0.0
Comment on lines +42 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm what is this doing? If there are no movies in "watched", I don't think we can then add a key to a non-existent dictionary. I think this might error out. Let's remove these lines.

Also, we don't want to be making modifications to the user's data unless that's the purpose of the function. This function get_watched_avg_rating is only supposed to make calculations and return them.

rating_list.append(movie['rating'])

for rating in rating_list:
rating_total += rating
Comment on lines +41 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works fine! How could we do this in one for loop, though? We are already iterating over each movie on line 41. Why not sum up the ratings as we go?

If we do that, we don't even need a rating_list variable at all!


if len(rating_list) == 0:
return 0.0
else:
final_rating = rating_total / len(rating_list)
return final_rating
Comment on lines +49 to +53

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job handling the empty list case. Consider putting this check at the top as a guard clause in order to get it out of the way, which lets the main logic not be under a conditional.

    if len(movies) < 1: # there are many ways we can check to see if movies is falsy
        return 0.0

    # remaining logic goes here


def get_most_watched_genre(user_data):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

genre_dict = {}
if user_data["watched"] == []:
return None
Comment on lines +56 to +58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swamp these lines, so the guard clause at the tip-top of the function. That way we don't make any unnecessary variables that will just get discarded when line 57 and 58 execute.

for movie_dict in user_data["watched"]:
if movie_dict["genre"] in genre_dict:
genre_dict[movie_dict["genre"]] += 1
else:
genre_dict[movie_dict["genre"]] = 1
best_genre = max(genre_dict, key=genre_dict.get)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job! We talked about how this worked in the project review, but do you feel like you can explain this in your own words? When pulling in code from beyond what we've talked about, it's great if you could include a comment about the research that you did, and how you think it works.

Something to think about in future projects!

return best_genre


# -----------------------------------------
# ------------- WAVE 3 --------------------
# -----------------------------------------
def get_unique_watched(user_data):
user_watched_list = []
friends_movie_list = []
unique_user_watched = []
for i in range(len(user_data["friends"])):
for movie in user_data["friends"][i]["watched"]:
friends_movie_list.append(movie)

for i in range(len(user_data["watched"])):
movie = user_data["watched"][i]
user_watched_list.append(movie)
Comment on lines +79 to +81

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would make for a good helper function so we don't have to repeat code, like on lines 75-77


for movie in user_watched_list:
if movie not in friends_movie_list:
unique_user_watched.append(movie)

return unique_user_watched

def get_friends_unique_watched(user_data):
user_watched_list = []
friends_movie_list = []
friends_unique_watched = []
result = {}
new_list = []
for i in range(len(user_data["friends"])):
for movie in user_data["friends"][i]["watched"]:
friends_movie_list.append(movie)

for i in range(len(user_data["watched"])):
movie = user_data["watched"][i]
user_watched_list.append(movie)
Comment on lines +95 to +101

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha! we've done this once already! Sounds like a job for a helper function!


for movie in friends_movie_list:
if movie not in user_watched_list and movie not in friends_unique_watched:
friends_unique_watched.append(movie)


return friends_unique_watched



# -----------------------------------------
# ------------- WAVE 4 --------------------
# -----------------------------------------
def get_available_recs(user_data):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

friends_unique_movies = get_friends_unique_watched(user_data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job reusing functions!

movie_recommendations = []
for movie in friends_unique_movies:
if movie["host"] in user_data["subscriptions"]:
movie_recommendations.append(movie)

return movie_recommendations

# -----------------------------------------
# ------------- WAVE 5 --------------------
Expand Down