class Zoo(): """docstring for Zoo""" def __init__(self, name, capacity, budget): self.__animals = [] self.__name = name self.__capacity = capacity self.__budget = budget self.__database = Database(name + ".db") def get_name(self): return self.__name def get_capacity(self): return self.__capacity def get_animals(self): return self.__animals def get_database(self): return self.__database def accommodate_animal(self, species, age, name, gender, weight): if self.__capacity <= len(self.__animals): return False new_animal = Animal(species, age, name, gender, weight) self.__animals.append(new_animal) self.__database.insert_animal(new_animal) return True # <name> : <species>, <age>, <weight> def see_animals(self): formated_list = [] # animal_objects look like this: (id,species,age,name,gender,weight) for animal in self.__animals: species = animal.get_species() age = animal.get_age() name = animal.get_name() weight = animal.get_weight() animal_string = name + "\t - " + species + ", age: " + str(age) animal_string += " years, weight: " + str(weight) + " kg" formated_list.append(animal_string) output_string = '\n'.join(formated_list) return output_string def remove_animal(self, species, name): for animal in self.__animals: if animal.get_species() == species and animal.get_name() == name: self.__animals.remove(animal) break def move_animal(self, species, name): self.remove_animal(species, name) def daily_incomes(self): return 60 * len(self.__animals) def daily_expenses(self): money = 0 for animal in self.__animals: animal_type = self.__database.get_food_type(animal.get_species()) food_weight_ratio = self.__database.get_food_weight_ratio(animal.get_species()) if animal_type == "carnivore": money += 4 * animal.feed(food_weight_ratio) else: money += 2 * animal.feed(food_weight_ratio) return money def flip_a_coin(self): gender = randint(0, 1) if gender == 0: gender = 'male' else: gender = 'female' return gender def generate_name(self, species, name, gender): name = self.__database.get_male_name(species) if gender == 'female': return name + 'ka' else: return name + str(randint(1, 100)) def born_animal(self, species, name): self.__database.set_last_breed(species, name, 0) weight = self.__database.get_newborn_weight(species) gender = self.flip_a_coin() name = self.generate_name(species, name, gender) new_animal = Animal(species, 1, name, gender, weight) self.__animals.append(new_animal) self.__database.insert_animal(new_animal) if len(self.__animals) > self.__capacity: return False return True def ready_to_give_birth(self, species, name): breeding_period =\ 6 + self.database.get_gestation(species) if self.database.has_male(species): if breeding_period <= self.database.get_last_breed(species, name): return True return False def update_animals_from_database(self): self.__animals = [] db = self.get_database() db_name = db.get_name() conn = sqlite3.connect(db_name) cursor = conn.cursor() animals_from_db = cursor.execute('''SELECT * FROM zoo''').fetchall() for animal in animals_from_db: # (1, 'lion', 10.0, 'Svetla', 'female', 160.0) species = animal[1] age = animal[2] name = animal[3] gender = animal[4] weight = animal[5] this_animal = Animal(species, age, name, gender, weight) self.__animals.append(this_animal) return self.__animals
class DatabaseTest(unittest.TestCase): def setUp(self): self.db = Database("test_zoo.db") self.a = Animal("lion", 24, "niya", "female", 150) self.db.insert_animal(self.a) self.c = self.db.zoo_conn.cursor() def test_database_init(self): self.assertEqual("test_zoo.db", self.db.name) def test_insert_animal(self): animal_from_db = self.c.execute('''select * from zoo''').fetchall()[0] self.assertEqual((1, "lion", 24, "niya", "female", 150), animal_from_db) last_breed_from_db = self.c.execute('''select id, last_breed from breeding''').fetchall()[0] self.assertEqual((1, 0), last_breed_from_db) def test_remove_animal(self): self.db.remove_animal("lion", "niya") animal_from_db = self.c.execute("select * from zoo").fetchall() self.assertEqual(0, len(animal_from_db)) breed_from_db = self.c.execute("select * from breeding").fetchall() self.assertEqual(0, len(breed_from_db)) def test_get_males_with_no_males(self): self.assertEqual(0, len(self.db.get_males("lion"))) def test_get_males_with_one_male(self): a2 = Animal("lion", 24, "sharik", "male", 150) self.db.insert_animal(a2) self.assertEqual([("sharik", )], self.db.get_males("lion")) def test_has_male(self): self.assertFalse(self.db.has_male("lion")) def test_get_females(self): self.assertEqual([("lion", "niya")], self.db.get_females()) def test_get_life_expectancy(self): self.assertEqual(15, self.db.get_life_expectancy("lion")) def test_get_food_type(self): self.assertEqual("carnivore", self.db.get_food_type("lion")) def test_get_gestation(self): self.assertEqual(3, self.db.get_gestation("lion")) def test_get_newborn_weight(self): self.assertEqual(2.0, self.db.get_newborn_weight("lion")) def test_get_average_weight(self): self.assertEqual(200, self.db.get_average_weight("lion")) def test_get_weight_age_ratio(self): self.assertEqual(7.5, self.db.get_weight_age_ratio("lion")) def test_get_food_weight_ration(self): self.assertEqual(0.035, self.db.get_food_weight_ratio("lion")) def test_get_last_breed(self): self.assertEqual(0, self.db.get_last_breed("lion", "niya")) def test_set_last_breed(self): self.db.set_last_breed("lion", "niya", 3) result = self.db.get_last_breed("lion", "niya") self.assertEqual(result, 3) def tearDown(self): call("rm {}".format(self.db.name), shell=True)
class DatabaseTest(unittest.TestCase): def setUp(self): self.db = Database("test_zoo.db") self.a = Animal("lion", 24, "Svetla", "female", 150) self.db.insert_animal(self.a) self.c = self.db.zoo_conn.cursor() def test_get_name(self): self.assertEqual("test_zoo.db", self.db.get_name()) def test_insert_animal(self): animal_from_db = self.c.execute('''SELECT * FROM zoo''').fetchall()[0] self.assertEqual((1, "lion", 24, "Svetla", "female", 150), animal_from_db) last_breed_from_db = self.c.execute('''SELECT id, last_breed FROM breeding''').fetchall()[0] self.assertEqual((1, 0), last_breed_from_db) def test_insert_second_animal(self): self.a2 = Animal("tiger", 24, "Tsveta", "female", 120) self.db.insert_animal(self.a2) animal_from_db = self.c.execute('''SELECT * FROM zoo''').fetchall()[1] self.assertEqual((2, "tiger", 24, "Tsveta", "female", 120), animal_from_db) last_breed_from_db = self.c.execute('''SELECT id, last_breed FROM breeding''').fetchall()[1] self.assertEqual((2, 0), last_breed_from_db) def test_remove_animal(self): breed_from_db = self.c.execute("SELECT * from breeding").fetchall() self.assertEqual(1, len(breed_from_db)) self.db.remove_animal("lion", "Svetla") animal_from_db = self.c.execute("SELECT * FROM zoo").fetchall() self.assertEqual(0, len(animal_from_db)) breed_from_db = self.c.execute("SELECT * from breeding").fetchall() self.assertEqual(0, len(breed_from_db)) def test_initial_fill_with_animals(self): self.db.remove_animal("lion", "Svetla") self.db.initial_fill_with_animals() animals_from_db = self.c.execute("SELECT * FROM zoo").fetchall() self.assertEqual(10, len(animals_from_db)) breed_from_db = self.c.execute("SELECT * from breeding").fetchall() self.assertEqual(5, len(breed_from_db)) def test_get_males_with_no_males(self): self.assertEqual(0, len(self.db.get_males("lion"))) def test_get_males_with_one_male(self): a2 = Animal("lion", 24, "Pencho", "male", 150) self.db.insert_animal(a2) self.assertEqual([("Pencho", )], self.db.get_males("lion")) def test_has_a_male_species(self): self.assertFalse(self.db.has_a_male_species("lion")) def test_get_females(self): self.assertEqual([("lion", "Svetla")], self.db.get_females()) def test_get_life_expectancy(self): self.assertEqual(15, self.db.get_life_expectancy("lion")) def test_get_food_type(self): self.assertEqual("carnivore", self.db.get_food_type("lion")) def test_get_gestation(self): self.assertEqual(3, self.db.get_gestation("lion")) def test_get_newborn_weight(self): self.assertEqual(2.0, self.db.get_newborn_weight("lion")) def test_get_average_weight(self): self.assertEqual(200, self.db.get_average_weight("lion")) def test_get_weight_age_ratio(self): self.assertEqual(7.5, self.db.get_weight_age_ratio("lion")) def test_get_food_weight_ration(self): self.assertEqual(0.035, self.db.get_food_weight_ratio("lion")) def test_get_last_breed(self): self.assertEqual(0, self.db.get_last_breed("lion", "Svetla")) def test_set_last_breed(self): self.db.set_last_breed("lion", "Svetla", 3) result = self.db.get_last_breed("lion", "Svetla") self.assertEqual(result, 3) def test_set_age(self): self.db.set_age("lion", "Svetla", 30) query = "SELECT age FROM zoo WHERE species = ? AND name = ?" result = self.c.execute(query, ("lion", "Svetla")).fetchall() self.assertEqual(30, result[0][0]) def test_set_weight(self): self.db.set_weight("lion", "Svetla", 195) query = "SELECT weight FROM zoo WHERE species = ? AND name = ?" result = self.c.execute(query, ("lion", "Svetla")).fetchall() self.assertEqual(195, result[0][0]) def tearDown(self): call("rm {}".format(self.db.name), shell=True)