def test_movie_update(self): movie = MovieModel(movie_schema.load(self.movie1)) movie.save() res = MovieService.update(1, movie_schema.load(self.movie2)) json_data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(json_data.get('title'), 'Singularity')
def test_movie_get_by_title(self): movie = MovieModel(movie_schema.load(self.movie1)) movie.save() res = MovieService.get_by_title(movie.title) json_data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(json_data[0]['title'], 'Star Wars')
def test_movie_get_all(self): movie = MovieModel(movie_schema.load(self.movie1)) movie.save() movie = MovieModel(movie_schema.load(self.movie2)) movie.save() res = MovieService.get_all() json_data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(json_data.__len__(), 2)
def test_genre_get_movies(self): genre = GenreModel(genre_schema.load(self.genre1)) genre.save() movie = MovieModel(movie_schema.load({'title': 'ScaryMovie'})) movie.genres.append(genre) movie.save() res = GenreService.get_movies(1) json_data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertIsInstance(json_data, List) self.assertEqual(json_data.__len__(), 1) self.assertEqual(json_data[0]['title'], 'ScaryMovie')
def setUp(self): self.app = create_app("testing") self.client = self.app.test_client self.user1 = { 'name': 'user', 'email': '*****@*****.**', 'password': '******' } self.rental1 = {'movie_id': 1, 'user_id': 1} with self.app.app_context(): # create all tables db.create_all() movie = MovieModel(movie_schema.load({'title': 'test'})) movie.save() user = UserModel(user_schema.load(self.user1)) user.save()
def seed_things(): genre_names = [ {"name": "Horror"}, {"name": "Action"}, {"name": "Romance"}, {"name": "Comedy"}, {"name": "Adventure"} ] genres = [] for genre in genre_names: g = GenreModel(genre) g.save() genres.append(g) m = MovieModel({"title": "Saw"}) m.genres.extend([genres[0], genres[3]]) m.save() m = MovieModel({"title": "Hitman"}) m.genres.append(genres[1]) m.save() m = MovieModel({"title": "Notebook"}) m.genres.append(genres[2]) m.save() m = MovieModel({"title": "Star Wars"}) m.genres.extend([genres[1], genres[4]]) m.save() users = [ {"name": "user1", "email": "*****@*****.**", "password": "******"}, {"name": "user2", "email": "*****@*****.**", "password": "******"}, ] for user in users: u = UserModel(user) u.save()
class RentalModelTest(unittest.TestCase): def setUp(self): self.app = create_app("testing") self.client = self.app.test_client self.user = UserModel({ 'name': 'user', 'email': '*****@*****.**', 'password': '******' }) self.movie = MovieModel({'title': 'Star Wars'}) with self.app.app_context(): # create all tables db.create_all() self.user.save() self.movie.save() def create_rental(self): rental = RentalModel({'user_id': 1, 'movie_id': 1}) db.session.add(rental) db.session.commit() return rental @patch('src.model.RentalModel.get_now') def test_return_same_day(self, mock_now): mock_now.return_value = rent_day rental = self.create_rental() self.assertIsNotNone(rental) mock_now.return_value = day0 rental.return_movie() self.assertEqual(1, rental.fee) self.assertEqual(day0, rental.returned_at) @patch('src.model.RentalModel.get_now') def test_return_after_two_days(self, mock_now): mock_now.return_value = rent_day rental = self.create_rental() self.assertIsNotNone(rental) mock_now.return_value = day2 rental.return_movie() self.assertEqual(2, rental.fee) self.assertEqual(day2, rental.returned_at) @patch('src.model.RentalModel.get_now') def test_return_after_three_days(self, mock_now): mock_now.return_value = rent_day rental = self.create_rental() self.assertIsNotNone(rental) mock_now.return_value = day3 rental.return_movie() self.assertEqual(3, rental.fee) self.assertEqual(day3, rental.returned_at) @patch('src.model.RentalModel.get_now') def test_return_after_four_days(self, mock_now): mock_now.return_value = rent_day rental = self.create_rental() self.assertIsNotNone(rental) mock_now.return_value = day4 rental.return_movie() self.assertEqual(3.5, rental.fee) self.assertEqual(day4, rental.returned_at) @patch('src.model.RentalModel.get_now') def test_return_after_five_days(self, mock_now): mock_now.return_value = rent_day rental = self.create_rental() self.assertIsNotNone(rental) mock_now.return_value = day5 rental.return_movie() self.assertEqual(4, rental.fee) self.assertEqual(day5, rental.returned_at) def tearDown(self): with self.app.app_context(): db.session.remove() db.drop_all()
def test_delete_movie(self): movie = MovieModel(movie_schema.load(self.movie1)) movie.save() res = MovieService.delete_by_id(1) self.assertEqual(res.status_code, 204)