def test_order_creator_source_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = get_frontend_request_for_command(get_frontend_order_state(contact), "source_data", admin_user) response =OrderEditView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert len(data.get("orderLines")) == 5
def test_company_contact_creation(rf, admin_user): get_initial_order_status() contact = create_random_company() test_tax_number = "1234567-1" contact.tax_number = test_tax_number contact.save() contact.default_billing_address.tax_number = test_tax_number contact.default_billing_address.save() state = get_frontend_order_state(contact=contact) state["customer"] = { "id": None, "name": None, "billingAddress": encode_address(contact.default_billing_address), "shipToBillingAddress": True, "saveAddress": True, "isCompany": True } order = get_order_from_state(state, admin_user) assert order.lines.count() == 5 assert order.customer.id != contact.id assert order.customer.name == contact.name assert order.customer.tax_number == test_tax_number assert order.billing_address.tax_number == contact.default_billing_address.tax_number assert order.billing_address.street == contact.default_billing_address.street assert order.billing_address.street == order.shipping_address.street
def test_company_contact_creation(rf, admin_user): get_initial_order_status() contact = create_random_company() test_tax_number = "1234567-1" contact.tax_number = test_tax_number contact.save() contact.default_billing_address.tax_number = test_tax_number contact.default_billing_address.save() state = get_frontend_order_state(contact=contact) state["customer"] = { "id": None, "name": None, "billingAddress": encode_address(contact.default_billing_address), "shipToBillingAddress": True, "saveAddress": True, "isCompany": True } order = get_order_from_state(state, admin_user) assert order.lines.count() == 5 assert order.customer.id != contact.id assert order.customer.name == contact.name assert order.customer.tax_number == test_tax_number assert order.billing_address.tax_number == contact.default_billing_address.tax_number assert order.billing_address.street == contact.default_billing_address.street assert order.billing_address.street == order.shipping_address.street
def test_order_creator_source_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = get_frontend_request_for_command(get_frontend_order_state(contact), "source_data", admin_user) response = OrderCreateView.as_view()(request) data = json.loads(response.content.decode("utf8")) assert len(data.get("orderLines")) == 5
def test_order_creator_invalid_base_data(rf, admin_user): get_initial_order_status() # Needed for the API state = get_frontend_order_state(contact=None) # Remove some critical data... state["customer"]["id"] = None state["shop"]["selected"]["id"] = None request = get_frontend_request_for_command(state, "create", admin_user) response = OrderCreateView.as_view()(request) assert_contains(response, "errorMessage", status_code=400)
def test_order_creator_invalid_base_data(rf, admin_user): get_initial_order_status() # Needed for the API state = get_frontend_order_state(contact=None) # Remove some critical data... state["customer"]["id"] = None state["shop"]["selected"]["id"] = None request = get_frontend_request_for_command(state, "create", admin_user) response = OrderCreateView.as_view()(request) assert_contains(response, "errorMessage", status_code=400)
def test_editing_existing_order(rf, admin_user): modifier = UserFactory() get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact) shop = get_default_shop() order = create_empty_order(shop=shop) order.payment_data = {"payment_data": True} order.shipping_data = {"shipping_data": True} order.extra_data = {"external_id": "123"} order.save() assert order.lines.count() == 0 assert order.pk is not None assert order.modified_by == order.creator request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains( response, "orderIdentifier") # this checks for status codes as a side effect data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) # Re fetch the initial order # Check that identifiers has not changed assert edited_order.identifier == data[ "orderIdentifier"] == order.identifier assert edited_order.pk == order.pk # Check that the product content is updated based on state assert edited_order.lines.count() == 5 assert edited_order.customer == contact # Check that product line have right taxes for line in edited_order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount # Make sure order modification information is correct assert edited_order.modified_by != order.modified_by assert edited_order.modified_by == modifier assert edited_order.modified_on > order.modified_on # Make sure all non handled attributes is preserved from original order assert edited_order.creator == order.creator assert edited_order.ip_address == order.ip_address assert edited_order.orderer == order.orderer assert edited_order.customer_comment == order.customer_comment assert edited_order.marketing_permission == order.marketing_permission assert edited_order.order_date == order.order_date assert edited_order.status == order.status assert edited_order.payment_data == order.payment_data assert edited_order.shipping_data == order.shipping_data assert edited_order.extra_data == order.extra_data
def test_order_creator_valid(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) request = get_frontend_request_for_command(get_frontend_order_state(contact), "create", admin_user) response = OrderCreateView.as_view()(request) assert_contains(response, "orderIdentifier") # this checks for status codes as a side effect data = json.loads(response.content.decode("utf8")) order = Order.objects.get(identifier=data["orderIdentifier"]) assert order.lines.count() == 5 # 3 submitted, two for the shipping and payment method assert order.creator == admin_user assert order.customer == contact
def test_order_creator_invalid_line_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact, valid_lines=False) request = get_frontend_request_for_command(state, "create", admin_user) response = OrderCreateView.as_view()(request) # Let's see that we get a cornucopia of trouble: assert_contains(response, "does not exist", status_code=400) assert_contains(response, "does not have a product", status_code=400) assert_contains(response, "is not available", status_code=400) assert_contains(response, "The price", status_code=400) assert_contains(response, "The quantity", status_code=400)
def test_order_creator_valid(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) order = get_order_from_state(get_frontend_order_state(contact), admin_user) assert order.lines.count() == 5 # 3 submitted, two for the shipping and payment method assert order.creator == admin_user assert order.customer == contact # Check that product line have right taxes for line in order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount
def test_order_creator_valid(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) order = get_order_from_state(get_frontend_order_state(contact), admin_user) assert order.lines.count() == 5 # 3 submitted, two for the shipping and payment method assert order.creator == admin_user assert order.customer == contact # Check that product line have right taxes for line in order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount
def test_order_creator_invalid_line_data(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact, valid_lines=False) request = get_frontend_request_for_command(state, "create", admin_user) response = OrderCreateView.as_view()(request) # Let's see that we get a cornucopia of trouble: assert_contains(response, "does not exist", status_code=400) assert_contains(response, "does not have a product", status_code=400) assert_contains(response, "is not available", status_code=400) assert_contains(response, "The price", status_code=400) assert_contains(response, "The quantity", status_code=400) assert_contains(response, "not visible", status_code=400)
def test_editing_existing_order(rf, admin_user): modifier = UserFactory() get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact) shop = get_default_shop() order = create_empty_order(shop=shop) order.payment_data = {"payment_data": True} order.shipping_data = {"shipping_data": True} order.extra_data = {"external_id": "123"} order.save() assert order.lines.count() == 0 assert order.pk is not None assert order.modified_by == order.creator request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains(response, "orderIdentifier") # this checks for status codes as a side effect data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) # Re fetch the initial order # Check that identifiers has not changed assert edited_order.identifier == data["orderIdentifier"] == order.identifier assert edited_order.pk == order.pk # Check that the product content is updated based on state assert edited_order.lines.count() == 5 assert edited_order.customer == contact # Check that product line have right taxes for line in edited_order.lines.all(): if line.type == OrderLineType.PRODUCT: assert [line_tax.tax.code for line_tax in line.taxes.all()] == ["test_code"] assert line.taxful_price.amount > line.taxless_price.amount # Make sure order modification information is correct assert edited_order.modified_by != order.modified_by assert edited_order.modified_by == modifier assert edited_order.modified_on > order.modified_on # Make sure all non handled attributes is preserved from original order assert edited_order.creator == order.creator assert edited_order.ip_address == order.ip_address assert edited_order.orderer == order.orderer assert edited_order.customer_comment == order.customer_comment assert edited_order.marketing_permission == order.marketing_permission assert edited_order.order_date == order.order_date assert edited_order.status == order.status assert edited_order.payment_data == order.payment_data assert edited_order.shipping_data == order.shipping_data assert edited_order.extra_data == order.extra_data
def test_package(): shop = get_default_shop() supplier = get_default_supplier() package_product = create_product("PackageParent", shop=shop, supplier=supplier) assert not package_product.get_package_child_to_quantity_map() children = [create_product("PackageChild-%d" % x, shop=shop, supplier=supplier) for x in range(4)] package_def = {child: 1 + i for (i, child) in enumerate(children)} package_product.make_package(package_def) assert package_product.mode == ProductMode.PACKAGE_PARENT package_product.save() sp = package_product.get_shop_instance(shop) assert not list(sp.get_orderability_errors(supplier=supplier, quantity=1, customer=AnonymousContact())) with pytest.raises(ValueError): # Test re-packaging fails package_product.make_package(package_def) # Check that OrderCreator can deal with packages source = BasketishOrderSource() source.lines.append(SourceLine( type=OrderLineType.PRODUCT, product=package_product, supplier=get_default_supplier(), quantity=10, unit_price=TaxlessPrice(10), )) source.shop = get_default_shop() source.status = get_initial_order_status() creator = OrderCreator(request=None) order = creator.create_order(source) pids_to_quantities = order.get_product_ids_and_quantities() for child, quantity in six.iteritems(package_def): assert pids_to_quantities[child.pk] == 10 * quantity
def get_order_and_source(admin_user): # create original source to tamper with source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.billing_address = MutableAddress.objects.create(name="Original Billing") source.shipping_address = MutableAddress.objects.create(name="Original Shipping") source.customer = get_person_contact(admin_user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) assert len(source.get_lines()) == 2 source.creator = admin_user creator = OrderCreator() order = creator.create_order(source) return order, source
def seed_source(user, shop): source = BasketishOrderSource(shop) source.status = get_initial_order_status() source.customer = get_person_contact(user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() return source
def seed_source(user, shop): source = BasketishOrderSource(shop) source.status = get_initial_order_status() source.customer = get_person_contact(user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() return source
def get_order_and_source(admin_user): # create original source to tamper with source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.billing_address = MutableAddress.objects.create( name="Original Billing") source.shipping_address = MutableAddress.objects.create( name="Original Shipping") source.customer = get_person_contact(admin_user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), supplier=get_default_supplier(), quantity=1, base_unit_price=source.create_price(10), ) source.add_line( type=OrderLineType.OTHER, quantity=1, base_unit_price=source.create_price(10), require_verification=True, ) assert len(source.get_lines()) == 2 source.creator = admin_user creator = OrderCreator() order = creator.create_order(source) return order, source
def test_campaign_with_non_active_coupon(rf): initial_status = get_initial_order_status() request, shop, group = initialize_test(rf, include_tax=False) order = _get_order_with_coupon(request, initial_status) coupon = order.coupon_usages.first().coupon coupon.active = False coupon.save() modifier = UserFactory() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) assert order.customer != contact state = _get_frontend_order_state(shop, contact) assert order.shop.id == state["shop"]["selected"]["id"] request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains(response, "orderIdentifier") data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) assert edited_order.identifier == data[ "orderIdentifier"] == order.identifier assert edited_order.pk == order.pk assert edited_order.lines.count() == 3 assert OrderLineType.DISCOUNT not in [ l.type for l in edited_order.lines.all() ] assert edited_order.coupon_usages.count() == 0
def test_campaign_with_non_active_coupon(rf): initial_status = get_initial_order_status() request, shop, group = initialize_test(rf, include_tax=False) order = _get_order_with_coupon(request, initial_status) coupon = order.coupon_usages.first().coupon coupon.active = False coupon.save() modifier = UserFactory() contact = create_random_person(locale="en_US", minimum_name_comp_len=5) assert order.customer != contact state = _get_frontend_order_state(shop, contact) assert order.shop.id == state["shop"]["selected"]["id"] request = get_frontend_request_for_command(state, "finalize", modifier) response = OrderEditView.as_view()(request, pk=order.pk) assert_contains(response, "orderIdentifier") data = json.loads(response.content.decode("utf8")) edited_order = Order.objects.get(pk=order.pk) assert edited_order.identifier == data["orderIdentifier"] == order.identifier assert edited_order.pk == order.pk assert edited_order.lines.count() == 3 assert OrderLineType.DISCOUNT not in [l.type for l in edited_order.lines.all()] assert edited_order.coupon_usages.count() == 0
def test_person_contact_creation(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact) state["customer"] = { "id": None, "name": None, "billingAddress": encode_address(contact.default_billing_address), "shipToBillingAddress": True, "saveAddress": True, "isCompany": False } order = get_order_from_state(state, admin_user) assert order.lines.count() == 5 assert order.customer.id != contact.id assert order.customer.name == contact.name assert order.billing_address.name == contact.default_billing_address.name assert order.billing_address.street == contact.default_billing_address.street assert order.billing_address.street == order.shipping_address.street
def test_person_contact_creation(rf, admin_user): get_initial_order_status() # Needed for the API contact = create_random_person(locale="en_US", minimum_name_comp_len=5) state = get_frontend_order_state(contact=contact) state["customer"] = { "id": None, "name": None, "billingAddress": encode_address(contact.default_billing_address), "shipToBillingAddress": True, "saveAddress": True, "isCompany": False } order = get_order_from_state(state, admin_user) assert order.lines.count() == 5 assert order.customer.id != contact.id assert order.customer.name == contact.name assert order.billing_address.name == contact.default_billing_address.name assert order.billing_address.street == contact.default_billing_address.street assert order.billing_address.street == order.shipping_address.street
def create_order(request, creator, customer, product): billing_address = get_address() shipping_address = get_address(name="Shippy Doge") shipping_address.save() order = Order(creator=creator, customer=customer, shop=get_default_shop(), payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status()) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product(order_line=product_order_line, product=product, request=request, quantity=5, supplier=supplier) product_order_line.unit_price = TaxlessPrice(100) assert product_order_line.taxful_total_price.amount > 0 product_order_line.save() product_order_line.taxes.add( OrderLineTax.from_tax(get_default_tax(), product_order_line.taxless_total_price)) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.total_discount = TaxfulPrice(30) assert discount_order_line.taxful_total_discount.amount == 30 assert discount_order_line.taxful_total_price.amount == -30 assert discount_order_line.taxful_unit_price.amount == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base_amount = 5 * 100 tax_value = get_default_tax().calculate_amount(base_amount) assert order.taxful_total_price == base_amount + tax_value - 30, "Math works" shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products( ), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter( pk=order.pk).exists(), "It was paid! Honestly!"
def test_order_creator_parent_linkage(): """ Test OrderCreator creates parent links from OrderSource. """ source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.add_line( line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='parent', text='Parent line', ) source.add_line( line_id='LINE1.1', parent_line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='child1.1', text='Child line 1.1', ) source.add_line( line_id='LINE1.2', parent_line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='child1.2', text='Child line 1.2', ) source.add_line( line_id='LINE1.2.1', parent_line_id='LINE1.2', type=OrderLineType.OTHER, quantity=1, sku='child1.2.1', text='Child line 1.2.1', ) source.add_line( line_id='LINE1.3', parent_line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='child1.3', text='Child line 1.3', ) order = OrderCreator().create_order(source) lines = [prettify_order_line(line) for line in order.lines.all()] assert lines == [ '#0 1 x parent', '#1 1 x child1.1, child of #0', '#2 1 x child1.2, child of #0', '#3 1 x child1.2.1, child of #2', '#4 1 x child1.3, child of #0', ]
def test_campaign_with_coupons(rf): status = get_initial_order_status() request, shop, group = initialize_test(rf, False) basket = get_basket(request) supplier = get_default_supplier() for x in range(2): product = create_product(printable_gibberish(), shop, supplier=supplier, default_price="50") basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) dc = Coupon.objects.create(code="TEST", active=True) campaign = BasketCampaign.objects.create(shop=shop, name="test", public_name="test", coupon=dc, active=True) BasketDiscountAmount.objects.create( discount_amount=shop.create_price("20"), campaign=campaign) rule = BasketTotalProductAmountCondition.objects.create(value=2) campaign.conditions.add(rule) campaign.save() assert len(basket.get_final_lines() ) == 2 # no discount was applied because coupon is required basket.add_code(dc.code) assert len(basket.get_final_lines() ) == 3 # now basket has codes so they will be applied too assert OrderLineType.DISCOUNT in [l.type for l in basket.get_final_lines()] # Ensure codes persist between requests, so do what the middleware would, i.e. basket.save() # and then reload the basket: del request.basket basket = get_basket(request) assert basket.codes == [dc.code] assert len(basket.get_final_lines() ) == 3 # now basket has codes so they will be applied too assert OrderLineType.DISCOUNT in [l.type for l in basket.get_final_lines()] basket.status = status creator = OrderCreator(request) order = creator.create_order(basket) assert CouponUsage.objects.filter(order=order).count() == 1 assert CouponUsage.objects.filter(order=order, coupon__code=dc.code).count() == 1
def test_order_address_immutability_unsaved_address(save): billing_address = get_address() if save: billing_address.save() order = Order(shop=get_default_shop(), billing_address=billing_address.to_immutable(), order_date=now(), status=get_initial_order_status()) order.save() order.billing_address.name = "Mute Doge" with pytest.raises(ValidationError): order.billing_address.save()
def seed_source(user): source = BasketishOrderSource(get_default_shop()) billing_address = get_address() shipping_address = get_address(name="Shippy Doge") source.status = get_initial_order_status() source.billing_address = billing_address source.shipping_address = shipping_address source.customer = get_person_contact(user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() assert source.payment_method_id == get_default_payment_method().id assert source.shipping_method_id == get_default_shipping_method().id return source
def seed_source(user): source = BasketishOrderSource(get_default_shop()) billing_address = get_address() shipping_address = get_address(name="Shippy Doge") source.status = get_initial_order_status() source.billing_address = billing_address source.shipping_address = shipping_address source.customer = get_person_contact(user) source.payment_method = get_default_payment_method() source.shipping_method = get_default_shipping_method() assert source.payment_method_id == get_default_payment_method().id assert source.shipping_method_id == get_default_shipping_method().id return source
def test_order_address_immutability_unsaved_address(save): billing_address = get_address() if save: billing_address.save() order = Order( shop=get_default_shop(), billing_address=billing_address, order_date=now(), status=get_initial_order_status() ) order.save() order.billing_address.name = "Mute Doge" with pytest.raises(ImmutabilityError): order.billing_address.save()
def get_order_source_with_a_package(): package_product = get_package_product() source = BasketishOrderSource(get_default_shop()) source.add_line( type=OrderLineType.PRODUCT, product=package_product, supplier=get_default_supplier(), quantity=10, base_unit_price=source.create_price(10), sku=package_product.sku, text=package_product.name, ) source.status = get_initial_order_status() return source
def get_order_source_with_a_package(): package_product = get_package_product() source = BasketishOrderSource(get_default_shop()) source.add_line( type=OrderLineType.PRODUCT, product=package_product, supplier=get_default_supplier(), quantity=10, base_unit_price=source.create_price(10), sku=package_product.sku, text=package_product.name, ) source.status = get_initial_order_status() return source
def create_order(request, creator, customer, product): billing_address = get_address() shipping_address = get_address(name="Shippy Doge") shipping_address.save() order = Order( creator=creator, customer=customer, shop=get_default_shop(), payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status() ) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product(order_line=product_order_line, product=product, request=request, quantity=5, supplier=supplier) product_order_line.unit_price = TaxlessPrice(100) assert product_order_line.taxful_total_price.amount > 0 product_order_line.save() product_order_line.taxes.add(OrderLineTax.from_tax(get_default_tax(), product_order_line.taxless_total_price)) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.total_discount = TaxfulPrice(30) assert discount_order_line.taxful_total_discount.amount == 30 assert discount_order_line.taxful_total_price.amount == -30 assert discount_order_line.taxful_unit_price.amount == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base_amount = 5 * 100 tax_value = get_default_tax().calculate_amount(base_amount) assert order.taxful_total_price == base_amount + tax_value - 30, "Math works" shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products(), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter(pk=order.pk).exists(), "It was paid! Honestly!"
def test_order_creator_parent_linkage(): """ Test OrderCreator creates parent links from OrderSource. """ source = BasketishOrderSource(get_default_shop()) source.status = get_initial_order_status() source.add_line( line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='parent', text='Parent line', ) source.add_line( line_id='LINE1.1', parent_line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='child1.1', text='Child line 1.1', ) source.add_line( line_id='LINE1.2', parent_line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='child1.2', text='Child line 1.2', ) source.add_line( line_id='LINE1.2.1', parent_line_id='LINE1.2', type=OrderLineType.OTHER, quantity=1, sku='child1.2.1', text='Child line 1.2.1', ) source.add_line( line_id='LINE1.3', parent_line_id='LINE1', type=OrderLineType.OTHER, quantity=1, sku='child1.3', text='Child line 1.3', ) order = OrderCreator().create_order(source) lines = [prettify_order_line(line) for line in order.lines.all()] assert lines == [ '#0 1 x parent', '#1 1 x child1.1, child of #0', '#2 1 x child1.2, child of #0', '#3 1 x child1.2.1, child of #2', '#4 1 x child1.3, child of #0', ]
def test_campaign_with_coupons(rf): status = get_initial_order_status() request, shop, group = initialize_test(rf, False) basket = get_basket(request) supplier = get_default_supplier() for x in range(2): product = create_product(printable_gibberish(), shop, supplier=supplier, default_price="50") basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) dc = Coupon.objects.create(code="TEST", active=True) campaign = BasketCampaign.objects.create( shop=shop, name="test", public_name="test", coupon=dc, discount_amount_value=shop.create_price("20"), active=True ) rule = BasketTotalProductAmountCondition.objects.create(value=2) campaign.conditions.add(rule) campaign.save() assert len(basket.get_final_lines()) == 2 # no discount was applied because coupon is required basket.add_code(dc.code) assert len(basket.get_final_lines()) == 3 # now basket has codes so they will be applied too assert OrderLineType.DISCOUNT in [l.type for l in basket.get_final_lines()] # Ensure codes persist between requests, so do what the middleware would, i.e. basket.save() # and then reload the basket: del request.basket basket = get_basket(request) assert basket.codes == [dc.code] assert len(basket.get_final_lines()) == 3 # now basket has codes so they will be applied too assert OrderLineType.DISCOUNT in [l.type for l in basket.get_final_lines()] basket.status = status creator = OrderCreator(request) order = creator.create_order(basket) assert CouponUsage.objects.filter(order=order).count() == 1 assert CouponUsage.objects.filter(order=order, coupon__code=dc.code).count() == 1
def create_order(request, creator, customer, product): billing_address = get_address().to_immutable() shipping_address = get_address(name="Shippy Doge").to_immutable() shipping_address.save() shop = request.shop order = Order( creator=creator, customer=customer, shop=shop, payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status(), currency=shop.currency, prices_include_tax=shop.prices_include_tax, ) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product( pricing_context=request, order_line=product_order_line, product=product, quantity=5, supplier=supplier ) product_order_line.base_unit_price = shop.create_price(100) assert product_order_line.price.value > 0 product_order_line.save() line_tax = get_line_taxes_for(product_order_line)[0] product_order_line.taxes.add( OrderLineTax.from_tax(tax=line_tax.tax, base_amount=line_tax.base_amount, order_line=product_order_line) ) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.discount_amount = shop.create_price(30) assert discount_order_line.discount_amount.value == 30 assert discount_order_line.price.value == -30 assert discount_order_line.base_unit_price.value == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base = 5 * shop.create_price(100).amount discount = shop.create_price(30).amount tax_value = line_tax.amount if not order.prices_include_tax: assert order.taxless_total_price.amount == base - discount assert order.taxful_total_price.amount == base + tax_value - discount else: assert_almost_equal(order.taxless_total_price.amount, base - tax_value - discount) assert_almost_equal(order.taxful_total_price.amount, base - discount) shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products(), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter(pk=order.pk).exists(), "It was paid! Honestly!"
def create_order(request, creator, customer, product): billing_address = get_address().to_immutable() shipping_address = get_address(name="Shippy Doge").to_immutable() shipping_address.save() shop = request.shop order = Order( creator=creator, customer=customer, shop=shop, payment_method=get_default_payment_method(), shipping_method=get_default_shipping_method(), billing_address=billing_address, shipping_address=shipping_address, order_date=now(), status=get_initial_order_status(), currency=shop.currency, prices_include_tax=shop.prices_include_tax, ) order.full_clean() order.save() supplier = get_default_supplier() product_order_line = OrderLine(order=order) update_order_line_from_product(pricing_context=request, order_line=product_order_line, product=product, quantity=5, supplier=supplier) assert product_order_line.text == product.safe_translation_getter("name") product_order_line.base_unit_price = shop.create_price(100) assert product_order_line.price.value > 0 product_order_line.save() line_tax = get_line_taxes_for(product_order_line)[0] product_order_line.taxes.add( OrderLineTax.from_tax( tax=line_tax.tax, base_amount=line_tax.base_amount, order_line=product_order_line, )) discount_order_line = OrderLine(order=order, quantity=1, type=OrderLineType.OTHER) discount_order_line.discount_amount = shop.create_price(30) assert discount_order_line.discount_amount.value == 30 assert discount_order_line.price.value == -30 assert discount_order_line.base_unit_price.value == 0 discount_order_line.save() order.cache_prices() order.check_all_verified() order.save() base = 5 * shop.create_price(100).amount discount = shop.create_price(30).amount tax_value = line_tax.amount if not order.prices_include_tax: assert order.taxless_total_price.amount == base - discount assert order.taxful_total_price.amount == base + tax_value - discount else: assert_almost_equal(order.taxless_total_price.amount, base - tax_value - discount) assert_almost_equal(order.taxful_total_price.amount, base - discount) shipment = order.create_shipment_of_all_products(supplier=supplier) assert shipment.total_products == 5, "All products were shipped" assert shipment.weight == product.net_weight * 5, "Gravity works" assert not order.get_unshipped_products( ), "Nothing was left in the warehouse" order.create_payment(order.taxful_total_price) assert order.is_paid() assert Order.objects.paid().filter( pk=order.pk).exists(), "It was paid! Honestly!"