예제 #1
0
 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)
예제 #2
0
 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)
예제 #3
0
 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)
예제 #4
0
    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)
예제 #5
0
 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)
예제 #6
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)
예제 #7
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)
예제 #8
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)
예제 #9
0
    def test_create_an_inventory_bad_data(self):
        """ Create an inventory with bad data  """
        inventory_no_product_id = Inventory(quantity=100,\
                                            restock_level=50, condition="new",\
                                            available=True)
        with self.assertRaises(DataValidationError) as error:
            inventory_no_product_id.save()
        self.assertEqual(str(error.exception), 'product_id is not set')

        inventory_no_quantity = Inventory(product_id=1,\
                                          restock_level=50, condition="new",\
                                          available=True)
        with self.assertRaises(DataValidationError) as error:
            inventory_no_quantity.save()
        self.assertEqual(str(error.exception), 'quantity is not set')

        inventory_no_restock_level = Inventory(product_id=1, quantity=100,\
                                               condition="new", available=True)
        with self.assertRaises(DataValidationError) as error:
            inventory_no_restock_level.save()
        self.assertEqual(str(error.exception), 'restock_level is not set')

        inventory_no_condition = Inventory(product_id=1, quantity=100,\
                                            restock_level=50, available=True)
        with self.assertRaises(DataValidationError) as error:
            inventory_no_condition.save()
        self.assertEqual(str(error.exception),
                         'condition is not set to new/open_box/used')
예제 #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)
예제 #11
0
 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}
예제 #12
0
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})
예제 #13
0
 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)
예제 #14
0
 def test_find_or_404_found(self):
     """ Find or return 404 found """
     Inventory(product_id=1,
               quantity=100,
               restock_level=50,
               condition="new",
               available=False).save()
     used_inventory = Inventory(product_id=2,
                                quantity=21,
                                restock_level=20,
                                condition="used",
                                available=True)
     used_inventory.save()
     inventory = Inventory.find_or_404(used_inventory.inventory_id)
     self.assertNotEqual(inventory, None)
     self.assertEqual(inventory.product_id, 2)
     self.assertEqual(inventory.quantity, 21)
     self.assertEqual(inventory.restock_level, 20)
     self.assertEqual(inventory.condition, "used")
     self.assertEqual(inventory.available, True)
예제 #15
0
 def test_find_an_inventory(self):
     """ Find an inventory by id """
     new_inventory = Inventory(product_id=1,
                               quantity=100,
                               restock_level=50,
                               condition="new",
                               available=True)
     used_inventory = Inventory(product_id=2,
                                quantity=21,
                                restock_level=20,
                                condition="used",
                                available=True)
     new_inventory.save()
     used_inventory.save()
     inventory = Inventory.find(used_inventory.id)
     self.assertNotEqual(inventory, None)
     self.assertEqual(inventory.product_id, 2)
     self.assertEqual(inventory.quantity, 21)
     self.assertEqual(inventory.restock_level, 20)
     self.assertEqual(inventory.condition, "used")
     self.assertEqual(inventory.available, True)
예제 #16
0
 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)
예제 #17
0
    def test_find(self):
        """ Find a Inventory by ID """
        Inventory(product_id=1,
                  quantity=100,
                  restock_level=50,
                  condition="new",
                  available=False).save()
        inventory = Inventory(product_id=2,
                              quantity=21,
                              restock_level=20,
                              condition="used",
                              available=True)
        inventory.save()

        res = Inventory.find(inventory.id)
        self.assertIsNot(res, None)
        self.assertEqual(res.product_id, inventory.product_id)
        self.assertEqual(res.quantity, inventory.quantity)
        self.assertEqual(res.condition, inventory.condition)
        self.assertEqual(res.available, inventory.available)
        self.assertEqual(res.id, inventory.id)