def test_create_pet(self): """It should Create a new Pet""" test_pet = PetFactory() logging.debug("Test Pet: %s", test_pet.serialize()) response = self.client.post(BASE_URL, json=test_pet.serialize()) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Make sure location header is set location = response.headers.get("Location", None) self.assertIsNotNone(location) # Check the data is correct new_pet = response.get_json() self.assertEqual(new_pet["name"], test_pet.name) self.assertEqual(new_pet["category"], test_pet.category) self.assertEqual(new_pet["available"], test_pet.available) self.assertEqual(new_pet["gender"], test_pet.gender.name) # Check that the location header was correct response = self.client.get(location) self.assertEqual(response.status_code, status.HTTP_200_OK) new_pet = response.get_json() self.assertEqual(new_pet["name"], test_pet.name) self.assertEqual(new_pet["category"], test_pet.category) self.assertEqual(new_pet["available"], test_pet.available) self.assertEqual(new_pet["gender"], test_pet.gender.name)
def test_deserialize_bad_gender(self): """It should not deserialize a bad gender attribute""" test_pet = PetFactory() data = test_pet.serialize() data["gender"] = "male" # wrong case pet = Pet() self.assertRaises(DataValidationError, pet.deserialize, data)
def test_deserialize_bad_available(self): """Test deserialization of bad available attribute""" test_pet = PetFactory() data = test_pet.serialize() data["available"] = "true" pet = Pet() self.assertRaises(DataValidationError, pet.deserialize, data)
def test_create_pet_bad_available(self): """It should not Create a Pet with bad available data""" test_pet = PetFactory() logging.debug(test_pet) # change available to a string test_pet.available = "true" response = self.client.post(BASE_URL, json=test_pet.serialize()) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_deserialize_lower_case_gender(self): """Test deserialization with lower case gender""" test_pet = PetFactory() data = test_pet.serialize() data["gender"] = "male" # lower case pet = Pet() pet.deserialize(data) self.assertEqual(pet.gender, Gender.MALE)
def test_create_pet_bad_gender(self): """It should not Create a Pet with bad gender data""" pet = PetFactory() logging.debug(pet) # change gender to a bad string test_pet = pet.serialize() test_pet["gender"] = "male" # wrong case response = self.client.post(BASE_URL, json=test_pet) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def _create_pets(self, count): """Factory method to create pets in bulk""" pets = [] for _ in range(count): test_pet = PetFactory() response = self.client.post(BASE_URL, json=test_pet.serialize()) self.assertEqual(response.status_code, status.HTTP_201_CREATED, "Could not create test pet") new_pet = response.get_json() test_pet.id = new_pet["id"] pets.append(test_pet) return pets
def test_serialize_a_pet(self): """Test serialization of a Pet""" pet = PetFactory() data = pet.serialize() self.assertNotEqual(data, None) self.assertIn("name", data) self.assertEqual(data["name"], pet.name) self.assertIn("category", data) self.assertEqual(data["category"], pet.category) self.assertIn("available", data) self.assertEqual(data["available"], pet.available) self.assertIn("gender", data) self.assertEqual(data["gender"], pet.gender.name)
def test_update_pet(self): """It should Update an existing Pet""" # create a pet to update test_pet = PetFactory() response = self.client.post(BASE_URL, json=test_pet.serialize()) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # update the pet new_pet = response.get_json() logging.debug(new_pet) new_pet["category"] = "unknown" response = self.client.put(f"{BASE_URL}/{new_pet['id']}", json=new_pet) self.assertEqual(response.status_code, status.HTTP_200_OK) updated_pet = response.get_json() self.assertEqual(updated_pet["category"], "unknown")
def test_purchase_not_available(self): """Purchase a Pet that is not available""" pet = PetFactory() pet.available = False resp = self.app.post( BASE_URL, json=pet.serialize(), content_type=CONTENT_TYPE_JSON ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) pet_data = resp.get_json() pet_id = pet_data["_id"] logging.info(f"Created Pet with id {pet_id} = {pet_data}") # Request to purchase a Pet should fail resp = self.app.put(f"{BASE_URL}/{pet_id}/purchase") self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT)
def test_add_a_pet(self): """Create a pet and add it to the database""" pets = Pet.all() self.assertEqual(pets, []) pet = PetFactory() logging.debug("Pet: %s", pet.serialize()) self.assertNotEqual(pet, None) self.assertEqual(pet.id, None) pet.create() # Assert 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.assertEqual(pets[0].name, pet.name) self.assertEqual(pets[0].category, pet.category) self.assertEqual(pets[0].available, pet.available) self.assertEqual(pets[0].gender, pet.gender)
def test_serialize_a_pet(self): """It should serialize a Pet""" pet = PetFactory() data = pet.serialize() self.assertNotEqual(data, None) self.assertIn("id", data) self.assertEqual(data["id"], pet.id) self.assertIn("name", data) self.assertEqual(data["name"], pet.name) self.assertIn("category", data) self.assertEqual(data["category"], pet.category) self.assertIn("available", data) self.assertEqual(data["available"], pet.available) self.assertIn("gender", data) self.assertEqual(data["gender"], pet.gender.name) self.assertIn("birthday", data) self.assertEqual(date.fromisoformat(data["birthday"]), pet.birthday)
def _create_pets(self, count): """Factory method to create pets in bulk""" pets = [] for _ in range(count): test_pet = PetFactory() logging.info(f"PetFactory: {test_pet.serialize()}") resp = self.app.post( BASE_URL, json=test_pet.serialize(), content_type=CONTENT_TYPE_JSON ) self.assertEqual( resp.status_code, status.HTTP_201_CREATED, "Could not create test pet" ) new_pet = resp.get_json() logging.info(f"JSON Returned: {new_pet}") test_pet.id = new_pet["_id"] pets.append(test_pet) return pets
def test_update_pet(self): """Update an existing Pet""" # create a pet to update test_pet = PetFactory() resp = self.app.post( BASE_URL, json=test_pet.serialize(), content_type=CONTENT_TYPE_JSON ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # update the pet new_pet = resp.get_json() logging.debug(new_pet) new_pet["category"] = "unknown" resp = self.app.put( f"{BASE_URL}/{new_pet.get('_id')}", json=new_pet, content_type=CONTENT_TYPE_JSON, ) self.assertEqual(resp.status_code, status.HTTP_200_OK) updated_pet = resp.get_json() self.assertEqual(updated_pet["category"], "unknown")
def test_purchase_a_pet(self): """Purchase a Pet""" pet = PetFactory() pet.available = True resp = self.app.post( BASE_URL, json=pet.serialize(), content_type=CONTENT_TYPE_JSON ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) pet_data = resp.get_json() pet_id = pet_data["_id"] logging.info(f"Created Pet with id {pet_id} = {pet_data}") # Request to purchase a Pet resp = self.app.put(f"{BASE_URL}/{pet_id}/purchase") self.assertEqual(resp.status_code, status.HTTP_200_OK) # Retrieve the Pet and make sue it is no longer available resp = self.app.get(f"{BASE_URL}/{pet_id}") self.assertEqual(resp.status_code, status.HTTP_200_OK) pet_data = resp.get_json() self.assertEqual(pet_data["_id"], pet_id) self.assertEqual(pet_data["available"], False)
def test_create_pet(self): """Create a new Pet""" test_pet = PetFactory() logging.debug(test_pet) resp = self.app.post( BASE_URL, json=test_pet.serialize(), content_type=CONTENT_TYPE_JSON ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Make sure location header is set location = resp.headers.get("Location", None) self.assertIsNotNone(location) # Check the data is correct new_pet = resp.get_json() self.assertEqual(new_pet["name"], test_pet.name, "Names do not match") self.assertEqual( new_pet["category"], test_pet.category, "Categories do not match" ) self.assertEqual( new_pet["available"], test_pet.available, "Availability does not match" ) self.assertEqual( new_pet["gender"], test_pet.gender.name, "Gender does not match" ) # Check that the location header was correct resp = self.app.get(location) self.assertEqual(resp.status_code, status.HTTP_200_OK) new_pet = resp.get_json() self.assertEqual(new_pet["name"], test_pet.name, "Names do not match") self.assertEqual( new_pet["category"], test_pet.category, "Categories do not match" ) self.assertEqual( new_pet["available"], test_pet.available, "Availability does not match" ) self.assertEqual( new_pet["gender"], test_pet.gender.name, "Gender does not match" )