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 _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_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 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_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 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")
def test_get_related_objects(self): new_pet = PetFactory.create(owner=self.instance) self.assertEqual( list(self.model_map.get_related_objects(self.instance)), [self.pet, new_pet])
def setUpTestData(cls): super(ModelMapTests, cls).setUpTestData() cls.pet = PetFactory.create(owner=cls.instance)
def test_get_related_objects_extra_kwargs(self): new_pet = PetFactory.create(owner=self.instance) self.assertEqual( list( self.model_map.get_related_objects(self.instance, pk=new_pet.pk)), [new_pet])