def test_find_by_quantity(self): inventory = Inventory(product_id=333, condition="new", quantity=4, restock_level=10, available=1) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() inventory = Inventory(product_id=444, condition="new", quantity=2, restock_level=10, available=0) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() inventories = Inventory.find_by_quantity(2) self.assertEqual(len(list(inventories)), 2)
def post(self): """ Creates a Inventory This endpoint will create a Inventory based the data in the body that is posted """ try: app.logger.info("Request to create an Inventory record") inventory = Inventory() inventory.deserialize(api.payload) inventory.validate_data() if Inventory.find_by_product_id_condition( api.payload[keys.KEY_PID], api.payload[keys.KEY_CND]): api.abort( status.HTTP_409_CONFLICT, "Inventory with ({}, {})".format(inventory.product_id, inventory.condition)) inventory.create() location_url = api.url_for(InventoryResource, product_id=inventory.product_id, condition=inventory.condition, _external=True) app.logger.info("Inventory ({}, {}) created."\ .format(inventory.product_id, inventory.condition)) return inventory.serialize(), status.HTTP_201_CREATED, { 'Location': location_url } except DataValidationError as err: api.abort(status.HTTP_400_BAD_REQUEST, err)
def test_deserialize_bad_data(self): """ Test deserialization of bad data """ data = "this is not a dictionary" inventory = Inventory() self.assertRaises(DataValidationError, inventory.deserialize, data) data = {} self.assertRaises(DataValidationError, inventory.deserialize, data)
def test_find_by_product_id_condition(self): """Find an Inventory by product_id and condition""" inventory = Inventory(product_id=555, condition="new", quantity=1, restock_level=10, available=1) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() inventory = Inventory(product_id=666, condition="new", quantity=1, restock_level=10, available=0) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() result = Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition) self.assertIsNot(result, None) self.assertEqual(result.product_id, 666) self.assertEqual(result.condition, "new") self.assertEqual(result.quantity, 1) self.assertEqual(result.restock_level, 10) self.assertEqual(result.available, 0)
def test_delete(self): """Delete an Inventory""" inventory = Inventory(product_id=777, condition="new", quantity=1, restock_level=10, available=1) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() self.assertEqual(len(Inventory.find_all()), 1) inventory.delete() self.assertEqual(len(Inventory.find_all()), 0)
def test_update(self): """Update an Inventory""" inventory = Inventory(product_id=666, condition="new", quantity=1, restock_level=10, available=1) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() inventory.product_id = 667 inventory.update() inventories = Inventory.find_all() self.assertEqual(len(inventories), 1) self.assertEqual(inventories[0].product_id, 667)
def call_create(self,pid,cnd,qty,lvl,avl,err): inventory = Inventory(product_id=pid, condition=cnd, quantity=qty, restock_level=lvl, available=avl) if err==1: self.assertRaises(DataValidationError, inventory.validate_data) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() self.assertTrue(inventory is not None) self.assertTrue(inventory.product_id, int(pid)) self.assertEqual(inventory.condition, cnd) self.assertEqual(inventory.quantity, int(qty)) self.assertEqual(inventory.restock_level, int(lvl)) self.assertEqual(inventory.available, int(avl))
def call_serialize(self,pid,cnd,qty,lvl,avl,err): inventory = Inventory(product_id=pid, condition=cnd, quantity=qty, restock_level=lvl, available=avl) if err==1: self.assertRaises(DataValidationError, inventory.validate_data) data = inventory.serialize() self.assertNotEqual(data, None) self.assertIn(keys.KEY_PID, data) self.assertEqual(data[keys.KEY_PID], pid) self.assertIn(keys.KEY_CND, data) self.assertEqual(data[keys.KEY_CND], cnd) self.assertIn(keys.KEY_QTY, data) self.assertEqual(data[keys.KEY_QTY], qty) self.assertIn(keys.KEY_LVL, data) self.assertEqual(data[keys.KEY_LVL], lvl) self.assertIn(keys.KEY_AVL, data) self.assertEqual(data[keys.KEY_AVL], avl)
def call_validate_data(self,pid,cnd,qty,lvl,avl,res): inventory = Inventory(product_id=pid, condition=cnd, quantity=qty, restock_level=lvl, available=avl) self.assertTrue(inventory != None) res_pid = inventory.validate_data_product_id() self.assertEqual(res_pid,res) res_cnd = inventory.validate_data_condition() self.assertEqual(res_cnd,res) res_qty = inventory.validate_data_quantity() self.assertEqual(res_qty,res) res_lvl = inventory.validate_data_restock_level() self.assertEqual(res_lvl,res) res_avl = inventory.validate_data_available() self.assertEqual(res_avl,res) try: err = inventory.validate_data() except DataValidationError as err: print(err)
def call_deserialize(self,pid,cnd,qty,lvl,avl,err): data = { keys.KEY_PID: pid, keys.KEY_CND: cnd, keys.KEY_QTY: qty, keys.KEY_LVL: lvl, keys.KEY_AVL: avl } inventory = Inventory() inventory.deserialize(data) if err==1: self.assertRaises(DataValidationError, inventory.validate_data) self.assertNotEqual(inventory, None) self.assertEqual(inventory.product_id, pid) self.assertEqual(inventory.condition, cnd) self.assertEqual(inventory.quantity, qty) self.assertEqual(inventory.restock_level, lvl) self.assertEqual(inventory.available, avl)
def create_inventory(): """ Creates a new inventory in the Inventory DB based the data in the body POST /inventory """ app.logger.info("Request to create an Inventory record") check_content_type("application/json") json = request.get_json() inventory = Inventory() inventory.deserialize(json) inventory.validate_data() if Inventory.find(json['product_id'], json['condition']): return create_conflict_error( "The Record you're trying to create already exists!") inventory.create() location_url = url_for("get_inventory_by_pid_condition",\ product_id=inventory.product_id, condition=inventory.condition, _external=True) app.logger.info("Inventory ({}, {}) created."\ .format(inventory.product_id, inventory.condition)) return make_response(jsonify(inventory.serialize()), status.HTTP_201_CREATED, {"Location": location_url})
def test_repr(self): """ Test Inventory __repr__ """ pid = 1234567 inventory = Inventory(product_id=pid) inventory.__repr__()