def test_deserialize_a_shopcart(self): """ Test deserialization of a Shopcart """ data = {"user_id": 120} shopcart = Shopcart() shopcart.deserialize(data) self.assertNotEqual(shopcart, None) self.assertEqual(shopcart.user_id, 120)
def test_deserialize_a_shopcart(self): """ Test deserialization of a Shopcart """ data = {"id": 1, "product_id": 1, "customer_id": 1, "quantity": 2, "price": "45.66", "text": "Headphones","state":1} shopcart = Shopcart() shopcart.deserialize(data) self.assertNotEqual(shopcart, None) self.assertEqual(shopcart.id, None) self.assertEqual(shopcart.product_id,1) self.assertEqual(shopcart.customer_id,1) self.assertEqual(shopcart.quantity, 2) self.assertEqual(shopcart.price,"45.66") self.assertEqual(shopcart.text,"Headphones") self.assertEqual(shopcart.state,None)
def put(self, customer_id, product_id): """ Update an item from shopcart This endpoint will update a item for the selected product in the shopcart """ app.logger.info( 'Request to update shopcart item with customer_id: %s, product_id: %s', customer_id, product_id) cart_item = Shopcart.find_by_customer_id_and_product_id( customer_id, product_id) if not cart_item: app.logger.info( "Customer_id and product_id for update have not been found") api.abort( status.HTTP_400_BAD_REQUEST, 'No product with id [{}] found for customer id [{}].'.format( product_id, customer_id)) app.logger.debug('Payload = %s', api.payload) data = api.payload update_cart_item = Shopcart() update_cart_item.deserialize(data) try: requested_quantity = int(update_cart_item.quantity) app.logger.info("requested_quantity = %s", requested_quantity) except ValueError: app.logger.info('Non-integer quantity requested') api.abort(status.HTTP_400_BAD_REQUEST, 'Non-integer quantity given') # bounds check if requested_quantity < 1: app.logger.info('Negative quantity requested') api.abort( status.HTTP_400_BAD_REQUEST, 'No positive product with id [{}] found for customer id [{}].'. format(product_id, customer_id)) # process to update the request cart_item.quantity = requested_quantity app.logger.info("cart_item.quantity = %s", cart_item.quantity) cart_item.state = SHOPCART_ITEM_STAGE['ADDED'] cart_item.save() app.logger.info( 'Quantity for customer id %s and product id %s has been updated', customer_id, product_id) return cart_item.serialize(), status.HTTP_200_OK
def post(self): """ Create a Shopcart """ logger.info("Request to create a shopcart") check_content_type("application/json") logger.debug('Payload = %s', api.payload) shopcart = None if 'user_id' in api.payload: shopcart = Shopcart.find_by_user(api.payload['user_id']).first() if shopcart is None: shopcart = Shopcart() shopcart.deserialize(api.payload) shopcart.create() logger.info("Shopcart with ID [%s] created.", shopcart.id) location_url = api.url_for(ShopcartResource, shopcart_id=shopcart.id, _external=True) return shopcart.serialize(), status.HTTP_201_CREATED, {"Location": location_url}
def post(self, customer_id): """ Creates a new item entry for the cart """ app.logger.info('Request to create shopcart item for costomer: %s', customer_id) check_content_type('application/json') if not customer_id == int(api.payload['customer_id']): app.logger.info("Coustomer id doesn't match") abort(400, description="Coustomer id doesn't match") product_id = api.payload['product_id'] if Shopcart.check_cart_exist(customer_id, product_id): abort(409, description="Item already in the cart") shopcart = Shopcart() shopcart.deserialize(api.payload) shopcart.save() location_url = api.url_for(ShopcartItem, customer_id=customer_id, product_id=product_id, _extrenal=True) return shopcart.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }