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_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_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 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 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 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})