class AnimalFileAdapterTest(unittest.TestCase):
    """docstring for AnimalFileAdapterTest"""
    def setUp(self):
        call("py create_animals_database.py",shell=True)
        self.db_conn = sqlite3.connect("animals.db")
        self.animal = Animal(self.db_conn, "tiger", 12, "Pol", "male")

        self.animals_adapter = AnimalFileAdapter(self.db_conn, self.animal)

    def test_prepare_to_save(self):
        expected = ('Pol', 'tiger', 12, 'male', 144.0)
        self.assertEqual(expected, self.animals_adapter.prepare_to_save())

    def test_save_new_animal(self):
        self.animals_adapter.save()
        self.assertEqual(1, self.animal.get_id())

    def test_save_update_animal(self):
        self.animals_adapter.save()
        self.animal.age = 13
        self.animals_adapter.save()

        sql = 'select age from zoo_animals where id=?'
        cursor = self.db_conn.cursor()
        actual = cursor.execute(sql, (self.animal.get_id(), )).fetchone()[0]

        self.assertEqual(13, actual)

    def test_load(self):
        self.animals_adapter.save()
        file_adapter = AnimalFileAdapter(self.db_conn, Animal(self.db_conn,
            'lion', 1, '', ''))
        self.assertTrue(file_adapter.load(1))
        expected = ('Pol', 'tiger', 12, 'male', 144.0)
        self.assertEqual(expected, file_adapter.prepare_to_save())

    def tearDown(self):
        self.db_conn.commit()
        self.db_conn.close()
        call('rm -f animals.db', shell=True)
示例#2
0
class TestAnimal(unittest.TestCase):
    '''docstring for TestAnimal'''
    def setUp(self):
        call('py create_animals_database.py',shell=True)
        self.db_conn = sqlite3.connect('animals.db')

    def test_get_name(self):
        self.animal = Animal(self.db_conn, 'tiger', 12, 'Pol', 'male')
        self.assertEqual('Pol',self.animal.get_name())

    def test_grow_young_animal(self):
        self.animal = Animal(self.db_conn, 'tiger', 12, 'Pol', 'male')
        self.animal.grow()
        self.assertEqual(13, self.animal.age)
        self.assertEqual(156, self.animal.weight)

    def test_grow_old_animal(self):
        self.animal = Animal(self.db_conn, 'lion', 180, 'John', 'male')
        self.animal.grow()
        self.assertEqual(200, self.animal.weight)
        self.assertEqual(181, self.animal.age)

    def test_die(self):
        self.animal = Animal(self.db_conn, 'raccoon', 36, 'Cohnen', 'male')
        self.assertEqual(True, self.animal.die())

    def test_get_gestation_period(self):
        self.animal = Animal(self.db_conn, 'lion', 12, 'Pol', 'female')
        self.assertEqual(3, self.animal.get_gestation_period())

    def test_get_id(self):
        self.animal = Animal(self.db_conn, 'lion', 12, 'Pol', 'female')
        self.assertEqual(-1, self.animal.get_id())

    def test_food_for_day(self):
        self.animal = Animal(self.db_conn, 'lion', 12, 'Pol', 'female')
        self.assertEqual(3.15, self.animal.food_for_day())

    def test_type_food(self):
        self.animal = Animal(self.db_conn, 'lion', 12, 'Pol', 'female')
        self.assertEqual('carnivore', self.animal.type_food())

    def test_expenses_for_food(self):
        self.animal = Animal(self.db_conn, 'lion', 1, 'Pol', 'female')
        self.assertEqual(1.04, self.animal.expenses_for_food())

    def tearDown(self):
        self.db_conn.commit()
        self.db_conn.close()
        call('rm -f animals.db', shell=True)