Exemplo n.º 1
0
 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")
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
 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)
Exemplo n.º 12
0
 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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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
Exemplo n.º 15
0
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
Exemplo n.º 16
0
 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)
Exemplo n.º 17
0
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)
Exemplo n.º 18
0
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)
Exemplo n.º 19
0
    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
Exemplo n.º 20
0
    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
Exemplo n.º 21
0
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
Exemplo n.º 22
0
    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
Exemplo n.º 23
0
 def test_pet_not_found(self):
     """ Find a Pet that doesnt exist """
     Pet("fido", "dog").save()
     pet = Pet.find("2")
     self.assertIs(pet, None)
Exemplo n.º 24
0
 def test_find_with_no_pets(self):
     """ Find a Pet with empty database """
     pet = Pet.find("1")
     self.assertIs(pet, None)
Exemplo n.º 25
0
 def test_pet_not_found(self):
     """Pet not found"""
     pet = Pet.find("foo")
     self.assertIsNone(pet)