def test_update_wishlist_no_name(self): """Test updating a existing Wishlist with no name""" wishlist = Wishlist.find_by_customer_id(1)[0] new_wishlist = {'customer_id': 1} '''new_wishlist['items'] = [{"wishlist_id": 3, "product_id": 3, "name": "soda", "description": "I need some soft drinks"}]''' data = json.dumps(new_wishlist) resp = self.app.put('/wishlists/{}'.format(wishlist.id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_wishlist_item_list(self): """ Test getting a list of Items from one specific Wishlist """ wishlist = Wishlist.find_by_customer_id(1)[0] print wishlist.id resp = self.app.get('/wishlists/{}/items'.format(wishlist.id), content_type='application/json') print json.loads(resp.data) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = json.loads(resp.data) self.assertEqual(len(data), 2)
def get_wishlist_list(): """ Returns the Wishlists by searching the keywords of wishlist_name or the customer_id. This function returns a wishlist based on wishlist_name or customer id. If the customer_id and the wishlist id parameters are empty, returns all the wishlists in the database --- tags: - Wishlist produces: - application/json definitions: Wishlist: type: object properties: id: type: integer customer_id: type: integer wishlist_name: type: string parameters: - name: keyword in: query description: the name of the wishlist type: string - name: query in: query description: the id of the customer type: integer responses: 200: description: A Wishlist schema: $ref: '#/definitions/Wishlist' """ query_lists = [] customer_id = request.args.get('customer_id') keyword = request.args.get('keyword') if keyword: query_lists = Wishlist.find_by_wishlist_name(keyword) elif customer_id: query_lists = Wishlist.find_by_customer_id(customer_id) else: """ Returns all of the Wishlists """ query_lists = Wishlist.all() results = [wishlist.serialize() for wishlist in query_lists] return make_response(jsonify(results), status.HTTP_200_OK)
def test_delete_wishlist(self): """ Test deleting a Wishlist """ wishlist = Wishlist.find_by_customer_id(1)[0] # Save the current number of wishlists for assertion wishlist_count = self.get_wishlist_count() resp = self.app.delete('/wishlists/{}'.format(wishlist.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_wishlist_count() self.assertEqual(new_count, wishlist_count - 1)
def test_update_wishlist(self): """Test updating a Wishlist already exists """ wishlist = Wishlist.find_by_customer_id(1)[0] new_wishlist = {'customer_id': 1, 'wishlist_name': "alex's wishlist"} new_wishlist['items'] = [{ "wishlist_id": 3, "product_id": 3, "name": "soda", "description": "I need some soft drinks" }] data = json.dumps(new_wishlist) resp = self.app.put('/wishlists/{}'.format(wishlist.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['wishlist_name'], "alex's wishlist")
def test_get_wishlist(self): """Test getting a wishlist""" wishlist_id = Wishlist.find_by_customer_id(2)[0].id resp = self.app.get('/wishlists/{}'.format(wishlist_id)) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(resp.data)['wishlist_name'], 'beverage')