def test_find_by_cartId(self): """ Find all items for a particular CartId """ ShoppingCartItems(productID=1234, price=12.99, quantity=1, cartId=5).add() ShoppingCartItems(productID=4321, price=18.99, quantity=2, cartId=8).add() items = ShoppingCartItems.allItems(8) self.assertEqual(items[0].productID, 4321) self.assertEqual(items[0].price, 18.99) self.assertEqual(items[0].quantity, 2) self.assertEqual(items[0].cartId, 8)
def test_deserialize_a_shopcartitem(self): """ Test deserialization of a Cart Item """ data = { "id": 1, "productID": 1234, "price": 12.99, "quantity": 1, "cartId": 5 } item = ShoppingCartItems() item.deserialize(data) self.assertNotEqual(item, None) self.assertEqual(item.productID, 1234) self.assertEqual(item.price, 12.99) self.assertEqual(item.quantity, 1) self.assertEqual(item.cartId, 5)
def test_serialize_a_shopcartitem(self): """ Test serialization of a Cart Item """ item = ShoppingCartItems(productID=1234, price=12.99, quantity=1, cartId=5) data = item.serialize() self.assertNotEqual(data, None) self.assertIn('id', data) self.assertEqual(data['id'], None) self.assertIn('productID', data) self.assertEqual(data['productID'], 1234) self.assertIn('price', data) self.assertEqual(data['price'], 12.99) self.assertIn('quantity', data) self.assertEqual(data['quantity'], 1) self.assertIn('cartId', data) self.assertEqual(data['cartId'], 5)
def action_to_delete_item(cart_id, item_id): app.logger.info( 'Action to delete item with id: {} from cart with id: {}'.format( item_id, cart_id)) results = ShoppingCartItems.find(cart_id, item_id) if results: for item in results: item.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def delete_items(cart_id, product_id): app.logger.info( 'Request to delete item with id: {} from cart with id: {}'.format( product_id, cart_id)) results = ShoppingCartItems.find(cart_id, product_id) if results: for item in results: item.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def test_delete_a_shopcartitem(self): """ Delete a Cart Item """ item = ShoppingCartItems(productID=1234, price=12.99, quantity=1, cartId=5) item.add() self.assertEqual(len(ShoppingCartItems.all()), 1) # delete the cart item and make sure it isn't in the database item.delete() self.assertEqual(len(ShoppingCartItems.all()), 0)
def get_items(cart_id): """ Returns all the items in a cart """ app.logger.info('Getting items of Cart with id: {}'.format(cart_id)) cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) results = ShoppingCartItems.allItems(cart_id) if not results: raise NotFound( 'Cart with id: {} does not have any item'.format(cart_id)) return jsonify([item.serialize() for item in results]), status.HTTP_200_OK
def test_create_a_shopcartitem(self): """ Create a cart and assert that it exists """ item = ShoppingCartItems(productID=1234, price=12.99, quantity=1, cartId=5) self.assertTrue(item != None) self.assertEqual(item.id, None) self.assertEqual(item.productID, 1234) self.assertEqual(item.price, 12.99) self.assertEqual(item.quantity, 1) self.assertEqual(item.cartId, 5)
def get_item(cart_id, item_id): app.logger.info( 'Getting particular item of id: {} in Cart with id: {}'.format( item_id, cart_id)) cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) results = ShoppingCartItems.find(cart_id, item_id) if not results: raise NotFound( 'Cart with id: {} does not have any item with id: {}'.format( cart_id, item_id)) return jsonify([item.serialize() for item in results]), status.HTTP_200_OK
def create_items(cart_id): app.logger.info('Create Item requested') cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('No Cart with id: {} exist'.format(cart_id)) item = ShoppingCartItems() item.deserialize(request.get_json()) item.cartId = cart_id item.add() app.logger.info('Created Item with id: {}'.format(item.id)) return make_response( jsonify(item.serialize()), status.HTTP_201_CREATED, { 'Location': url_for( 'get_item', cart_id=cart_id, item_id=item.id, _external=True) })
def test_update_a_shopcartitem(self): """ Update a ShopCart Item""" item = ShoppingCartItems(productID=1234, price=12.99, quantity=1, cartId=5) item.add() self.assertEqual(item.id, 1) # Change it and save it item.productID = 4321 item.add() self.assertEqual(item.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change items = ShoppingCartItems.all() self.assertEqual(len(items), 1) self.assertEqual(items[0].productID, 4321)
def test_add_a_shopcartitem(self): """ Create an item and add it to the database """ items = ShoppingCartItems.all() self.assertEqual(items, []) item = ShoppingCartItems(productID=1234, price=12.99, quantity=1, cartId=5) self.assertTrue(item != None) self.assertEqual(item.id, None) item.add() # Asert that it was assigned an id and shows up in the database self.assertEqual(item.id, 1) items = ShoppingCartItems.all() self.assertEqual(len(items), 1)
def update_cartitems(cart_id, item_id): """ Update a cart with the given cart ID and item ID """ app.logger.info('Updating cart with id: {} and item {}'.format( cart_id, item_id)) check_content_type('application/json') cart = ShoppingCart.find(cart_id) if not cart: raise NotFound('Cart with id: {} was not found'.format(cart_id)) results = ShoppingCartItems.find(cart_id, item_id) if not results: raise NotFound('CartID: {} does not have item ID {}'.format( cart_id, item_id)) # process the update request for item in results: item.deserialize(request.get_json()) item.id = item_id item.add() app.logger.info('CartID {} and item ID {} has been updated'.format( cart_id, item_id)) return jsonify([item.serialize() for item in results]), status.HTTP_200_OK
def init_db(): """ Initialies the SQLAlchemy app """ global app ShoppingCart.init_db(app) ShoppingCartItems.init_db(app)
def test_deserialize_bad_data(self): """ Test deserialization of bad data """ data = "this is not a ctionary" item = ShoppingCartItems() self.assertRaises(DataValidationError, item.deserialize, data)
def setUp(self): ShoppingCartItems.init_db(app) db.drop_all() # clean up the last tests db.create_all() # make our sqlalchemy tables
def list_items(): """ Returns all of the Cart items """ results = [] app.logger.info('Getting all items of all cart') results = ShoppingCartItems.all() return jsonify([item.serialize() for item in results]), status.HTTP_200_OK