def test_rental_delete_by_id(self): rental = RentalModel(rental_schema.load(self.rental1)) rental.save() res = RentalService.delete_by_id(1234) self.assertEqual(res.status_code, 404) res = RentalService.delete_by_id(1) self.assertEqual(res.status_code, 204)
def create(req_data): req_data['user_id'] = RentalService.get_user() try: data = rental_schema.load(req_data) except ValidationError as e: return custom_response(e.messages, 400) rental = RentalModel(data) rental.save() data = rental_schema.dump(rental) return custom_response(data, 201)
def test_rental_get_by_id(self): rental = RentalModel(rental_schema.load(self.rental1)) rental.save() res = RentalService.get_by_id(13) self.assertEqual(res.status_code, 404) res = RentalService.get_by_id(1) json_data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(json_data.get('movie_id'), 1)
def test_rental_get_all(self): rental = RentalModel(rental_schema.load(self.rental1)) rental.save() rental = RentalModel(rental_schema.load(self.rental1)) rental.save() res = RentalService.get_all() json_data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(json_data.__len__(), 2)
def get_by_id(rental_id: int): rental = RentalModel.get_by_id(rental_id) if not rental: return custom_response({'error': 'not found'}, 404) data = rental_schema.dump(rental) if data.get('user_id') != RentalService.get_user(): return custom_response({'error': 'permission denied'}, 400) return custom_response(data, 200)
def return_movie(req_data): rental = RentalModel.get_by_id(req_data["rental_id"]) if not rental: return custom_response({'error': 'rental not found'}, 404) data = rental_schema.dump(rental) if data.get('user_id') != RentalService.get_user(): return custom_response({'error': 'permission denied'}, 400) rental = rental.return_movie() data = rental_schema.dump(rental) return custom_response(data, 200)
def update(rental_id: int, req_data): rental = RentalModel.get_by_id(rental_id) if not rental: return custom_response({'error': 'not found'}, 404) data = rental_schema.dump(rental) if data.get('user_id') != RentalService.get_user(): return custom_response({'error': 'permission denied'}, 400) try: data = rental_schema.load(req_data, partial=True) except ValidationError as e: return custom_response(e.messages, 400) rental.update(data) data = rental_schema.dump(rental) return custom_response(data, 200)
def create_rental(self): rental = RentalModel({'user_id': 1, 'movie_id': 1}) db.session.add(rental) db.session.commit() return rental
def test_rental_update_without_permission(self): rental = RentalModel(rental_schema.load(self.rental1)) rental.save() res = RentalService.update(1, rental_schema.load({}, partial=True)) self.assertEqual(res.status_code, 400)
def test_rental_update_not_found(self): rental = RentalModel(rental_schema.load(self.rental1)) rental.save() res = RentalService.update(33, rental_schema.load({}, partial=True)) self.assertEqual(res.status_code, 404)
def test_rental_get_by_id_without_permission(self): rental = RentalModel(rental_schema.load(self.rental1)) rental.save() res = RentalService.get_by_id(1) self.assertEqual(res.status_code, 400)
def get_all(): rentals = RentalModel.get_all_rentals() data = rental_schema.dump(rentals, many=True) return custom_response(data, 200)