def test_chai_apples(self): cust = customers.get_customer_by_customer_name('Arun') customer = Customer(cust.name, cust.eid, cust.aid, cust.joinDate) test_chai_apples_cart = Cart(customer) test_chai_apples_cart.add_multiple_products_to_cart(['CH1', 'AP1']) total = test_chai_apples_cart.display()[1] self.assertEqual(total, 9.11)
def post(self, user_id): args = parser.parse_args() response = {} # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='post') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (Cart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 # Authorized buyer product_id = args['product_id'] count = args['count'] cart = CartDB.add_document(user_id, product_id, count) cart.save() products = get_cart_json(cart) products_details = Cart.get_products_details(products) # log_info(products_details) response = Cart.create_json_body(products, products_details) return response, 201 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500
def get(self, user_id): args = parser.parse_args() response = {} # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='get') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (Cart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 cart = CartDB.objects(user_id=user_id).first() if cart is None: response['status'] = 'success' response['msg'] = 'Empty' response['data'] = [] return response, 200 products = get_cart_json(cart) products_details = Cart.get_products_details(products) response = Cart.create_json_body(products, products_details) return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500
def test_chai_apples(self): test_chai_apples_cart = Cart() test_chai_apples_cart.add_multiple_products_to_cart(['CH1', 'AP1']) total = test_chai_apples_cart.display()[1] self.assertEqual(total, 9.11)
def test_big_cart(self): test_big_cart_cart = Cart() test_big_cart_cart.add_multiple_products_to_cart( TestData.random_items_10k) total = test_big_cart_cart.display()[1] self.assertEqual(total, 38867.35)
def test_oatmeal_apples(self): test_oatmeal_apples_cart = Cart() test_oatmeal_apples_cart.add_multiple_products_to_cart( ['OM1', 'AP1', 'AP1', 'AP1']) total = test_oatmeal_apples_cart.display()[1] self.assertEqual(total, 14.94)
def test_free_with_limits(self): test_free_with_limits_cart = Cart() test_free_with_limits_cart.add_multiple_products_to_cart( ['CH1', 'MK1', 'CH1', 'MK1']) total = test_free_with_limits_cart.display()[1] self.assertEqual(total, 10.97)
def test_coffee(self): test_coffee_cart = Cart() test_coffee_cart.add_multiple_products_to_cart(['CF1', 'CF1']) total = test_coffee_cart.display()[1] self.assertEqual(total, 11.23)
def test_milk_apples(self): test_milk_apples_cart = Cart() test_milk_apples_cart.add_multiple_products_to_cart(['MK1', 'AP1']) total = test_milk_apples_cart.display()[1] self.assertEqual(total, 10.75)
def test_chai_apples_coffee_milk(self): test_chai_apples_coffee_milk_cart = Cart() test_chai_apples_coffee_milk_cart.add_multiple_products_to_cart( ['CH1', 'AP1', 'CF1', 'MK1']) total = test_chai_apples_coffee_milk_cart.display()[1] self.assertEqual(total, 20.34)
def delete(self, user_id, product_id, operation): args = parser.parse_args() response = {} if operation != 'delete': return {'status': 'fail', 'msg': 'Invalid endpoint'}, 400 # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='delete') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (UpdateCart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 # Authorized buyer cart = CartDB.objects(user_id=user_id).first() if cart is None: response['status'] = 'fail' response['msg'] = 'Empty' response['data'] = [] return response, 403 for product in cart.products: if product_id == product.product_id: # Select specified product cart.products.remove(product) if len(cart.products) == 0: # If specified product count is 0 cart.delete() else: cart.save() break products = get_cart_json(cart) products_details = UpdateCart.get_products_details(products) # log_info(products_details) response = UpdateCart.create_json_body(products, products_details) return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500
def delete(self, user_id): args = parser.parse_args() response = {} # Check aruments are valid or not error_mssgs = check_valid_request(args, ops='delete') # log_info(error_mssgs) # Error for invalid arguments if error_mssgs: return {'status': 'fail', 'msg': error_mssgs}, 400 try: content, status_code = (UpdateCart.decode_token( token=args['Authorization'])) if content['status'] == 'success': if user_id != content['data']['user_id']: response['status'] = 'fail' response['msg'] = 'Invalid Token' return response, 403 # Authorized buyer cart = CartDB.objects(user_id=user_id).first() if cart is None: response['status'] = 'fail' response['msg'] = 'Empty' response['data'] = [] return response, 403 cart.delete() response['status'] = 'success' response['msg'] = 'Products deleted from cart' response['data'] = [] return response, 200 elif content['status'] == 'fail': return content, status_code except KeyError as e: response['status'] = 'fail' response['msg'] = str(e) + ' KeyError' response['data'] = [] return response, 400 except ValidationError as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 400 except Exception as e: response['status'] = 'fail' response['msg'] = str(e) response['data'] = [] return response, 500