class TestZoo(unittest.TestCase): def setUp(self): self.zoo1 = Zoo(30, 2000) def test_accomodate(self): self.zoo1.accommodate("Lion", 9, "Ivan", "male", 90) self.assertEqual(self.zoo1.animalsCollection[0].species, "Lion") self.assertEqual(self.zoo1.animalsCollection[0].name, "Ivan")
def main(): myzoo = Zoo() create_animal_table(cursor) while True: command = input('>>>') if command == 'list_animals' or command == 'l': see_animals(myzoo, cursor) elif command == 'simulate' or command == 's': interval = int(input('Enter interval of time in days:')) period = int(input('Enter period:')) for x in range (period): simulate(myzoo, interval, period, cursor) elif command == 'accommodate' or command == 'a': if myzoo.count_animals(cursor) >= myzoo.get_capacity(): print("Your zoo is full") print(myzoo.count_animals(cursor)) species = input('Enter species:') name = input('Enter name:') age = input('Enter age:') weight = input('Enter weight:') myzoo.accommodate(species, name, age, weight, cursor) conn.commit() elif command == 'move_to_habitat' or command == 'move': name = input('Name:') myzoo.move_to_habitat(name, cursor) print(name + ' has gone home!') conn.commit() elif command == 'exit': break conn.commit() conn.close()
class TestZoo(unittest.TestCase): def setUp(self): self.zoo = Zoo(10, 1000) self.tiger = Animal('Tiger', 2, 'Gosho', 'Male', 20) self.zoo.accommodate(self.tiger) def tearDown(self): Zoo.animals = [] Animal.NAMES = {} def test_init(self): self.assertEqual(self.zoo.capacity, 10) self.assertEqual(self.zoo.budget, 1000) self.assertEqual(self.zoo.animals, [self.tiger]) def test_accommodate(self): self.assertIn(self.tiger, self.zoo.animals) def test_move_to_habitat(self): before = self.zoo.animals after = copy.deepcopy(self.zoo.animals) self.zoo.accommodate(self.tiger) self.zoo.move_to_habitat("Tiger", "Gosho") after = [x for x in self.zoo.animals if x.name != "Gosho" and x.species != "Tiger"] self.assertEqual(len(before), len(after) + 1) def test_interval_in_days(self): result = self.zoo.interval_in_days("days") self.assertEqual(result, 1) def test_interval_in_months(self): result = self.zoo.interval_in_days("months") self.assertEqual(result, 30) def test_interval_in_weeks(self): result = self.zoo.interval_in_days("weeks") self.assertEqual(result, 7) def test_interval_in_years(self): result = self.zoo.interval_in_days("years") self.assertEqual(result, 365) def test_calc_daily_incomes(self): rabbit = Animal('Rabbit', 2, 'Pesho', 'Female', 25) self.zoo.accommodate(rabbit) self.zoo.calculate_daily_income() self.assertEqual(self.zoo.budget, 1120)
class ZooGarden: def __init__(self): self.zoo = Zoo(50, 10000) def see_animals(self): if len(self.zoo.animals) != 0: for animal in self.zoo.animals: print(animal) else: print("\nThere are no animals yet. Accomodate some?") def accomodate(self, species, name, age, weight): gender = input("What gender is the animal?: ") self.zoo.accommodate(species, age, name, gender, weight) def move_to_habitat(self, species, name): has_such_animal = False for animal in self.zoo.animals: if animal.species == species and animal.name == name: print("{} is now a free {}!".format(animal.name, animal.species)) self.zoo.animals.remove(animal) Animal.SPECIES_NAMES[animal.species].remove(animal.name) has_such_animal = True if not has_such_animal: print("\nThere is no such animal in the zoo anyway.") def simulate(self, interval_of_time, period): if len(self.zoo.animals) != 0: time_in_months = self.__time_in_months(interval_of_time, period) for female in self.zoo.animals: for male in self.zoo.animals: self.zoo.reproduce(female, male) self.__grow_all_animals(time_in_months) print('\nTheese animals have grown:') self.see_animals() self.__print_dead_animals(self.zoo.dead_animals()) self.__could_budget_afford_food(time_in_months) self.__born_animals() else: print("\nThere are no animals yet. Accomodate some?") def __time_in_months(self, interval_of_time, period): if interval_of_time == 'days': return period * 1 / 30 # rough approximation elif interval_of_time == 'weeks': return period * 1 / 4 # rough approximation elif interval_of_time == 'months': return period elif interval_of_time == 'years': return period * 12 else: print("\nWrong interval of time, it should be " "days, weeks, months or years.") def __grow_all_animals(self, time_in_months): for animal in self.zoo.animals: animal.grow(time_in_months) def __print_dead_animals(self, list_dead_animals): if len(list_dead_animals) != 0: print("\nTheese animals had died:") for animal in list_dead_animals: print(animal) else: print("\nNo animals have died.") def __could_budget_afford_food(self, time_in_months): time_in_days = time_in_months * 30 # rough approximation self.zoo.budget += self.zoo.get_income() * time_in_days self.zoo.get_outcome() if self.zoo.budget >= 0: print("\nZoo's budget can afford feeding all animals:)") else: print("T\nhere aren't enough money to feed all animals." "Move some to habitat?") def __born_animals(self): babies = self.zoo.newborn_baby() if len(babies) != 0: print("\nZoo's babies are:") for baby in babies: print(baby) else: print("\nThere're no babies.") def show_database_species(self): file = open("database.json", "r") content = json.loads(file.read()) file.close() print("\nDatabase species are:") for species_key in content: print(species_key) def interface(self): print("\nWelcome to our Zoo! You can use one of the following" " commands:" "\n\nshow_database_species -> to know wich species" " are in our database" "\n\nsee_animals -> to see all current animals" "\n\naccomodate <species> <name> <age_in_months> <weight_in_" "kilos> -> to add animal in the zoo" "\n\nmove_to_habitat <species> <name> -> removes an animal from " "the Zoo and returns it to its natural habitat" "\n\nsimulate <interval_of_time> <period> -> shows what happens" " in the Zoo for that time" "\n - <interval_of_time> is 'days', 'weeks', 'months' or 'years'" "\n - <period> is a number" "\n\nexit -> to exit the program") while True: reply = input("\nYour choice: ") if reply == 'exit': break else: reply = reply.split() # make list of arguments if reply[0] == "see_animals": self.see_animals() elif reply[0] == "accomodate" and len(reply) == 5: self.accomodate(reply[1], reply[2], float(reply[3]), float(reply[4])) elif reply[0] == "move_to_habitat" and len(reply) == 3: self.move_to_habitat(reply[1], reply[2]) elif reply[0] == "simulate" and len(reply) == 3: self.simulate(reply[1], float(reply[2])) elif reply[0] == "show_database_species": self.show_database_species() else: print("Wrong command! Try again.")
def test_accommodate_no_same_species(self): new_zoo = Zoo(1, 5) new_zoo.accommodate("pantera", 6, "misho", "male", 20) self.assertEqual(len(new_zoo.animals), 0)
def test_accommodate(self): new_zoo = Zoo(1, 5) new_zoo.accommodate("tiger", 6, "misho", "male", 20) self.assertEqual(len(new_zoo.animals), 1)
class ZooTest(unittest.TestCase): def setUp(self): self.first_animals = [Animal("tiger", 144, "DiviaLud", "male", 300), Animal("tiger", 96, "Pam", "female", 280), Animal("panda", 72, "Pom-Pom", "male", 340)] self.zoo = Zoo(self.first_animals, 100, 1200) self.animal1 = Animal("panda", 48, "Jully", "female", 350) self.animal2 = Animal("panda", 48, "Lilly", "female", 350) self.animal3 = Animal("tiger", 144, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 96, "Pam", "female", 280) def test_init(self): self.assertEqual(self.zoo.animals, self.first_animals) self.assertEqual(self.zoo.capacity, 100) self.assertEqual(self.zoo.budget, 1200) def test_names_for_species(self): self.assertEqual(self.zoo.names_for_species(self.animal3), ["DiviaLud", "Pam"]) def test_names_for_species_doesnot_have_such_species(self): self.assertEqual(self.zoo.names_for_species(Animal( "horse", 96, "Pam", "female", 280)), []) def test_accomodate(self): self.assertTrue(self.zoo.accommodate(self.animal1)) self.assertIn(self.animal1, self.zoo.animals) def test_accomodate_no_more_space(self): zoo1 = Zoo(self.first_animals, 3, 1200) self.assertEqual(zoo1.accommodate(self.animal1), "No more space!") def test_accomodate_exists_name(self): animal = Animal("panda", 48, "Pom-Pom", "female", 350) message = "There is such species with the same name. Rename your animal" self.assertEqual(self.zoo.accommodate(animal), message) def test_get_income(self): self.zoo.get_income() self.assertEqual(self.zoo.budget, 1380) def test_outcome(self): self.first_animals[0].eat("meat", 8) # tiger self.first_animals[1].eat("meat", 10) # tiger self.first_animals[2].eat("bamboo", 4) # panda # 8 * 4 + 10 * 4 + 4 * 2 = 80 self.zoo.outcome() self.assertEqual(self.zoo.budget, 1120) def test_clear_dead_animals_all_died(self): for i in range(100): self.zoo.clear_dead_animals(180) self.assertEqual(self.zoo.animals, []) def test_gender_baby(self): genders = [] for i in range(1000): genders.append(self.zoo.gender_baby()) self.assertIn("female", genders) self.assertIn("male", genders) def test_reproduce_same_gender(self): self.assertEqual(self.zoo.reproduce(self.animal1, self.animal2), "Cannot reproduce") def test_reproduce_different_species(self): self.assertEqual(self.zoo.reproduce(self.animal1, self.animal3), "Cannot reproduce") def test_reproduce_before_six_months(self): self.animal4.last_pregnancy = 48 self.assertEqual(self.zoo.reproduce(self.animal1, self.animal4), "Cannot reproduce") #################################################### def test_reproduce_female_is_second_argument(self): self.animal4.last_pregnancy = 0 self.assertNotEqual(self.zoo.reproduce(self.animal3, self.animal4), "Cannot reproduce") def test_reproduce_female_is_first_argument(self): self.animal4.last_pregnancy = 0 self.assertNotEqual(self.zoo.reproduce(self.animal4, self.animal3), "Cannot reproduce") #################################################### def test_reproduce_first_animal_too_small(self): self.animal4.last_pregnancy = 0 self.animal3 = Animal("tiger", 12, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 96, "Pam", "female", 280) self.assertEqual(self.zoo.reproduce(self.animal3, self.animal4), "Cannot reproduce. Animals are too young") def test_reproduce_second_animal_too_small(self): self.animal4.last_pregnancy = 0 self.animal3 = Animal("tiger", 12, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 96, "Pam", "female", 280) self.assertEqual(self.zoo.reproduce(self.animal4, self.animal3), "Cannot reproduce. Animals are too young") def test_reproduce_both_animals_too_small(self): self.animal4.last_pregnancy = 0 self.animal3 = Animal("tiger", 12, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 12, "Pam", "female", 280) self.assertEqual(self.zoo.reproduce(self.animal4, self.animal3), "Cannot reproduce. Animals are too young")
def test_accomodate_no_more_space(self): zoo1 = Zoo(self.first_animals, 3, 1200) self.assertEqual(zoo1.accommodate(self.animal1), "No more space!")
class TestZoo(unittest.TestCase): def setUp(self): self.zoo = Zoo(5, 50, Database("test_zoo.db")) self.a = ["lion", 24, "sharik", "male", 150] self.a2 = ["lion", 24, "niya", "female", 150] def test_zoo_init(self): self.assertEqual(5, self.zoo.capacity) self.assertEqual(50, self.zoo.budget) def test_accommodate(self): self.zoo.capacity = 0 self.assertFalse(self.zoo.accommodate(*self.a)) self.zoo.capacity = 5 animal = Animal("lion", 24, "sharik", "male", 150) self.assertEqual(True, self.zoo.accommodate(*self.a)) self.assertEqual(animal, self.zoo.animals[0]) animals_from_db = self.zoo.database.zoo_conn.execute\ ('''select * from zoo''').fetchall() self.assertEqual(1, len(animals_from_db)) def test_move_to_habitat(self): self.zoo.accommodate(*self.a) self.zoo.move_to_habitat("lion", "sharik") self.assertEqual(0, len(self.zoo.animals)) animals_from_db = self.zoo.database.zoo_conn.execute\ ('''select * from zoo''').fetchall() self.assertEqual(0, len(animals_from_db)) def test_see_animals(self): self.zoo.accommodate(*self.a) expected = "sharik the lion: 24 months, 150 kgs" self.assertEqual(expected, self.zoo.see_animals()) def test_daily_incomes(self): self.assertEqual(0, self.zoo.daily_incomes()) def test_daily_expenses(self): self.zoo.accommodate(*self.a) expected = 150 * 0.035 self.assertEqual(expected * 4, self.zoo.daily_expenses()) def test_spend_budget(self): result = self.zoo.spend_budget(50) self.assertEqual(0, self.zoo.budget) self.assertTrue(result) result = self.zoo.spend_budget(1) self.assertEqual(0, self.zoo.budget) self.assertFalse(result) def test_earn_budget(self): self.zoo.earn_budget(20) self.assertEqual(70, self.zoo.budget) def test_generate_name(self): self.zoo.accommodate(*self.a) male_name = self.zoo._generate_name("lion", "niya", "male") female_name = self.zoo._generate_name("lion", "niya", "female") self.assertEqual("sharik niya", male_name) self.assertEqual("niya sharik", female_name) def test_born_animal(self): self.zoo.accommodate(*self.a) self.zoo.accommodate(*self.a2) result = self.zoo.born_animal("lion", "niya") self.assertTrue(result) self.assertEqual(3, len(self.zoo.animals)) def test_will_it_mate(self): self.zoo.accommodate(*self.a) self.zoo.accommodate(*self.a2) self.assertFalse(self.zoo.will_it_mate("lion", "niya")) self.zoo.database.set_last_breed("lion", "niya", 10) self.assertTrue(self.zoo.will_it_mate("lion", "niya")) def tearDown(self): call("rm -r {0}".format("test_zoo.db"), shell=True)