def test_find_pet(self): """ Find a Pet by id """ Pet(0, "fido", "dog").save() Pet(0, "kitty", "cat").save() pet = Pet.find(2) self.assertIsNot(pet, None) self.assertEqual(pet.id, 2) self.assertEqual(pet.name, "kitty")
def test_find_pet(self): """ Find a Pet by id """ Pet("fido", "dog").create() new_pet = Pet("kitty", "cat") new_pet.create() pet = Pet.find(new_pet.id) self.assertIsNot(pet, None) self.assertEqual(pet.id, new_pet.id) self.assertEqual(pet.name, "kitty")
def purchase_pets(pet_id): """ Purchasing a Pet makes it unavailable """ pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) if not pet.available: abort(status.HTTP_400_BAD_REQUEST, "Pet with id '{}' is not available.".format(pet_id)) pet.available = False pet.save() return make_response(jsonify(pet.serialize()), status.HTTP_200_OK)
def test_find_pet(self): """ Find a Pet by ID """ Pet(name="fido", category="dog", available=True).create() kitty = Pet(name="kitty", category="cat", available=False) kitty.create() pet = Pet.find(kitty.id) self.assertIsNot(pet, None) self.assertEqual(pet.id, kitty.id) self.assertEqual(pet.name, "kitty") self.assertEqual(pet.available, False)
def delete_pets(pet_id): """ Delete a Pet This endpoint will delete a Pet based the id specified in the path """ app.logger.info('Request to Delete a pet with id [%s]', pet_id) pet = Pet.find(pet_id) if pet: pet.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def get_pets(pet_id): """ Retrieve a single Pet This endpoint will return a Pet based on it's id """ app.logger.info("Request to Retrieve a pet with id [%s]", pet_id) pet = Pet.find(pet_id) if not pet: raise NotFound("Pet with id '{}' was not found.".format(pet_id)) return make_response(jsonify(pet.serialize()), status.HTTP_200_OK)
def purchase_pets(pet_id): """ Purchase a Pet """ app.logger.info('Request to purchase Pet with id %s', pet_id) pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) if not pet.available: abort(status.HTTP_400_BAD_REQUEST, "Pet with id '{}' is not available.".format(pet_id)) pet.available = False pet.save() return make_response(jsonify(pet.serialize()), status.HTTP_200_OK)
def test_find_pet(self): """Find a Pet by id""" pets = self._create_pets(5) saved_pet = pets[0] pet = Pet.find(saved_pet.id) self.assertIsNot(pet, None) self.assertEqual(pet.id, saved_pet.id) self.assertEqual(pet.name, saved_pet.name) self.assertEqual(pet.category, saved_pet.category) self.assertEqual(pet.available, saved_pet.available) self.assertEqual(pet.gender, saved_pet.gender)
def get_pets(pet_id): """ Retrieve a single Pet This endpoint will return a Pet based on it's id """ app.logger.info('Request to get Pet with id %s', pet_id) pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) return make_response(jsonify(pet.serialize()), status.HTTP_200_OK)
def get(self, pet_id): """ Retrieve a single Pet This endpoint will return a Pet based on it's id """ app.logger.info("Request to Retrieve a pet with id [%s]", pet_id) pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) return pet.serialize(), status.HTTP_200_OK
def test_read_a_pet(self): """Read a Pet""" pet = PetFactory() logging.debug(pet) pet.create() self.assertIsNotNone(pet.id) # Fetch it back found_pet = Pet.find(pet.id) self.assertEqual(found_pet.id, pet.id) self.assertEqual(found_pet.name, pet.name) self.assertEqual(found_pet.category, pet.category) self.assertEqual(found_pet.gender, pet.gender)
def put(self, pet_id): """ Purchase a Pet """ pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) if not pet.available: abort(status.HTTP_400_BAD_REQUEST, "Pet with id '{}' is not available.".format(pet_id)) pet.available = False pet.save() return pet.serialize(), status.HTTP_200_OK
def delete_pets(pet_id): """ Delete a Pet This endpoint will delete a Pet based the id specified in the path """ app.logger.info("Request to delete pet with id: %s", pet_id) pet = Pet.find(pet_id) if pet: pet.delete() app.logger.info("Pet with ID [%s] delete complete.", pet_id) return "", status.HTTP_204_NO_CONTENT
def get_pets(pet_id): """ Retrieve a single Pet This endpoint will return a Pet based on it's id """ app.logger.info("Request for pet with id: %s", pet_id) pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, f"Pet with id '{pet_id}' was not found.") app.logger.info("Returning pet: %s", pet.name) return jsonify(pet.serialize()), status.HTTP_200_OK
def purchase_pets(pet_id): """Endpoint to Purchase a Pet""" app.logger.info("Request to Purchase pet with id: %s", pet_id) pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, f"Pet with id '{pet_id}' was not found.") if not pet.available: abort(status.HTTP_409_CONFLICT, f"Pet with id '{pet_id}' is not available.") pet.available = False pet.update() return jsonify(pet.serialize()), status.HTTP_200_OK
def test_find_pet(self): """ Find a Pet by ID """ pets = PetFactory.create_batch(3) for pet in pets: pet.create() logging.debug(pets) # make sure they got saved self.assertEqual(len(Pet.all()), 3) # find the 2nd pet in the list pet = Pet.find(pets[1].id) self.assertIsNot(pet, None) self.assertEqual(pet.id, pets[1].id) self.assertEqual(pet.name, pets[1].name) self.assertEqual(pet.available, pets[1].available)
def update_pets(pet_id): """ Update a Pet This endpoint will update a Pet based the body that is posted """ app.logger.info('Request to update Pet with id %s', pet_id) pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) pet.deserialize(request.get_json()) pet.id = pet_id pet.save() return make_response(jsonify(pet.serialize()), status.HTTP_200_OK)
def update_pets(pet_id): """ Update a Pet This endpoint will update a Pet based the body that is posted """ app.logger.info("Request to update pet with id: %s", pet_id) check_content_type("application/json") pet = Pet.find(pet_id) if not pet: raise NotFound("Pet with id '{}' was not found.".format(pet_id)) pet.deserialize(request.get_json()) pet.id = pet_id pet.save() return make_response(jsonify(pet.serialize()), status.HTTP_200_OK)
def put(self, pet_id): """ Update a Pet This endpoint will update a Pet based the body that is posted """ app.logger.info('Request to Update a pet with id [%s]', pet_id) pet = Pet.find(pet_id) if not pet: api.abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) app.logger.debug('Payload = %s', api.payload) data = api.payload pet.deserialize(data) pet.id = pet_id pet.save() return pet.serialize(), status.HTTP_200_OK
def put(self, pet_id): """ Purchase a Pet This endpoint will purchase a Pet and make it unavailable """ app.logger.info('Request to Purchase a Pet') pet = Pet.find(pet_id) if not pet: api.abort(status.HTTP_404_NOT_FOUND, 'Pet with id [{}] was not found.'.format(pet_id)) if not pet.available: api.abort(status.HTTP_409_CONFLICT, 'Pet with id [{}] is not available.'.format(pet_id)) pet.available = False pet.save() app.logger.info('Pet with id [%s] has been purchased!', pet.id) return pet.serialize(), status.HTTP_200_OK
def update_pets(pet_id): """ Update a Pet This endpoint will update a Pet based the body that is posted """ app.logger.info("Request to Update pet with id: %s", pet_id) check_content_type("application/json") pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, f"Pet with id '{pet_id}' was not found.") pet.deserialize(request.get_json()) pet.id = pet_id pet.update() app.logger.info("Pet with ID [%s] updated.", pet.id) return jsonify(pet.serialize()), status.HTTP_200_OK
def put(self, pet_id): """ Update a Pet This endpoint will update a Pet based the body that is posted """ app.logger.info('Request to Update a pet with id [%s]', pet_id) #check_content_type('application/json') pet = Pet.find(pet_id) if not pet: abort(status.HTTP_404_NOT_FOUND, "Pet with id '{}' was not found.".format(pet_id)) payload = request.get_json() try: pet.deserialize(payload) except DataValidationError as error: raise BadRequest(str(error)) pet.id = pet_id pet.save() return pet.serialize(), status.HTTP_200_OK
def test_pet_not_found(self): """ Find a Pet that doesnt exist """ Pet("fido", "dog").save() pet = Pet.find("2") self.assertIs(pet, None)
def test_find_with_no_pets(self): """ Find a Pet with empty database """ pet = Pet.find("1") self.assertIs(pet, None)
def test_pet_not_found(self): """Pet not found""" pet = Pet.find("foo") self.assertIsNone(pet)