Exemplo n.º 1
0
class WarehouseTests(unittest.TestCase):
    def setUp(self):
        self.warehouse = Warehouse()
        self.warehouse.add('Glenlivit', 10)

    def test_warehouse_shows_new_inventory(self):
        self.assertEqual(self.warehouse.get_inventory('Glenlivit'), 10)

    def test_warehouse_shows_added_inventory(self):
        self.warehouse.add('Glenlivit', 15)
        self.assertEqual(self.warehouse.get_inventory('Glenlivit'), 25)

    def test_warehouse_shows_removed_inventory(self):
        self.warehouse.remove('Glenlivit', 10)
        self.assertEqual(self.warehouse.get_inventory('Glenlivit'), 0)
Exemplo n.º 2
0
class WarehouseTests(unittest.TestCase):

    def setUp(self):
        self.warehouse = Warehouse()
        self.warehouse.add('Glenlivit', 10)

    def test_warehouse_shows_new_inventory(self):
       self.assertEqual(self.warehouse.get_inventory('Glenlivit'), 10)

    def test_warehouse_shows_added_inventory(self):
        self.warehouse.add('Glenlivit', 15)
        self.assertEqual(self.warehouse.get_inventory('Glenlivit'), 25)

    def test_warehouse_shows_removed_inventory(self):
        self.warehouse.remove('Glenlivit', 10)
        self.assertEqual(self.warehouse.get_inventory('Glenlivit'), 0)
Exemplo n.º 3
0
class OrderTests(unittest.TestCase):
    def setUp(self):
        self.warehouse = Warehouse()
        self.warehouse.add(TALISKER, 50)
        self.warehouse.add(HIGHLAND_PARK, 25)

    def test_order_is_filled_if_enough_in_warehouse(self):
        order = Order(TALISKER, 50)
        order.fill(self.warehouse)
        self.assertTrue(order.is_filled())
        self.assertEqual(self.warehouse.get_inventory(TALISKER), 0)

    def test_order_does_not_remove_if_not_enough(self):
        order = Order(TALISKER, 51)
        order.fill(self.warehouse)
        self.assertFalse(order.is_filled())
        self.assertEqual(self.warehouse.get_inventory(TALISKER), 50)
Exemplo n.º 4
0
class OrderTests(unittest.TestCase):

    def setUp(self):
        self.warehouse = Warehouse()
        self.warehouse.add(TALISKER, 50)
        self.warehouse.add(HIGHLAND_PARK, 25)

    def test_order_is_filled_if_enough_in_warehouse(self):
        order = Order(TALISKER, 50)
        order.fill(self.warehouse)
        self.assertTrue(order.is_filled())
        self.assertEqual(self.warehouse.get_inventory(TALISKER), 0)

    def test_order_does_not_remove_if_not_enough(self):
        order = Order(TALISKER, 51)
        order.fill(self.warehouse)
        self.assertFalse(order.is_filled())
        self.assertEqual(self.warehouse.get_inventory(TALISKER), 50)
Exemplo n.º 5
0
class Player:
    # This class contains all player data.
    # it is just a wrapper around warehouse class.
    def __init__(self, configuration):
        self._ressources = Ressources(configuration)

        # build a dict with all keys
        # contains all units available
        self._units = Warehouse("units", configuration)

        #contains all defenses available
        self._defenses = Warehouse("defenses", configuration)

        # define tech available
        self._tech = Warehouse("tech", configuration)

        # mail box
        # @TODO
        self._mail_box = []

    def check_min_ressources(self, values):
        # from a list a ressource values, return True if ressources are available
        self._ressources.is_available(values)

    def check_min_units(self, units):
        # check a min quantity of units
        return self._units.check_min(units)

    def check_min_tech(self, tech):
        # check a min tech level
        return  self._tech.check_min(tech)

    def get_ressources(self):
        return zip(self._ressources.get_value(), self._ressources.get_delta())

    def get_units(self):
        return self._units.get()

    def get_defenses(self):
        return self._defenses.get()

    def get_tech(self):
        return self._tech.get()

    def add_units(self, units):
        self._units.add(units)

    def add_defenses(self, defenses):
        self._defenses.add(defenses)

    def inc_tech(self, tech_name):
        self._tech.add({tech_name:1})

    def sub_units(self, units):
        self._units.sub(units)

    def sub_defenses(self, defenses):
        self._defenses.sub(defenses)
class BasicTest(unittest.TestCase):
    """ A set of tests for the basic required functionality.
    """
    def setUp(self):
        """ Create some objects: one Warehouse, one Shelf, three of each
            Container type (Bin, Box, Bag), and three each of four different
            sizes of Item.
        """
        self.warehouse = Warehouse()
        self.shelf = Shelf()
        self.bin1, self.bin2, self.bin3 = [Bin() for _ in range(3)]
        self.box1, self.box2, self.box3 = [Box() for _ in range(3)]
        self.bag1, self.bag2, self.bag3 = [Bag() for _ in range(3)]

        self.itemS1, self.itemS2, self.itemS3 = [Item(1) for _ in range(3)]
        self.itemM1, self.itemM2, self.itemM3 = [Item(6) for _ in range(3)]
        self.itemL1, self.itemL2, self.itemL3 = [Item(10) for _ in range(3)]
        self.itemXL1, self.itemXL2, self.itemXL3 = [Item(15) for _ in range(3)]

    def test_structure(self):
        """ Test basic ability to place Containers within other Containers.
        """
        # A Warehouse can contain a Shelf
        self.assertTrue(self.warehouse.add(self.shelf))
        self.assertTrue(self.warehouse.contains(self.shelf))

        # Shelves can contain Bins
        self.assertTrue(self.shelf.add(self.bin1))
        self.assertTrue(self.shelf.contains(self.bin1))
        self.assertTrue(self.shelf.add(self.bin2))
        self.assertTrue(self.shelf.contains(self.bin2))

        # Bins can contain Boxes
        self.assertTrue(self.bin1.add(self.box1))
        self.assertTrue(self.bin1.contains(self.box1))
        self.assertTrue(self.bin1.add(self.box2))
        self.assertTrue(self.bin1.contains(self.box2))

        # Boxes can contain Bags
        self.assertTrue(self.box1.add(self.bag1))
        self.assertTrue(self.box1.contains(self.bag1))
        self.assertTrue(self.box1.add(self.bag2))
        self.assertTrue(self.box1.contains(self.bag2))

        # A Shelf "contains" nested Containers within it
        self.assertTrue(self.shelf.contains(self.bin1))
        self.assertTrue(self.shelf.contains(self.box1))
        self.assertTrue(self.shelf.contains(self.box2))
        self.assertTrue(self.shelf.contains(self.bag1))
        self.assertTrue(self.shelf.contains(self.bag2))
        self.assertFalse(self.shelf.contains(self.box3))

    def test_contents(self):
        """ Test the ability to add Items to Containers and find their
            contents.
        """
        # Reuse the work done by `test_structure()` to set up hierarchy
        self.test_structure()

        # Bins and Boxes can contain Items
        self.assertTrue(self.bin2.add(self.itemS1))
        self.assertTrue(self.bin2.contains(self.itemS1))
        self.assertTrue(self.box1.add(self.itemS2))
        self.assertTrue(self.box1.contains(self.itemS2))

        # Containers "contain" Items in nested Containers within them
        self.assertTrue(self.bin1.contains(self.itemS2))
        self.assertTrue(self.shelf.contains(self.itemS1))
        self.assertTrue(self.shelf.contains(self.itemS2))

    def test_limits(self):
        """ Test capacity limits.
        """
        # Containers can only hold smaller Containers, not the same or larger.
        # Bins cannot contain Bins
        self.assertFalse(self.bin1.add(self.bin3))
        self.assertFalse(self.bin1.contains(self.bin3))

        # Boxes cannot contain Boxes
        self.assertFalse(self.box1.add(self.box3))
        self.assertFalse(self.box1.contains(self.box3))

        # Bags cannot contain Bags
        self.assertFalse(self.bag1.add(self.bag3))
        self.assertFalse(self.bag1.contains(self.bag3))

        # Containers cannot contain Items that are too large
        self.assertFalse(self.bag1.add(self.itemM1))
        self.assertFalse(self.bag1.contains(self.itemM1))
        self.assertFalse(self.box1.add(self.itemL1))
        self.assertFalse(self.box1.contains(self.itemL1))
        self.assertFalse(self.bin1.add(self.itemXL1))
        self.assertFalse(self.bin1.contains(self.itemXL1))

        # bin1 should be empty now, otherwie the next assertions will fail
        self.assertEqual(len(self.bin1), 0)

        # Bins can't fit more than two Boxes
        self.assertTrue(self.bin1.add(self.box1))
        self.assertTrue(self.bin1.add(self.box2))
        self.assertFalse(self.bin1.add(self.box3))
        self.assertFalse(self.bin1.contains(self.box3))

    def test_remove(self):
        """ Test item removal.
        """
        # Add some items to remove.
        self.assertTrue(self.box1.add(self.itemS1))
        self.assertTrue(self.box1.add(self.itemS2))
        self.assertTrue(self.box1.add(self.itemS3))

        # Test removal mode 1: no arguments, removes last added object.
        self.assertIs(self.box1.remove(), self.itemS3)
        self.assertFalse(self.box1.contains(self.itemS3))

        # Test removal mode 2: removing a specific object.
        self.assertIs(self.box1.remove(self.itemS1), self.itemS1)
        self.assertFalse(self.box1.contains(self.itemS1))

        # Check that itemS2 is still there.
        self.assertTrue(self.box1.contains(self.itemS2))

    def test_pack(self):
        """ Test the `pack()` method (puts an object in the first nested
            container with space for it).
        """
        # An oddly-sized, space-filling Item
        fillerItem = Item(5)

        # Add a Box to a Bin; make the Bin too full to add another Item
        self.assertTrue(self.bin1.add(self.box1))
        self.assertTrue(self.bin1.add(fillerItem))

        # self.itemS1 should get packed into the Box inside the Bin
        self.assertTrue(self.bin1.pack(self.itemS1))
        self.assertTrue(self.box1.contains(self.itemS1))

    def test_shelfConstraint(self):
        """ Test that a Shelf can only contain Items larger than 7
            volume units. Containers smaller than 7 units are allowed.
        """
        # Shelves can contain Containers smaller than 7 units, but not Items.
        self.assertTrue(self.shelf.add(self.bag1))

        # Shelves cannot contain items smaller than 7 units.
        self.assertTrue(self.shelf.add(self.itemXL1))
        self.assertTrue(self.shelf.add(self.itemL1))
        self.assertFalse(self.shelf.add(self.itemM1))
        self.assertFalse(self.shelf.add(self.itemS1))

        # Shelves can contain Containers with smaller Items.
        self.assertTrue(self.bin1.add(self.itemS1))
        self.assertTrue(self.shelf.add(self.bin1))

    def test_exclusivity(self):
        """ Test that moving an Item from one Container to another
            automatically removes it from the first Container.
        """
        self.assertTrue(self.bin1.add(self.itemS1))
        self.assertTrue(self.bin1.contains(self.itemS1))
        self.assertFalse(self.bin2.contains(self.itemS1))

        self.assertTrue(self.bin2.add(self.itemS1))
        self.assertTrue(self.bin2.contains(self.itemS1))
        self.assertFalse(self.bin1.contains(self.itemS1))

    def test_extract(self):
        """ Test the `extract()` method, which removes an item from
            a Container or any Container within it.
        """
        self.assertTrue(self.warehouse.add(self.shelf))
        self.assertTrue(self.shelf.add(self.bin1))
        self.assertTrue(self.bin1.add(self.box1))
        self.assertTrue(self.box1.add(self.bag1))

        self.assertTrue(self.bag1.add(self.itemS1))
        self.assertTrue(self.bag1.add(self.itemS2))

        self.assertIsNotNone(self.bag1.extract(self.itemS1))
        self.assertIsNotNone(self.warehouse.extract(self.itemS2))

        # Make sure they've actually been removed
        self.assertIsNone(self.bag1.extract(self.itemS1))
        self.assertIsNone(self.warehouse.extract(self.itemS2))