diff --git a/tests/test_constants.py b/tests/test_constants.py index 55d4dceb5..cc41d6f32 100644 --- a/tests/test_constants.py +++ b/tests/test_constants.py @@ -11,6 +11,10 @@ GENRE_1 = "Horror" RATING_1 = 3.5 +MOVIE_TITLE_2 = "Ratatouille" +GENRE_2 = "Comedy" +RATING_2 = 4.3 + #----------WAVE02------------- HORROR_1 = { "title": MOVIE_TITLE_1, diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index 6be6994a5..a2d23f94e 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -4,7 +4,6 @@ from viewing_party.party import * from tests.test_constants import * -@pytest.mark.skip() def test_create_successful_movie(): # Arrange movie_title = MOVIE_TITLE_1 @@ -19,7 +18,6 @@ def test_create_successful_movie(): assert new_movie["genre"] == GENRE_1 assert new_movie["rating"] == pytest.approx(RATING_1) -@pytest.mark.skip() def test_create_no_title_movie(): # Arrange movie_title = None @@ -32,7 +30,6 @@ def test_create_no_title_movie(): # Assert assert new_movie is None -@pytest.mark.skip() def test_create_no_genre_movie(): # Arrange movie_title = "Title A" @@ -45,7 +42,6 @@ def test_create_no_genre_movie(): # Assert assert new_movie is None -@pytest.mark.skip() def test_create_no_rating_movie(): # Arrange movie_title = "Title A" @@ -58,8 +54,20 @@ def test_create_no_rating_movie(): # Assert assert new_movie is None -@pytest.mark.skip() -def test_adds_movie_to_user_watched(): +def test_adds_no_movie_to_user_watched(): + # Arrange + movie = {} + user_data = { + "watched": [] + } + + # Act + updated_data = add_to_watched(user_data, movie) + + # Assert + assert len(updated_data["watched"]) == 0 + +def test_adds_movie_to_empty_user_watched_list(): # Arrange movie = { "title": MOVIE_TITLE_1, @@ -79,8 +87,45 @@ 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() -def test_adds_movie_to_user_watchlist(): +def test_adds_movie_to_user_watched(): + # Arrange + movie = { + "title": MOVIE_TITLE_1, + "genre": GENRE_1, + "rating": RATING_1 + } + movie_watched = { + "title": MOVIE_TITLE_2, + "genre": GENRE_2, + "rating": RATING_2 + } + user_data = { + "watched": [movie_watched] + } + + # Act + updated_data = add_to_watched(user_data, movie) + + # Assert + assert len(updated_data["watched"]) == 2 + assert updated_data["watched"][1]["title"] == MOVIE_TITLE_1 + assert updated_data["watched"][1]["genre"] == GENRE_1 + assert updated_data["watched"][1]["rating"] == RATING_1 + +def test_adds_no_movie_to_user_watchlist(): + # Arrange + movie = {} + user_data = { + "watchlist": [] + } + + # Act + updated_data = add_to_watchlist(user_data, movie) + + # Assert + assert len(updated_data["watchlist"]) == 0 + +def test_adds_movie_to_empty_user_watchlist(): # Arrange movie = { "title": MOVIE_TITLE_1, @@ -100,7 +145,31 @@ 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() +def test_adds_movie_to_user_watchlist(): + # Arrange + movie = { + "title": MOVIE_TITLE_1, + "genre": GENRE_1, + "rating": RATING_1 + } + movie_watched = { + "title": MOVIE_TITLE_2, + "genre": GENRE_2, + "rating": RATING_2 + } + user_data = { + "watchlist": [movie_watched] + } + + # Act + updated_data = add_to_watchlist(user_data, movie) + + # Assert + assert len(updated_data["watchlist"]) == 2 + assert updated_data["watchlist"][1]["title"] == MOVIE_TITLE_1 + assert updated_data["watchlist"][1]["genre"] == GENRE_1 + assert updated_data["watchlist"][1]["rating"] == RATING_1 + def test_moves_movie_from_watchlist_to_empty_watched(): # Arrange janes_data = { @@ -118,13 +187,10 @@ def test_moves_movie_from_watchlist_to_empty_watched(): # Assert assert len(updated_data["watchlist"]) == 0 assert len(updated_data["watched"]) == 1 - - raise Exception("Test needs to be completed.") - # ******************************************************************************************* - # ****** Add assertions here to test that the correct movie was added to "watched" ********** - # ******************************************************************************************* + assert updated_data["watched"][0]["title"] == MOVIE_TITLE_1 + assert updated_data["watched"][0]["genre"] == GENRE_1 + assert updated_data["watched"][0]["rating"] == RATING_1 -@pytest.mark.skip() def test_moves_movie_from_watchlist_to_watched(): # Arrange movie_to_watch = HORROR_1 @@ -142,13 +208,10 @@ def test_moves_movie_from_watchlist_to_watched(): # Assert assert len(updated_data["watchlist"]) == 1 assert len(updated_data["watched"]) == 2 - - raise Exception("Test needs to be completed.") - # ******************************************************************************************* - # ****** Add assertions here to test that the correct movie was added to "watched" ********** - # ******************************************************************************************* + assert updated_data["watched"][1]["title"] == movie_to_watch["title"] + assert updated_data["watched"][1]["genre"] == movie_to_watch["genre"] + assert updated_data["watched"][1]["rating"] == movie_to_watch["rating"] -@pytest.mark.skip() def test_does_nothing_if_movie_not_in_watchlist(): # Arrange movie_to_watch = HORROR_1 diff --git a/tests/test_wave_02.py b/tests/test_wave_02.py index 3a588299e..25f0bf034 100644 --- a/tests/test_wave_02.py +++ b/tests/test_wave_02.py @@ -2,7 +2,6 @@ from viewing_party.party import * from tests.test_constants import * -@pytest.mark.skip() def test_calculates_watched_average_rating(): # Arrange janes_data = clean_wave_2_data() @@ -14,7 +13,6 @@ def test_calculates_watched_average_rating(): assert average == pytest.approx(3.58333) assert janes_data == clean_wave_2_data() -@pytest.mark.skip() def test_empty_watched_average_rating_is_zero(): # Arrange janes_data = { @@ -27,7 +25,7 @@ def test_empty_watched_average_rating_is_zero(): # Assert assert average == pytest.approx(0.0) -@pytest.mark.skip() + def test_most_watched_genre(): # Arrange janes_data = clean_wave_2_data() @@ -39,7 +37,7 @@ def test_most_watched_genre(): assert popular_genre == "Fantasy" assert janes_data == clean_wave_2_data() -@pytest.mark.skip() + def test_genre_is_None_if_empty_watched(): # Arrange janes_data = { diff --git a/tests/test_wave_03.py b/tests/test_wave_03.py index 046429360..2c73b6499 100644 --- a/tests/test_wave_03.py +++ b/tests/test_wave_03.py @@ -2,21 +2,20 @@ from viewing_party.party import * from tests.test_constants import * -@pytest.mark.skip() + def test_my_unique_movies(): # Arrange amandas_data = clean_wave_3_data() # Act amandas_unique_movies = get_unique_watched(amandas_data) - # Assert assert len(amandas_unique_movies) == 2 assert FANTASY_2 in amandas_unique_movies assert INTRIGUE_2 in amandas_unique_movies assert amandas_data == clean_wave_3_data() -@pytest.mark.skip() + def test_my_not_unique_movies(): # Arrange amandas_data = clean_wave_3_data() @@ -28,7 +27,7 @@ def test_my_not_unique_movies(): # Assert assert len(amandas_unique_movies) == 0 -@pytest.mark.skip() + def test_friends_unique_movies(): # Arrange amandas_data = clean_wave_3_data() @@ -43,24 +42,23 @@ def test_friends_unique_movies(): assert FANTASY_4 in friends_unique_movies assert amandas_data == clean_wave_3_data() -@pytest.mark.skip() + def test_friends_unique_movies_not_duplicated(): # Arrange amandas_data = clean_wave_3_data() amandas_data["friends"][0]["watched"].append(INTRIGUE_3) + # Act friends_unique_movies = get_friends_unique_watched(amandas_data) # 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.") - # ************************************************************************************************* - # ****** Add assertions here to test that the correct movies are in friends_unique_movies ********** - # ************************************************************************************************** -@pytest.mark.skip() def test_friends_not_unique_movies(): # Arrange amandas_data = { diff --git a/tests/test_wave_04.py b/tests/test_wave_04.py index 499669077..67f432045 100644 --- a/tests/test_wave_04.py +++ b/tests/test_wave_04.py @@ -2,7 +2,7 @@ from viewing_party.party import * from tests.test_constants import * -@pytest.mark.skip() + def test_get_available_friend_rec(): # Arrange amandas_data = clean_wave_4_data() @@ -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() + def test_no_available_friend_recs(): # Arrange amandas_data = { @@ -38,7 +38,7 @@ def test_no_available_friend_recs(): # Assert assert len(recommendations) == 0 -@pytest.mark.skip() + def test_no_available_friend_recs_watched_all(): # Arrange amandas_data = { diff --git a/tests/test_wave_05.py b/tests/test_wave_05.py index 85ebb8b18..4925085de 100644 --- a/tests/test_wave_05.py +++ b/tests/test_wave_05.py @@ -2,7 +2,7 @@ from viewing_party.party import * from tests.test_constants import * -@pytest.mark.skip() + def test_new_genre_rec(): # Arrange sonyas_data = clean_wave_5_data() @@ -17,7 +17,7 @@ def test_new_genre_rec(): assert FANTASY_4b in recommendations assert sonyas_data == clean_wave_5_data() -@pytest.mark.skip() + def test_new_genre_rec_from_empty_watched(): # Arrange sonyas_data = { @@ -38,7 +38,7 @@ def test_new_genre_rec_from_empty_watched(): # Assert assert len(recommendations) == 0 -@pytest.mark.skip() + def test_new_genre_rec_from_empty_friends(): # Arrange sonyas_data = { @@ -53,12 +53,13 @@ def test_new_genre_rec_from_empty_friends(): ] } - raise Exception("Test needs to be completed.") - # ********************************************************************* - # ****** Complete the Act and Assert Portions of theis tests ********** - # ********************************************************************* + # Act + recommendations = get_new_rec_by_genre(sonyas_data) + + # Assert + assert len(recommendations) == 0 + -@pytest.mark.skip() def test_unique_rec_from_favorites(): # Arrange sonyas_data = clean_wave_5_data() @@ -72,7 +73,7 @@ def test_unique_rec_from_favorites(): assert INTRIGUE_2b in recommendations assert sonyas_data == clean_wave_5_data() -@pytest.mark.skip() + def test_unique_from_empty_favorites(): # Arrange sonyas_data = { @@ -94,7 +95,7 @@ def test_unique_from_empty_favorites(): # Assert assert len(recommendations) == 0 -@pytest.mark.skip() + def test_new_rec_from_empty_friends(): # Arrange sonyas_data = { diff --git a/viewing_party/party.py b/viewing_party/party.py index 6d34a6b5f..98bee32c8 100644 --- a/viewing_party/party.py +++ b/viewing_party/party.py @@ -1,23 +1,146 @@ # ------------- WAVE 1 -------------------- def create_movie(title, genre, rating): - pass + if not bool(title) or not bool(genre) or not bool(rating): + return None + + new_movie = {"title": title, "genre": genre, "rating": rating} + return new_movie + + +def add_to_watched(user_data, movie): + if not bool(movie): + return user_data + user_data["watched"].append(movie) + return user_data + + +def add_to_watchlist(user_data, movie): + if not bool(movie): + return user_data + user_data["watchlist"].append(movie) + return user_data + + +def watch_movie(user_data, title): + for movie in user_data["watchlist"]: + if movie["title"] == title: + add_to_watched(user_data, movie) + user_data["watchlist"].remove(movie) + return user_data + return user_data # ----------------------------------------- # ------------- WAVE 2 -------------------- # ----------------------------------------- +def get_watched_avg_rating(user_data): + watched_len = len(user_data["watched"]) + if watched_len == 0: + return 0.0 + + rating_sum = 0 + for movie in user_data["watched"]: + rating_sum += movie["rating"] + + avg_rating = rating_sum / watched_len + return avg_rating + + +def get_most_watched_genre(user_data): + if len(user_data["watched"]) == 0: + return None + + watched_genres = {} + for movie in user_data["watched"]: + watched_genres[movie["genre"]] = watched_genres.get(movie["genre"], 0) + 1 + + popular_genre = max(watched_genres, key=watched_genres.get) + return popular_genre + # ----------------------------------------- # ------------- WAVE 3 -------------------- # ----------------------------------------- - + +def get_unique_watched(user_data): + watched_movies_list = user_data["watched"] + friends_watched_movies_list = get_friends_watched_movies(user_data) + return [movie for movie in watched_movies_list if movie not in friends_watched_movies_list] + + +def get_friends_unique_watched(user_data): + watched_movies_list = user_data["watched"] + friends_watched_movies_list = get_friends_watched_movies(user_data) + remove_movie_duplicates(friends_watched_movies_list) + + friends_unique_list = [movie for movie in friends_watched_movies_list if movie not in watched_movies_list] + return friends_unique_list + + +def get_friends_watched_movies(user_data): + friends_watched_movies_list = [] + + for friends_watched_movies in user_data["friends"]: + friends_watched_movies_list.append(friends_watched_movies["watched"]) + + friends_watched_movies_list = sum(friends_watched_movies_list, []) + return friends_watched_movies_list + + +def remove_movie_duplicates(friends_watched_movies_list): + friends_watched_movie_titles = {} + for movie in friends_watched_movies_list: + friends_watched_movie_titles[movie["title"]] = friends_watched_movie_titles.get(movie["title"], 0) + 1 + if friends_watched_movie_titles[movie["title"]] > 1: + friends_watched_movies_list.remove(movie) + friends_watched_movie_titles[movie["title"]] = friends_watched_movie_titles[movie["title"]] - 1 + return friends_watched_movies_list + + # ----------------------------------------- # ------------- WAVE 4 -------------------- # ----------------------------------------- + +def get_available_recs(user_data): + friends_available_recs = get_friends_unique_watched(user_data) + available_recs = [] + for movie in friends_available_recs: + if movie["host"] in user_data["subscriptions"]: + available_recs.append(movie) + return available_recs + # ----------------------------------------- # ------------- WAVE 5 -------------------- # ----------------------------------------- + +def get_new_rec_by_genre(user_data): + friends_available_recs = get_friends_unique_watched(user_data) + if len(user_data["watched"]) == 0 or len(friends_available_recs) == 0: + return [] + + popular_genre = get_most_watched_genre(user_data) + recs_by_genre = [] + for movie in friends_available_recs: + if movie["genre"] == popular_genre: + recs_by_genre.append(movie) + + return recs_by_genre + + +def get_rec_from_favorites(user_data): + if len(user_data["watched"]) == 0: + return [] + + if len(user_data["friends"]) == 0: + return user_data["watched"] + + unique_watched = get_unique_watched(user_data) + rec_from_favorites = [] + for movie in unique_watched: + if movie in user_data["favorites"]: + rec_from_favorites.append(movie) + return rec_from_favorites