def test_repr_method(self):
     paint_f = PaintFactory('test', 20)
     paint_f.add_ingredient('red', 10)
     expected_output = f"Factory name: {paint_f.name} with capacity {paint_f.capacity}.\n"
     for key, value in paint_f.ingredients.items():
         expected_output += f"{key}: {value}\n"
     self.assertEqual(paint_f.__repr__(), expected_output)
Exemplo n.º 2
0
 def test_remove_raise_error_if_quantity_bigger(self):
     pfac = PaintFactory('pfac', 3)
     pfac.add_ingredient('white', 2)
     with self.assertRaises(ValueError) as cm:
         pfac.remove_ingredient('white', 3)
     self.assertEqual(str(cm.exception),
                      "Ingredient quantity cannot be less than zero")
Exemplo n.º 3
0
 def test_add_ingredients_sets_new_ingredients_types_quantities_to_given_quantities(
         self):
     test_paint_factory = PaintFactory("Charley's", 10)
     test_paint_factory.add_ingredient("white", 2)
     test_paint_factory.add_ingredient("blue", 3)
     self.assertEqual(test_paint_factory.ingredients["white"], 2)
     self.assertEqual(test_paint_factory.ingredients["blue"], 3)
Exemplo n.º 4
0
 def test_remove_raise_error_if_ingredient_not_in(self):
     pfac = PaintFactory('pfac', 3)
     pfac.add_ingredient('white', 2)
     with self.assertRaises(KeyError) as cm:
         pfac.remove_ingredient('blue', 1)
     self.assertEqual("'No such ingredient in the factory'",
                      str(cm.exception))
 def test_can_add_method(self):
     paint_f = PaintFactory('test', 20)
     paint_f.add_ingredient('red', 5)
     self.assertIn('red', paint_f.ingredients.keys())
     with self.assertRaises(ValueError) as ex:
         paint_f.add_ingredient('red', 20)
     self.assertEqual(str(ex.exception), "Not enough space in factory")
Exemplo n.º 6
0
 def test_add_ingredient_raise_error_if_ingreient_not_valid(self):
     pfac = PaintFactory('pfac', 2)
     with self.assertRaises(TypeError) as cm:
         pfac.add_ingredient('cghfg', 1)
     self.assertEqual(
         str(cm.exception),
         "Ingredient of type cghfg not allowed in PaintFactory")
Exemplo n.º 7
0
 def test_remove_ingredient_raises_error_when_not_enough_quantity_of_remaining_product_to_be_removed(
         self):
     test_paint_factory = PaintFactory("Charley's", 10)
     test_paint_factory.add_ingredient("white", 5)
     with self.assertRaises(ValueError) as context:
         test_paint_factory.remove_ingredient("white", 7)
     self.assertEqual(str(context.exception),
                      "Ingredient quantity cannot be less than zero")
Exemplo n.º 8
0
 def test_remove_ingredient_raises_error_when_no_such_product_to_be_removed(
         self):
     test_paint_factory = PaintFactory("Charley's", 10)
     test_paint_factory.add_ingredient("white", 5)
     with self.assertRaises(KeyError) as context:
         test_paint_factory.remove_ingredient("black", 7)
     self.assertEqual(str(context.exception),
                      "'No such ingredient in the factory'")
Exemplo n.º 9
0
 def test_add_ingredient_raises_exception_when_wrong_ingredient_type_given(
         self):
     test_paint_factory = PaintFactory("Charley's", 10)
     with self.assertRaises(TypeError) as context:
         test_paint_factory.add_ingredient("banana", 2)
     self.assertEqual(
         str(context.exception),
         "Ingredient of type banana not allowed in PaintFactory")
 def test_remove_ingredient(self):
     paint_f = PaintFactory('test', 20)
     paint_f.add_ingredient('red', 5)
     paint_f.remove_ingredient('red', 2)
     self.assertEqual(paint_f.ingredients['red'], 3)
     with self.assertRaises(KeyError) as ex:
         paint_f.remove_ingredient('sd', 2)
     self.assertEqual(str(ex.exception),
                      "'No such ingredient in the factory'")
     with self.assertRaises(ValueError) as ex:
         paint_f.remove_ingredient('red', 10)
     self.assertEqual(str(ex.exception),
                      "Ingredient quantity cannot be less than zero")
Exemplo n.º 11
0
 def test_products(self):
     pfac = PaintFactory('pfac', 3)
     pfac.add_ingredient('yellow', 1)
     pfac.add_ingredient('white', 1)
     pfac.add_ingredient('blue', 1)
     actual = pfac.products
     expected = {'blue': 1, 'white': 1, 'yellow': 1}
     self.assertEqual(expected, actual)
 def test_add_ingredient(self):
     paint_f = PaintFactory('test', 20)
     paint_f.add_ingredient('red', 5)
     self.assertIn('red', paint_f.ingredients)
     self.assertEqual(paint_f.ingredients['red'], 5)
     with self.assertRaises(TypeError) as ex:
         paint_f.add_ingredient('sd', 5)
     self.assertEqual(str(ex.exception),
                      f"Ingredient of type sd not allowed in PaintFactory")
     with self.assertRaises(ValueError) as ex:
         paint_f.add_ingredient('red', 20)
     self.assertEqual(str(ex.exception), "Not enough space in factory")
Exemplo n.º 13
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self):
        self.fabrika = PaintFactory('Qica', 2500)

    def test_one(self):
        result = (self.fabrika.name, self.fabrika.capacity,
                  self.fabrika.ingredients)
        expected = ('Qica', 2500, {})

        self.assertEqual(result, expected)

    def test_two(self):
        with self.assertRaises(TypeError):
            self.fabrika.add_ingredient('test', 2000)

        with self.assertRaises(ValueError):
            self.fabrika.add_ingredient('white', 3000)

        self.fabrika.add_ingredient('white', 5)
        self.assertEqual(self.fabrika.products, {'white': 5})
        self.assertEqual(self.fabrika.capacity, 2495)

    def test_three(self):
        with self.assertRaises(KeyError):
            self.fabrika.remove_ingredient('test', 5)

        self.fabrika.add_ingredient('white', 5)

        with self.assertRaises(ValueError):
            self.fabrika.remove_ingredient('white', 6)

        self.fabrika.remove_ingredient('white', 2)
        self.assertEqual(self.fabrika.products, {'white': 3})

    def test_four(self):
        self.assertFalse(self.fabrika.can_add(5000))
        self.assertTrue(self.fabrika.can_add(10))
Exemplo n.º 14
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self) -> None:
        self.factory = PaintFactory(name='Sharo', capacity=5)

    def test_paint_factory_if_all_attributes_are_set_correctly(self):
        self.assertEqual('Sharo', self.factory.name)
        self.assertEqual(5, self.factory.capacity)
        self.assertEqual({}, self.factory.ingredients)
        self.assertEqual({}, self.factory.products)
        # self.assertEqual(["white", "yellow", "blue", "green", "red"], self.factory.valid_ingredients)

    def test_paint_factory_add_ingredient_if_is_NOT_in_ingredients_list(self):
        with self.assertRaises(Exception) as ex:
            self.factory.add_ingredient('pink', 5)
        self.assertEqual("Ingredient of type pink not allowed in PaintFactory", str(ex.exception))

    def test_paint_factory_add_ingredient_if_capacity_is_NOT_enough(self):
        with self.assertRaises(Exception) as ex:
            self.factory.add_ingredient('white', 10)
        self.assertEqual("Not enough space in factory", str(ex.exception))

    def test_paint_factory_add_ingredient_if_ingredient_type_NOT_in_ingredients_dict(self):
        self.assertTrue('white' not in self.factory.ingredients)
        self.factory.add_ingredient('white', 3)
        self.assertTrue('white' in self.factory.ingredients)
        self.assertEqual(3, self.factory.ingredients['white'])

    def test_paint_factory_remove_ingredient_if_ingredient_type_NOT_in_ingredients_dict(self):
        with self.assertRaises(Exception) as ex:
            self.factory.remove_ingredient('white', 3)
        self.assertEqual("'No such ingredient in the factory'", str(ex.exception))

    def test_paint_factory_remove_ingredients_if_quantity_is_bigger_than_capacity(self):
        self.factory.ingredients['white'] = 5
        with self.assertRaises(Exception) as ex:
            self.factory.remove_ingredient('white', 6)
        self.assertEqual("Ingredients quantity cannot be less than zero", str(ex.exception))

    def test_paint_factory_remove_ingredients_if_all_conditions_are_covered(self):
        self.factory.ingredients['white'] = 5
        self.factory.remove_ingredient('white', 3)
        self.assertEqual(2, self.factory.ingredients['white'])
Exemplo n.º 15
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self):
        self.paint_factory = PaintFactory("PF", 100)

    def test__init(self):
        self.assertEqual(self.paint_factory.name, "PF")
        self.assertEqual(self.paint_factory.capacity, 100)
        self.assertEqual(self.paint_factory.ingredients, {})
        self.assertEqual(self.paint_factory.valid_ingredients,
                         ["white", "yellow", "blue", "green", "red"])

    def test__prop_products__return_ingredients_dict(self):
        self.assertEqual(self.paint_factory.products, {})

    def test__add_ingredient__non_type__raise_type_error(self):
        with self.assertRaises(TypeError) as te:
            self.paint_factory.add_ingredient("magenta", 100)
        expected_msg = str(te.exception)
        actual_msg = "Ingredient of type magenta not allowed in PaintFactory"
        self.assertEqual(expected_msg, actual_msg)

    def test__add_ingredient___less_capacity__raise_value_error(self):
        with self.assertRaises(ValueError) as ve:
            self.paint_factory.add_ingredient("blue", 110)
            expected_msg = str(ve.exception)
            actual_msg = "Not enough space in factory"
            self.assertEqual(expected_msg, actual_msg)

    def test_inherits_from_factory(self):
        self.assertTrue(issubclass(PaintFactory, Factory))

    def test__add_ingredient__add_in_dict__set_val_to_qty(self):
        self.assertEqual(self.paint_factory.products, {})
        self.paint_factory.add_ingredient("blue", 10)
        self.assertEqual(self.paint_factory.products, {"blue": 10})
        self.paint_factory.add_ingredient("red", 10)
        self.assertEqual(self.paint_factory.products, {"blue": 10, "red": 10})
        self.paint_factory.add_ingredient("red", 10)
        self.assertEqual(self.paint_factory.products, {"blue": 10, "red": 20})

    def test__remove_ingredient__not_in_dict__raise_key_error(self):
        self.paint_factory.add_ingredient("blue", 100)
        with self.assertRaises(KeyError) as ke:
            self.paint_factory.remove_ingredient("magenta", 10)
        expected_msg = str(ke.exception)
        actual_msg = "'No such product in the factory'"
        self.assertEqual(expected_msg, actual_msg)

    def test__remove_ingredient__more_qty__raise_value_error(self):
        self.paint_factory.add_ingredient("blue", 100)
        with self.assertRaises(ValueError) as ve:
            self.paint_factory.remove_ingredient("blue", 110)
        expected_msg = str(ve.exception)
        actual_msg = "Ingredient quantity cannot be less than zero"
        self.assertEqual(expected_msg, actual_msg)

    def test_remove_ingredient_quantity(self):
        self.paint_factory.add_ingredient("white", 5)
        self.paint_factory.remove_ingredient("white", 3)
        self.assertEqual(self.paint_factory.ingredients, {"white": 2})
        self.paint_factory.remove_ingredient("white", 2)
        self.assertEqual(self.paint_factory.ingredients, {"white": 0})

    def test__remove_ingredient__remove_qty_from_ingredient_val(self):
        self.paint_factory.add_ingredient("blue", 100)
        self.paint_factory.remove_ingredient("blue", 50)
        self.assertEqual(self.paint_factory.products, {"blue": 50})

    def test_get_ingredients(self):
        self.paint_factory.add_ingredient("white", 5)
        self.paint_factory.add_ingredient("yellow", 10)
        dict = self.paint_factory.ingredients
        self.assertEqual(dict, {"white": 5, "yellow": 10})

    def test__can_add__return_true(self):
        self.assertTrue(self.paint_factory.can_add(10))
        self.assertFalse(self.paint_factory.can_add(110))

    def test_add_zero(self):
        self.paint_factory.add_ingredient("white", 0)
        self.assertEqual(self.paint_factory.products, {"white": 0})

    def test_can_add(self):
        self.paint_factory.add_ingredient("white", 5)
        self.assertEqual(self.paint_factory.can_add(100), False)
        self.assertEqual(self.paint_factory.can_add(10), True)

    def test_if_isinstance(self):
        self.assertIsInstance(PaintFactory, Factory)
Exemplo n.º 16
0
 def test_add_ingredient_raises_ingredient_type_quantity_with_given_quantity(
         self):
     test_paint_factory = PaintFactory("Charley's", 10)
     test_paint_factory.add_ingredient("white", 2)
     test_paint_factory.add_ingredient("white", 4)
     self.assertEqual(test_paint_factory.ingredients["white"], 6)
Exemplo n.º 17
0
 def test_add_ingredient_raises_error_when_capacity_is_less_than_quantity(
         self):
     test_paint_factory = PaintFactory("Charley's", 10)
     with self.assertRaises(ValueError) as context:
         test_paint_factory.add_ingredient("white", 20)
     self.assertEqual(str(context.exception), "Not enough space in factory")
class PaintFactoryTests(unittest.TestCase):
    def setUp(self):
        self.paint_factory = PaintFactory("name", 100)
        # self.valid_ingredients = ["white", "yellow", "blue", "green", "red"]

    def test_paint_factory_initialized(self):
        self.assertEqual(self.paint_factory.name, "name")
        self.assertEqual(self.paint_factory.capacity, 100)
        self.assertEqual(self.paint_factory.ingredients, {})
        self.assertEqual(self.paint_factory.valid_ingredients,
                         ["white", "yellow", "blue", "green", "red"])

    def test_add_ingredient_ingredient_type_not_in_list(self):
        self.assertRaises(TypeError, self.paint_factory.add_ingredient,
                          "brown", 5)

    def test_add_ingredient_capacity_less_than_quantity(self):
        self.assertRaises(ValueError, self.paint_factory.add_ingredient,
                          "green", 105)

    def test_inherits_from_factory(self):
        self.assertTrue(issubclass(PaintFactory, Factory))

    def test_add_zero(self):
        self.paint_factory.add_ingredient("white", 0)
        self.assertEqual(self.paint_factory.ingredients, {"white": 0})

    def test_add_ingredient_add_new_ingredient_type(self):
        self.paint_factory.add_ingredient("white", 5)
        self.assertEqual(self.paint_factory.ingredients, {"white": 5})

    def test_remove_ingredient_ingredient_type_not_in_list(self):
        self.assertRaises(KeyError, self.paint_factory.remove_ingredient,
                          "brown", 5)

    def test_remove_ingredient_quantity_more_than_existing(self):
        self.paint_factory.add_ingredient("white", 5)
        self.assertRaises(ValueError, self.paint_factory.remove_ingredient,
                          "white", 6)

    def test_remove_ingredient_quantity(self):
        self.paint_factory.add_ingredient("white", 5)
        self.paint_factory.remove_ingredient("white", 3)
        self.assertEqual(self.paint_factory.ingredients, {"white": 2})
        self.paint_factory.remove_ingredient("white", 2)
        self.assertEqual(self.paint_factory.ingredients, {"white": 0})

    def test_get_ingredients(self):
        self.paint_factory.add_ingredient("white", 5)
        self.paint_factory.add_ingredient("yellow", 10)
        dict = self.paint_factory.ingredients
        self.assertEqual(dict, {"white": 5, "yellow": 10})

    def test_can_add(self):
        self.paint_factory.add_ingredient("white", 5)
        self.assertEqual(self.paint_factory.can_add(100), False)
        self.assertEqual(self.paint_factory.can_add(10), True)

    def test_if_instanse(self):
        self.assertIsInstance(PaintFactory)

    def test_property_products(self):
        self.assertEqual({}, self.paint_factory.products)
Exemplo n.º 19
0
 def test_remove_ingredient_quantity_with_given_quantity(self):
     test_paint_factory = PaintFactory("Charley's", 10)
     test_paint_factory.add_ingredient("white", 5)
     test_paint_factory.remove_ingredient("white", 3)
     self.assertEqual(test_paint_factory.ingredients["white"], 2)
Exemplo n.º 20
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self) -> None:
        self.paint_factory = PaintFactory('PF', 1000)

    def test_paint_factory_init_method(self):
        ingredients = ["white", "yellow", "blue", "green", "red"]
        self.assertEqual(self.paint_factory.name, 'PF')
        self.assertEqual(self.paint_factory.capacity, 1000)
        self.assertEqual(self.paint_factory.valid_ingredients, ingredients)
        self.assertEqual(self.paint_factory.products, {})

    def test_paint_factory_add_ingredient_method_when_igredient_in_list(self):
        collor = 'white'
        self.paint_factory.add_ingredient(collor, 10)
        self.assertEqual(self.paint_factory.ingredients, {'white': 10})

    def test_paint_factory_add_igredient_method_when_not_enough_space_should_raises(
            self):
        collor = 'white'

        with self.assertRaises(ValueError) as ex:
            self.paint_factory.add_ingredient(collor, 1001)

        self.assertIsNotNone(str(ex.exception))

    def test_paint_factory_add_igredient_method_when_product_not_allowed_should_raises(
            self):
        collor = 'purple'

        with self.assertRaises(TypeError) as ex:
            self.paint_factory.add_ingredient(collor, 10)

        self.assertIsNotNone(str(ex.exception))

    def test_paint_factory_remove_ingredient_method_when_product_in_list(self):
        collor = 'white'
        self.paint_factory.add_ingredient(collor, 10)
        self.paint_factory.remove_ingredient(collor, 5)
        self.assertEqual(self.paint_factory.ingredients, {'white': 5})

    def test_paint_factory_remove_ingredient_method_when_product_less_than_zero_should_raises(
            self):
        collor = 'white'
        self.paint_factory.add_ingredient(collor, 10)

        with self.assertRaises(ValueError) as ex:
            self.paint_factory.remove_ingredient(collor, 15)

        self.assertIsNotNone(str(ex.exception))

    def test_paint_factory_remove_ingredient_method_when_product_not_allowed_should_raises(
            self):
        collor2 = 'purple'
        collor = 'green'
        self.paint_factory.add_ingredient(collor, 10)

        with self.assertRaises(KeyError) as ex:
            self.paint_factory.remove_ingredient(collor2, 5)

        self.assertIsNotNone(str(ex.exception))
class TestPaintFactory(TestCase):
    def setUp(self):
        self.paint_factory = PaintFactory("My name", 30)

    def test_attributes_are_set(self):
        self.assertEqual("My name", self.paint_factory.name)
        self.assertEqual(30, self.paint_factory.capacity)
        self.assertDictEqual({}, self.paint_factory.ingredients)

    def test_add_ingredient_not_valid_raises_exception(self):
        with self.assertRaises(TypeError) as ex:
            self.paint_factory.add_ingredient("purple", 5)
        expected = "Ingredient of type purple not allowed in PaintFactory"
        self.assertEqual(expected, str(ex.exception))

    def test_try_to_add_ingredient__not_enough_capacity_raises(self):
        with self.assertRaises(ValueError) as ex:
            self.paint_factory.add_ingredient("blue", 40)
        expected = "Not enough space in factory"
        self.assertEqual(expected, str(ex.exception))

    def test_add_ingredient__ingredient_type_not_in_ingredients_dict(self):
        self.paint_factory.add_ingredient("blue", 10)
        expected = {"blue": 10}
        actual_result = self.paint_factory.ingredients
        self.assertDictEqual(expected, actual_result)

    def test_add_ingredient__ingredient_type_already_in_ingredients_dict(self):
        self.assertDictEqual({}, self.paint_factory.ingredients)
        self.paint_factory.add_ingredient("blue", 12)
        self.paint_factory.add_ingredient("blue", 5)
        expected = {"blue": 17}
        actual_result = self.paint_factory.ingredients
        self.assertDictEqual(expected, actual_result)

    def test_try_to_remove_ingredient__type_not_in_ingredients_dict_raises(self):
        self.paint_factory.add_ingredient("yellow", 5)
        with self.assertRaises(KeyError) as ex:
            self.paint_factory.remove_ingredient("green", 8)
        expected = "'No such product in the factory'"
        self.assertEqual(expected, str(ex.exception))

    def test_try_to_remove_ingredient__quantity_gets_less_than_zero_raises(self):
        self.paint_factory.add_ingredient("red", 15)
        with self.assertRaises(ValueError) as ex:
            self.paint_factory.remove_ingredient("red", 17)
        expected = "Ingredient quantity cannot be less than zero"
        self.assertEqual(expected, str(ex.exception))

    def test_remove_ingredient_from_ingredients(self):
        self.paint_factory.add_ingredient("red", 15)
        self.paint_factory.remove_ingredient("red", 10)
        expected = {"red": 5}
        actual_result = self.paint_factory.ingredients
        self.assertDictEqual(expected, actual_result)
        self.assertNotIn("blue", self.paint_factory.ingredients.keys())

    def test_paint_factory_products__expect_ingredients(self):
        self.paint_factory.add_ingredient("blue", 30)
        expected_result = self.paint_factory.products
        self.assertDictEqual(expected_result, self.paint_factory.ingredients)
Exemplo n.º 22
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self):
        self.p = PaintFactory("Factory", 100)

    def test_init(self):  # 1 test
        self.assertEqual(self.p.name, "Factory")
        self.assertEqual(self.p.capacity, 100)
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})

    def test_which_are_valid(self):
        self.assertEqual(self.p.valid_ingredients,
                         ['white', 'yellow', 'blue', 'green', 'red'])

    def test_change_the_valid_pieces(self):
        self.assertEqual(self.p.valid_ingredients,
                         ['white', 'yellow', 'blue', 'green', 'red'])
        self.p.valid_ingredients = ['blackish']
        self.assertEqual(self.p.valid_ingredients, ['blackish'])

    def test_add_successfully(self):  # 2, 3, 4, 5 test
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 1)
        self.assertEqual(self.p.ingredients, {"white": 1})
        self.assertEqual(self.p.products, {"white": 1})
        self.p.add_ingredient("white", 0)
        self.assertEqual(self.p.ingredients, {"white": 1})
        self.assertEqual(self.p.products, {"white": 1})

    def test_add_all_successfully(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 1)
        self.p.add_ingredient("yellow", 1)
        self.p.add_ingredient("blue", 1)
        self.p.add_ingredient("green", 1)
        self.p.add_ingredient("red", 1)
        self.assertEqual(self.p.ingredients, {
            "white": 1,
            "yellow": 1,
            "blue": 1,
            "green": 1,
            "red": 1
        })
        self.assertEqual(self.p.products, {
            "white": 1,
            "yellow": 1,
            "blue": 1,
            "green": 1,
            "red": 1
        })

    def test_add_zero(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 0)
        self.assertEqual(self.p.ingredients, {"white": 0})
        self.assertEqual(self.p.products, {"white": 0})

    def test_products_and_capacity(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.assertEqual(self.p.capacity, 100)
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 5)
        self.assertEqual(self.p.products, {"white": 5})
        self.assertEqual(self.p.ingredients, {"white": 5})
        self.assertEqual(self.p.capacity, 100)

    def test_remove_successfully(self):  # 6 test
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        self.p.remove_ingredient("white", 1)
        self.assertEqual(self.p.ingredients, {"white": 3})
        self.assertEqual(self.p.products, {"white": 3})
        self.p.remove_ingredient("white", 3)
        self.assertEqual(self.p.ingredients, {"white": 0})
        self.assertEqual(self.p.products, {"white": 0})

    def test_can_add_true(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        self.assertEqual(self.p.can_add(10), True)
        self.p.add_ingredient("white", 10)
        self.assertEqual(self.p.ingredients, {"white": 14})
        self.assertEqual(self.p.products, {"white": 14})

    def test_can_add_false(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        self.assertEqual(self.p.can_add(100000), False)
        self.assertEqual(self.p.ingredients, {"white": 4})
        self.assertEqual(self.p.products, {"white": 4})

    def test_not_allowed(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        black = "cheren"
        with self.assertRaises(TypeError) as ex:
            self.p.add_ingredient(black, 5)
        expected = str(ex.exception)
        actual = f"Ingredient of type {black} not allowed in PaintFactory"
        self.assertEqual(expected, actual)
        self.assertEqual(self.p.ingredients, {"white": 4})
        self.assertEqual(self.p.products, {"white": 4})

    def test_not_enough_space(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        with self.assertRaises(ValueError) as ex:
            self.p.add_ingredient("white", 4000000)
        expected = str(ex.exception)
        actual = "Not enough space in factory"
        self.assertEqual(expected, actual)
        self.assertEqual(self.p.ingredients, {"white": 4})
        self.assertEqual(self.p.products, {"white": 4})

    def test_remove_non_existing_ingredient(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        with self.assertRaises(KeyError) as ex:
            self.p.remove_ingredient("blackmagic", 5)
        expected = str(ex.exception)
        actual = "\'No such product in the factory\'"
        self.assertEqual(expected, actual)
        self.assertEqual(self.p.ingredients, {"white": 4})
        self.assertEqual(self.p.products, {"white": 4})

    def test_remove_ingredient_with_less_than_zero(self):
        self.assertEqual(self.p.ingredients, {})
        self.assertEqual(self.p.products, {})
        self.p.add_ingredient("white", 4)
        with self.assertRaises(ValueError) as ex:
            self.p.remove_ingredient("white", 400)
        expected = str(ex.exception)
        actual = "Ingredient quantity cannot be less than zero"
        self.assertEqual(expected, actual)
        self.assertEqual(self.p.ingredients, {"white": 4})
        self.assertEqual(self.p.products, {"white": 4})
Exemplo n.º 23
0
class PaintFactoryTests(unittest.TestCase):
    def setUp(self):
        self.paint_factory = PaintFactory("Factory", 100)

    def test_init__expect_attributes_to_be_set(self):
        self.assertEqual("Factory", self.paint_factory.name)
        self.assertEqual(100, self.paint_factory.capacity)
        # self.assertEqual(["white", "yellow", "blue", "green", "red"], self.paint_factory.valid_ingredients)
        self.assertEqual({}, self.paint_factory.ingredients)
        self.assertEqual({}, self.paint_factory.products)

    def test_add_ingredient__when_ingredient_is_not_valid_expect_type_error(
            self):
        with self.assertRaises(TypeError) as err:
            self.paint_factory.add_ingredient("purple", 10)
        expected_msg = "Ingredient of type purple not allowed in PaintFactory"
        self.assertEqual(expected_msg, str(err.exception))

    def test_add_ingredient__when_capacity_is_not_enough_expect_value_error(
            self):
        with self.assertRaises(ValueError) as err:
            self.paint_factory.add_ingredient("white", 105)
        expected_msg = "Not enough space in factory"
        self.assertEqual(expected_msg, str(err.exception))

    def test_add_ingredient__when_ingredient_is_not_in_ingredients_expect_to_add_it(
            self):
        self.paint_factory.add_ingredient("white", 10)
        expected = {"white": 10}
        self.assertEqual(expected, self.paint_factory.products)

    def test_add_ingredient__when_ingredient_is_in_ingredients_expect_to_raise_it_s_value(
            self):
        self.paint_factory.add_ingredient("white", 10)
        self.paint_factory.add_ingredient("white", 10)
        expected = {"white": 20}
        self.assertEqual(expected, self.paint_factory.products)

    def test_remove_ingredient__when_ingredient_type_not_in_ingredients_expect_key_error(
            self):
        with self.assertRaises(KeyError) as err:
            self.paint_factory.remove_ingredient("white", 10)
        expected_msg = "'No such ingredient in the factory'"
        self.assertEqual(expected_msg, str(err.exception))

    def test_remove_ingredient__when_quantity_exceeds_ingredient_quantity_expect_value_error(
            self):
        self.paint_factory.add_ingredient("white", 5)
        with self.assertRaises(ValueError) as err:
            self.paint_factory.remove_ingredient("white", 10)
        expected_msg = "Ingredients quantity cannot be less than zero"
        self.assertEqual(expected_msg, str(err.exception))

    def test_remove_ingredient__expect_to_decrease_ingredient_quantity_with_given(
            self):
        self.paint_factory.add_ingredient("white", 10)
        self.paint_factory.remove_ingredient("white", 5)
        expected = {"white": 5}
        self.assertEqual(expected, self.paint_factory.products)
Exemplo n.º 24
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self) -> None:
        self.paint_factory = PaintFactory('TestName', 10)

    def test_init__expect_attrs_set(self) -> None:
        self.assertEqual(self.paint_factory.name, 'TestName')
        self.assertEqual(self.paint_factory.capacity, 10)
        self.assertEqual(self.paint_factory.ingredients, {})

    def test_add_ingredient__when_valid_enough_space__expect_added(
            self) -> None:
        self.paint_factory.add_ingredient('white', 5)
        self.assertEqual(self.paint_factory.ingredients, {'white': 5})
        self.paint_factory.add_ingredient('white', 3)
        self.assertEqual(self.paint_factory.ingredients, {'white': 8})

    def test_add_ingredient__when_valid_not_enough_space__expect_exception(
            self) -> None:
        with self.assertRaises(ValueError) as context:
            self.paint_factory.add_ingredient('white', 15)

        self.assertEqual(context.exception.args[0],
                         'Not enough space in factory')

    def test_add_ingredient__when_invalid__expect_exception(self) -> None:
        with self.assertRaises(TypeError) as context:
            self.paint_factory.add_ingredient('purple', 5)

        self.assertEqual(
            context.exception.args[0],
            'Ingredient of type purple not allowed in PaintFactory')

    def test_remove_ingredient__when_invalid__expect_exception(self) -> None:
        with self.assertRaises(KeyError) as context:
            self.paint_factory.remove_ingredient('white', 10)

        self.assertEqual(context.exception.args[0],
                         'No such ingredient in the factory')

    def test_remove_ingredient__when_valid_enough_quantity(self) -> None:
        self.paint_factory.add_ingredient('white', 8)
        self.paint_factory.remove_ingredient('white', 3)
        self.assertEqual(self.paint_factory.ingredients, {'white': 5})

    def test_remove_ingredient__when_valid_not_enough_quantity__expect_exception(
            self) -> None:
        self.paint_factory.add_ingredient('white', 5)
        with self.assertRaises(ValueError) as context:
            self.paint_factory.remove_ingredient('white', 10)

        self.assertEqual(context.exception.args[0],
                         'Ingredients quantity cannot be less than zero')

    def test_property_products(self) -> None:
        self.assertEqual(self.paint_factory.products, {})
        self.paint_factory.add_ingredient('white', 8)
        self.assertEqual(self.paint_factory.products, {'white': 8})

    def test_repr_method(self) -> None:
        self.paint_factory.add_ingredient('white', 5)
        self.paint_factory.add_ingredient('blue', 3)
        self.assertEqual(
            repr(self.paint_factory),
            'Factory name: TestName with capacity 10.\nwhite: 5\nblue: 3\n')
Exemplo n.º 25
0
class TestPaintFactory(TestCase):
    def setUp(self) -> None:
        self.paint_factory = PaintFactory("Test", 100)

    def test_initializing_all_attributes(self):
        self.assertEqual("Test", self.paint_factory.name)
        self.assertEqual(100, self.paint_factory.capacity)
        self.assertListEqual(["white", "yellow", "blue", "green", "red"], self.paint_factory.valid_ingredients)
        self.assertDictEqual(self.paint_factory.products, self.paint_factory.ingredients)

    def test_can_add(self):
        self.paint_factory.ingredients = {"white": 10, "yellow": 10}
        result = self.paint_factory.can_add(50)
        self.assertEqual(True, result)

        result2 = self.paint_factory.can_add(90)
        self.assertEqual(False, result2)

    def test_add_gradient_invalid_product_type_raises(self):
        with self.assertRaises(TypeError) as ex:
            self.paint_factory.add_ingredient("pink", 10)

        self.assertEqual("Ingredient of type pink not allowed in PaintFactory", str(ex.exception))

    def test_add_gradient_not_enough_space_raises(self):
        with self.assertRaises(ValueError) as ex:
            self.paint_factory.add_ingredient("white", 120)

        self.assertEqual("Not enough space in factory", str(ex.exception))

    def test_add_gradient_successfully(self):
        self.assertDictEqual({}, self.paint_factory.products)
        result = self.paint_factory.add_ingredient("white", 50)
        self.assertDictEqual({"white": 50}, self.paint_factory.products)
        self.assertIsNone(result)

        self.paint_factory.add_ingredient("white", 10)
        self.assertDictEqual({"white": 60}, self.paint_factory.products)

        self.paint_factory.add_ingredient("yellow", 10)
        self.assertDictEqual({"white": 60, "yellow": 10}, self.paint_factory.products)

    def test_remove_gradient_not_found_rises(self):
        with self.assertRaises(KeyError) as ex:
            self.paint_factory.remove_ingredient("white", 10)

        self.assertEqual("'No such ingredient in the factory'", str(ex.exception))

    def test_remove_gradient_less_than_zero(self):
        self.paint_factory.ingredients = {"white": 10}
        with self.assertRaises(ValueError) as ex:
            self.paint_factory.remove_ingredient("white", 20)

        self.assertEqual("Ingredients quantity cannot be less than zero", str(ex.exception))

    def test_remove_gradient_successfully(self):
        self.paint_factory.ingredients = {"white": 20}
        result = self.paint_factory.remove_ingredient("white", 10)
        self.assertDictEqual({"white": 10}, self.paint_factory.ingredients)
        self.assertIsNone(result)

    def test_repr_representation(self):
        self.paint_factory.ingredients = {"white": 10, "yellow": 10}
        expected = "Factory name: Test with capacity 100.\n" \
                   "white: 10\n" \
                   "yellow: 10\n"
        self.assertEqual(expected, repr(self.paint_factory))
Exemplo n.º 26
0
class TestPaintFactory(TestCase):
    def setUp(self):
        self.pf = PaintFactory("Test", 100)

    def test_init(self):
        pf = PaintFactory("Test", 100)
        self.assertEqual("Test", pf.name)
        self.assertEqual(100, pf.capacity)
        self.assertEqual({}, pf.ingredients)
        self.assertEqual(["white", "yellow", "blue", "green", "red"],
                         pf.valid_ingredients)

    # add_ingredient
    def test_add_ingredient_invalid_product_type_raises(self):
        with self.assertRaises(TypeError) as ex:
            self.pf.add_ingredient("purple", 2)
        self.assertEqual(
            "Ingredient of type purple not allowed in PaintFactory",
            str(ex.exception))

    def test_add_ingredient_invalid_quantity_raises(self):
        with self.assertRaises(ValueError) as ex:
            self.pf.add_ingredient("white", 101)
        self.assertEqual("Not enough space in factory", str(ex.exception))

    def test_add_ingredient(self):
        self.assertEqual({}, self.pf.ingredients)
        self.pf.add_ingredient("white", 2)
        self.assertEqual({"white": 2}, self.pf.ingredients)

    def test_add_ingredient_already_existing_increases(self):
        self.assertEqual({}, self.pf.ingredients)
        self.pf.add_ingredient("white", 2)
        self.assertEqual({"white": 2}, self.pf.ingredients)
        self.pf.add_ingredient("white", 2)
        self.assertEqual({"white": 4}, self.pf.ingredients)

    # remove_ingredient
    def test_remove_invalid_product_type_raises(self):
        with self.assertRaises(KeyError) as ex:
            self.pf.remove_ingredient("test_type", 2)
        self.assertEqual("'No such ingredient in the factory'",
                         str(ex.exception))

    def test_remove_ingredient_if_quantity_less_than_0(self):
        self.pf.add_ingredient("white", 2)
        with self.assertRaises(ValueError) as ex:
            self.pf.remove_ingredient("white", 3)
        self.assertEqual("Ingredients quantity cannot be less than zero",
                         str(ex.exception))

    def test_remove_ingredient(self):
        self.pf.add_ingredient("white", 2)
        self.pf.remove_ingredient("white", 1)
        self.assertEqual({"white": 1}, self.pf.ingredients)

    def test_product_property(self):
        self.pf.add_ingredient("white", 2)
        self.assertEqual({"white": 2}, self.pf.products)
Exemplo n.º 27
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self):
        self.factory = PaintFactory("Test", 5)

    def test_paint_factory_init__expect_correct_attributes(self):
        self.assertEqual("Test", self.factory.name)
        self.assertEqual(5, self.factory.capacity)
        self.assertEqual({}, self.factory.ingredients)

    def test_paint_factory_add_ingredient__when_not_valid_ingredients_type__expect_type_error(
            self):
        with self.assertRaises(TypeError) as context:
            self.factory.add_ingredient("test", 2)

        self.assertEqual(
            f"Ingredient of type test not allowed in PaintFactory",
            str(context.exception))

    def test_paint_factory_add_ingredient__when_not_enough_capacity__expect_value_error(
            self):
        with self.assertRaises(ValueError) as context:
            self.factory.add_ingredient("white", 6)

        self.assertEqual("Not enough space in factory", str(context.exception))

    def test_paint_factory_add_ingredient__when_ingredient_not_in_ingredients__expect_to_add_it_with_given_value(
            self):
        self.assertEqual({}, self.factory.ingredients)
        self.factory.add_ingredient("white", 2)
        self.assertEqual({"white": 2}, self.factory.ingredients)

    def test_paint_factory_add_ingredient__when_ingredient_in_ingredients__expect_to_addition(
            self):
        self.factory.add_ingredient("white", 2)
        self.factory.add_ingredient("white", 2)
        self.assertEqual({"white": 4}, self.factory.ingredients)

    def test_paint_factory_remove_ingredient__when_ingredient_not_in_ingredients__expect_key_error(
            self):
        with self.assertRaises(KeyError) as context:
            self.factory.remove_ingredient("test", 2)

        self.assertEqual("'No such ingredient in the factory'",
                         str(context.exception))

    def test_paint_factory_remove_ingredient__when_given_quantity_is_more_than_item_has__expect_value_error(
            self):
        self.factory.add_ingredient("white", 2)
        with self.assertRaises(ValueError) as context:
            self.factory.remove_ingredient("white", 3)

        self.assertEqual("Ingredients quantity cannot be less than zero",
                         str(context.exception))

    def test_paint_factory_remove_ingredient__when_correct_params__expect_to_subtract_item_quantity(
            self):
        self.factory.add_ingredient("white", 2)
        self.factory.remove_ingredient("white", 2)
        self.assertEqual({"white": 0}, self.factory.ingredients)

    def test_paint_factory_product_prop__expect_to_return_ingredients(self):
        self.assertEqual({}, self.factory.products)
Exemplo n.º 28
0
class TestPaintFactory(unittest.TestCase):
    name = 'Art Factory'
    capacity = 10
    valid_ingredients = ["white", "yellow", "blue", "green", "red"]
    ingredients = {}

    def setUp(self):
        self.paint_factory = PaintFactory(self.name, self.capacity)

    def test_init_attributes(self):
        self.assertEqual('Art Factory', self.paint_factory.name)
        self.assertEqual(10, self.paint_factory.capacity)
        self.assertDictEqual({}, self.paint_factory.ingredients)
        self.assertDictEqual({}, self.paint_factory.products)
        expect = ["white", "yellow", "blue", "green", "red"]
        self.assertListEqual(expect, self.paint_factory.valid_ingredients)

    def test_add_ingredient__when_ingredient_not_in_ingredients__expect_to_add(self):
        self.paint_factory.add_ingredient('white', 5)
        self.assertDictEqual({'white': 5}, self.paint_factory.ingredients)

    def test_add_ingredient__when_ingredient_in_ingredients__expect_to_increase_quantity(self):
        self.paint_factory.add_ingredient('white', 5)
        self.assertDictEqual({'white': 5}, self.paint_factory.ingredients)
        self.paint_factory.add_ingredient('white', 5)
        self.assertDictEqual({'white': 10}, self.paint_factory.ingredients)

    def test_add_ingredient__when_ingredient_type_not_in_valid_ingredient_types__expect_to_raise(self):
        with self.assertRaises(TypeError) as ex:
            self.paint_factory.add_ingredient('pink', 5)
        expect = f"Ingredient of type pink not allowed in PaintFactory"
        actual = str(ex.exception)
        self.assertEqual(expect, actual)

    def test_add_ingredient__when_ingredient_is_valid_and_capacity_is_less_than_quantity__expect_to_raise(self):
        with self.assertRaises(ValueError) as ex:
            self.paint_factory.add_ingredient('white', 15)
        expect = "Not enough space in factory"
        actual = str(ex.exception)
        self.assertEqual(expect, actual)

    def test_remove_ingredient__when_ingredient_in_ingredients__expect_to_decrease_quantity(self):
        self.paint_factory.add_ingredient('white', 5)
        self.paint_factory.remove_ingredient('white', 3)
        self.assertDictEqual({'white': 2}, self.paint_factory.ingredients)
        self.paint_factory.remove_ingredient('white', 2)
        self.assertDictEqual({'white': 0}, self.paint_factory.ingredients)

    def test_remove_ingredient__when_ingredient_not_in_ingredients__expect_to_raise_exception(self):
        with self.assertRaises(KeyError) as ex:
            self.paint_factory.remove_ingredient('pink', 5)
        expect = "No such ingredient in the factory" # Correct in Judge - Wrong in skeleton
        # expect = "No such product in the factory" # Wrong in Judge - Correct in skeleton
        actual = str(ex.exception.args[0])
        self.assertEqual(expect, actual)

    def test_remove_ingredient__when_ingredient_quantity_is_big_than_stored_quantity__expect_to_raise_exception(self):
        self.paint_factory.add_ingredient('white', 10)
        with self.assertRaises(ValueError) as ex:
            self.paint_factory.remove_ingredient('white', 25)
        expect = "Ingredient quantity cannot be less than zero"
        actual = str(ex.exception)
        self.assertEqual(expect, actual)

    def test_product_property__expect_to_return_ingredients(self):
        self.assertDictEqual({}, self.paint_factory.products)
        self.paint_factory.add_ingredient('white', 10)
        self.assertDictEqual({'white': 10}, self.paint_factory.products)

    def test_can_add__expect_to_return_true(self):
        self.assertTrue(self.paint_factory.can_add(10))

    def test_can_add__expect_to_return_false(self):
        self.assertFalse(self.paint_factory.can_add(11))
Exemplo n.º 29
0
class TestPaintFactory(TestCase):
    def setUp(self):
        self.pf = PaintFactory("Anycomp", 100)

    def test_init(self):
        self.assertEqual("Anycomp", self.pf.name)
        self.assertEqual(100, self.pf.capacity)
        self.assertEqual(["white", "yellow", "blue", "green", "red"],
                         self.pf.valid_ingredients)
        self.assertEqual({}, self.pf.ingredients)

    def test_add_ingredient_valid_product_type_and_quantity_product_type_exist(
            self):
        self.pf.ingredients = {"white": 10}
        self.pf.add_ingredient("white", 20)
        self.assertEqual({"white": 30}, self.pf.ingredients)

    def test_add_ingredient_valid_product_type_and_quantity_product_type_not_exist(
            self):
        self.assertEqual({}, self.pf.ingredients)
        self.pf.add_ingredient("white", 20)
        self.assertEqual({"white": 20}, self.pf.ingredients)

    def test_add_ingredient_insufficient_capacity_raises(self):
        with self.assertRaises(ValueError) as ex:
            self.pf.add_ingredient("white", 150)

        self.assertEqual("Not enough space in factory", str(ex.exception))

    def test_add_ingredient_invalid_ingredients_raises(self):
        with self.assertRaises(TypeError) as ex:
            self.pf.add_ingredient("black", 50)

        self.assertEqual(
            f"Ingredient of type black not allowed in {self.pf.__class__.__name__}",
            str(ex.exception))

    def test_remove_ingredient_if_ingredient_exist_greater_than_zero(self):
        self.pf.ingredients = {"white": 120}
        self.pf.remove_ingredient("white", 20)
        self.assertEqual({"white": 100}, self.pf.ingredients)

    def test_remove_ingredient_if_ingredient_exist_equal_to__zero(self):
        self.pf.ingredients = {"white": 120}
        self.pf.remove_ingredient("white", 120)
        self.assertEqual({"white": 0}, self.pf.ingredients)

    def test_remove_ingredient_if_ingredient_exist_less_than__zero_raises(
            self):
        self.pf.ingredients = {"white": 120}
        with self.assertRaises(ValueError) as ex:
            self.pf.remove_ingredient("white", 130)

        self.assertEqual("Ingredients quantity cannot be less than zero",
                         str(ex.exception))

    def test_remove_ingredient_if_ingredient_not_exist_raises(self):
        with self.assertRaises(KeyError) as ex:
            self.pf.remove_ingredient("white", 130)

        self.assertEqual("'No such ingredient in the factory'",
                         str(ex.exception))

    def test_product_property(self):
        self.pf.ingredients = {"white": 120}
        result = self.pf.products
        self.assertEqual({"white": 120}, result)
class TestPaintFactory(unittest.TestCase):
    def setUp(self) -> None:
        self.factory = PaintFactory('test_name', 2)

    def test_init(self):
        test_factory = PaintFactory('name', 10)
        self.assertEqual(test_factory.name, 'name')
        self.assertEqual(test_factory.capacity, 10)

    def test_add_ingredient_should_increase_quantity(self):
        self.factory.add_ingredient('blue', 1)
        expected_len = 1
        actual_len = len(self.factory.ingredients)

        self.assertEqual(expected_len, actual_len)

        expected_quantity = 1
        actual_quantity = self.factory.ingredients['blue']
        self.assertEqual(expected_quantity, actual_quantity)

    def test_add_ingredient__when_not_valid_ingredient__should_raise(self):
        with self.assertRaises(TypeError) as ex:
            self.factory.add_ingredient('test', 1)

        expected = "Ingredient of type test not allowed in PaintFactory"
        actual = ex.exception.args[0]
        self.assertEqual(expected, actual)

    def test_add_ingredient__when_not_enough_capacity__should_raise(self):
        with self.assertRaises(ValueError) as ex:
            self.factory.add_ingredient('blue', 10)
        expected = "Not enough space in factory"
        actual = ex.exception.args[0]
        self.assertEqual(expected, actual)

    def test_remove_ingredient__should_decrease_quantity(self):
        self.factory.add_ingredient('blue', 2)
        self.factory.remove_ingredient('blue', 1)

        expected_len = 1
        actual_len = self.factory.ingredients['blue']

        self.assertEqual(expected_len, actual_len)

    def test_remove_ingredient__when_missing_in_factory__should_raise(self):
        with self.assertRaises(KeyError) as ex:
            self.factory.remove_ingredient('red', 1)
        expected = "No such product in the factory"
        actual = ex.exception.args[0]
        self.assertEqual(expected, actual)

    def test_remove_ingredient__when_given_quantity_bigger_then_current__should_raise(
            self):
        self.factory.add_ingredient('red', 2)
        with self.assertRaises(ValueError) as ex:
            self.factory.remove_ingredient('red', 20)
        expected = "Ingredient quantity cannot be less than zero"
        actual = ex.exception.args[0]
        self.assertEqual(expected, actual)

    def test_products_should_return_ingredients_in_factory(self):
        self.factory.add_ingredient('red', 2)
        self.factory.add_ingredient('blue', 2)
        self.assertEqual(self.factory.products, {'blue': 2, 'red': 2})