def load(self): alpha = ['A', 'B', 'C', 'D'] self.warehouseObjLoad = Warehouse( self.data["warehouse"][0]["filename"], (self.data["warehouse"][0]["height"], self.data["warehouse"][0]["width"])) self.warehouseObjLoad.itemCount = self.data["warehouse"][0][ "itemCount"] self.warehouseObjLoad.storageCap = self.data["warehouse"][0][ "storageCap"] for i in range(0, 4): for j in range(1, 5): index = alpha[i] + str(j) itemList = self.data["warehouse"][0]["storageLocation"][index][ "items"] for k in range(len(itemList)): self.warehouseObjLoad.addItem( (itemList[k]["dimensions"][0], itemList[k]["dimensions"][1]), itemList[k]["name"], itemList[k]["description"]) return self.warehouseObjLoad
def setUp(self): # empty warehouse self.wh_1 = Warehouse("wh_1", (1000, 1000)) # warehouse with 1 item self.item_1 = Item(0, (40000, 1), "Boombox", "loud", (0, 0)) self.item_2 = Item(1, (100, 1), "Guitar", "6-string", (0, 1)) self.wh_2 = Warehouse("wh_2", (1000, 1000), items=2, id=2) self.customMatrix = [[StorageSpace(self.wh_2.storageCap, {0: self.item_1}), StorageSpace(self.wh_2.storageCap, {1: self.item_2}), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())], [StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())], [StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())], [StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())]] self.customMatrix[0][0].remainingArea = 0 self.customMatrix[0][0].spaceLeft = False self.customMatrix[0][1].category = "Instruments" self.wh_2.spaceMatrix = self.customMatrix pass
class database: data = {} newData = {} warehouseList = {} nextUniqueID = 0 def __init__(self, filename): if path.exists(filename + ".json"): with open(filename + '.json') as json_file: self.data = json.load(json_file) else: with open(filename + '.json', 'a+') as json_file: json.dump({"warehouse": []}, json_file, indent=4, sort_keys=True) json_file.close() with open(filename + '.json') as json_file: self.data = json.load(json_file) json_file.close() with open('warehouselist.json') as json_file: self.warehouseList = json.load(json_file) json_file.close() def save(self, warehouse): print("Writing to Database....") self.warehouseObj = warehouse self.newData = {u'warehouse': []} self.add_warehouse(self.warehouseObj) with open(self.warehouseObj.filename + '.json', 'w') as json_file: json.dump(self.newData, json_file, indent=4, sort_keys=True) with open('warehouseList.json', 'w+') as json_file: json.dump(self.warehouseList, json_file, indent=4, sort_keys=True) def load(self): alpha = ['A', 'B', 'C', 'D'] self.warehouseObjLoad = Warehouse( self.data["warehouse"][0]["filename"], (self.data["warehouse"][0]["height"], self.data["warehouse"][0]["width"])) self.warehouseObjLoad.itemCount = self.data["warehouse"][0][ "itemCount"] self.warehouseObjLoad.storageCap = self.data["warehouse"][0][ "storageCap"] for i in range(0, 4): for j in range(1, 5): index = alpha[i] + str(j) itemList = self.data["warehouse"][0]["storageLocation"][index][ "items"] for k in range(len(itemList)): self.warehouseObjLoad.addItem( (itemList[k]["dimensions"][0], itemList[k]["dimensions"][1]), itemList[k]["name"], itemList[k]["description"]) return self.warehouseObjLoad def add_warehouse(self, warehouse): print("Creating Warehouse....") duplicate = False if len(self.warehouseList["warehouseList"]) == 0: print("I DID THIS") self.warehouseList["warehouseList"].append(warehouse.filename) else: for i in self.warehouseList["warehouseList"]: if i == warehouse.filename: duplicate = True if duplicate == False: self.warehouseList["warehouseList"].append(warehouse.filename) alpha = ['A', 'B', 'C', 'D'] self.newData["warehouse"].append({ "filename": warehouse.filename, "height": warehouse.dimensions[0], "width": warehouse.dimensions[1], "storageCap": warehouse.storageCap, "itemCount": warehouse.itemCount, "storageLocation": {}, "nextUniqueID": warehouse.nextUniqueID }) for i in range(0, 4): for j in range(1, 5): self.newData["warehouse"][0]["storageLocation"].update({ alpha[i] + str(j): { "remainingArea": warehouse.spaceMatrix[i][j - 1].remainingArea, "category": warehouse.spaceMatrix[i][j - 1].category, "items": [], "spaceLeft": warehouse.spaceMatrix[i][j - 1].spaceLeft } }) newItemList = warehouse.spaceMatrix[i][j - 1].getAllItems() index = alpha[i] + str(j) for k in range(len(newItemList)): self.newData["warehouse"][0]["storageLocation"][index][ "items"].append({ "itemID": newItemList[k].itemID, "dimensions": newItemList[k].dimensions, "name": newItemList[k].name, "description": newItemList[k].description }) def get_warehouse_list(self): list = [] for i in self.warehouseList["warehouseList"]: list.append(i) return list
class TestWarehouse(unittest.TestCase): def setUp(self): # empty warehouse self.wh_1 = Warehouse("wh_1", (1000, 1000)) # warehouse with 1 item self.item_1 = Item(0, (40000, 1), "Boombox", "loud", (0, 0)) self.item_2 = Item(1, (100, 1), "Guitar", "6-string", (0, 1)) self.wh_2 = Warehouse("wh_2", (1000, 1000), items=2, id=2) self.customMatrix = [[StorageSpace(self.wh_2.storageCap, {0: self.item_1}), StorageSpace(self.wh_2.storageCap, {1: self.item_2}), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())], [StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())], [StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())], [StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict()), StorageSpace(self.wh_2.storageCap, dict())]] self.customMatrix[0][0].remainingArea = 0 self.customMatrix[0][0].spaceLeft = False self.customMatrix[0][1].category = "Instruments" self.wh_2.spaceMatrix = self.customMatrix pass def test_findAvailableSpot(self): self.assertIsNone(self.wh_1.findAvailableSpot((40001, 1))) self.assertEqual(self.wh_1.findAvailableSpot((40000, 1)), [0, 0]) self.assertEqual(self.wh_2.findAvailableSpot((1, 1)), [0, 2]) self.wh_2.spaceMatrix[0][2].category = "Games" self.assertEqual(self.wh_2.findAvailableSpot((1, 1)), [0, 3]) self.assertEqual(self.wh_2.findAvailableSpot((1, 1), "Instruments"), [0, 1]) self.assertEqual(self.wh_2.findAvailableSpot((1, 1), "Random"), [0, 3]) def test_addItem(self): self.wh_1.addItem((40000, 1), "Boombox", "loud", None) self.assertEqual(len(self.wh_1.spaceMatrix[0][0].itemList), 1) self.assertEqual(self.wh_1.spaceMatrix[0][0].itemList[0].name, "Boombox") self.assertEqual(self.wh_1.nextUniqueID, 1) self.assertEqual(self.wh_1.itemCount, 1) def test_removeItem(self): self.wh_2.removeItem(0) self.assertEqual(len(self.wh_2.spaceMatrix[0][0].itemList), 0) self.assertEqual(self.wh_2.nextUniqueID, 2) self.assertEqual(self.wh_2.itemCount, 1) def test_searchByID(self): self.assertEqual(self.wh_2.searchByID(0), self.item_1) self.assertIsNone(self.wh_2.searchByID(5)) def test_searchByName(self): self.assertEqual(self.wh_2.searchByName("Boombox"), [[self.item_1]]) self.assertIsNone(self.wh_2.searchByName("Yoyo")) def test_searchByCategory(self): self.assertEqual(self.wh_2.searchByCategory("Instruments"), [[0, 1]]) self.assertIsNone(self.wh_2.searchByCategory("Computers"))