Esempio n. 1
0
 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)
Esempio n. 2
0
    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_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)
Esempio n. 4
0
 def test_add_an_inventory(self):
     """ Create an inventory and add it to the database """
     inventory = Inventory.all()
     self.assertEqual(inventory, [])
     inventory = Inventory(product_id=1, quantity=100,\
                           restock_level=50, condition="new",\
                           available=True)
     self.assertNotEqual(inventory, None)
     self.assertEqual(inventory.inventory_id, None)
     inventory.save()
     self.assertEqual(inventory.inventory_id, 1)
     inventory = Inventory.all()
     self.assertEqual(len(inventory), 1)
Esempio n. 5
0
 def test_add_an_inventory_item(self):
     """ Create an inventory item and add it to the database """
     inventory = Inventory.all()
     self.assertEqual(inventory, [])
     inv_item = Inventory(name="Rolex Watch",
                          sku="R1232020",
                          quantity=10,
                          restockLevel=12)
     self.assertTrue(inv_item != None)
     self.assertEqual(inv_item.id, None)
     inv_item.create()
     self.assertEqual(inv_item.id, 1)
     inventory = Inventory.all()
     self.assertEqual(len(inventory), 1)
 def test_update_inventory(self):
     """ Update an existing inventory """
     inventory = Inventory(product_id=1,
                           quantity=100,
                           restock_level=50,
                           condition="new",
                           available=True)
     inventory.save()
     self.assertNotEqual(inventory.id, None)
     # Change it and save it
     inventory.product_id = 2
     inventory.quantity = 200
     inventory.restock_level = 100
     inventory.condition = "used"
     inventory.available = False
     inventory.save()
     # Fetch it back and make sure the id hasn't change
     # but the data did change
     inventory = Inventory.all()
     self.assertEqual(len(inventory), 1)
     self.assertNotEqual(inventory[0].id, None)
     self.assertEqual(inventory[0].product_id, 2)
     self.assertEqual(inventory[0].quantity, 200)
     self.assertEqual(inventory[0].restock_level, 100)
     self.assertEqual(inventory[0].condition, 'used')
     self.assertEqual(inventory[0].available, False)
Esempio n. 7
0
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)
Esempio n. 8
0
    def get(self):
        """ Returns all of the inventory """
        app.logger.info('Request for inventory list')
        inventories = []
        args = inventory_args.parse_args()
        restock = args['restock']
        restock_level = args['restock-level']
        condition = args['condition']
        product_id = args['product-id']
        available = args['available']
        args_len = len(request.args)

        message_invalid_fields = \
        'Only accept query by product-id, available, ' \
        + 'product-id & availabe, condition, product-id & condition, ' \
        + 'restock-level, restock (list all the inventory that need ' \
        + 'to be restocked).'
        message_condition_empty = '{} can\'t be empty'.format('condition')
        message_condition_invalid = '{} must be new, open_box, used'\
        .format('condition')

        if args_len is 0:
            inventories = Inventory.all()
        elif args_len is 1:
            if product_id is not None:
                inventories = Inventory.find_by_product_id(int(product_id))
            elif restock is not None:
                inventories = Inventory.find_by_restock(restock)
            elif restock_level is not None:
                inventories = Inventory.find_by_restock_level\
                (int(restock_level))
            elif condition is not None:
                if condition is '':
                    api.abort(400, message_condition_empty)
                elif condition not in ('new', 'open_box', 'used'):
                    api.abort(400, message_condition_invalid)
                else:
                    inventories = Inventory.find_by_condition(condition)
            elif available is not None:
                inventories = Inventory.find_by_availability(available)
            else:
                api.abort(400, message_invalid_fields)
        elif args_len is 2:
            if condition is not None and product_id is not None:
                if condition is '':
                    api.abort(400, message_condition_empty)
                elif condition not in ('new', 'open_box', 'used'):
                    api.abort(400, message_condition_invalid)
                else:
                    inventories = Inventory.find_by_condition_with_pid(
                        condition, int(product_id))
            elif available is not None and product_id is not None:
                inventories = Inventory.\
                find_by_availability_with_pid(available, int(product_id))
            else:
                api.abort(400, message_invalid_fields)
        else:
            api.abort(400, message_invalid_fields)
        results = [e.serialize() for e in inventories]
        return results, status.HTTP_200_OK
Esempio n. 9
0
 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)
Esempio n. 10
0
    def test_disable_an_inventory(self):
        """ Disable an existing product """
        inventory = Inventory(product_id=1, quantity=100,
                              restock_level=50, condition="new",\
                              available=True)
        inventory.save()
        self.assertEqual(inventory.available, True)
        # Change the status and save it
        inventory.available = False
        inventory.save()

        # Fetch it back and make sure the data did change
        inventory = Inventory.all()
        self.assertEqual(len(inventory), 1)
        self.assertEqual(inventory[0].available, False)
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)
Esempio n. 12
0
 def test_update_an_inventory_item(self):
     """ Update an inv item """
     inv_item = InventoryFactory()
     logging.debug(inv_item)
     inv_item.create()
     logging.debug(inv_item)
     self.assertEqual(inv_item.id, 1)
     # Change the item snd save it
     inv_item.restockLevel = 20
     original_id = inv_item.id
     inv_item.save()
     self.assertEqual(inv_item.id, original_id)
     self.assertEqual(inv_item.restockLevel, 20)
     #Fetch back to make sure the id has not changed
     #but the data did change
     inventory = Inventory.all()
     self.assertEqual(len(inventory), 1)
     self.assertEqual(inventory[0].id, 1)
     self.assertEqual(inventory[0].restockLevel, 20)