def setUpClass(cls):
     """ This runs once before the entire test suite """
     app.config['TESTING'] = True
     app.config['DEBUG'] = False
     app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
     app.logger.setLevel(logging.CRITICAL)
     ShopCart.init_db(app)
 def test_deserialize_cart(self):
     """ Deserializes a ShopCart """
     item = self._create_item()
     shopcart = self._create_shopcart(items=[item])
     serial_shopcart = shopcart.serialize()
     new_shopcart = ShopCart()
     new_shopcart.deserialize(serial_shopcart)
     self.assertEqual(new_shopcart.id, shopcart.id)
 def test_add_shopcart(self):
     """ Create a ShopCart -- Add it to the database """
     shopcarts = ShopCart.all()
     self.assertEqual(shopcarts, [])
     shopcart = self._create_shopcart()
     shopcart.create()
     # Assert that it was assigned an id and shows up in the database
     self.assertEqual(shopcart.id, 1)
     shopcarts = ShopCart.all()
     self.assertEqual(len(shopcarts), 1)
Esempio n. 4
0
def list_shopcarts():
    """ Returns IDs of the ShopCarts """
    app.logger.info("Request for ShopCart list")
    shopcarts = []
    id = request.args.get("id")
    if id:
        shopcarts = ShopCart.find(id)
    else:
        shopcarts = ShopCart.all()

    results = [shopcart.serialize() for shopcart in shopcarts]
    return make_response(jsonify(results), status.HTTP_200_OK)
Esempio n. 5
0
def delete_all_shopcarts():
    """ Returns IDs of the ShopCarts """
    app.logger.info("Request for ShopCart list")
    shopcarts = []
    id = request.args.get("id")
    if id:
        shopcarts = ShopCart.find(id)
    else:
        shopcarts = ShopCart.all()

    results = [shopcart.delete() for shopcart in shopcarts]
    return make_response("", status.HTTP_204_NO_CONTENT)
Esempio n. 6
0
def clear_shopcart (shopcart_id):
    """ Returns all of the items within the shopcart """
    app.logger.info("Request to clear items from the shopping cart")

    shopcart = ShopCart.find_or_404(shopcart_id)
    results = [item.delete() for item in shopcart.items]
    return make_response("", status.HTTP_204_NO_CONTENT)
 def _create_shopcart(self, items=[]):
     """ Creates a ShopCart from Factory """
     fake_shopcart = ShopCartFactory()
     shopcart = ShopCart(customer_id=fake_shopcart.customer_id, items=items)
     self.assertTrue(shopcart != None)
     self.assertEqual(shopcart.id, None)
     return shopcart
 def test_create_shopcart(self):
     """ Create a ShopCart -- Asserts that it exists """
     fake_shopcart = ShopCartFactory()
     shopcart = ShopCart(customer_id=fake_shopcart.customer_id)
     self.assertTrue(shopcart != None)
     self.assertEqual(shopcart.id, None)
     self.assertEqual(shopcart.customer_id, fake_shopcart.customer_id)
Esempio n. 9
0
def list_items(shopcart_id):
    """ Returns all of the items within the shopcart """
    app.logger.info("Request to list items from the shopping cart")

    shopcart = ShopCart.find_or_404(shopcart_id)
    results = [item.serialize() for item in shopcart.items]
    return make_response(jsonify(results), status.HTTP_200_OK)
Esempio n. 10
0
def get_shopcarts(shopcart_id):
    """
    Retrieve a single ShopCart
    This endpoint will return an ShopCart based on its id
    """
    app.logger.info("Request for ShopCart with id: %s", shopcart_id)
    shopcart = ShopCart.find_or_404(shopcart_id)
    return make_response(jsonify(shopcart.serialize()), status.HTTP_200_OK)
Esempio n. 11
0
def delete_shopcarts(shopcart_id):
    """
    Delete a shopcart
    This endpoint will delete an shopcart based the id specified in the path
    """
    app.logger.info("Request to delete shopcart with id: %s", shopcart_id)
    shopcart = ShopCart.find(shopcart_id)
    if shopcart:
        shopcart.delete()
    return make_response("", status.HTTP_204_NO_CONTENT)
    def test_delete_shopcart_item(self):
        """ Delete a ShopCart item """
        shopcarts = ShopCart.all()
        self.assertEqual(shopcarts, [])

        item = self._create_item()
        shopcart = self._create_shopcart(items=[item])
        shopcart.create()
        # Assert that it was assigned an id and shows up in the database
        self.assertEqual(shopcart.id, 1)
        shopcarts = ShopCart.all()
        self.assertEqual(len(shopcarts), 1)

        # Fetch it back
        shopcart = ShopCart.find(shopcart.id)
        item = shopcart.items[0]
        item.delete()
        shopcart.save()

        # Fetch it back again
        shopcart = ShopCart.find(shopcart.id)
        self.assertEqual(len(shopcart.items), 0)
Esempio n. 13
0
def update_shopcarts(shopcart_id):
    """
    Update an existing shopcart
    This endpoint will update an shopcart based the body that is posted
    """
    app.logger.info("Request to update shopcart with id: %s", shopcart_id)
    check_content_type("application/json")
    shopcart = ShopCart.find(shopcart_id)
    if not shopcart:
        raise NotFound("ShopCart with id '{}' was not found.".format(shopcart_id))
    shopcart.deserialize(request.get_json())
    shopcart.id = shopcart_id
    shopcart.save()
    return make_response(jsonify(shopcart.serialize()), status.HTTP_200_OK)
Esempio n. 14
0
def create_items(shopcart_id):
    """
    Create an Item in a Shopcart
    This endpoint will add an item to a shopcart
    """
    app.logger.info("Request to add an item to a shopcart")
    check_content_type("application/json")
    shopcart = ShopCart.find_or_404(shopcart_id)
    item = CartItem()
    item.deserialize(request.get_json())
    shopcart.items.append(item)
    shopcart.save()
    message = item.serialize()
    return make_response(jsonify(message), status.HTTP_201_CREATED)
Esempio n. 15
0
def create_shopcarts():
    """
    Creates a shopping cart
    This endpoint will create a Shopcart based the data in the body that is posted
    """
    app.logger.info("Request to create a ShopCart")
    app.logger.info(request.get_json())
    check_content_type("application/json")
    shopcart = ShopCart()
    shopcart.deserialize(request.get_json())
    shopcart.create()
    message = shopcart.serialize()
    location_url = url_for("get_shopcarts", shopcart_id=shopcart.id, _external=True)
    return make_response(
        jsonify(message), status.HTTP_201_CREATED, {"Location": location_url}
    ) 
 def test_deserialize_cart_type_error(self):
     """ Deserializes a ShopCart with a TypeError """
     shopcart = ShopCart()
     self.assertRaises(DataValidationError, shopcart.deserialize, [])
 def test_deserialize_cart_key_error(self):
     """ Deserializes a ShopCart with a KeyError """
     shopcart = ShopCart()
     self.assertRaises(DataValidationError, shopcart.deserialize, {})
Esempio n. 18
0
def init_db():
    """ Initialies the SQLAlchemy app """
    global app
    ShopCart.init_db(app)