def test_rules_with_anonymous(): """ Test the DefaultTaxModule with anonymous customer. """ tax_class = TaxClass.objects.create(name="test") # Create a product shop = get_shop(prices_include_tax=False, currency='USD') product = create_product('PROD', shop=shop, default_price=1000) product.tax_class = tax_class product.save() price = product.get_shop_instance(shop).default_price # create taxes # When customer is company, it should pay additional taxes tax_for_anyone = Tax.objects.create(code="any", rate=0.1, name="Tax for any customer") tax_for_companies = Tax.objects.create(code="companies", rate=0.3, name="Additional tax for companies") # create tax group for companies companies_tax_group = CustomerTaxGroup.get_default_company_group() # create the tax rule as follows: # - 10% for any kind of customer, no matter what # - 30% only for companies any_tax_rule = TaxRule.objects.create(tax=tax_for_anyone) any_tax_rule.tax_classes.add(tax_class) company_tax_rule = TaxRule.objects.create(tax=tax_for_companies) company_tax_rule.tax_classes.add(tax_class) company_tax_rule.customer_tax_groups.add(companies_tax_group) with override_settings(SHUUP_TAX_MODULE='default_tax'): module = get_tax_module() assert isinstance(module, DefaultTaxModule) # 1) check the tax for anonymous anonymous_context = TaxingContext() taxed_price = module.get_taxed_price_for(anonymous_context, product, price) expected_anonymous_codes = set(["any"]) assert set(x.tax.code for x in taxed_price.taxes) == expected_anonymous_codes # 2) check the tax for comanies company_context = TaxingContext(customer_tax_group=companies_tax_group) taxed_price = module.get_taxed_price_for(company_context, product, price) expected_companies_codes = set(["any", "companies"]) assert set(x.tax.code for x in taxed_price.taxes) == expected_companies_codes # Clean-up the rules TaxRule.objects.all().delete()
def test_rules_with_disabled_tax(): """ Test whether rules match when tax is disabled. """ tax_class = TaxClass.objects.create(name="test") # Create a product shop = get_shop(prices_include_tax=False, currency='USD') product = create_product('PROD', shop=shop, default_price=1000) product.tax_class = tax_class product.save() price = product.get_shop_instance(shop).default_price # create disabled tax tax = Tax.objects.create(code="any", rate=0.1, name="Tax for any customer", enabled=False) tax_rule = TaxRule.objects.create(tax=tax) tax_rule.tax_classes.add(tax_class) with override_settings(SHUUP_TAX_MODULE='default_tax'): module = get_tax_module() assert isinstance(module, DefaultTaxModule) # 1) check the tax for anonymous anonymous_context = TaxingContext() taxed_price = module.get_taxed_price_for(anonymous_context, product, price) assert len(list(taxed_price.taxes)) == 0 # Clean-up the rules TaxRule.objects.all().delete()
def test_module(address, expected_taxes): """ Test the DefaultTaxModule. """ # Create a product shop = get_shop(prices_include_tax=False, currency='USD') product = create_product('PROD', shop=shop, default_price=1000) price = product.get_shop_instance(shop).default_price # Put the tax rules into database for ruledef in shuffled(TAX_RULE_DEFS): rule = ruledef.get_tax_rule() rule.tax.save() rule.tax = rule.tax # refresh the id rule.save() rule.tax_classes.add(product.tax_class) assert TaxRule.objects.count() == len(TAX_RULE_DEFS) with override_settings(SHUUP_TAX_MODULE='default_tax'): module = get_tax_module() assert isinstance(module, DefaultTaxModule) context = TaxingContext(location=address) taxed_price = module.get_taxed_price_for(context, product, price) expected_codes = set(sum([x.split() for x in expected_taxes], [])) assert set(x.tax.code for x in taxed_price.taxes) == expected_codes expected_tax = Money(TAX_AMOUNTS[expected_taxes], 'USD') assert taxed_price.taxful.amount == price.amount + expected_tax # Clean-up the rules TaxRule.objects.all().delete()
def test_get_taxes_of_effective_rules(address, expected_taxes): context = TaxingContext(location=address) tax_rules = [ruledef.get_tax_rule() for ruledef in TAX_RULE_DEFS] result = get_taxes_of_effective_rules(context, tax_rules) grouped_codes_of_result = [[x.code for x in group] for group in result] expected_codes = [x.split() for x in expected_taxes] assert grouped_codes_of_result == expected_codes