def test_addition_with_other_types(): price1 = TaxedMoney(Money(10, 'EUR'), Money(15, 'EUR')) price2 = TaxedMoney(Money(30, 'EUR'), Money(45, 'EUR')) price_range = TaxedMoneyRange(price1, price2) with pytest.raises(TypeError): price_range + 1
def test_apply_tax_to_price_no_taxes_return_taxed_money(): money = Money(100, "USD") taxed_money = TaxedMoney(net=Money(100, "USD"), gross=Money(100, "USD")) assert apply_tax_to_price(None, "standard", money) == taxed_money assert apply_tax_to_price(None, "medical", taxed_money) == taxed_money
def get_total(self): return Money(self.total, self.currency or settings.DEFAULT_CURRENCY)
def unavailable_product(product_type, category): product = Product.objects.create( name='Test product', price=Money('10.00', 'USD'), product_type=product_type, is_published=False, category=category) return product
assert tax_rate == taxes[DEFAULT_TAX_RATE_NAME]["value"] def test_get_tax_rate_by_name_empty_taxes(product): rate_name = "unexisting tax rate" tax_rate = get_tax_rate_by_name(rate_name) assert tax_rate == 0 @pytest.mark.parametrize( "price, charge_taxes, expected_price", [ ( Money(10, "USD"), False, TaxedMoney(net=Money(10, "USD"), gross=Money(10, "USD")), ), ( Money(10, "USD"), True, TaxedMoney(net=Money("8.13", "USD"), gross=Money(10, "USD")), ), ], ) def test_get_taxed_shipping_price(site_settings, vatlayer, price, charge_taxes, expected_price): site_settings.charge_taxes_on_shipping = charge_taxes site_settings.save()
def get_total_price(self): return TaxedMoney( net=Money(self.total - self.tax, self.currency), gross=Money(self.total, self.currency))
def shipping_method(shipping_zone): return ShippingMethod.objects.create( name='DHL', minimum_order_price=Money(0, 'USD'), type=ShippingMethodType.PRICE_BASED, price=Money(10, 'USD'), shipping_zone=shipping_zone)
def test_calculate_order_line_unit_the_order_changed( order_line, shipping_zone, site_settings, address_usa, plugin_configuration, cigar_product_type, address_usa_va): """ Ensure that when the order order lines have changed the method will return the correct value. """ plugin_configuration() manager = get_plugins_manager() site_settings.company_address = address_usa_va site_settings.save() order_line.id = None unit_price = TaxedMoney(net=Money("10.00", "USD"), gross=Money("10.00", "USD")) order_line.unit_price = unit_price order_line.base_unit_price = unit_price.gross order_line.undiscounted_unit_price = unit_price order_line.undiscounted_base_unit_price = unit_price.gross order_line.save() variant = order_line.variant variant.sku = "202015500" variant.save() product = variant.product product.product_type = cigar_product_type product.save() order = order_line.order method = shipping_zone.shipping_methods.get() order.shipping_address = address_usa_va order.billing_address = address_usa_va order.shipping_method_name = method.name order.shipping_method = method order.save() # calculating the order line unit for the first time line_price_data = manager.calculate_order_line_unit( order, order_line, order_line.variant, order_line.variant.product) expected_line_price = TaxedMoney(net=Money("10.00", "USD"), gross=Money("10.57", "USD")) assert line_price_data.price_with_discounts == expected_line_price # remove the first line add a new one order.lines.first().delete() second_order_line = order.lines.last() second_order_line.id = None # set different price than the first line second_order_line.unit_price = TaxedMoney(net=Money("25.00", "USD"), gross=Money("25.00", "USD")) second_order_line.undiscounted_unit_price = TaxedMoney( net=Money("25.00", "USD"), gross=Money("25.00", "USD")) second_order_line.save() # calculating the order line unit for the second time line_price_data = manager.calculate_order_line_unit( order, order_line, order_line.variant, order_line.variant.product) expected_line_price = TaxedMoney(net=Money("10.00", "USD"), gross=Money("10.57", "USD")) assert line_price_data.price_with_discounts == expected_line_price
def test_manager_calculates_order_line(order_line, plugins, amount): currency = order_line.unit_price.currency expected_price = Money(amount, currency) unit_price = PluginsManager(plugins=plugins).calculate_order_line_unit(order_line) assert expected_price == unit_price.gross
def test_calculate_checkout_total( reset_sequences, # pylint: disable=unused-argument with_discount, expected_net, expected_gross, voucher_amount, taxes_in_prices, checkout_with_item, product_with_single_variant, discount_info, shipping_zone, address_usa_va, address_usa, site_settings, monkeypatch, plugin_configuration, non_default_category, ): plugin_configuration() monkeypatch.setattr( "saleor.plugins.avatax.excise.plugin.AvataxExcisePlugin._skip_plugin", lambda *_: False, ) manager = get_plugins_manager() checkout_with_item.shipping_address = address_usa_va checkout_with_item.save() checkout_variant = checkout_with_item.lines.first().variant checkout_variant.sku = "202015500" checkout_variant.save(update_fields=["sku"]) site_settings.company_address = address_usa site_settings.include_taxes_in_prices = taxes_in_prices site_settings.save() voucher_amount = Money(voucher_amount, "USD") checkout_with_item.shipping_method = shipping_zone.shipping_methods.get() checkout_with_item.discount = voucher_amount checkout_with_item.save() product_with_single_variant.charge_taxes = False product_with_single_variant.category = non_default_category product_with_single_variant.save() variant = product_with_single_variant.variants.first() variant.sku = "202165300" variant.save(update_fields=["sku"]) discounts = [discount_info] if with_discount else None checkout_info = fetch_checkout_info(checkout_with_item, [], discounts, manager) add_variant_to_checkout(checkout_info, product_with_single_variant.variants.get()) lines, _ = fetch_checkout_lines(checkout_with_item) total = manager.calculate_checkout_total(checkout_info, lines, address_usa_va, discounts) total = quantize_price(total, total.currency) assert total == TaxedMoney(net=Money(expected_net, "USD"), gross=Money(expected_gross, "USD"))
def test_calculate_checkout_total_excise_data( reset_sequences, # pylint: disable=unused-argument expected_net, expected_gross, taxes_in_prices, variant_sku, price, destination, metadata, checkout, product, shipping_zone, address_usa, site_settings, monkeypatch, plugin_configuration, cigar_product_type, ): plugin_configuration() monkeypatch.setattr( "saleor.plugins.avatax.excise.plugin.AvataxExcisePlugin._skip_plugin", lambda *_: False, ) manager = get_plugins_manager() address_usa.city = destination["city"] address_usa.postal_code = destination["postal_code"] address_usa.country_area = destination["country_area"] address_usa.save() checkout.shipping_address = address_usa checkout.billing_address = address_usa shipping_method = shipping_zone.shipping_methods.get() shipping_method.price_amount = 0 shipping_method.save() checkout.shipping_method = shipping_method metadata = { get_metadata_key("UnitQuantity"): metadata["UnitQuantity"], get_metadata_key("CustomNumeric1"): metadata["CustomNumeric1"], get_metadata_key("CustomNumeric2"): metadata["CustomNumeric2"], get_metadata_key("CustomNumeric3"): metadata["CustomNumeric3"], } product.product_type = cigar_product_type product.save() variant = product.variants.get() variant.sku = variant_sku variant.private_metadata = metadata variant.price_amount = Decimal(price) variant.save() checkout_info = fetch_checkout_info(checkout, [], [], manager) add_variant_to_checkout(checkout_info, variant, 1) checkout.save() site_settings.company_address = address_usa site_settings.include_taxes_in_prices = taxes_in_prices site_settings.save() lines, _ = fetch_checkout_lines(checkout) total = manager.calculate_checkout_total(checkout_info, lines, address_usa, []) total = quantize_price(total, total.currency) assert total == TaxedMoney(net=Money(expected_net, "USD"), gross=Money(expected_gross, "USD"))
def test_calculate_checkout_line_total( reset_sequences, # pylint: disable=unused-argument with_discount, expected_net, expected_gross, taxes_in_prices, discount_info, checkout_with_item, address_usa_va, address_usa, site_settings, shipping_zone, plugin_configuration, ): plugin_configuration() manager = get_plugins_manager() checkout_with_item.shipping_address = address_usa_va checkout_with_item.shipping_method = shipping_zone.shipping_methods.get() checkout_with_item.save() site_settings.company_address = address_usa site_settings.include_taxes_in_prices = taxes_in_prices site_settings.save() line = checkout_with_item.lines.first() product = line.variant.product product.metadata = {} # TODO consider adding ATE fields here product.charge_taxes = True product.save() product.product_type.save() discounts = [discount_info] if with_discount else None lines, _ = fetch_checkout_lines(checkout_with_item) checkout_info = fetch_checkout_info(checkout_with_item, lines, discounts, manager) total = manager.calculate_checkout_line_total( checkout_info, lines, lines[0], checkout_with_item.shipping_address, discounts, ) price_with_discounts = total.price_with_discounts price_with_discounts = quantize_price(price_with_discounts, price_with_discounts.currency) assert price_with_discounts == TaxedMoney(net=Money(expected_net, "USD"), gross=Money( expected_gross, "USD")) checkout_with_item.refresh_from_db() taxes_metadata = checkout_with_item.metadata.get( get_metadata_key("itemized_taxes")) sales_tax = checkout_with_item.metadata.get(get_metadata_key("sales_tax")) other_tax = checkout_with_item.metadata.get(get_metadata_key("other_tax")) assert taxes_metadata is not None assert len(taxes_metadata) > 0 assert sales_tax >= 0 assert other_tax >= 0
def sum_order_totals(qs): zero = Money(0, currency=settings.DEFAULT_CURRENCY) taxed_zero = TaxedMoney(zero, zero) return sum([order.total for order in qs], taxed_zero)
) tax_rate = PluginsManager(plugins=plugins).get_checkout_line_tax_rate( checkout_with_item, checkout_line_info, checkout_with_item.shipping_address, [discount_info], unit_price, ) assert tax_rate == Decimal("0.08") @pytest.mark.parametrize( "unit_price, expected_tax_rate", [ (TaxedMoney(Money(12, "USD"), Money(15, "USD")), Decimal("0.25")), (Decimal("0.0"), Decimal("0.0")), ], ) def test_manager_get_checkout_line_tax_rate_no_plugins( checkout_with_item, discount_info, unit_price, expected_tax_rate ): line = checkout_with_item.lines.all()[0] variant = line.variant checkout_line_info = CheckoutLineInfo( line=line, variant=variant, channel_listing=variant.channel_listings.first(), product=variant.product, collections=[], )
def calculate_checkout_line_total(self, checkout_line, discounts, previous_value): price = Money("1.0", currency=checkout_line.get_total().currency) return TaxedMoney(price, price)
def test_applicable_shipping_methods_price_rate_use_proper_channel( shipping_zone, channel_USD, other_channel_USD ): # given # Price method with different min and max in channels to total to low to apply price_method_1 = shipping_zone.shipping_methods.create( type=ShippingMethodType.PRICE_BASED, ) # Price method with different min and max in channels total correct to apply price_method_2 = shipping_zone.shipping_methods.create( type=ShippingMethodType.PRICE_BASED, ) # Price method with different min and max in channels total to hight to apply price_method_3 = shipping_zone.shipping_methods.create( type=ShippingMethodType.PRICE_BASED, ) # Price method not assigned to channel price_method_4 = shipping_zone.shipping_methods.create( type=ShippingMethodType.PRICE_BASED, ) ShippingMethodChannelListing.objects.bulk_create( [ # price_method_1 ShippingMethodChannelListing( minimum_order_price=Money("10.0", "USD"), maximum_order_price=Money("100.0", "USD"), shipping_method=price_method_1, channel=channel_USD, ), ShippingMethodChannelListing( minimum_order_price=Money("1.0", "USD"), maximum_order_price=Money("100.0", "USD"), shipping_method=price_method_1, channel=other_channel_USD, ), # price_method_2 ShippingMethodChannelListing( minimum_order_price=Money("4.0", "USD"), maximum_order_price=Money("10.0", "USD"), shipping_method=price_method_2, channel=channel_USD, ), ShippingMethodChannelListing( minimum_order_price=Money("1.0", "USD"), maximum_order_price=Money("100.0", "USD"), shipping_method=price_method_2, channel=other_channel_USD, ), # price_method_3 ShippingMethodChannelListing( minimum_order_price=Money("1.0", "USD"), maximum_order_price=Money("4.0", "USD"), shipping_method=price_method_3, channel=channel_USD, ), ShippingMethodChannelListing( minimum_order_price=Money("1.0", "USD"), maximum_order_price=Money("100.0", "USD"), shipping_method=price_method_3, channel=other_channel_USD, ), # price_method_4 ShippingMethodChannelListing( minimum_order_price=Money("1.0", "USD"), maximum_order_price=Money("100.0", "USD"), shipping_method=price_method_4, channel=other_channel_USD, ), ] ) # when result = ShippingMethod.objects.applicable_shipping_methods( price=Money("5.0", "USD"), weight=Weight(kg=5), country_code="PL", channel_id=channel_USD.id, ) # then assert price_method_1 not in result assert price_method_3 not in result assert price_method_4 not in result assert price_method_2 in result
def calculate_order_line_unit(self, order_line, previous_value): currency = order_line.unit_price.currency price = Money("1.0", currency) return TaxedMoney(price, price)
def set_field_as_money(defaults, field): amount_field = f"{field}_amount" if amount_field in defaults and defaults[amount_field] is not None: defaults[field] = Money(defaults[amount_field], settings.DEFAULT_CURRENCY)
def get_captured_price(self): return Money(self.captured_amount, self.currency)
def money(self): return Money(fake.pydecimal(2, 2, positive=True), settings.DEFAULT_CURRENCY)
def variant(product): product_variant = ProductVariant.objects.create( product=product, sku='SKU_A', cost_price=Money(1, 'USD'), quantity=5, quantity_allocated=3) return product_variant
def apply_taxes_to_product(self, product, price, country, previous_value, **kwargs): price = Money("1.0", price.currency) return TaxedMoney(price, price)
digital_content.use_default_settings = False digital_content.url_valid_days = 10 digital_content.save() with freeze_time("2018-05-31 12:00:01"): digital_content_url = DigitalContentUrl.objects.create( content=digital_content) url = digital_content_url.get_absolute_url() response = client.get(url) assert response.status_code == 404 @pytest.mark.parametrize("price, cost", [(Money("0", "USD"), Money("1", "USD")), (Money("2", "USD"), None)]) def test_costs_get_margin_for_variant_channel_listing(variant, price, cost, channel_USD): variant_channel_listing = variant.channel_listings.filter( channel_id=channel_USD.id).first() variant_channel_listing.cost_price = cost variant_channel_listing.price = price assert not get_margin_for_variant_channel_listing(variant_channel_listing) @patch("saleor.product.thumbnails.create_thumbnails") def test_create_product_thumbnails(mock_create_thumbnails, product_with_image): product_image = product_with_image.media.first() create_product_thumbnails(product_image.pk) assert mock_create_thumbnails.call_count == 1
def apply_taxes_to_shipping(self, price, shipping_address, previous_value) -> TaxedMoney: price = Money("1.0", price.currency) return TaxedMoney(price, price)
def test_apply_tax_to_price_do_not_include_tax(site_settings, taxes): site_settings.include_taxes_in_prices = False site_settings.save() money = Money(100, "USD") assert apply_tax_to_price(taxes, "standard", money) == TaxedMoney(net=Money(100, "USD"), gross=Money(123, "USD")) assert apply_tax_to_price(taxes, "medical", money) == TaxedMoney(net=Money(100, "USD"), gross=Money(108, "USD")) taxed_money = TaxedMoney(net=Money(100, "USD"), gross=Money(100, "USD")) assert apply_tax_to_price(taxes, "standard", taxed_money) == TaxedMoney(net=Money(100, "USD"), gross=Money( 123, "USD")) assert apply_tax_to_price(taxes, "medical", taxed_money) == TaxedMoney(net=Money(100, "USD"), gross=Money( 108, "USD"))
def calculate_checkout_subtotal(self, checkout, discounts, previous_value): subtotal = Money("1.0", currency=checkout.get_total().currency) return TaxedMoney(subtotal, subtotal)
def test_format_money(): money = Money('123.99', 'USD') assert format_money(money) == '$123.99'
def calculate_order_shipping(self, order, previous_value): price = Money("1.0", currency=order.total.currency) return TaxedMoney(price, price)
def get_captured_amount(self): return Money(self.captured_amount, self.currency or settings.DEFAULT_CURRENCY)
def test_addition_with_taxed_money_range(): price1 = TaxedMoney(Money(10, 'EUR'), Money(15, 'EUR')) price2 = TaxedMoney(Money(30, 'EUR'), Money(45, 'EUR')) price_range1 = TaxedMoneyRange(price1, price2) price3 = TaxedMoney(Money(40, 'EUR'), Money(60, 'EUR')) price4 = TaxedMoney(Money(80, 'EUR'), Money(120, 'EUR')) price_range2 = TaxedMoneyRange(price3, price4) result = price_range1 + price_range2 assert result.start == price1 + price3 assert result.stop == price2 + price4 with pytest.raises(ValueError): price_range1 + TaxedMoneyRange( TaxedMoney(Money(1, 'BTC'), Money(1, 'BTC')), TaxedMoney(Money(2, 'BTC'), Money(2, 'BTC')))