def test_delete_a_cart(self): """ Delete a Cart """ shopcart = ShoppingCart(state="activated", userId=5) shopcart.save() self.assertEqual(len(ShoppingCart.all()), 1) # delete the cart and make sure it isn't in the database shopcart.delete() self.assertEqual(len(ShoppingCart.all()), 0)
def test_add_a_cart(self): """ Create a cart and add it to the database """ shopcarts = ShoppingCart.all() self.assertEqual(shopcarts, []) shopcart = ShoppingCart(state="activated", userId=5) self.assertTrue(shopcart != None) self.assertEqual(shopcart.id, None) shopcart.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(shopcart.id, 1) shopcarts = ShoppingCart.all() self.assertEqual(len(shopcarts), 1)
def list_carts(): """ Returns all of the Carts """ if request.args.get('userId'): user_id = request.args.get('userId') app.logger.info('Getting Cart for user with id: {}'.format(user_id)) results = ShoppingCart.find_by_user(user_id) if not results: raise NotFound( 'Cart with user id: {} was not found'.format(user_id)) return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK elif request.args.get('state'): state = request.args.get('state') app.logger.info('Getting Carts with state: {}'.format(state)) results = ShoppingCart.find_by_state(state) if not results: raise NotFound('Carts with state: {} was not found'.format(state)) return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK else: results = [] app.logger.info('Getting all Carts') results = ShoppingCart.all() return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK
def test_update_a_cart(self): """ Update a ShopCart """ shopcart = ShoppingCart(state="activated", userId=5) shopcart.save() self.assertEqual(shopcart.id, 1) # Change it an save it shopcart.state = "deactivated" shopcart.save() self.assertEqual(shopcart.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change shopcarts = ShoppingCart.all() self.assertEqual(len(shopcarts), 1) self.assertEqual(shopcarts[0].state, "deactivated")