def test_deserialize_missing_data(self): """ Test deserialization of missing data """ inventory = Inventory() miss_product_id = {"quantity": 100, "restock_level":50,\ "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(miss_product_id) self.assertEqual(str(error.exception), 'Invalid Inventory: '\ 'missing product_id')
def test_deserialize_bad_data(self): """ Test deserialization of bad data """ data = "this is not a dictionary" inventory = Inventory() with self.assertRaises(DataValidationError) as error: inventory.deserialize(data) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : string indices must be integers')
def test_deserialize_an_inventory(self): """ Test deserialization of an inventory """ data = {"id": 1, "product_id":100, "quantity": 100,\ "restock_level":50, "condition": "new", "available": True} inventory = Inventory() inventory.deserialize(data) self.assertNotEqual(inventory, None) self.assertEqual(inventory.id, None) self.assertEqual(inventory.product_id, 100) self.assertEqual(inventory.quantity, 100) self.assertEqual(inventory.restock_level, 50) self.assertEqual(inventory.condition, "new") self.assertEqual(inventory.available, True)
def create_inventory(): """ Creates an Inventory Item This endpoint will create an Inventory Item based the data in the body that is posted """ app.logger.info("Request to create inventory") check_content_type("application/json") inv = Inventory() inv.deserialize(request.get_json()) inv.create() message = inv.serialize() location_url = url_for("get_inventory_item_by_id", inv_id=inv.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def create_inventory(): """ Creates an Inventory This endpoint will create an Inventory based the data in the body that is posted """ app.logger.info('Request to create an inventory') check_content_type('application/json') inventory = Inventory() inventory.deserialize(request.get_json()) inventory.save() message = inventory.serialize() location_url = url_for('get_inventory', inventory_id=inventory.inventory_id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
def post(self): """ Creates an Inventory This endpoint will create an Inventory based the data in the body that is posted """ app.logger.info('Request to create an inventory') check_content_type('application/json') inventory = Inventory() app.logger.debug('Payload = %s', api.payload) inventory.deserialize(api.payload) inventory.save() location_url = api.url_for(InventoryResource, inventory_id=inventory.id, _external=True) return inventory.serialize(), status.HTTP_201_CREATED, \ {'Location': location_url}
def test_deserialize_an_inventory_item(self): """ Test deserialization of an Inventory Item """ data = { "id": 1, "name": "Rolex", "sku": "Rol1232020", "quantity": 20, "restockLevel": 10 } inv_item = Inventory() inv_item.deserialize(data) self.assertNotEqual(inv_item, None) self.assertEqual(inv_item.id, None) self.assertEqual(inv_item.name, "Rolex") self.assertEqual(inv_item.sku, "Rol1232020") self.assertEqual(inv_item.quantity, 20) self.assertEqual(inv_item.restockLevel, 10)
def test_query_understocked_inventory(self): """ Query Understocked Inventory """ inventory = [] inventory.append( Inventory(name="Rolex Watch", sku="R1232020", quantity=10, restockLevel=12).create()) inventory.append( Inventory(name="Cartier Watch", sku="C1232020", quantity=12, restockLevel=6).create()) inventory.append( Inventory(name="Tissot Watch", sku="T1232020", quantity=12).create()) resp = self.app.get("/inventory/restock") self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() inv_item = Inventory() inv_item.deserialize(data) self.assertEqual(inv_item.name, "Rolex Watch")
def test_deserialize_wrong_type_data(self): """ Test deserialization of wrong type data """ inventory = Inventory() product_id_boolean = {"id": 1, "product_id": True,\ "quantity": 100, "restock_level":50, "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(product_id_boolean) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : product_id required int') product_id_string = {"id": 1, "product_id":"100",\ "quantity": 100, "restock_level":50, "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(product_id_string) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : product_id required int') quantity_boolean = {"id": 1, "product_id":100,\ "quantity": True, "restock_level":50,\ "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(quantity_boolean) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : quantity required int') quantity_string = {"id": 1, "product_id":100,\ "quantity": "100", "restock_level":50,\ "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(quantity_string) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : quantity required int') restock_level_bool = {"id": 1, "product_id":100,\ "quantity": 100, "restock_level": True, "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(restock_level_bool) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : restock_level required int') restock_level_string = {"id": 1, "product_id":100,\ "quantity": 100, "restock_level":"50", "condition": "new", "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(restock_level_string) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : restock_level required int') condition_int = {"id": 1, "product_id":100,\ "quantity": 100, "restock_level": 50, "condition": 1, "available": True} with self.assertRaises(DataValidationError) as error: inventory.deserialize(condition_int) self.assertEqual(str(error.exception), 'Invalid Inventory: body'\ ' of request contained '\ 'bad or no data : condition required string') available_int = {"id": 1, "product_id":100,\ "quantity": 100, "restock_level":50, "condition": "new", "available": 1} with self.assertRaises(DataValidationError) as error: inventory.deserialize(available_int) self.assertEqual(str(error.exception), 'Invalid Inventory: '\ 'body of request contained '\ 'bad or no data : available required bool') available_string = {"id": 1, "product_id":100,\ "quantity": 100, "restock_level":50, "condition": "new", "available": "true"} with self.assertRaises(DataValidationError) as error: inventory.deserialize(available_string) self.assertEqual(str(error.exception), 'Invalid Inventory: '\ 'body of request contained '\ 'bad or no data : available required bool')