def test_limited_methods(): """ Test that products can declare that they limit available shipping methods. """ unique_shipping_method = ShippingMethod(tax_class=get_default_tax_class(), module_data={"price": 0}) unique_shipping_method.save() shop = get_default_shop() common_product = create_product(sku="SH_COMMON", shop=shop) # A product that does not limit shipping methods unique_product = create_product(sku="SH_UNIQUE", shop=shop) # A product that only supports unique_shipping_method unique_shop_product = unique_product.get_shop_instance(shop) unique_shop_product.limit_shipping_methods = True unique_shop_product.shipping_methods.add(unique_shipping_method) unique_shop_product.save() impossible_product = create_product(sku="SH_IMP", shop=shop) # A product that can't be shipped at all imp_shop_product = impossible_product.get_shop_instance(shop) imp_shop_product.limit_shipping_methods = True imp_shop_product.save() for product_ids, method_ids in [ ((common_product.pk, unique_product.pk), (unique_shipping_method.pk,)), ((common_product.pk,), ShippingMethod.objects.values_list("pk", flat=True)), ((unique_product.pk,), (unique_shipping_method.pk,)), ((unique_product.pk, impossible_product.pk), ()), ((common_product.pk, impossible_product.pk), ()), ]: product_ids = set(product_ids) assert ShippingMethod.objects.available_ids(shop=shop, products=product_ids) == set(method_ids)
def test_weight_limits(): sm = ShippingMethod(tax_class=get_default_tax_class()) sm.module_data = {"min_weight": "100", "max_weight": "500"} source = BasketishOrderSource(get_default_shop()) assert any(ve.code == "min_weight" for ve in sm.get_validation_errors(source)) source.add_line(type=OrderLineType.PRODUCT, weight=600) assert any(ve.code == "max_weight" for ve in sm.get_validation_errors(source))
def get_expensive_sweden_shipping_method(): sm = ShippingMethod( identifier=ExpensiveSwedenShippingModule.identifier, module_identifier=ExpensiveSwedenShippingModule.identifier, tax_class=get_default_tax_class(), ) sm.module_data = {"min_weight": "0.11000000", "max_weight": "3.00000000", "price_waiver_product_minimum": "370"} sm.save() return sm
def get_expensive_sweden_shipping_method(): sm = ShippingMethod( identifier=ExpensiveSwedenShippingModule.identifier, module_identifier=ExpensiveSwedenShippingModule.identifier, tax_class=get_default_tax_class()) sm.module_data = { "min_weight": "0.11000000", "max_weight": "3.00000000", "price_waiver_product_minimum": "370" } sm.save() return sm
def test_waiver(): sm = ShippingMethod( name="Waivey", tax_class=get_default_tax_class(), module_data={"price_waiver_product_minimum": "370", "price": "100"}, ) source = BasketishOrderSource(get_default_shop()) assert sm.get_effective_name(source) == u"Waivey" assert sm.get_effective_price_info(source).price == source.shop.create_price(100) source.add_line( type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.shop.create_price(400), quantity=1, ) assert sm.get_effective_price_info(source).price == source.shop.create_price(0)
def test_basket_campaign_case2(rf): request, shop, group = initialize_test(rf, False) price = shop.create_price basket = get_basket(request) supplier = get_default_supplier() # create a basket rule that requires atleast value of 200 rule = BasketTotalAmountCondition.objects.create(value="200") single_product_price = "50" discount_amount_value = "10" unique_shipping_method = ShippingMethod(tax_class=get_default_tax_class(), module_data={"price": 50}) unique_shipping_method.save() for x in range(3): product = create_product( printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price ) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) assert basket.product_count == 3 campaign = BasketCampaign.objects.create( shop=shop, public_name="test", name="test", discount_amount_value=discount_amount_value, active=True ) campaign.conditions.add(rule) campaign.save() assert len(basket.get_final_lines()) == 3 assert basket.total_price == price(single_product_price) * basket.product_count # check that shipping method affects campaign basket.shipping_method = unique_shipping_method basket.save() basket.uncache() assert len(basket.get_final_lines()) == 4 # Shipping should not affect the rule being triggered line_types = [l.type for l in basket.get_final_lines()] assert OrderLineType.DISCOUNT not in line_types product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) assert len(basket.get_final_lines()) == 6 # Discount included assert OrderLineType.DISCOUNT in [l.type for l in basket.get_final_lines()]
def test_waiver(): sm = ShippingMethod(name="Waivey", tax_class=get_default_tax_class(), module_data={ "price_waiver_product_minimum": "370", "price": "100" }) source = BasketishOrderSource(get_default_shop()) assert sm.get_effective_name(source) == u"Waivey" assert sm.get_effective_price_info( source).price == source.shop.create_price(100) source.add_line(type=OrderLineType.PRODUCT, product=get_default_product(), base_unit_price=source.shop.create_price(400), quantity=1) assert sm.get_effective_price_info( source).price == source.shop.create_price(0)
def test_limited_methods(): """ Test that products can declare that they limit available shipping methods. """ unique_shipping_method = ShippingMethod(tax_class=get_default_tax_class(), module_data={"price": 0}) unique_shipping_method.save() shop = get_default_shop() common_product = create_product( sku="SH_COMMON", shop=shop) # A product that does not limit shipping methods unique_product = create_product( sku="SH_UNIQUE", shop=shop) # A product that only supports unique_shipping_method unique_shop_product = unique_product.get_shop_instance(shop) unique_shop_product.limit_shipping_methods = True unique_shop_product.shipping_methods.add(unique_shipping_method) unique_shop_product.save() impossible_product = create_product( sku="SH_IMP", shop=shop) # A product that can't be shipped at all imp_shop_product = impossible_product.get_shop_instance(shop) imp_shop_product.limit_shipping_methods = True imp_shop_product.save() for product_ids, method_ids in [ ((common_product.pk, unique_product.pk), (unique_shipping_method.pk, )), ((common_product.pk, ), ShippingMethod.objects.values_list("pk", flat=True)), ((unique_product.pk, ), (unique_shipping_method.pk, )), (( unique_product.pk, impossible_product.pk, ), ()), (( common_product.pk, impossible_product.pk, ), ()), ]: product_ids = set(product_ids) assert ShippingMethod.objects.available_ids( shop=shop, products=product_ids) == set(method_ids)
def test_basket_campaign_case2(rf): request, shop, group = initialize_test(rf, False) price = shop.create_price basket = get_basket(request) supplier = get_default_supplier() # create a basket rule that requires at least value of 200 rule = BasketTotalAmountCondition.objects.create(value="200") single_product_price = "50" discount_amount_value = "10" unique_shipping_method = ShippingMethod(tax_class=get_default_tax_class(), module_data={"price": 50}) unique_shipping_method.save() for x in range(3): product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) assert basket.product_count == 3 campaign = BasketCampaign.objects.create( shop=shop, public_name="test", name="test", discount_amount_value=discount_amount_value, active=True) campaign.conditions.add(rule) campaign.save() assert len(basket.get_final_lines()) == 3 assert basket.total_price == price( single_product_price) * basket.product_count # check that shipping method affects campaign basket.shipping_method = unique_shipping_method basket.save() basket.uncache() assert len(basket.get_final_lines() ) == 4 # Shipping should not affect the rule being triggered line_types = [l.type for l in basket.get_final_lines()] assert OrderLineType.DISCOUNT not in line_types product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=single_product_price) basket.add_product(supplier=supplier, shop=shop, product=product, quantity=1) assert len(basket.get_final_lines()) == 6 # Discount included assert OrderLineType.DISCOUNT in [l.type for l in basket.get_final_lines()]
def test_method_list(admin_user): with override_provides_for_expensive_sweden_shipping_method(): assert any(name == "Expensive Sweden Shipping" for (spec, name) in ShippingMethod.get_module_choices())