Esempio n. 1
0
 def test_example_one(self):
     warehouses = [{"name":"owd", "inventory":{"apple":5, "orange":10}}, {"name":"dm", "inventory":{"banana":5, "orange":10}}]
     orders = {"apple":5, "banana":5, "orange":5}
     expected = [OrderedDict([('owd', {'apple': 5})]), OrderedDict([('dm', {'banana': 5})]), OrderedDict([('dm', {'orange': 5})])]
     
     allocator = InventoryAllocator(warehouses, orders)
     self.assertEqual(allocator.get_shipments(), expected)
 def test_not_enough_inventory_1(self):
     order = {'apple': 1}
     inventory_distribution = [{'name': 'owd', 'inventory': {'apple': 0}}]
     result = InventoryAllocator(order,
                                 inventory_distribution).fulfill_order()
     expected = []
     self.assertCountEqual(result, expected)
 def test_empty_order_and_inventory(self):
     order = {}
     inventory_distribution = []
     result = InventoryAllocator(order,
                                 inventory_distribution).fulfill_order()
     expected = []
     self.assertCountEqual(result, expected)
 def test_inventory_all_out_of_stock(self):
     order = {'apple': 1}
     inventory_distribution = inventory_distribution = [{
         'name': 'owd',
         'inventory': {
             'apple': 0,
             'banana': 30,
             'pie': 1
         }
     }, {
         'name': 'factory',
         'inventory': {
             'chocolate': 12,
             'pie': 10
         }
     }, {
         'name': 'factory',
         'inventory': {
             'apple': 0,
             'chocolate': 12,
             'pie': 10
         }
     }]
     result = InventoryAllocator(order,
                                 inventory_distribution).fulfill_order()
     expected = []
     self.assertCountEqual(result, expected)
    def test_order_fulfilled_entirely_by_cheapest_warehouse(self):
        order = {'banana': 10}
        inventory_distribution = [{
            'name': 'warehouse1',
            'inventory': {
                'apple': 20,
                'juice': 35
            }
        }, {
            'name': 'warehouse2',
            'inventory': {
                'apple': 50,
                'banana': 30,
            }
        }, {
            'name': 'warehouse3',
            'inventory': {
                'apple': 5,
            }
        }, {
            'name': 'warehouse4',
            'inventory': {
                'apple': 5,
                'strawberry': 1
            }
        }]
        result = InventoryAllocator(order,
                                    inventory_distribution).fulfill_order()

        expected = [{'warehouse2': {'banana': 10}}]
        self.assertCountEqual(result, expected)
 def test_oreder_item_zero_quantity_not_considered(self):
     order = {'banana': 30, 'pie': 13, 'pencil': 0, 'book': 0}
     inventory_distribution = [{
         'name': 'owd',
         'inventory': {
             'apple': 120,
             'banana': 30,
             'pie': 10
         }
     }, {
         'name': 'factory',
         'inventory': {
             'banana': 30,
             'chocolate': 12,
             'pie': 20
         }
     }]
     result = InventoryAllocator(order,
                                 inventory_distribution).fulfill_order()
     expected = [{
         "owd": {
             'banana': 30,
             'pie': 10
         }
     }, {
         "factory": {
             "pie": 3
         }
     }]
     self.assertCountEqual(result, expected)
Esempio n. 7
0
 def test_large_scenario(self):
     warehouses = [{"name":"a", "inventory":{"1":115, "2": 55, "3": 16, "4": 71, "5": 28}},
                   {"name":"b", "inventory":{"1": 82, "2": 34, "3": 74, "4":158, "5": 31}},
                   {"name":"c", "inventory":{"1":172, "2":114, "3": 43, "4": 84, "5":118}},
                   {"name":"d", "inventory":{"1": 15, "2":199, "3":161, "4":154, "5":154}}, 
                   {"name":"e", "inventory":{"1":114, "2": 89, "3": 15, "4":189, "5":152}},
                   {"name":"f", "inventory":{"1": 36, "2":131, "3": 84, "4":103, "5": 63}},
                   {"name":"g", "inventory":{"1": 37, "2":156, "3":133, "4": 51, "5": 34}},
                   {"name":"h", "inventory":{"1":197, "2": 75, "3": 97, "4": 44, "5": 85}},
                   {"name":"i", "inventory":{"1":189, "2":85, "3":179, "4":82, "5":122}},
                   {"name":"j", "inventory":{"1":187, "2":65, "3":78, "4":152, "5":146}}]
     orders = {"1":1000, "2": 1000, "3": 1000, "4": 1000, "5": 1000}
     expected = [OrderedDict([('a', {'1': 115}), ('b', {'1': 26}), ('c', {'1': 172}), ('e', {'1': 114}), ('h', {'1': 197}), ('i', {'1': 189}), ('j', {'1': 187})]), 
                 OrderedDict([('a', {'2': 55}), ('b', {'2': 31}), ('c', {'2': 114}), ('d', {'2': 199}), ('e', {'2': 89}), ('f', {'2': 131}), ('g', {'2': 156}), 
                              ('h', {'2': 75}), ('i', {'2': 85}), ('j', {'2': 65})]), 
                 OrderedDict([('a', {'4': 71}), ('b', {'4': 158}), ('c', {'4': 84}), ('d', {'4': 154}), ('e', {'4': 189}), ('f', {'4': 103}), ('g', {'4': 7}), 
                              ('i', {'4': 82}), ('j', {'4': 152})])]
     allocator = InventoryAllocator(warehouses, orders)
     self.assertEqual(allocator.get_shipments(), expected)
    def test_skip_unused_warehouse(self):
        order = {'strawberry': 3, 'juice': 35, 'apple': 25}
        inventory_distribution = [{
            'name': 'warehouse1',
            'inventory': {
                'apple': 20,
                'juice': 35
            }
        }, {
            'name': 'warehouse2',
            'inventory': {
                'apple': 50,
                'banana': 30,
                'pear': 12,
                'strawberry': 2
            }
        }, {
            'name': 'warehouse3',
            'inventory': {
                'apple': 5,
            }
        }, {
            'name': 'warehouse4',
            'inventory': {
                'apple': 5,
                'strawberry': 1
            }
        }]
        result = InventoryAllocator(order,
                                    inventory_distribution).fulfill_order()

        # the order takes nothing from warehouse3, expect warehouse3 to not show up
        # in the shipping results
        expected = [{
            'warehouse1': {
                'juice': 35,
                'apple': 20
            }
        }, {
            'warehouse2': {
                'strawberry': 2,
                'apple': 5
            }
        }, {
            'warehouse4': {
                'strawberry': 1
            }
        }]
        self.assertCountEqual(result, expected)
 def test_missing_order_items(self):
     order = {'orange': 3}
     inventory_distribution = [{
         'name': 'warehouse1',
         'inventory': {
             'pineapple': 3
         }
     }, {
         'name': 'warehouse2',
         'inventory': {
             'pear': 2
         }
     }]
     result = InventoryAllocator(order,
                                 inventory_distribution).fulfill_order()
     expected = []
     self.assertCountEqual(result, expected)
 def test_order_allocated_from_different_warehouses(self):
     order = {'apple': 10}
     inventory_distribution = [
         {
             'name': 'owd',
             'inventory': {
                 'apple': 5
             }
         },
         {
             'name': 'dm',
             'inventory': {
                 'apple': 5
             }
         },
     ]
     result = InventoryAllocator(order,
                                 inventory_distribution).fulfill_order()
     expected = [{"dm": {'apple': 5}}, {"owd": {"apple": 5}}]
     self.assertCountEqual(result, expected)
    def test_complex_order(self):
        order = {'apple': 31, 'banana': 4, 'pie': 10, 'meat': 1}
        inventory_distribution = [{
            'name': 'warehouse1',
            'inventory': {
                'meat': 0,
                'apple': 17,
                'pear': 3,
                'peach': 9,
                'pie': 5
            }
        }, {
            'name': 'warehouse2',
            'inventory': {
                'box': 5,
                'onion': 14,
                'pie': 4,
                'banana': 5,
                'meat': 0,
                'strawberry': 2
            }
        }, {
            'name': 'warehouse3',
            'inventory': {
                'apple': 500,
                'meat': 0,
                'banna': 13,
                'pie': 1
            }
        }, {
            'name': 'warehouse4',
            'inventory': {
                'apple': 5,
                'strawberry': 1,
                'meat': 1
            }
        }]
        result = InventoryAllocator(order,
                                    inventory_distribution).fulfill_order()

        expected = [{
            'warehouse1': {
                'apple': 17,
                'pie': 5
            }
        }, {
            'warehouse2': {
                'pie': 4,
                'banana': 4
            }
        }, {
            'warehouse3': {
                'apple': 14,
                'pie': 1
            }
        }, {
            'warehouse4': {
                'meat': 1
            }
        }]
        self.assertCountEqual(result, expected)
Esempio n. 12
0
                      {"name":"j", "inventory":{"1":187, "2":65, "3":78, "4":152, "5":146}}]
        orders = {"1":1000, "2": 1000, "3": 1000, "4": 1000, "5": 1000}
        expected = [OrderedDict([('a', {'1': 115}), ('b', {'1': 26}), ('c', {'1': 172}), ('e', {'1': 114}), ('h', {'1': 197}), ('i', {'1': 189}), ('j', {'1': 187})]), 
                    OrderedDict([('a', {'2': 55}), ('b', {'2': 31}), ('c', {'2': 114}), ('d', {'2': 199}), ('e', {'2': 89}), ('f', {'2': 131}), ('g', {'2': 156}), 
                                 ('h', {'2': 75}), ('i', {'2': 85}), ('j', {'2': 65})]), 
                    OrderedDict([('a', {'4': 71}), ('b', {'4': 158}), ('c', {'4': 84}), ('d', {'4': 154}), ('e', {'4': 189}), ('f', {'4': 103}), ('g', {'4': 7}), 
                                 ('i', {'4': 82}), ('j', {'4': 152})])]
        allocator = InventoryAllocator(warehouses, orders)
        self.assertEqual(allocator.get_shipments(), expected)


# gives the user the ability to run either some unit tests or custom tests
# note that the custom tests don't take in an expected value
if __name__ == '__main__':
    while True:
        response = input("Would you like to run tests? Type 'Y' for \"Yes\" or type 'N' for \"No\": ")
        if response.upper() == 'N':
            break
        elif response.upper() == 'Y':
            response = input("Would you like to run the unit tests? Type 'Y' for \"Yes\" or type 'N' for \"No\": ")
            if response.upper() == 'N':
                warehouses = loads(input("Enter the list representation of your warehouses (with brackets): "))
                orders = loads(input("Enter the hashmap representation of your orders (with brackets): "))
                allocator = InventoryAllocator(warehouses, orders)
                print("shipments: {}".format(allocator.get_shipments()))
            elif response.upper() == 'Y':
                unittest.main()
            else:
                print("INVALID INPUT: You chose {}, but the only valid inputs are 'Y' or 'N'. Please try again.".format(response))
        else:
            print("INVALID INPUT: You chose {}, but the only valid inputs are 'Y' or 'N'. Please try again.".format(response))