def test_initOrder_product_unavailable(self, app): product = Product(name="Brown eggs", type="dairy", description="Raw organic brown eggs in a basket", image="0.jpg", height=600, weight=400, price=28.1, rating=5, in_stock=False) product.save() response = OrderServices.initOrder( data={"product": { "id": 1, "quantity": 1 }}) assert response == { 'orderInitialized': False, 'object': { 'errors': { 'product': { "code": "out-of-inventory", "name": "The product you asked for is not in the inventory for now." } } } }
def test_setCreditCard_order_already_paid(self, app): product = Product(name="Brown eggs", type="dairy", description="Raw organic brown eggs in a basket", image="0.jpg", height=600, weight=400, price=28.1, rating=5, in_stock=True) product.save() shipping_information = ShippingInformation( country="Canada", address="201, rue des rosiers", postal_code="G7X 3Y9", city="Chicoutimi", province="QC") shipping_information.save() order = Order(product=product, product_quantity=1, email="*****@*****.**", shipping_information=shipping_information, paid=True) order.setShippingPrice() order.setTotalPrice() order.save() response = OrderServices.setCreditCard(cCardDict={ "credit_card": { "name": "John Doe", "number": "4242 4242 4242 4242", "expiration_year": 2024, "cvv": "123", "expiration_month": 9 } }, orderId=1) assert response == { 'set': False, "object": { "errors": { "order": { "code": "already-paid", "name": "The order has already been paid." } } }, "status_code": 422 }
def test_load_product_db(app): with app.app_context(): products = test_product_get_request() for product in products['products']: Product.create(name=product['name'], type=product['type'], description=product['description'], image=product['image'], height=product['height'], weight=product['weight'], price=product['price'], rating=product['rating'], in_stock=product['in_stock']) assert Product.select().count() == 50
def test_CheckExistance(app): with app.app_context(): p = test_product_get_request() x = CheckExistance_Test(app, p['products'][0]) assert x == False Product.create(id=p['products'][0]['id'], name=p['products'][0]['name'], type=p['products'][0]['type'], description=p['products'][0]['description'], image=p['products'][0]['image'], height=p['products'][0]['height'], weight=p['products'][0]['weight'], price=p['products'][0]['price'], rating=p['products'][0]['rating'], in_stock=p['products'][0]['in_stock']) assert CheckExistance_Test(app, p['products'][0]) == True
def test_setCreditCard_missing_client_informations(self, app): product = Product(name="Brown eggs", type="dairy", description="Raw organic brown eggs in a basket", image="0.jpg", height=600, weight=400, price=28.1, rating=5, in_stock=True) product.save() order = Order(product=product, product_quantity=1) order.save() response = OrderServices.setCreditCard(cCardDict={ "credit_card": { "name": "John Doe", "number": "4242 4242 4242 4242", "expiration_year": 2024, "cvv": "123", "expiration_month": 9 } }, orderId=1) assert response == { 'set': False, "object": { "errors": { "order": { "code": "missing-fields", "name": "Client informations are required before applying a credit card to the order." } } }, "status_code": 422 }
def test_initOrder_invalid_quantity(self, app): product = Product(name="Brown eggs", type="dairy", description="Raw organic brown eggs in a basket", image="0.jpg", height=600, weight=400, price=28.1, rating=5, in_stock=True) product.save() response = OrderServices.initOrder( data={"product": { "id": 1, "quantity": -1 }}) assert response == {'orderInitialized': False, 'object': {'errors': { 'product': {"code": "missing-fields", "name": "The creation of an order requires a single product. " \ "The product dict must have the following form: { 'product': " \ "{ 'id': id, 'quantity': quantity } }. Quantity must be an integer > 0."}}}}
def test_set_order_total_price(): product_price = 200 product_quantity = 2 total_price = product_price * product_quantity product = Product(id="1", name="product 1", type="type 1", description="description 1", image="image 1", price=product_price) order = Order(product=product, product_quantity=product_quantity) order.setTotalPrice() assert order.total_price == total_price
def test_set_order_shipping_price_with_2001g_weight(): product_price = 200 product_quantity = 1 product_weight = 2001 product = Product(id="1", name="product 1", type="type 1", description="description 1", image="image 1", price=product_price, weight=product_weight) order = Order(product=product, product_quantity=product_quantity) order.setShippingPrice() assert order.shipping_price == 25.00
def ProductsGET(): prod = Product.select() prodsDict = {"products": []} for p in prod: prodsDict["products"].append({ 'id': p.id, 'name': p.name, 'type': p.type, 'description': p.description, 'image': p.image, "height": p.height, "weight": p.weight, "price": p.price, "rating": p.rating, "in_stock": p.in_stock }) return app.response_class(response=json.dumps(prodsDict), status=200, mimetype='application/json')
def CheckQuantity(pID, pQuantity): p=Product.get_or_none(Product.id==pID) if (pQuantity > 0) and (p.in_stock is True): return True else: return False
def CheckAvailability(pID): p = Product.get_or_none(Product.id == pID) if p is not None: return True else: return False