def test_find_by_name(self): """ Find a Item by Name """ Item(0, "fido", "dog").save() Item(0, "kitty", "cat").save() items = Item.find_by_name("fido") self.assertNotEqual(len(items), 0) self.assertEqual(items[0].price, "dog") self.assertEqual(items[0].name, "fido")
def test_get_item(self): """ Get a single Item """ # get the id of a item item = Item.find_by_name('hammer')[0] resp = self.app.get('/items/{}'.format(item.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = json.loads(resp.data) self.assertEqual(data['price'], 11.5)
def test_get_item(self): """ Get a single Item """ # get the sku of an item item = Item.find_by_name('test_item')[0] resp = self.app.get('/shopcarts/items/{}'.format(item.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = json.loads(resp.data) self.assertEqual(data['sku'], item.sku)
def test_for_case_insensitive(self): """ Test for Case Insensitive Search """ Item(0, "Fido", "DOG").save() Item(0, "Kitty", "CAT").save() items = Item.find_by_name("fido") self.assertNotEqual(len(items), 0) self.assertEqual(items[0].name, "Fido") items = Item.find_by_price("cat") self.assertNotEqual(len(items), 0) self.assertEqual(items[0].price, "CAT")
def list_items(): """ Returns all of the Items """ items = [] price = request.args.get('price') name = request.args.get('name') if price: items = Item.find_by_price(price) elif name: items = Item.find_by_name(name) else: items = Item.all() results = [item.serialize() for item in items] return make_response(jsonify(results), status.HTTP_200_OK)
def test_update_item(self): """ Update an existing Item """ item = Item.find_by_name('toilet paper')[0] new_item = { 'order_id': 1, 'product_id': 2, 'name': "wrench", 'quantity': 1, 'price': 11.50 } data = json.dumps(new_item) resp = self.app.put('/orders/{}/items/{}'.format( new_item['order_id'], item.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) new_json = json.loads(resp.data) self.assertEqual(new_json['name'], 'wrench')
def test_find_by_name(self): """ Find Items by name""" item = Item(order_id=1, product_id=1, name="wrench", quantity=1, price=10.50) item.save() item2 = Item(order_id=1, product_id=2, name="hammer", quantity=2, price=11) item2.save() items = Item.find_by_name("wrench") self.assertEqual(items[0].product_id, 1) self.assertEqual(items[0].name, "wrench") self.assertEqual(items[0].quantity, 1) self.assertEqual(items[0].price, 10.50)
def test_delete_item(self): """ Deleting an Item from an Order""" item = Item.find_by_name('toilet paper')[0] # Save the current number of items for assertion item_count = self.get_item_count() resp = self.app.delete('/orders/{}/items/{}'.format( item.order_id, item.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) new_count = self.get_item_count() self.assertEqual(new_count, item_count - 1) resp = self.app.delete('/orders/{}/items/{}'.format(5, item.id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND) resp = self.app.delete('/orders/{}/items/{}'.format(2, 1), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
def test_find_by_name(self): """ Find an Item by Name""" Item(sku="ID111", count=3, price=2.00, name="test_item", link="test.com", brand_name="gucci", is_available=True).save() Item(sku="ID222", count=5, price=10.00, name="some_item", link="link.com", brand_name="nike", is_available=False).save() items = Item.find_by_name("some_item") self.assertEqual(items[0].sku, "ID222") self.assertEqual(items[0].count, 5) self.assertEqual(items[0].price, 10.00) self.assertEqual(items[0].name, "some_item") self.assertEqual(items[0].link, "link.com") self.assertEqual(items[0].brand_name, "nike") self.assertEqual(items[0].is_available, False)
def list_items(): """ Returns all of the Items --- tags: - Items description: The Items endpoint allows you to query Items parameters: - name: order_id in: query description: the order_id of the Item you are looking for required: false type: integer - name: product_id in: query description: the product_id of the Item you are looking for required: false type: integer - name: quantity in: query description: the quantity of the Item you are looking for required: false type: integer - name: price in: query description: the price of the Item you are looking for required: false type: number - name: name in: query description: the name of the Item you are looking for required: false type: string definitions: Item: type: object properties: id: type: integer description: unique id assigned internally by service name: type: string description: the item name order_id: type: integer description: the order_id of the item product_id: type: integer description: the product_id of the item quantity: type: integer description: the quantity of the item price: type: number description: the price of the item responses: 200: description: An array of Items schema: type: array items: schema: $ref: '#/definitions/Item' """ items = [] order_id = request.args.get('order_id') product_id = request.args.get('product_id') quantity = request.args.get('quantity') price = request.args.get('price') name = request.args.get('name') if order_id: items = Item.find_by_order_id(order_id) elif product_id: items = Item.find_by_product_id(product_id) elif quantity: items = Item.find_by_quantity(quantity) elif price: items = Item.find_by_price(price) elif name: items = Item.find_by_name(name) else: items = Item.all() results = [item.serialize() for item in items] return make_response(jsonify(results), status.HTTP_200_OK)