def setUpClass(cls): """ This runs once before the entire test suite """ app.config['TESTING'] = True app.config['DEBUG'] = False app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI app.logger.setLevel(logging.CRITICAL) Inventory.init_db(app)
def test_disable_inventory(self): """ Disable an existing Inventory """ # create inventories to update test_inventory = [] for _ in range(0, 2): test = Inventory(product_id=1, quantity=100, restock_level=20, condition="new", available=True) test.save() test_inventory.append(test) # disable the inventory product_id = 1 resp = self.app.put('/inventory/{}/disable'.format(product_id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) disabled_data = resp.get_json() self.assertEqual(len(disabled_data), 2) for row in resp.get_json(): self.assertEqual(row['available'], False) # test disabling an inventory with wrong product_id wrong_product_id = 2 resp = self.app.put('/inventory/{}/disable'.format(wrong_product_id), content_type='application/json') disabled_data = resp.get_json() self.assertEqual(len(disabled_data), 0)
def test_find_by_condition(self): """ Find an Inventory by its condition """ Inventory(product_id=1, quantity=100, restock_level=50, condition="new", available=True).save() Inventory(product_id=2, quantity=100, restock_level=50, condition="new", available=True).save() Inventory(product_id=3, quantity=80, restock_level=20, condition="used", available=True).save() inventory = Inventory.find_by_condition("new") self.assertEqual(len(inventory), 2) inventory = Inventory.find_by_condition_with_pid('new', 1) self.assertEqual(len(inventory), 1) self.assertEqual(inventory[0].product_id, 1) self.assertEqual(inventory[0].quantity, 100) self.assertEqual(inventory[0].restock_level, 50) self.assertEqual(inventory[0].condition, 'new') self.assertEqual(inventory[0].available, True)
def test_delete_an_inventory_item(self): """ Delete an item """ inv_item = InventoryFactory() inv_item.create() self.assertEqual(len(Inventory.all()), 1) inv_item.delete() self.assertEqual(len(Inventory.all()), 0)
def reset_inventory(): """ Delete all Inventory This endpoint will delete all Inventory """ app.logger.info('Delete all inventory') Inventory.remove_all() return make_response('', status.HTTP_204_NO_CONTENT)
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_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 list_inventory(): """ Returns all of the inventory """ app.logger.info('Request for inventory list') inventories = [] restock = request.args.get('restock') restock_level = request.args.get('restock-level') condition = request.args.get('condition') product_id = request.args.get('product-id') available = request.args.get('available') if restock: if restock == "true": inventories = Inventory.find_by_restock(True) elif restock == "false": inventories = Inventory.find_by_restock(False) elif restock_level: inventories = Inventory.find_by_restock_level(restock_level) elif condition: if product_id: inventories = Inventory.find_by_condition_with_pid( condition, product_id) elif not product_id: inventories = Inventory.find_by_condition(condition) elif product_id: inventories = Inventory.find_by_product_id(product_id) elif available: if available == 'true': inventories = Inventory.find_by_availability(True) elif available == 'false': inventories = Inventory.find_by_availability(False) else: inventories = Inventory.all() results = [e.serialize() for e in inventories] return make_response(jsonify(results), status.HTTP_200_OK)
def test_create_inventory_with_bad_data(self): """ Create with wrong type""" test_inventory = Inventory(product_id=1, quantity=30, restock_level=20, condition='new', available="True") resp = self.app.post('/inventory', json=test_inventory.serialize(), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
def test_delete_nonexist(self): """ Test deleting a nonexist inventory""" inventory = Inventory(product_id=1, quantity=100, restock_level=50, condition="new", available=True) inventory.id = '1cak41-nonexist' try: inventory.delete() except KeyError: self.assertRaises(KeyError)
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 test_query_inventory_list_by_availability(self): """ Query an Inventory by availability """ inventories = [] for _ in range(0, 2): test = Inventory(product_id=_, quantity=30, restock_level=20, condition='new', available=True) test.save() inventories.append(test) for _ in range(2, 5): test = Inventory(product_id=_, quantity=30, restock_level=50, condition='new', available=False) test.save() inventories.append(test) resp = self.app.get('/inventory', query_string='available=true') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), 2) for inventory in data: self.assertEqual(inventory['available'], True) resp = self.app.get('/inventory', query_string='available=false') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), 3) for inventory in data: self.assertEqual(inventory['available'], False)
def test_find_by_name(self): """ Find Inventory items by name """ Inventory(name="Rolex Watch", sku="R1232020", quantity=10, restockLevel=12).create() Inventory(name="Cartier Watch", sku="C1232020", quantity=12, restockLevel=6).create() inv_items = Inventory.find_by_name("Cartier Watch") self.assertEqual(inv_items[0].sku, "C1232020") self.assertEqual(inv_items[0].quantity, 12) self.assertEqual(inv_items[0].restockLevel, 6)
def test_find_item(self): """ Find an item by ID """ inv_items = InventoryFactory.create_batch(3) for inv_item in inv_items: inv_item.create() logging.debug(inv_items) # make sure they got saved self.assertEqual(len(Inventory.all()), 3) # find the 2nd inventory item in the list inv_item = Inventory.find(inv_items[1].id) self.assertIsNot(inv_item, None) self.assertEqual(inv_item.id, inv_items[1].id) self.assertEqual(inv_item.name, inv_items[1].name) self.assertEqual(inv_item.sku, inv_items[1].sku)
def test_query_inventory_list_by_restock_level(self): """ Query Inventories by restock_level """ inventories = [] for _ in range(0, 2): test = Inventory(product_id=_, quantity=_, restock_level=20, condition="new", available=True) test.save() inventories.append(test) for _ in range(2, 5): test = Inventory(product_id=_, quantity=_, restock_level=50, condition="new", available=True) test.save() inventories.append(test) resp = self.app.get('/inventory', query_string='restock-level={}'.format(20)) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), 2) resp = self.app.get('/inventory', query_string='restock-level={}'.format(50)) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), 3)
def test_query_by_condition(self): """ Query an Inventory by Condition """ inventories = [] for _ in range(1, 3): test = Inventory(product_id=_, quantity=_, restock_level=20, condition='new', available=True) test.save() inventories.append(test) for _ in range(1, 3): test = Inventory(product_id=_, quantity=_, restock_level=20, condition='used', available=True) test.save() inventories.append(test) # inventories = self._create_inventories(5) test_condition = inventories[0].condition test_pid = inventories[0].product_id condition_inventories = [ i for i in inventories if i.condition == test_condition ] pid_condition_inventories = [ i for i in inventories if (i.condition == test_condition and i.product_id == test_pid) ] # /inventory?condition={condition} resp = self.app.get('/inventory?condition={}'.format(test_condition)) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), len(condition_inventories)) for i in data: self.assertEqual(i['condition'], test_condition) resp = self.app.get('/inventory?condition={}'.format('invalid')) self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(b'must be new, open_box, used', resp.data) resp = self.app.get('/inventory?condition={}'.format('')) self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(b'can\'t be empty', resp.data) # /inventory?product-id={pid}&condition={condition} resp = self.app.get('/inventory', query_string='product-id={0}&condition={1}'.format( test_pid, test_condition)) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), len(pid_condition_inventories)) for i in data: self.assertEqual(i['condition'], test_condition) self.assertEqual(i['product_id'], test_pid) resp = self.app.get( '/inventory?product-id=1&condition={}'.format('invalid')) self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(b'must be new, open_box, used', resp.data) resp = self.app.get('/inventory?product-id=1&condition={}'.format('')) self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn(b'can\'t be empty', resp.data)
def test_serialize_an_inventory_item(self): """ Test serialization of an inventory item """ inv_item = Inventory() data = inv_item.serialize() self.assertNotEqual(data, None) self.assertIn("id", data) self.assertEqual(data["id"], inv_item.id) self.assertIn("name", data) self.assertEqual(data["name"], inv_item.name) self.assertIn("sku", data) self.assertEqual(data["sku"], inv_item.sku) self.assertIn("quantity", data) self.assertEqual(data["quantity"], inv_item.quantity) self.assertIn("restockLevel", data) self.assertEqual(data["restockLevel"], inv_item.restockLevel)
def list_inventory(): """ Returns entire Inventory """ app.logger.info("Request for entire inventory") inventory = [] sku = request.args.get("sku") name = request.args.get("name") if sku: inventory = Inventory.find_by_sku(sku) elif name: inventory = Inventory.find_by_name(name) else: inventory = Inventory.all() results = [inv.serialize() for inv in inventory] return make_response(jsonify(results), status.HTTP_200_OK)
def test_delete_an_inventory_with_inventory_id(self): """ Delete an inventory """ inventory = Inventory(product_id=1, available=True) inventory.save() self.assertEqual(len(Inventory.all()), 1) # delete the inventory and make sure it isn't in the database inventory.delete() self.assertEqual(len(Inventory.all()), 0)
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_serialize_an_inventory(self): """ Test serialization of an inventory """ inventory = Inventory(product_id=1, quantity=100,\ restock_level=50, condition="new",\ available=True) data = inventory.serialize() self.assertNotEqual(inventory, None) self.assertIn('product_id', data) self.assertEqual(data['product_id'], 1) self.assertIn('quantity', data) self.assertEqual(data['quantity'], 100) self.assertIn('restock_level', data) self.assertEqual(data['restock_level'], 50) self.assertIn('condition', data) self.assertEqual(data['condition'], "new") self.assertIn('available', data) self.assertEqual(data['available'], True)
def test_find_by_restock(self): """ Find inventories if quantity lower than their restock level """ Inventory(product_id=1, quantity=100, restock_level=50, condition="new", available=True).save() Inventory(product_id=2, quantity=20, restock_level=50, condition="new", available=True).save() Inventory(product_id=3, quantity=30, restock_level=50, condition="new", available=True).save() Inventory(product_id=4, quantity=120, restock_level=50, condition="new", available=True).save() Inventory(product_id=5, quantity=49, restock_level=50, condition="new", available=True).save() inventory = Inventory.find_by_restock(True) self.assertEqual(len(inventory), 3) inventory = Inventory.find_by_restock(False) self.assertEqual(len(inventory), 2)
def test_find_by_restock_level(self): """ Find inventories by restock_level""" Inventory(product_id=1, quantity=100, restock_level=20, condition="new", available=True).save() Inventory(product_id=2, quantity=20, restock_level=30, condition="new", available=True).save() Inventory(product_id=3, quantity=30, restock_level=50, condition="new", available=True).save() Inventory(product_id=4, quantity=120, restock_level=50, condition="new", available=True).save() Inventory(product_id=5, quantity=49, restock_level=50, condition="new", available=True).save() inventory = Inventory.find_by_restock_level(20) self.assertEqual(len(inventory), 1) inventory = Inventory.find_by_restock_level(50) self.assertEqual(len(inventory), 3)
def get_understocked_inventory(): """ Retrieve records that are out of stock """ app.logger.info("Request for understocked inventory") inv = Inventory.find_understocked() if not inv: raise NotFound("Understocked Inventory was not found.") return make_response(jsonify(inv[0].serialize()), status.HTTP_200_OK)
def test_query_inventory_list_by_restock(self): """ Query Inventories if quatity is lower than restock_level """ inventories = [] for _ in range(5): test = Inventory(product_id=_, quantity=_, restock_level=3, condition="new", available=True) test.save() inventories.append(test) resp = self.app.get('/inventory', query_string='restock=true') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), 3) resp = self.app.get('/inventory', query_string='restock=false') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), 2)
def test_find_an_inventory_by_product_id(self): """ Find an inventory by product_id """ Inventory(product_id=1, quantity=100, restock_level=50, condition="new", available=True).save() Inventory(product_id=2, quantity=21, restock_level=20, condition="used", available=True).save() inventory = Inventory.find_by_product_id(2) self.assertEqual(len(inventory), 1) self.assertEqual(inventory[0].product_id, 2) self.assertEqual(inventory[0].quantity, 21) self.assertEqual(inventory[0].restock_level, 20) self.assertEqual(inventory[0].condition, "used") self.assertEqual(inventory[0].available, True)
def test_delete_an_inventory_with_id(self): """ Delete an inventory """ inventory = Inventory(product_id=1, quantity=100, restock_level=50, condition="new", available=True) inventory.save() self.assertEqual(len(Inventory.all()), 1) # delete the inventory and make sure it isn't in the database inventory.delete() self.assertEqual(len(Inventory.all()), 0)
def delete_inventory_item_by_sku(inv_sku): """ Delete an Inventory Item This endpoint will delete and inventory item based the sku specified in the path """ app.logger.info("Request to delete inventory item with sku: ", inv_sku) inv = Inventory.find_by_sku(inv_sku) if inv: inv.delete() return make_response("", status.HTTP_204_NO_CONTENT)
def delete_inventory(inventory_id): """ Delete an inventory This endpoint will delete an inventory based on the id specified in the path """ app.logger.info('Request to delete inventory with id: %s', inventory_id) inventory = Inventory.find(inventory_id) if inventory: inventory.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def test_invalid_query(self): """ Query Inventories if the request argument is invalid """ inventories = [] for _ in range(5): test = Inventory(product_id=_, quantity=_, restock_level=3, condition="new", available=True) test.save() inventories.append(test) resp = self.app.get('/inventory', query_string='?invalidpara=1') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) resp = self.app.get('/inventory', query_string='?invalidpara=1&invalidpara=1') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) resp = self.app.get('/inventory', query_string=\ '?invalidpara=1&invalidpara=1' + \ '&nvalidpara=1&invalidpara=1') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)