Exemplo n.º 1
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self):
        self.factory = PaintFactory('a', 10)

    def test_constructor(self):
        self.assertEqual(self.factory.name, 'a')
        self.assertEqual(self.factory.capacity, 10)
        self.assertEqual(self.factory.valid_ingredients, ["white", "yellow", "blue", "green", "red"])
        self.assertEqual(self.factory.ingredients, {})

    def test_add_invalid_ingredient(self):
        with self.assertRaises(TypeError) as exc:
            self.factory.add_ingredient('brrr', 1)

    def test_add_quantity_above_capacity(self):
        with self.assertRaises(ValueError) as exc:
            self.factory.add_ingredient('white', 100)


    def test_ingredient_not_ingredientsdict_add_it_to_dict_then_add_quantity(self):
        self.factory.add_ingredient('white', 1)
        self.assertEqual(self.factory.ingredients['white'], 1)

    def test_remove_ingredient_cannot_be_less_than_zero(self):
        self.factory.add_ingredient('white', 1)
        with self.assertRaises(ValueError) as exc:
            self.factory.remove_ingredient('white', 6)


    def test_remove_ingredient_works_as_expected(self):
        self.factory.add_ingredient('white', 1)
        self.factory.remove_ingredient('white', 1)
        self.assertEqual(0, self.factory.ingredients['white'])

    def test_products_return_ingredients_dict(self):
        self.assertEqual(self.factory.products, self.factory.ingredients)

    def test_can_add(self):
        self.assertEqual(self.factory.can_add(1), True)
        self.factory.add_ingredient('white', 10)
        self.assertEqual(self.factory.can_add(1), False)
Exemplo n.º 2
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))
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.º 4
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.º 5
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.º 6
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.º 7
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})
class Test(unittest.TestCase):
    def setUp(self):
        self.paint_factory = PaintFactory("Colors_factory", 5)

    def test__init__(self):
        self.assertEqual(self.paint_factory.name, "Colors_factory")
        self.assertEqual(self.paint_factory.capacity, 5)
        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_valid_ingredients_raise_type_error(self):
        with self.assertRaises(TypeError) as exc:
            self.paint_factory.add_ingredient("brown", 5)
        msg = f"Ingredient of type brown not allowed in PaintFactory"
        self.assertEqual(str(exc.exception), msg)

    def test_add_ingredient_ingredient_quantity_exceeds_the_paint_factory_capacity_raise_value_error(self):
        with self.assertRaises(ValueError) as exc:
            self.paint_factory.add_ingredient("yellow", 6)
        msg = "Not enough space in factory"
        self.assertEqual(str(exc.exception), msg)

    def test_can_add_method(self):
        self.paint_factory.add_ingredient("blue", 3)
        self.assertEqual(self.paint_factory.can_add(3), False)

        # with self.assertRaises(ValueError) as exc:
        #     self.paint_factory.can_add(3)
        # self.assertEqual(str(exc.exception), "Not enough space in factory")

    def test_add_ingredient_to_the_dict_successfully_ingredient_not_in_the_dict(self):
        self.paint_factory.add_ingredient("yellow", 5)
        self.assertEqual(self.paint_factory.ingredients, {"yellow": 5})

    def test_add_ingredient_to_the_dict_successfully_ingredient_already_in_the_dict(self):
        self.paint_factory.add_ingredient("blue", 3)
        self.paint_factory.add_ingredient("blue", 2)
        self.assertEqual(self.paint_factory.ingredients, {"blue": 5})

    def test_remove_ingredient_from_the_dict_ingredient_not_in_the_dict_raises_key_error(self):
        self.paint_factory.add_ingredient("blue", 3)
        self.paint_factory.add_ingredient("red", 2)
        with self.assertRaises(KeyError) as exc:
            self.paint_factory.remove_ingredient("yellow", 4)
        msg = "No such product in the factory"
        self.assertEqual(msg, "No such product in the factory")

    def test_remove_ingredient_from_the_dict_divide_ingredient_quantity(self):
        self.paint_factory.add_ingredient("red", 3)
        self.paint_factory.remove_ingredient("red", 2)
        self.assertEqual(self.paint_factory.ingredients, {"red": 1})

    def test_remove_ingredient_raises_value_error_quantity_cannot_be_less_than_zero(self):
        self.paint_factory.add_ingredient("red", 3)
        with self.assertRaises(ValueError) as exc:
            self.paint_factory.remove_ingredient("red", 4)
        self.assertEqual(str(exc.exception), "Ingredients quantity cannot be less than zero")

    def test_products_property(self):
        self.paint_factory.add_ingredient("blue", 3)
        self.paint_factory.add_ingredient("red", 2)
        self.assertEqual(self.paint_factory.products, {"blue": 3, "red": 2})
Exemplo n.º 9
0
class TestPaintFactory(unittest.TestCase):
    def setUp(self):
        self.paint = PaintFactory('Paint', 100)

    def test_all_set_up(self):
        self.assertEqual(self.paint.name, 'Paint')
        self.assertEqual(self.paint.capacity, 100)
        self.assertEqual(self.paint.valid_ingredients,
                         ["white", "yellow", "blue", "green", "red"])
        self.assertEqual(self.paint.ingredients, {})

    def test_ingredianet_not_in_default(self):
        self.assertTrue('black' not in self.paint.valid_ingredients)

    def test_ingrediant_in_default(self):
        self.assertTrue('yellow' in self.paint.valid_ingredients)

    def test_add_ingrediant_when_not_valid_type_should_raise_error(self):
        with self.assertRaises(TypeError) as ex:
            self.paint.add_ingredient('black', 100)
        self.assertEqual(
            str(ex.exception),
            "Ingredient of type black not allowed in PaintFactory")

    def test_add_ingrediant_when_capacity_less_then_quantity_should_raise_error(
            self):
        with self.assertRaises(ValueError) as ex:
            self.paint.add_ingredient('white', 150)
        self.assertEqual(str(ex.exception), "Not enough space in factory")

    def test_add_ingrediant_not_in_ing_type_add_to_dict_with_tpes(self):
        self.paint.add_ingredient('white', 50)
        self.assertEqual(self.paint.ingredients['white'], 50)

    def test_add_to_ingrediant_secod_type(self):
        self.paint.add_ingredient('white', 50)
        self.paint.add_ingredient('white', 50)
        self.assertEqual(self.paint.ingredients['white'], 100)

    def test_remove_ingrediant_when_type_not_in_keys_should_raise_error(self):
        with self.assertRaises(KeyError) as ex:
            self.paint.remove_ingredient('white', 50)
        self.assertEqual(str(ex.exception), "'No such product in the factory'")

    def test_remove_ingrediant_succesfully_reduce_quntity(self):
        self.paint.add_ingredient('white', 50)
        self.paint.remove_ingredient('white', 50)
        self.assertEqual(self.paint.ingredients['white'], 0)

    def test_remove_ingrediant_with_big_quantity_should_raise_error(self):
        self.paint.add_ingredient('white', 50)
        with self.assertRaises(ValueError) as ex:
            self.paint.remove_ingredient('white', 60)
        self.assertEqual(str(ex.exception),
                         "Ingredient quantity cannot be less than zero")

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

    def test_product(self):
        self.assertEqual(self.paint.ingredients, {})
        self.paint.add_ingredient('white', 50)
        self.assertEqual(self.paint.products, {'white': 50})

    def test_cann_add(self):
        self.paint.add_ingredient('white', 90)
        actual = self.paint.can_add(20)
        self.assertEqual(actual, False)

    def test_cannot_instnces(self):
        with self.assertRaises(TypeError):
            Factory('test', 100)

    def test_add_ingrediant_is_abstract(self):
        with self.assertRaises(TypeError):
            Factory('test', 100).add_ingredient('white', 10)

    def test_repr(self):
        self.paint.add_ingredient('white', 50)
        self.assertEqual(
            str(self.paint),
            'Factory name: Paint with capacity 100.\nwhite: 50\n')

    def test_implement_all_abstract(self):
        self.assertNotEqual(self.paint, 'TypeError:')

    def test_add_multi(self):
        self.paint.add_ingredient('white', 50)
        self.paint.add_ingredient('white', 50)
        self.paint.add_ingredient('white', 50)
        self.assertEqual(self.paint.ingredients['white'], 150)
Exemplo n.º 10
0
 def test_2(self):
     pfac = PaintFactory('pfac', 1)
     self.assertEqual(pfac.can_add(2), False)
Exemplo n.º 11
0
 def test_1(self):
     pfac = PaintFactory('pfac', 3)
     self.assertEqual(pfac.can_add(1), True)