def load_comments(data_path: str, repo: MemoryRepository, users): for data_row in read_csv_file(os.path.join(data_path, 'comments.csv')): comment = make_comment(comment_text=data_row[3], user=users[data_row[1]], movie=repo.get_movie(int(data_row[2])), timestamp=datetime.fromisoformat(data_row[4])) repo.add_comment(comment)
def test_repository_can_add_a_comment(in_memory_repo): user = in_memory_repo.get_user('thorke') movie = in_memory_repo.get_movie(2) comment = make_comment("Trump's onto it!", user, movie) in_memory_repo.add_comment(comment) assert comment in in_memory_repo.get_comments()
def test_make_comment_establishes_relationships(movie, user): comment_text = 'A nice movie!' comment = make_comment(comment_text, user, movie) # Check that the User object knows about the Comment. assert comment in user.comments # Check that the Comment knows about the User. assert comment.user is user # Check that Movie knows about the Comment. assert comment in movie.comments # Check that the Comment knows about the Movie. assert comment.movie is movie
def add_comment(movie_rank: int, comment_text: str, username: str, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_rank) if movie is None: raise NonExistentMovieException user = repo.get_user(username) if user is None: user = User("Guest account", "Abcd1234") # Create comment. comment = make_comment(comment_text, user, movie) # Update the repository. repo.add_comment(comment)