Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 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"]))
Ejemplo 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)
Ejemplo n.º 12
0
 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
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
 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)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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")
Ejemplo n.º 17
0
 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
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
0
 def test_update_a_pet(self):
     """Update a Pet"""
     pet = PetFactory()
     logging.debug(pet)
     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, pet.id)
     self.assertEqual(pets[0].category, "k9")
Ejemplo n.º 21
0
    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")
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
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"
     )
Ejemplo n.º 24
0
 def test_update_no_id(self):
     """It should not Update a Pet with no id"""
     pet = PetFactory()
     logging.debug(pet)
     pet.id = None
     self.assertRaises(DataValidationError, pet.update)