Example #1
0
class PetShopTest(unittest.TestCase):

    def test_correct__init__(self):
        self.shop = PetShop("Monkey")
        self.assertEqual(self.shop.name, "Monkey")
        self.assertEqual(self.shop.food, {})
        self.assertEqual(self.shop.pets, [])

    def test_incorrect__init__(self):
        self.shop = PetShop("Monkey")
        self.assertNotEqual(self.shop.name, "Donkey")
        self.assertEqual(self.shop.food, {})
        self.assertEqual(self.shop.pets, [])

    def test_add_food_method_raises_error(self):
        self.shop = PetShop("Monkey")
        with self.assertRaises(ValueError) as exc:
            self.shop.add_food("seeds", 0)

        msg = "Quantity cannot be equal to or less than 0"
        self.assertEqual(str(exc.exception), msg)

    def test_add_food_not_in_foods_dict(self):
        self.shop = PetShop("Monkey")
        self.shop.add_food("seeds", 5)
        self.assertEqual(self.shop.food, {"seeds": 5})

    def test_add_food_return_message(self):
        self.shop = PetShop("Monkey")
        msg = f"Successfully added 5.00 grams of seeds."
        self.assertEqual(self.shop.add_food("seeds", 5), msg)

    def test_add_food_method_food_already_in_food_dict(self):
        self.shop = PetShop("Monkey")
        self.shop.add_food("seeds", 5)
        self.shop.add_food("seeds", 5)
        self.assertEqual(self.shop.food, {"seeds": 10})

    def test_add_pet_method(self):
        self.shop = PetShop("Monkey")
        msg = "Successfully added Jerry."
        self.assertEqual(self.shop.add_pet("Jerry"), msg)
        self.assertEqual(self.shop.pets, ["Jerry"])

    def test_add_pet_raises_error(self):
        self.shop = PetShop("Monkey")
        self.shop.add_pet("Jerry")
        with self.assertRaises(Exception) as exc:
            self.shop.add_pet("Jerry")

        msg = "Cannot add a pet with the same name"
        self.assertEqual(str(exc.exception), msg)

    def test_feed_pet_method_pet_not_in_pets_list(self):
        self.shop = PetShop("Monkey")
        with self.assertRaises(Exception) as exc:
            self.shop.feed_pet("pet_food", "Jerry")

        msg = "Please insert a valid pet name"
        self.assertEqual(str(exc.exception), msg)

    def test_feed_pet_method_food_not_in_foods_dict(self):
        self.shop = PetShop("Monkey")
        self.shop.add_pet("Jerry")
        msg = "You do not have pet food"
        self.assertEqual(self.shop.feed_pet("pet food", "Jerry"), msg)

    def test_feed_pet_method_food_quantity_less_than_100(self):
        self.shop = PetShop("Monkey")
        self.shop.add_pet("Jerry")
        self.shop.add_food("seeds", 99)
        self.assertEqual(self.shop.feed_pet("seeds", "Jerry"), "Adding food...")
        self.assertEqual(self.shop.food, {"seeds": 1099.00})

    def test_feed_pet_positive_result(self):
        self.shop = PetShop("Monkey")
        self.shop.add_pet("Jerry")
        self.shop.add_food("seeds", 101)
        self.assertEqual(self.shop.feed_pet("seeds", "Jerry"), "Jerry was successfully fed")
        self.assertEqual(self.shop.food, {"seeds": 1})

    def test__repr__method(self):
        self.shop = PetShop("Monkey")
        self.shop.add_pet("Jerry")
        self.shop.add_pet("Tom")
        msg = "Shop Monkey:\nPets: Jerry, Tom"
        self.assertEqual(self.shop.__repr__(), msg)
Example #2
0
class TestPetShop(unittest.TestCase):
    def setUp(self):
        self.pet_shop = PetShop('PetShop')

    def test_petshop_init_method(self):
        self.assertEqual(self.pet_shop.name, 'PetShop')
        self.assertEqual(self.pet_shop.food, {})
        self.assertEqual(self.pet_shop.pets, [])
    
    def test_petshop_add_food_method_when_quantity_zero_should_raises(self):
        with self.assertRaises(ValueError) as ex:
            self.pet_shop.add_food('food', 0)
        
        self.assertTrue(str(ex.exception))

    def test_petshop_add_food_method_when_quantity_below_zero_should_raises(self):
        with self.assertRaises(Exception) as ex:
            self.pet_shop.add_food('food', -7)
        
        self.assertIsNotNone(str(ex.exception))

    def test_petshop_add_food_method(self):
        self.pet_shop.add_food('food', 10)
        
        self.assertEqual(self.pet_shop.food, {'food': 10})

    def test_pethshop_add_food_method_when_food_in_list(self):
        self.pet_shop.add_food('food', 10)
        self.pet_shop.add_food('food', 110)

        self.assertEqual(self.pet_shop.food, {'food': 120})

    def test_petshop_add_food_method_return_massage(self):
        result = self.pet_shop.add_food('food', 10)
        expected_result = f'Successfully added 10.00 grams of food.'
        
        self.assertEqual(result, expected_result)
    
    def test_pethsop_add_pet_method_when_pet_already_in_list_should_raises(self):
        self.pet_shop.add_pet('Rusty')
        with self.assertRaises(Exception) as ex:
            self.pet_shop.add_pet('Rusty')
        
        # self.assertIsNotNone(str(ex.exception))
        self.assertEqual(str(ex.exception), 'Cannot add a pet with the same name')
    
    def test_pethsop_add_pet_method(self):
        self.pet_shop.add_pet('Rusty')
        
        self.assertEqual(self.pet_shop.pets[0], 'Rusty')
    
    def test_pethsop_add_pet_method_return_massage(self):
        result = self.pet_shop.add_pet('Rusty')
        expected_result = f'Successfully added Rusty.'

        self.assertEqual(self.pet_shop.pets[0], 'Rusty')
        self.assertEqual(result, expected_result)
    
    def test_petshop_feed_pet_method_when_pet_not_in_list_should_raises(self):
        self.pet_shop.add_pet('Jupyter')
        self.pet_shop.add_food('pulneny chushki', 100)
        with self.assertRaises(Exception) as ex:
            self.pet_shop.feed_pet('pulneny chushki', 'Rusty')
        
        # self.assertIsNotNone(str(ex.exception))
        self.assertEqual(str(ex.exception), 'Please insert a valid pet name')

    def test_petshop_feed_pet_method_when_food_non_in_list(self):
        self.pet_shop.add_pet('Rusty')
        self.pet_shop.add_food('pulneny chushki', 100)

        result = self.pet_shop.feed_pet('chushki', 'Rusty')
        expected_result = f'You do not have chushki'

        self.assertEqual(result, expected_result)
    
    def test_petshop_feed_pet_method_when_food_below_100(self):
        self.pet_shop.add_pet('Rusty')
        self.pet_shop.add_food('pulneny chushki', 10)

        result = self.pet_shop.feed_pet('pulneny chushki', 'Rusty')
        expected_result = f'Adding food...'

        self.assertEqual(self.pet_shop.food['pulneny chushki'], 1010)
        self.assertEqual(result, expected_result)

    def test_petshop_feed_pet_method_when_food_above_100(self):
        self.pet_shop.add_pet('Rusty')
        self.pet_shop.add_food('pulneny chushki', 200)

        result = self.pet_shop.feed_pet('pulneny chushki', 'Rusty')
        expected_result = f'Rusty was successfully fed'

        self.assertEqual(self.pet_shop.food['pulneny chushki'], 100)
        self.assertEqual(result, expected_result)
    
    def test_petshop_repr_method(self):
        self.pet_shop.add_pet('Rusty')
        result = self.pet_shop.__repr__()
        expected_result = f'Shop PetShop:\nPets: Rusty'

        self.assertEqual(result, expected_result)