def test_key_error_on_update(self, bad_mock): """Test KeyError on update""" bad_mock.side_effect = KeyError() pet = Pet("fido", "dog", False) pet.create() pet.name = "Fifi" pet.update()
def test_delete_a_pet(self): """ Delete a Pet """ pet = Pet(name="fido", category="dog", available=True) pet.create() self.assertEqual(len(Pet.all()), 1) # delete the pet and make sure it isn't in the database pet.delete() self.assertEqual(len(Pet.all()), 0)
def test_delete_a_pet(self): """ Delete a Pet """ pet = Pet("fido", "dog") pet.create() self.assertEqual(len(Pet.all()), 1) # delete the pet and make sure it isn't in the database pet.delete() self.assertEqual(len(Pet.all()), 0)
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 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 test_add_a_pet(self): """ Create a pet and add it to the database """ pets = Pet.all() self.assertEqual(pets, []) pet = Pet(name="fido", category="dog", available=True) self.assertTrue(pet != None) self.assertEqual(pet.id, None) pet.create() # Asert that it was assigned an id and shows up in the database self.assertEqual(pet.id, 1) pets = Pet.all() self.assertEqual(len(pets), 1)
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted """ app.logger.info("Request to create a pet") check_content_type("application/json") pet = Pet() pet.deserialize(request.get_json()) pet.create() message = pet.serialize() location_url = url_for("get_pets", pet_id=pet.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def test_update_a_pet(self): """ Update a Pet """ pet = Pet(name="fido", category="dog", available=True) pet.create() self.assertEqual(pet.id, 1) # Change it an update it pet.category = "k9" pet.update() self.assertEqual(pet.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change pets = Pet.all() self.assertEqual(len(pets), 1) self.assertEqual(pets[0].category, "k9")
def test_update_a_pet(self): """ Update a Pet """ pet = Pet("fido", "dog", True, Gender.Male) pet.create() self.assertNotEqual(pet.id, None) # Change it an update it pet.category = "k9" pet.update() # Fetch it back and make sure the id hasn't changed # but the data did change pets = Pet.all() self.assertEqual(len(pets), 1) self.assertEqual(pets[0].category, "k9") self.assertEqual(pets[0].name, "fido")
def test_add_a_pet(self): """It should Create a pet and add it to the database""" pets = Pet.all() self.assertEqual(pets, []) pet = Pet(name="Fido", category="dog", available=True, gender=Gender.MALE) self.assertTrue(pet is not None) self.assertEqual(pet.id, None) pet.create() # Assert that it was assigned an id and shows up in the database self.assertIsNotNone(pet.id) pets = Pet.all() self.assertEqual(len(pets), 1)
def post(self): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted """ app.logger.info('Request to Create a Pet') pet = Pet() app.logger.debug('Payload = %s', api.payload) pet.deserialize(api.payload) pet.create() app.logger.info('Pet with new id [%s] saved!', pet.id) location_url = api.url_for(PetResource, pet_id=pet.id, _external=True) return pet.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }
def test_add_a_pet(self): """ Create a pet and add it to the database """ pets = Pet.all() self.assertEqual(pets, []) pet = Pet("fido", "dog", True, Gender.Unknown) self.assertNotEqual(pet, None) self.assertEqual(pet.id, None) pet.create() # Asert that it was assigned an id and shows up in the database self.assertNotEqual(pet.id, None) pets = Pet.all() self.assertEqual(len(pets), 1) self.assertNotEqual(pets[0].id, None) self.assertEqual(pets[0].name, "fido") self.assertEqual(pets[0].category, "dog") self.assertEqual(pets[0].available, True) self.assertEqual(pets[0].gender, Gender.Unknown)
def create_pets(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted or data that is sent via an html form post. """ app.logger.info("Request to Create a pet") content_type = request.headers.get("Content-Type") if not content_type: abort(status.HTTP_400_BAD_REQUEST, "No Content-Type set") data = {} # Check for form submission data if content_type == "application/x-www-form-urlencoded": app.logger.info("Processing FORM data") app.logger.info(type(request.form)) app.logger.info(request.form) data = { "name": request.form["name"], "category": request.form["category"], "available": request.form["available"].lower() in ["yes", "y", "true", "t", "1"], "gender": request.form["gender"], } app.logger.info("Available: {} = {}".format(request.form["available"], data["available"])) elif content_type == "application/json": app.logger.info("Processing JSON data") data = request.get_json() else: message = "Unsupported Content-Type: {}".format(content_type) app.logger.info(message) abort(status.HTTP_400_BAD_REQUEST, message) # Create the Pet from the data pet = Pet() pet.deserialize(data) pet.create() message = pet.serialize() location_url = url_for("get_pets", pet_id=pet.id, _external=True) app.logger.info("Pet with ID [%s] created.", pet.id) return jsonify(pet.serialize()), status.HTTP_201_CREATED, {"Location": location_url}
def test_key_error_on_delete(self, bad_mock): """ Test KeyError on delete """ bad_mock.side_effect = KeyError() pet = Pet("fido", "dog", False) pet.create() pet.delete()
def test_document_not_exist(self, bad_mock): """ Test a Bad Document Exists """ bad_mock.return_value = False pet = Pet("fido", "dog", False) pet.create() self.assertIsNone(pet.id)
def test_http_error(self, bad_mock): """ Test a Bad Create with HTTP error """ bad_mock.side_effect = HTTPError() pet = Pet("fido", "dog", False) pet.create() self.assertIsNone(pet.id)
def test_connect(self): """Test Connect""" Pet.disconnect() Pet.connect() pet = Pet("fido", "dog", False) pet.create()