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 put(self, product_id, condition): """ Update an Inventory This endpoint will update a Inventory based the body that is posted """ app.logger.info("Request to update inventory with key ({}, {})"\ .format(product_id, condition)) try: inventory = Inventory.find_by_product_id_condition( product_id, condition) if not inventory: api.abort( status.HTTP_404_NOT_FOUND, "Inventory with ({}, {})".format(product_id, condition)) resp_old = inventory.serialize() resp_new = api.payload for key in resp_old.keys(): if key in resp_new: resp_old[key] = resp_new[key] inventory.deserialize(resp_old) inventory.validate_data() inventory.update() app.logger.info("Inventory ({}, {}) updated.".format( product_id, condition)) return inventory.serialize(), status.HTTP_200_OK except DataValidationError as err: api.abort(status.HTTP_400_BAD_REQUEST, err)
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_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 delete(self, product_id, condition): """ Delete a Inventory This endpoint will delete a Inventory based the id specified in the path """ app.logger.info("Request to delete inventory with key ({}, {})"\ .format(product_id, condition)) inventory = Inventory.find_by_product_id_condition( product_id, condition) if inventory: inventory.delete() app.logger.info( "Inventory with product_id {} and condition {} deleted".format( product_id, condition)) return '', status.HTTP_204_NO_CONTENT
def get(self, product_id, condition): """ Retrieve a single Inventory This endpoint will return a Inventory based on it's id """ app.logger.info("A GET request for inventories with product_id {} and condition {}"\ .format(product_id, condition)) inventory = Inventory.find_by_product_id_condition( product_id, condition) if not inventory: api.abort( status.HTTP_404_NOT_FOUND, "Inventory ({}, {}) NOT FOUND".format(product_id, condition)) app.logger.info("Return inventory with product_id {} and condition {}"\ .format(product_id, condition)) return inventory.serialize(), status.HTTP_200_OK
def put(self, product_id, condition): """ Restock an Inventory's Quantity """ app.logger.info("Request to update inventory with key ({}, {})"\ .format(product_id, condition)) # Check if the record exists inventory = Inventory.find_by_product_id_condition( product_id, condition) if not inventory: api.abort(status.HTTP_404_NOT_FOUND, "Inventory with ({}, {})".format(product_id, condition)) inventory.available = 0 inventory.validate_data() inventory.update() app.logger.info("Inventory ({}, {}) restocked.".format( product_id, condition)) return inventory.serialize(), status.HTTP_200_OK
def put(self, product_id, condition): """ Restock an Inventory's Quantity """ app.logger.info("Request to update inventory with key ({}, {})"\ .format(product_id, condition)) # Check if the record exists inventory = Inventory.find_by_product_id_condition( product_id, condition) if not inventory: api.abort(status.HTTP_404_NOT_FOUND, "Inventory with ({}, {})".format(product_id, condition)) # Checking for keys.KEY_AMT keyword json = request.get_json() json = api.payload if "amount" not in json.keys(): api.abort(status.HTTP_400_BAD_REQUEST, "Invalid data: Amount missing") # Checking for amount >= 0 amount = json[keys.KEY_AMT] regex = "^\-?\d+$" if not re.search(regex, str(amount)): api.abort(status.HTTP_400_BAD_REQUEST, "Invalid data: Amount must be an integer") else: if int(amount) <= 0: api.abort(status.HTTP_400_BAD_REQUEST, "Invalid data: Amount <= 0") inventory.quantity += int(amount) inventory.validate_data() inventory.update() app.logger.info("Inventory ({}, {}) restocked.".format( product_id, condition)) return inventory.serialize(), status.HTTP_200_OK