def _validate_reservation_payload(self, reservation_payload): reservation = ReservationsDBModel(**reservation_payload) with self.db: rows = self.db.get_book_by_id(reservation.book_id) if not rows: raise ResourceNotFound(resource_type="Book", field="book_id", value=reservation.book_id) rows = self.db.get_user_by_id(reservation.user_id) if not rows: raise ResourceNotFound(resource_type="User", field="user_id", value=reservation.user_id)
def test_get_book_invalid_id(self, mock_book_resource): with patch('library_backend.service.BookService.get_book') as test_mock: test_mock.side_effect = ResourceNotFound(resource_type="Book", field="book_id", value=mock_book_resource["id"]) with app.app.test_client() as client: response = client.get(f'/books/{mock_book_resource["id"]}') assert '400' == response.status assert f'"Book with book_id = {mock_book_resource["id"]} was not found"' == response.get_data().decode('utf-8')
def add_reservation(self, reservation_payload): try: self._validate_reservation_payload(reservation_payload) except Exception as e: raise e reservation = ReservationsDBModel(**reservation_payload) existing_reservation = self._get_reservation_by_user_id_and_book_id( reservation.user_id, reservation.book_id) if existing_reservation: raise ReservationAlreadyExists(reservation_payload) with self.db: self.db.add_reservation(reservation) try: user, book, reservation = self.db.get_reserved_book_by_user_id_and_book_id( user_id=reservation.user_id, book_id=reservation.book_id) reservation_details = { "user": user.serialize(), "book": book.serialize(), "reservation_date": reservation.reservation_date, "reservation_expiration_date": reservation.reservation_expiration_date } return reservation_details except TypeError: raise ResourceNotFound(resource_type="Reservation", field="(user_id, book_id)", value=(reservation_payload['user_id'], reservation_payload['book_id']))
def delete_reservation_for_book(self, book_id): with self.db: rows = self.db.delete_reservation_by_book(book_id=book_id) if rows == 0: raise ResourceNotFound(resource_type="Reservation", field="book_id", value=book_id) return f"Deleted reservation for book {book_id}"
def delete_reservations_for_user(self, user_id): with self.db: rows = self.db.delete_reservation_by_user(user_id) if rows == 0: raise ResourceNotFound(resource_type="Reservation", field="user_id", value=user_id) return f"Deleted {rows} reservations for user {user_id}"
def delete_user(self, user_id): db = SQLiteDatabaseConnection() with db: rows = db.delete_user_by_id(user_id) if rows == 0: raise ResourceNotFound(resource_type="User", field="id", value=user_id) return f"Successfully deleted user {user_id}"
def delete_reservation_by_user_and_book(self, user_id, book_id): with self.db: rows = self.db.delete_reservation_by_user_and_book_id( user_id=user_id, book_id=book_id) if rows == 0: raise ResourceNotFound(resource_type="Reservation", field="(user_id, book_id)", value=(user_id, book_id)) return f"Deleted reservation for user {user_id} and book {book_id}"
def delete_book(self, book_id): db = SQLiteDatabaseConnection() with db: rows = db.delete_book_by_id(book_id) if rows == 0: raise ResourceNotFound(resource_type="Book", field="id", value=book_id) return f"Successfully deleted book {book_id}"
def get_book(self, book_id): db = SQLiteDatabaseConnection() with db: book = db.get_book_by_id(book_id) if not book: raise ResourceNotFound(resource_type="Book", field="id", value=book_id) book = book.serialize() return book
def get_user(self, user_id): db = SQLiteDatabaseConnection() with db: user = db.get_user_by_id(user_id) if not user: raise ResourceNotFound(resource_type="User", field="id", value=user_id) user = user.serialize() return user
def test_delete_user_invalid_id(self, mock_user_resource): with patch('library_backend.service.UserService.delete_user' ) as test_mock: test_mock.side_effect = ResourceNotFound( resource_type="User", field="user_id", value=mock_user_resource["id"]) with app.app.test_client() as client: response = client.delete(f'/users/{mock_user_resource["id"]}') assert '400' == response.status assert f'"User with user_id = {mock_user_resource["id"]} was not found"' == response.get_data( ).decode('utf-8')
def test_reservation_delete_by_book_id_invalid(self, mock_reservation_resource): with patch( 'library_backend.service.ReservationService.delete_reservation_for_book' ) as test_mock: test_mock.side_effect = ResourceNotFound( resource_type="Reservation", field="book_id", value=f"{mock_reservation_resource['book']['id']}") endpoint_url = f"/reservations/book/{mock_reservation_resource['book']['id']}" with app.app.test_client() as client: response = client.delete(endpoint_url) assert '400' == response.status assert f'Reservation with book_id = {mock_reservation_resource["book"]["id"]} was not found'\ == json.loads(response.get_data().decode('utf-8'))
def test_reservation_get_by_valid_user_id_and_invalid_book_id( self, mock_reservation_resource): with patch( 'library_backend.service.ReservationService.get_reservation_by_user_id_and_book_id' ) as test_mock: test_mock.side_effect = ResourceNotFound( resource_type="reservation", field="user_id and book_id", value=f"{mock_reservation_resource['user']['id']} and 456") endpoint_url = f"/reservations/user/{mock_reservation_resource['user']['id']}/book/456" with app.app.test_client() as client: response = client.get(endpoint_url) assert '400' == response.status assert '"reservation with user_id and book_id = 123 and 456 was not found"' == response.get_data( ).decode('utf-8')
def test_reservation_delete_by_user_and_book_id_invalid( self, mock_reservation_resource): with patch( 'library_backend.service.ReservationService.delete_reservation_by_user_and_book' ) as test_mock: test_mock.side_effect = ResourceNotFound( resource_type="reservation", field="user_id and book_id", value=f"{mock_reservation_resource['user']['id']}" f" and {mock_reservation_resource['book']['id']}") endpoint_url = f'/reservations/user/{mock_reservation_resource["user"]["id"]}' \ f'/book/{mock_reservation_resource["book"]["id"]}' with app.app.test_client() as client: response = client.delete(endpoint_url) assert '400' == response.status assert '"reservation with user_id and book_id = 123 and 123 was not found"' == response.get_data( ).decode('utf-8')
def edit_book(self, book_id, new_book): db = SQLiteDatabaseConnection() existing_book = self.get_book(book_id) if not existing_book: raise ResourceNotFound(resource_type="Book", field="id", value=book_id) new_book["id"] = book_id new_book_model = BooksDBModel(**new_book) with db: existing_book = db.get_book_by_author_and_name( name=new_book_model.name, author=new_book_model.author) if existing_book and not existing_book.id == book_id: raise BookAlreadyExists(new_book) rows = db.update_book(book_id, new_book_model) if rows == 0: raise DatabaseCommunicationIssue("update book") return self.get_book(book_id)
def get_reservation_by_user_id_and_book_id(self, user_id, book_id): with self.db: try: user, book, reservation = self.db.get_reserved_book_by_user_id_and_book_id( user_id=user_id, book_id=book_id) reservation = { "user": user.serialize(), "book": book.serialize(), "reservation_date": reservation.reservation_date, "reservation_expiration_date": reservation.reservation_expiration_date } return reservation except TypeError: raise ResourceNotFound(resource_type="Reservation", field="(user_id, book_id)", value=(user_id, book_id))