def test_watching_simulation_movies(watching_simulation): users = [User('Martin', 'pw12345'), User('Ian', 'pw67890'), User('Daniel', 'pw87465')] for user in users: watching_simulation.add_user(user) assert user in watching_simulation.users assert user.time_spent_watching_movies_minutes == 0 watching_simulation.watch_movie() for user in watching_simulation.users: assert watching_simulation.movie in user.watched_movies assert user.time_spent_watching_movies_minutes == watching_simulation.movie.runtime_minutes assert sum(1 for _ in user.watched_movies) == 1 user4 = User('Bob', 'pw12347') watching_simulation.add_user(user4) watching_simulation.watch_movie() for user in users: assert watching_simulation.movie in user.watched_movies assert user.time_spent_watching_movies_minutes == 2 * watching_simulation.movie.runtime_minutes assert sum(1 for _ in user.watched_movies) == 1 assert watching_simulation.movie in user4.watched_movies assert user4.time_spent_watching_movies_minutes == watching_simulation.movie.runtime_minutes assert sum(1 for _ in user4.watched_movies) == 1
def test_watching_sim_file_reader(watching_sim_file_reader): watching_sim_file_reader.read_csv_file() watching_sims = [ watching_sim for watching_sim in watching_sim_file_reader.dataset_of_watching_sims ] assert len(watching_sims) == 5 assert watching_sims[0].movie == Movie('The Great Wall', 2016) assert watching_sims[1].movie == Movie('Split', 2016) assert watching_sims[2].movie == Movie('Sing', 2016) assert watching_sims[3].movie == Movie('The Lost City of Z', 2016) assert User('Martin', 'pw12345') in watching_sims[0].users assert sum(1 for _ in watching_sims[1].users) == 2 assert User('Daniel', 'pw87645') in watching_sims[2].users assert sum(1 for _ in watching_sims[3].users) == 3 review = next( (review for review in watching_sims[0].reviews if review.id == 5), None) assert review.rating == 7 and review.user in watching_sims[ 0].users and review.movie is watching_sims[0].movie assert len([review for review in watching_sims[2].reviews]) == 0 reviews = [review for review in watching_sims[3].reviews] assert len(reviews) == 2 assert reviews[0].movie == reviews[1].movie and reviews[ 0].movie == watching_sims[3].movie assert reviews[0].user in watching_sims[3].users and reviews[ 1].user in watching_sims[3].users
def test_repository_can_add_review(in_memory_repo): user = User("Martin", "pw12345") movie = Movie('Sing', 2016) review = Review(movie, "This movie was very enjoyable.", 8) user.add_review(review) in_memory_repo.add_review(review) assert in_memory_repo.get_review(review.id) is review
def test_user_hash(user, user_invalid): users = [ User(0, 0), User("Ian", "pw67890"), User(" MARTIN ", "pw54321") ] assert hash(user) == hash(users[2]) assert hash(user) != hash(users[1]) assert hash(user_invalid) == hash(users[0])
def test_user_eq(user, user_invalid): users = [ User(0, 0), User("Ian", "pw67890"), User(" MARTIN ", "pw54321") ] assert user_invalid == users[0] assert user != users[1] assert user == users[2]
def test_user_lt(user, user_invalid): users = [ User(0, 0), User("Ian", "pw67890"), User(" MARTIN ", "pw54321") ] assert users[1] < users[2] assert not user < users[2] assert user_invalid < user assert not user_invalid < users[0]
def test_user_review_relationship(user, review): user.add_review(review) assert review in user.reviews assert review.user == user user2 = User('Ian', 'pw67890') user2.add_review(review) assert review not in user2.reviews assert review.user == user user3 = User('Daniel', 'pw87465') review.user = user3 assert review.user == user
def test_repository_can_add_watchlist(in_memory_repo): user = User('Matt', 'pw4567') user.watchlist.add_movie(Movie('Sing', 2016)) in_memory_repo.add_watchlist(user.watchlist) assert in_memory_repo.get_watchlist_by_user_id(user.id) == user.watchlist assert in_memory_repo.get_watchlist_by_user_id(user.id).size() == 1
def test_review_file_reader(review_file_reader): review_file_reader.read_csv_file() reviews = [review for review in review_file_reader.dataset_of_reviews] assert len(reviews) == 8 assert reviews[0].user == User('Ian', 'pw67890') assert reviews[1].user == User('Martin', 'pw12345') assert reviews[2].user == User('Daniel', 'pw87465') assert reviews[0].movie == Movie('Suicide Squad', 2016) assert reviews[1].movie == Movie('Mindhorn', 2016) assert reviews[2].movie == Movie('Guardians of the Galaxy', 2014) assert "loved" in reviews[0].review_text and reviews[0].rating == 10 assert "enjoyable" in reviews[1].review_text and reviews[1].rating == 7 assert "entertaining" in reviews[2].review_text and reviews[2].rating == 8
def test_watching_simulation_users(watching_simulation): users = [User('Martin', 'pw12345'), User('Ian', 'pw67890'), User('Daniel', 'pw87465')] for user in users: watching_simulation.add_user(user) assert user in watching_simulation.users user_invalid = User(' ', '123') watching_simulation.add_user(user_invalid) assert user_invalid not in watching_simulation.users assert sum(1 for _ in watching_simulation.users) == 3 watching_simulation.add_user(users[0]) assert sum(1 for _ in watching_simulation.users) == 3 watching_simulation.remove_user(users[0]) watching_simulation.remove_user(users[0]) assert sum(1 for _ in watching_simulation.users) == 2
def test_repository_can_get_user(in_memory_repo): user = in_memory_repo.get_user('martin') assert user == User('Martin', 'pw12345') assert user.id == 1 review1 = in_memory_repo.get_review(2) review2 = in_memory_repo.get_review(4) assert review1 in user.reviews and review2 in user.reviews assert user.watchlist.size() == 6 assert in_memory_repo.get_movie_by_rank(7) in user.watchlist user = in_memory_repo.get_user('ian') assert user == User('Ian', 'pw67890') assert user.id == 2 review = in_memory_repo.get_review(1) movie = in_memory_repo.get_movie_by_rank(review.movie.rank) assert review in user.reviews and review in movie.reviews assert user.watchlist.size() == 0
def test_user_file_reader(user_file_reader): user_file_reader.read_csv_file() users = [user for user in user_file_reader.dataset_of_users] assert len(users) == 3 assert User('Martin', 'pw12345') == users[0] assert User('Ian', 'pw67890') == users[1] assert User('Daniel', 'pw87465') == users[2] assert User('Bob', 'pw01234') not in users assert Movie('Prometheus', 2012) in users[0].watched_movies assert users[1].time_spent_watching_movies_minutes == 0 assert Movie('Sing', 2016) in users[2].watched_movies for user in user_file_reader.dataset_of_users: assert user.user_name is not None and user.password is not None assert user.id is not None assert sum(1 for _ in user.reviews) == 0 assert sum(1 for _ in user.watchlist) == 0 assert user.watchlist.user is user
def add_user(user_name: str, password: str, repo: AbstractRepository): # Check that the given username is available and not currently used in repository user = repo.get_user(user_name) if user is not None: raise NameNotUniqueException # Encrypt password so that the database doesn't store passwords in plain text format. password_hash = generate_password_hash(password) # Create and store the new User with an encrypted password user = User(user_name, password_hash) repo.add_user(user)
def test_repository_cannot_add_invalid_review(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.add_review(Director('Joe')) with pytest.raises(RepositoryException): in_memory_repo.add_review(Review('', '', 0)) user = User("Martin", "pw12345") movie = Movie('Sing', 2016) review = Review(movie, "This movie was very enjoyable.", 8) review1 = Review(review.movie, review.review_text, review.rating) assert review is not review1 and review.id is not review1.id with pytest.raises(RepositoryException): in_memory_repo.add_review(review) movie.add_review(review) with pytest.raises(RepositoryException): in_memory_repo.add_review(review) review.user = user with pytest.raises(RepositoryException): in_memory_repo.add_review(review) user.add_review(review) with pytest.raises(RepositoryException): in_memory_repo.add_review(review) user.add_review(review1) in_memory_repo.add_review(review1) assert in_memory_repo.get_review(review1.id) is review1
def read_csv_file(self): with open(self.__file_name, mode='r', encoding='utf-8-sig') as csv_file: user_file_reader = csv.DictReader(csv_file) User.reset_id() for row in user_file_reader: user_name = row['Name'] password = row['Password'] user = User(user_name, password) for val in row['Watched Movie Ranks'].split(','): try: movie_rank = int(val) if movie_rank in self.__dataset_of_movies.keys(): watched_movie = self.__dataset_of_movies[ movie_rank] user.watch_movie(watched_movie) except ValueError: pass # Ignore exception and don't add movie to watched movies if user not in self.__dataset_of_users and user.id is not None: self.__dataset_of_users.append(user)
def test_repository_cannot_add_invalid_watching_sim(in_memory_repo): with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(Movie('A Movie', 2020)) watching_simulation = MovieWatchingSimulation(Movie('A Movie', 2020)) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation) watching_simulation = MovieWatchingSimulation( in_memory_repo.get_movie_by_rank(1)) users = [ in_memory_repo.get_user_by_id(1), User('Bob', 'pw1235'), in_memory_repo.get_user_by_id(3), in_memory_repo.get_user_by_id(2) ] for user in users: watching_simulation.add_user(user) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation) watching_simulation.remove_user(User('Bob', 'pw1235')) review = Review(watching_simulation.movie, 'Cool', 6) users[3].add_review(review) reviews = [ in_memory_repo.get_review(3), review, in_memory_repo.get_review(4) ] watching_simulation.watch_movie() for review in reviews: watching_simulation.add_user_review(review.user, review) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation) watching_simulation.remove_user_review(reviews[1]) for user in users: watching_simulation.remove_user(user) with pytest.raises(RepositoryException): in_memory_repo.add_watching_sim(watching_simulation)
def test_movie_review_relationship(movie, review, user): movie.add_review(review) assert review not in movie.reviews user.add_review(review) assert review in user.reviews assert review in movie.reviews assert review.movie == movie movie.add_review(review) movie2 = Movie('Batman', 1989) movie2.add_review(review) assert review not in movie2.reviews assert review.movie == movie user.remove_review(review) assert review not in user.reviews assert review not in movie.reviews movie.remove_review(review) user2 = User('Ian', 'pw67890') user2.add_review(review) assert review not in user2.reviews assert review.user == user
def test_repository_can_get_review(in_memory_repo): review = in_memory_repo.get_review(1) assert review.user == User('Ian', 'pw67890') assert review.movie == Movie('Suicide Squad', 2016) assert "loved" in review.review_text assert review.rating == 10 assert review in review.user.reviews and review in review.movie.reviews user = in_memory_repo.get_user(review.user.user_name) assert review in user.reviews movie = in_memory_repo.get_movie_by_rank(review.movie.rank) assert review in movie.reviews
def test_watchlist_file_reader(watchlist_file_reader): watchlist_file_reader.read_csv_file() watch_lists = [ watchlist for watchlist in watchlist_file_reader.dataset_of_watch_lists ] assert len(watch_lists) == 3 assert watch_lists[0].user == User('Daniel', 'pw87465') assert watch_lists[1].user == User('Martin', 'pw12345') assert watch_lists[2].user == User('Ian', 'pw67890') assert watch_lists[0].size() == 5 assert watch_lists[1].size() == 6 assert watch_lists[2].size() == 0 assert watch_lists[0].first_movie_in_watchlist() == Movie( 'Suicide Squad', 2016) assert watch_lists[1].first_movie_in_watchlist() == Movie('Split', 2016) assert watch_lists[2].first_movie_in_watchlist() is None assert watch_lists[0].select_movie_to_watch(5) is None assert watch_lists[1].select_movie_to_watch(5) == Movie( 'Suicide Squad', 2016) assert watch_lists[2].select_movie_to_watch(0) is None
def test_repository_cannot_add_invalid_watchlist(in_memory_repo): watchlist = WatchList() watchlist.add_movie(Movie('Guardians of the Galaxy', 2014)) with pytest.raises(RepositoryException): in_memory_repo.add_watchlist(watchlist) with pytest.raises(RepositoryException): in_memory_repo.add_watchlist(Director('Joe')) user = User('John', 'pw0135') user.watchlist.add_movie(Movie('A Movie', 2020)) with pytest.raises(RepositoryException): in_memory_repo.add_watchlist(user.watchlist) user.watchlist.remove_movie(Movie('A Movie', 2020)) in_memory_repo.add_watchlist(user.watchlist)
def test_user_constructor(user, user_invalid): assert repr(user) == "<User martin>" assert repr(user_invalid) == "<User None>" assert user_invalid.user_name is None user3 = User(0, 0) assert user3.user_name is None
def user(): return User("Martin", "pw12345")
def user_invalid(): return User(" ", " ")
def test_watching_simulation_reviews(watching_simulation): users = [User('Martin', 'pw12345'), User('Ian', 'pw67890'), User('Daniel', 'pw87465')] for user in users: watching_simulation.add_user(user) watching_simulation.remove_user(users[1]) watching_simulation.remove_user(users[1]) user4 = User('Bob', 'pw12347') watching_simulation.add_user(user4) watching_simulation.add_user(user4) assert sum(1 for _ in watching_simulation.users) == 3 review = Review(watching_simulation.movie, 'Fun', 8) watching_simulation.add_user_review(users[2], review) assert review not in watching_simulation.reviews and review not in watching_simulation.movie.reviews watching_simulation.watch_movie() movie = watching_simulation.movie assert watching_simulation.movie in users[0].watched_movies assert watching_simulation.movie not in users[1].watched_movies assert watching_simulation.movie in users[2].watched_movies assert watching_simulation.movie in user4.watched_movies reviews = [Review(movie, "good", 8), Review(movie, "great", 10), Review(movie, "boring", 1)] for i in range(len(reviews)): user = users[i] review = reviews[i] watching_simulation.add_user_review(user, review) assert reviews[0] in watching_simulation.reviews and reviews[0] in users[0].reviews and reviews[0] in movie.reviews assert reviews[1] not in watching_simulation.reviews and reviews[1] not in users[1].reviews \ and reviews[1] not in movie.reviews assert reviews[2] in watching_simulation.reviews and reviews[2] in users[2].reviews and reviews[2] in movie.reviews movie = Movie('Moana', 2016) review = Review(movie, 'Fun', 9) user4.add_review(review) watching_simulation.add_user_review(user4, review) assert review not in watching_simulation.reviews and review in user4.reviews review = Review(watching_simulation.movie, 'Fun', 9) watching_simulation.add_user_review(user4, review) assert review in watching_simulation.reviews and review in user4.reviews and \ review in watching_simulation.movie.reviews watching_simulation.remove_user_review(review) assert review not in watching_simulation.reviews and review not in user4.reviews and \ review not in watching_simulation.movie.reviews user5 = User('Tom', 'pw15567') user5.watch_movie(watching_simulation.movie) review = Review(watching_simulation.movie, 'Enjoyable', 8) watching_simulation.add_user_review(user5, review) assert review not in watching_simulation.reviews
def test_repository_can_add_user(in_memory_repo): user = User('John', 'pwxyz123') in_memory_repo.add_user(user) assert in_memory_repo.get_user(user.user_name) is user