Example #1
0
class TestAnimal(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")

    def test_animal_creation(self):

        bob = self.bob

        self.assertIsInstance(bob, Animal)
        self.assertIsInstance(bob, Animal)

    def test_animal_can_set_legs(self):
        self.bob.set_legs(6)
        self.assertEqual(self.bob.legs, 6)

    def test_animal_can_set_species(self):
        self.bob.set_species('Dog')
        self.assertEqual(self.bob.species, 'Dog')

    def test_animal_can_get_species(self):
        self.bob.get_species()
        self.assertEqual(self.bob.species, 'Dog')

    def test_animal_can_walk(self):
        self.bob.set_legs(4)
        self.bob.walk()
        self.assertEqual(self.bob.speed, .8)

    def test_animal_get_name(self):
        self.bob.name = 'Winston'
        self.assertEqual(self.bob.name, 'Winston')
Example #2
0
class TestAnimal(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")

    def test_animal_creation(self):
        # One off instance of a created animal
        murph = Dog("Murph")

        self.assertIsInstance(murph, Dog)
        self.assertIsInstance(murph, Animal)

    def test_animal_can_set_legs(self):
        self.bob.set_legs(6)
        self.assertEqual(self.bob.legs, 6)

    def test_animal_can_set_species(self):
        self.bob.set_species(Dog("Bob"))
        self.assertIsInstance(self.bob.species, Dog)

    def test_animal_can_get_name(self):
        name = self.bob.get_name
        self.assertIsNotNone(name)

    def test_animal_speed(self):
        speed = self.bob.speed
        self.assertIsNotNone(speed)

    def test_animal_walk(self):
        self.bob.set_legs(1)
        self.bob.walk()
        self.assertEqual(self.bob.speed, 0.2)
Example #3
0
class TestAnimal(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")

    def test_animal_creation(self):

        bob = Dog("Bob")
        #could also put = self.bob

        self.assertIsInstance(bob, Dog)
        self.assertIsInstance(bob, Animal)

    def test_animal_can_set_legs(self):
        self.bob.set_legs(6)
        self.assertEqual(self.bob.legs, 6)

    def test_animal_walk(self):
        self.bob.set_legs(4)
        self.bob.walk()
        self.assertEqual(self.bob.speed, .8)
    
    def test_animal_species(self):
        self.bob.set_species("basically human")
        self.assertIs(self.bob.species, "basically human")
Example #4
0
 def test_dog_can_walk(self):
     """ tests that a newly created dog object can call the walk method """
     murph = Dog("Murph")
     murph.walk()
     self.assertEqual(murph.speed, 0)
     murph.set_legs(4)
     murph.walk()
     self.assertEqual(murph.speed, 0.8)
Example #5
0
    def test_animal_creation(self):

        # bob = Dog("Bob")
        murph = Dog("Murphy")

        self.assertIsInstance(murph, Dog)
        self.assertIsInstance(murph, Animal)
 def test_animal_gender(self):
     """ Test the gender of the animal instances
     """
     self.assertEqual(self.rocco.get_gender(), 'M')
     self.assertEqual(
         Dog('Bobby', 'chow chow', 6, 'Unknown_gender').get_gender(),
         'Other')
Example #7
0
def create_animal(name):
    if name == 'dog':
        return Dog()
    elif name == 'cat':
        return Cat()
    else:
        raise Exception("error input")
Example #8
0
    def test_animal_creation(self):

        joe = Dog("Joe")

        self.assertIsInstance(joe, Dog)
        self.assertIsInstance(joe, Animal)
        self.assertIsInstance(self.jonnie, Animal)
Example #9
0
    def test_animal_creation(self):

        bob = Dog("Bob")
        #could also put = self.bob

        self.assertIsInstance(bob, Dog)
        self.assertIsInstance(bob, Animal)
 def test_animal_breed(self):
     """ Test the breed of the animal instances
     """
     self.assertEqual(self.lucy.get_breed(), 'collie')
     # Akita breed is not in the list of available breeds:
     with self.assertRaises(AssertionError):
         Dog('Mollie', 'Akita', 6, 'Male')
Example #11
0
class TestAnimal(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")

    def test_animal_creation(self):

        # bob = Dog("Bob")
        murph = Dog("Murphy")

        self.assertIsInstance(murph, Dog)
        self.assertIsInstance(murph, Animal)

    def test_animal_can_set_legs(self):
        self.bob.set_legs(6)
        self.assertEqual(self.bob.legs, 6)
        self.assertNotEqual(self.bob.legs, 12)

    def test_animal_can_set_species(self):
        # setting Bob's species as Canid
        self.bob.set_species("Caninae")
        # testing if user types in caninae in lowercase that it will still pass as true
        """this test passes if both values are true"""
        self.assertEqual(self.bob.species.lower(), "caninae")
        """this test passes if both values are not equal to each other"""
        self.assertNotEqual(self.bob.species.lower(), "caninaey")

    def test_animal_can_get_species(self):
        # Bob's species was set in line 28, so we just get his species by invoking get_species()
        """this tests SUCCESS - passes if both values are true"""
        self.assertEqual(self.bob.get_species().lower(), "caninae")
        """this tests FAILURE - passes if both values are not equal to each other"""
        self.assertNotEqual(self.bob.get_species().lower(), "poop")

    def test_str(self):
        self.assertEqual(self.bob.__str__().lower(), "bob is a caninae")
        self.assertNotEqual(self.bob.__str__().lower(), "Poop is a Butt")

    # def test_animal_walk(self):

    def test_setting_speed(self):
        #create a new instance of dog
        self.poopy = Dog("Poopy")
        self.assertEqual(self.poopy.speed, 0)
        self.poopy.walk()
        self.assertEqual(self.poopy.speed, 0.2)
Example #12
0
class TestAnimal(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")

    def test_animal_creation(self):
        """ tests that a new dog creation is an instance of dog and animal """
        buddy = Dog("Buddy")
        self.assertIsInstance(buddy, Dog)
        self.assertIsInstance(buddy, Animal)

    def test_getting_animal_name(self):
        """ tests that the get_name method returns the correct value """
        self.assertEqual(self.bob.get_name(), "Bob")
        self.assertNotEqual(self.bob.get_name(), "bob")
        self.assertNotEqual(self.bob.get_name(), "bill")

    def test_dog_can_walk(self):
        """ tests that a newly created dog object can call the walk method """
        murph = Dog("Murph")
        murph.walk()
        self.assertEqual(murph.speed, 0)
        murph.set_legs(4)
        murph.walk()
        self.assertEqual(murph.speed, 0.8)

    def test_animal_can_walk(self):
        """ tests that a newly created animal object can walk and errors will be raised if needed """
        test_animal = Animal()
        with self.assertRaises(ValueError):
            test_animal.walk()
        test_animal.set_legs(4)
        test_animal.walk()
        self.assertEqual(test_animal.speed, .4)

    def test_animal_can_set_species(self):
        """ tests that an animal object can set its species """
        self.bob.set_species("dog")
        self.assertEqual(self.bob.species, "dog")

    def test_animal_can_get_species(self):
        """ tests that an animal object can get its species """
        self.bob.set_species("doggo")
        self.assertEqual(self.bob.get_species(), "doggo")

    def test_animal_can_set_legs(self):
        """ tests that an animal object can set its legs """
        self.bob.set_legs(6)
        self.assertEqual(self.bob.legs, 6)
 def test_dog_walking(self):
     dog = Dog("Goodboy")
     dog.set_legs(4)
     dog.walk()
     expected = 0.8
     result = dog.speed
     self.assertEqual(result, expected)
Example #14
0
class TestAnimal(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        print('set up class')
        # Be sure to pass in a name argument
        self.animal = Animal("Amy")
        self.dog = Dog("Pup")

    @classmethod
    def tearDownClass(self):
        print('tear down class')

    def test_animal_name(self):
        # Animal object has the correct name property.
        result = self.animal.get_name()
        expected = "Amy"
        self.assertEqual(result, expected)

    def test_animal(self):
        # The animal object is an instance of Animal.
        bird = Animal("bird")
        self.assertIsInstance(bird, Animal)

    def test_dog(self):
        # The dog object is an instance of Dog.
        maci = Dog("Maci")
        self.assertIsInstance(maci, Dog)

    def test_species(self):
        # Set a species and verify that the object property of species has the correct value.
        self.assertEqual(self.dog.get_species(), "Dog")
        self.dog.set_species("Puppers")
        self.assertEqual(self.dog.get_species(), "Puppers")

    def test_walk(self):
        # Invoking the walk() method sets the correct speed on the both objects.
        self.dog.set_legs(4)
        speed = self.dog.speed
        self.dog.walk()
        self.assertEqual(self.dog.speed, speed + (0.2 * self.dog.legs))

        self.animal.set_legs(8)
        speed = self.animal.speed
        self.animal.walk()
        self.assertEqual(self.animal.speed, (speed + (0.1 * self.animal.legs)))
Example #15
0
class TestAnimal(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")
        self.jonnie = Animal("Jonnie")

    def test_animal_creation(self):

        joe = Dog("Joe")

        self.assertIsInstance(joe, Dog)
        self.assertIsInstance(joe, Animal)
        self.assertIsInstance(self.jonnie, Animal)

    def test_animal_can_set_legs(self):

        murph = Dog("murph")
        murph.set_legs(6)
        self.assertEqual(murph.legs, 6)

    def test_animal_can_set_species(self):

        self.bob.set_species("Labrador")
        self.assertIs(self.bob.species, "Labrador")

    def test_animal_can_get_species(self):

        self.assertIs(self.bob.get_species(), "Dog")

    def test_animal_can_walk(self):

        self.bob.set_legs(6)
        self.bob.walk()
        self.assertEqual(self.bob.speed, 1.2)

    def test_animal_name(self):

        self.assertEqual(self.bob.name, "Bob")
Example #16
0
class TestAnimal(unittest.TestCase):

    # Setting up the global references for Dog and Animal
    @classmethod
    def setUpClass(self):
        self.bob = Dog("Bob")
        self.animal = Animal("Lucy", "canine")

    # Testing that there are instances of Dog and Animal
    def test_animal_creation(self):

        murph = Dog("Murph")
        self.assertIsInstance(murph, Dog)
        self.assertIsInstance(murph, Animal)

    # Testing that when we pass in a number of legs, that is the number of legs returned
    def test_animal_can_set_legs(self):

        self.bob.set_legs(6)
        self.assertEqual(self.bob.legs, 6)

    # Testing the walk calculation by passing in number of legs , then seeing if that number of legs is returned and then seeing if the speed is what we expect
    def test_animal_can_calc_walk(self):

        self.bob.set_legs(4)
        self.bob.walk()
        self.assertEqual(self.bob.legs, 4)
        self.assertEqual(self.bob.speed, .8)

    # Testing to see if the species we pass in is returned back to us
    def test_animal_can_set_species(self):

        self.animal.set_species("human")
        self.assertEqual(self.animal.species, "human")
        # The value in species was changed to human so this should return as Failed (below)
        # self.assertEqual(self.animal.species, "canine")

    # Testing to see if we are getting the species we expect
    def test_animal_can_get_species(self):

        self.animal.get_species()
        self.assertEqual(self.animal.species, "canine")
Example #17
0
from animal import Dog, Cat

kiki = Dog (name='kiki',age=3)
mimi=  Cat(name='mimi',age=5)

print(kiki.speak())
print(mimi.speak())
Example #18
0
from animal import Cat, Dog

fufu = Dog(name='fufu', age=3)
bailey = Cat(name='bailey', age=7)

print(fufu.speak())
print(bailey.speak())

# Dog and Cat have inherited 'speak' from Class Animal
 def setUpClass(cls):
     cls.bob = Dog("Bob")
 def test_dog_walking_legless(self):
     dog = Dog("Doggo")
     with self.assertRaises(ValueError):
         dog.walk()
Example #21
0
from animal import Dog, Cat

Luna = Dog(name='Luna', age=3)
Loly = Cat(name='Loly', age=5)

print(Luna.speak())
print(Loly.speak())
Example #22
0
def main():
    zoo = []

    #Cats
    Carmen = Cat("Carmen")
    zoo.append(Carmen)
    Chloe = Cat("Chloe")
    zoo.append(Chloe)
    Caidy = Cat("Caidy")
    zoo.append(Caidy)
    #Lions
    Leeroy = Lion("Leeroy")
    Leeroy.setAnimalEat(AnimalEatMeat())
    zoo.append(Leeroy)
    Leon = Lion("Leon")
    Leon.setAnimalEat(AnimalEatMeat())
    zoo.append(Leon)
    #Tigers
    Tony = Tiger("Tony")
    Tony.setAnimalEat(AnimalEatMeat())
    zoo.append(Tony)
    Tom = Tiger("Tom")
    Tom.setAnimalEat(AnimalEatMeat())
    zoo.append(Tom)
    Timmy = Tiger("Timmy")
    Timmy.setAnimalEat(AnimalEatMeat())
    zoo.append(Timmy)
    #Elephants
    Edith = Elephant("Edith")
    Edith.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Edith)
    Erin = Elephant("Erin")
    Erin.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Erin)
    #Rhinos
    Ryan = Rhino("Ryan")
    Ryan.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Ryan)
    Robert = Rhino("Robert")
    Robert.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Robert)
    Ronan = Rhino("Ronan")
    Ronan.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Ronan)
    #Hippos
    Harper = Hippo("Harper")
    Harper.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Harper)
    Hector = Hippo("Hector")
    Hector.setAnimalEat(AnimalEatVegetarian())
    zoo.append(Hector)
    #Dogs
    Douglas = Dog("Douglas")
    Douglas.setAnimalEat(AnimalEatMeat())
    zoo.append(Douglas)
    Damian = Dog("Damian")
    Damian.setAnimalEat(AnimalEatMeat())
    zoo.append(Damian)
    Dominic = Dog("Dominic")
    Dominic.setAnimalEat(AnimalEatMeat())
    zoo.append(Dominic)
    Daisy = Dog("Daisy")
    Daisy.setAnimalEat(AnimalEatMeat())
    zoo.append(Daisy)
    #Wolves
    Wilson = Wolf("Wilson")
    Wilson.setAnimalEat(AnimalEatMeat())
    zoo.append(Wilson)
    Wendy = Wolf("Wendy")
    Wendy.setAnimalEat(AnimalEatMeat())
    zoo.append(Wendy)

    #PROOF OF DYNAMIC BEHAIVOR AT RUN TIME
    #(All other eat behaivor set at object instatiation above)
    Carmen.setAnimalEat(AnimalEatPreMixedFood())
    Caidy.setAnimalEat(AnimalEatPreMixedFood())
    Chloe.setAnimalEat(AnimalEatPreMixedFood())

    Kanye = Zookeeper(zoo)  #Subject
    Kim = ZooAnnouncer(Kanye)  #Observer
    Kanye.registerObserver(Kim)
    Kanye.beAZookeeper()
    del Kim
    del Kanye
    for animal in zoo:
        del animal
Example #23
0
from animal import Animal
from animal import Dog, Duck, Snake

animal = Animal('Jasmine')
dog = Dog('Casey')
duck = Duck('Donald')
snake = Snake('Python')


# We can pass this list of animals animal_barn
def count_all_legs(animal_barn):
    total_legs = 0
    for animal in animal_barn:
        # All of these are instance of animal, and we
        assert isinstance(animal, Animal)
        total_legs += animal.count_legs()

    return total_legs


print(count_all_legs([animal, dog, duck, snake]))

# Okay, this was an awkward example. But the idea is that since they're all
# inheriting from Animal, they should all have the count_legs and no subclass
# of Animal should not have it.
class TestAdoptionService(unittest.TestCase):
    def setUp(self):
        """ Create animals for testing purposes.
            TODO - take the animals from a database instead
        """
        self.sharo = Dog('Sharo', 'unbred', 5, "male")
        self.lucy = Dog('Lucy', 'collie', 3, "Female")
        self.daisy = Dog('Daisy', 'labrador', 2, "fem")
        self.rocco = Dog('Rocco', 'German Shepherd', 1, "M")
        self.duke = Dog('Duke', 'Doberman', 4, "Male")
        self.mike = Dog('Mike', 'Doberman', 4, "Male")
        self.max_ = Dog('Max', 'greyhound', 7, "M")

        self.maya = Cat('Maya', 'Sphynx', 5, "F")
        self.ruh = Cat('Ruh', 'unbred', 3, "male")
        self.tiger = Cat('Tiger', 'SAVANNAH', 4, "M")

        # Create a non-animal object
        self.non_animal_object = (5, "F")

        # Create a Pool with dogs for adoption
        self.dog_pool = [
            self.sharo, self.lucy, self.daisy, self.rocco, self.duke,
            self.mike, self.max_
        ]
        # Create a Pool with cats for adoption
        self.cat_pool = [self.maya, self.ruh, self.tiger]

    def test_class_instances(self):
        """Test the class of the set-up animal instances
        """
        self.assertIsInstance(self.sharo, Dog)
        self.assertNotIsInstance(self.lucy, Cat)
        self.assertIsInstance(self.maya, Cat)
        self.assertNotIsInstance(self.ruh, Dog)
        self.assertIsInstance(self.tiger, Animal)
        self.assertIsNot(self.non_animal_object, Animal)

    def test_animal_age(self):
        """ Test the age of the animal instances
        """
        self.assertEqual(self.sharo.get_age(), 5)
        self.assertNotEqual(self.daisy.get_age(), 512)

    def test_animal_breed(self):
        """ Test the breed of the animal instances
        """
        self.assertEqual(self.lucy.get_breed(), 'collie')
        # Akita breed is not in the list of available breeds:
        with self.assertRaises(AssertionError):
            Dog('Mollie', 'Akita', 6, 'Male')

    def test_animal_gender(self):
        """ Test the gender of the animal instances
        """
        self.assertEqual(self.rocco.get_gender(), 'M')
        self.assertEqual(
            Dog('Bobby', 'chow chow', 6, 'Unknown_gender').get_gender(),
            'Other')

    def test_eat_method(self):
        """ Test the eat method of the animal instances
        """
        self.assertEqual("I like to eat bones.", self.sharo.eat("bones"))
        self.assertEqual("I like to eat fish.", self.ruh.eat("fish"))
        self.assertEqual("I like to eat chicken soup.",
                         self.tiger.eat("chicken soup"))

    def test_adoption_methods(self):
        """ Test the adoption methods of the Adoption Center Factory
        """
        dog_adoption_center = AdoptionCenterFactory(self.dog_pool)
        cat_adoption_center = AdoptionCenterFactory(self.cat_pool)

        # Adopt a dog that exists in the Pool
        message, result = dog_adoption_center.adopt_animal("doberman", 4, "M")
        self.assertIsInstance(result, Dog)
        # Assert that the adopted dog has been removed from the Dog pool:
        self.assertNotIn(result, self.dog_pool)
        # Assert that the user received a proper message and only Duke is returned:
        self.assertEqual(
            "You adopted a Doberman named Duke that is 4 years old and a boy.",
            message)

        # Try to adopt a dog that doesn't exist in the Pool
        message, result_list = dog_adoption_center.adopt_animal(
            "labrador", 3, "F")
        # Assert that the proper message has been returned
        self.assertIn(
            "Sorry, we don't have this pet in our shop!"
            " Would you consider adopting one of these cuties instead: ",
            message)
        self.assertIn("Daisy",
                      str(result_list[0]))  # Daisy has same breed + gender
        self.assertIn("Lucy",
                      str(result_list[1]))  # Lucy has same gender + age

        # Adopt a random cat from the Pool
        message, result = cat_adoption_center.get_lucky()
        self.assertIsInstance(result, Cat)
        # Assert that the lucky cat has been removed from the Cat pool
        self.assertNotIn(result, self.cat_pool)
        # Assert that the user received a proper message
        self.assertEqual("You adopted a {}.".format(str(result)), message)
Example #25
0
    def test_animal_can_set_legs(self):

        murph = Dog("murph")
        murph.set_legs(6)
        self.assertEqual(murph.legs, 6)
    def setUp(self):
        """ Create animals for testing purposes.
            TODO - take the animals from a database instead
        """
        self.sharo = Dog('Sharo', 'unbred', 5, "male")
        self.lucy = Dog('Lucy', 'collie', 3, "Female")
        self.daisy = Dog('Daisy', 'labrador', 2, "fem")
        self.rocco = Dog('Rocco', 'German Shepherd', 1, "M")
        self.duke = Dog('Duke', 'Doberman', 4, "Male")
        self.mike = Dog('Mike', 'Doberman', 4, "Male")
        self.max_ = Dog('Max', 'greyhound', 7, "M")

        self.maya = Cat('Maya', 'Sphynx', 5, "F")
        self.ruh = Cat('Ruh', 'unbred', 3, "male")
        self.tiger = Cat('Tiger', 'SAVANNAH', 4, "M")

        # Create a non-animal object
        self.non_animal_object = (5, "F")

        # Create a Pool with dogs for adoption
        self.dog_pool = [
            self.sharo, self.lucy, self.daisy, self.rocco, self.duke,
            self.mike, self.max_
        ]
        # Create a Pool with cats for adoption
        self.cat_pool = [self.maya, self.ruh, self.tiger]
Example #27
0
 def setUpClass(self):
     self.bob = Dog("Bob")
     self.jonnie = Animal("Jonnie")
from animal import Animal, Dog, Dragon

red_panda = Animal("amber", 100)

red_panda.run().display_health()

hank = Dog("Hank")

hank.display_health()

hank.pet().display_health()

drogon = Dragon("Drogon")

drogon.fly().display_health()

print hank
Example #29
0
Descripcion:
Programa creado para mostrar ejemplos prácticos para contrastar
contra la programamación procedural
'''

__author__ = "Inove Coding School"
__email__ = "*****@*****.**"
__version__ = "1.1"

from animal import Dog, Cat, Cow
import person as p


if __name__ == '__main__':

    animal_1 = Dog('Sirius', 8)
    animal_2 = Cat('Minerva', 1)

    print('¿Cuanto duerme el perro por la noche?', animal_1.sleep())
    print('¿Cuanto duerme el gato por la noche?', animal_2.sleep())

    persona = p.Person('Max')
    print('{} adoptó 2 animales, un perro y un gato'.format(persona.name))

    persona.adopt(animal_1)
    persona.adopt(animal_2)

    print('¿Cómo saludan los animales de', persona.name, '?')

    for animal in persona.animales:
        animal.speak()
 def test_animal_creation(self):
     murph = Dog("Murph")
     self.assertIsInstance(murph, Dog)
     self.assertIsInstance(murph, Animal)