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_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_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_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 _create_pets(self, count: int) -> list: """Creates a collection of pets in the database""" pet_collection = [] for _ in range(count): pet = PetFactory() pet.create() pet_collection.append(pet) return pet_collection
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_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 test_list_all_pets(self): """List Pets in the database""" pets = Pet.all() self.assertEqual(pets, []) # Create 5 Pets for i in range(5): pet = PetFactory() pet.create() # See if we get back 5 pets pets = Pet.all() self.assertEqual(len(pets), 5)
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 _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_find_by_name(self): """Find a Pet by Name""" self._create_pets(5) saved_pet = PetFactory() saved_pet.name = "Rumpelstiltskin" saved_pet.create() # search by name pets = Pet.find_by_name("Rumpelstiltskin") self.assertNotEqual(len(pets), 0) pet = pets[0] self.assertEqual(pet.name, "Rumpelstiltskin") self.assertEqual(pet.category, saved_pet.category) self.assertEqual(pet.available, saved_pet.available) self.assertEqual(pet.gender, saved_pet.gender)
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_get_related_fk_objects(self): person = PersonFactory.create() for i in range(3): PetFactory.create(owner=person) location = PersonLocationFactory.create(owner=person) related_objects = get_related_objects(person) self.assertEqual(len(related_objects[MockPet.owner.field]), 3) for pet in related_objects[MockPet.owner.field]: self.assertEqual(pet.owner.id, person.id) self.assertEqual(len(related_objects[MockPersonLocation.owner.field]), 1) for location in related_objects[MockPersonLocation.owner.field]: self.assertEqual(location.owner.id, person.id)
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 _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_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 test_find_by_availability(self): """It should Find Pets by Availability""" pets = PetFactory.create_batch(10) for pet in pets: pet.create() available = pets[0].available count = len([pet for pet in pets if pet.available == available]) found = Pet.find_by_availability(available) self.assertEqual(found.count(), count) for pet in found: self.assertEqual(pet.available, available)
def test_find_by_category(self): """It should Find Pets by Category""" pets = PetFactory.create_batch(10) for pet in pets: pet.create() category = pets[0].category count = len([pet for pet in pets if pet.category == category]) found = Pet.find_by_category(category) self.assertEqual(found.count(), count) for pet in found: self.assertEqual(pet.category, category)
def test_find_by_gender(self): """It should Find Pets by Gender""" pets = PetFactory.create_batch(10) for pet in pets: pet.create() gender = pets[0].gender count = len([pet for pet in pets if pet.gender == gender]) found = Pet.find_by_gender(gender) self.assertEqual(found.count(), count) for pet in found: self.assertEqual(pet.gender, gender)
def test_deserialize_a_pet(self): """It should de-serialize a Pet""" data = PetFactory().serialize() pet = Pet() pet.deserialize(data) self.assertNotEqual(pet, None) self.assertEqual(pet.id, None) self.assertEqual(pet.name, data["name"]) self.assertEqual(pet.category, data["category"]) self.assertEqual(pet.available, data["available"]) self.assertEqual(pet.gender.name, data["gender"]) self.assertEqual(pet.birthday, date.fromisoformat(data["birthday"]))
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_find_by_name(self): """It should Find a Pet by Name""" pets = PetFactory.create_batch(5) for pet in pets: pet.create() name = pets[0].name found = Pet.find_by_name(name) self.assertEqual(found.count(), 1) self.assertEqual(found[0].category, pets[0].category) self.assertEqual(found[0].name, pets[0].name) self.assertEqual(found[0].available, pets[0].available) self.assertEqual(found[0].gender, pets[0].gender) self.assertEqual(found[0].birthday, pets[0].birthday)
def test_find_or_404_found(self): """It should Find or return 404 not found""" pets = PetFactory.create_batch(3) for pet in pets: pet.create() pet = Pet.find_or_404(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) self.assertEqual(pet.gender, pets[1].gender) self.assertEqual(pet.birthday, pets[1].birthday)
def test_delete_a_pet(self): """Delete a Pet""" pet = PetFactory() 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_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" )
def test_find_pet(self): """It should Find a Pet by ID""" pets = PetFactory.create_batch(5) for pet in pets: pet.create() logging.debug(pets) # make sure they got saved self.assertEqual(len(Pet.all()), 5) # 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) self.assertEqual(pet.gender, pets[1].gender) self.assertEqual(pet.birthday, pets[1].birthday)
def test_update_a_pet(self): """It should Update a Pet""" pet = PetFactory() logging.debug(pet) pet.id = None pet.create() logging.debug(pet) self.assertIsNotNone(pet.id) # Change it an save it pet.category = "k9" original_id = pet.id pet.update() self.assertEqual(pet.id, original_id) self.assertEqual(pet.category, "k9") # 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].id, original_id) self.assertEqual(pets[0].category, "k9")