def test_get_movie_by_id(setup_db): # ARRANGE # Insert movies in db movie_model_1 = Movie.load(movie_dict_example_1) db.session.add(movie_model_1) db.session.commit() # So we can re-use the Genre model inserted above movie_model_2 = Movie.load( movie_dict_example_2 ) # Instead of inserting new Genre we retrieve the one above from db db.session.add(movie_model_2) db.session.commit() # ACT movie1 = get_movie_by_id(1) movie2 = get_movie_by_id(2) # ASSERT assert movie1.dump() == movie_dict_example_1 assert movie2.dump() == movie_dict_example_2
def test_get_movie_with_existing_id(test_client): # ARRANGE # Insert movies in db movie_model_1 = Movie.load(movie_dict_example_1) db.session.add(movie_model_1) db.session.commit() # So we can re-use the Genre model inserted above movie_model_2 = Movie.load( movie_dict_example_2 ) # Instead of inserting new Genre we retrieve the one above from db db.session.add(movie_model_2) db.session.commit() movie_id = 1 # ACT response = test_client.get(f'/movies/{movie_id}') movie_dict = response.json # ASSERT assert response.status_code == 200 assert movie_dict == movie_dict_example_1
def test_get_all_movies(setup_db): # ARRANGE # Insert a movie in database movie_model = Movie.load(movie_dict_example_1) db.session.add(movie_model) db.session.commit() # ACT all_movies = get_all_movies() # ASSERT assert len(all_movies) == 1 assert all_movies[0].dump() == movie_dict_example_1
def test_delete_movie_by_id(setup_db): # ARRANGE # Insert a movie in database movie_model = Movie.load(movie_dict_example_1) db.session.add(movie_model) db.session.commit() # ACT delete_movie_by_id(1) # Retrieve all movies all_movies = Movie.query.all() # ASSERT assert len(all_movies) == 0
def test_read(test_client): # ARRANGE # Insert a movie in database movie_model = Movie.load(movie_dict_example_1) db.session.add(movie_model) db.session.commit() # ACT response = test_client.get('/movies') all_movies = response.json # ASSERT assert response.status_code == 200 assert len(all_movies) == 1 assert all_movies[0] == movie_dict_example_1
def test_delete_movie(test_client): # ARRANGE # Insert a movie in database movie_model = Movie.load(movie_dict_example_1) db.session.add(movie_model) db.session.commit() movie_id = 1 # ACT response = test_client.delete(f'/movies/{movie_id}') all_movies = Movie.query.all() # ASSERT assert response.status_code == 204 assert len(all_movies) == 0
def test_movie_load(setup_db): # ARRANGE # ACT movie_model = Movie.load(movie_dict_example_1) # ASSERT assert movie_model.name == movie_dict_example_1.get('name') assert movie_model.description == movie_dict_example_1.get('description') assert movie_model.duration == movie_dict_example_1.get('duration') assert movie_model.poster == movie_dict_example_1.get('poster') assert movie_model.rating == movie_dict_example_1.get('rating') assert movie_model.year == movie_dict_example_1.get('year') assert movie_model.director == movie_dict_example_1.get('director') assert movie_model.genre[0].id == None assert movie_model.genre[0].name == movie_dict_example_1.get( 'genre')[0].get('name')
def replace_movie_in_db(movie_id, movie): movie_to_replace = Movie.query.filter_by(id=movie_id).one_or_none() movie['id'] = movie_id if movie_to_replace is not None: movie_to_replace.update(**movie) logger.info(f'Updated movie with id: {movie_id} in database.') else: # Insert new movie logger.info( f'Movie with id: {movie_id} to update is not found in database. inserting movie: {pformat(movie)}' ) movie_model = Movie.load(movie) db.session.add(movie_model) db.session.commit( ) # movie_to_replace ORM object is tracked => commit saves changes done to the object
def test_replace_movie_in_db_with_existing_id(setup_db): # ARRANGE # Insert a movie in database movie_model = Movie.load(movie_dict_example_1) db.session.add(movie_model) db.session.commit() # ACT replace_movie_in_db(1, movie_dict_example_2) # Retrieve the replaced movie all_movies = Movie.query.all() # Change movie_dict_example_2's id to 1 (id of movie to replace) movie_dict_example_2_copy = movie_dict_example_2.copy() movie_dict_example_2_copy['id'] = 1 # ASSERT assert len(all_movies) == 1 assert all_movies[0].dump() == movie_dict_example_2_copy
def test_replace_movie(test_client): # ARRANGE # Insert a movie in database movie_model = Movie.load(movie_dict_example_1) db.session.add(movie_model) db.session.commit() movie_id = 1 # ACT response = test_client.put(f'/movies/{movie_id}', json=movie_dict_example_2) all_movies = Movie.query.all() # Change movie_dict_example_2's id to 1 (movie with id 1 in db has movie_dict_example_2 infos) movie_dict_example_2_copy = movie_dict_example_2.copy() movie_dict_example_2_copy['id'] = 1 assert response.status_code == 204 assert len(all_movies) == 1 assert all_movies[0].dump() == movie_dict_example_2_copy
def add_movie_to_db(movie): movie_model = Movie.load(movie) db.session.add(movie_model) db.session.commit() logger.info(f'Added movie to database, movie info: {pformat(movie)}')