Example #1
0
def test_as_rounded_values(currency, digits):
    set_precision_provider(babel_precision_provider.get_precision)

    amounts = [
        '1', '2', '3', '4',
        '1.23223', '12.24442', '42.26233',
        '1223.46636', '13.24655', '411.234554',
        '101.74363', '12.99346', '4222.57422',
        '112.93549', '199.2446', '422.29234',
        '1994.49654', '940.23452', '425.24566',
        '1994.496541234566', '940.2345298765', '425.2456612334',
    ]

    for amount in amounts:
        precision = Decimal('0.1') ** digits

        rounded = Decimal(amount).quantize(precision)
        rounded2 = Decimal(amount).quantize(Decimal('0.01'))
        rounded3 = Decimal(amount).quantize(Decimal('0.001'))

        # test using the currency
        assert Money(amount, currency).as_rounded().value == rounded

        # test using digits
        assert Money(amount, currency).as_rounded(3).value == rounded3

        # test using not existent currency code
        assert Money(amount, "XTS").as_rounded().value == rounded2
Example #2
0
def test_as_rounded_values(currency, digits):
    set_precision_provider(babel_precision_provider.get_precision)

    amounts = [
        '1', '2', '3', '4',
        '1.23223', '12.24442', '42.26233',
        '1223.46636', '13.24655', '411.234554',
        '101.74363', '12.99346', '4222.57422',
        '112.93549', '199.2446', '422.29234',
        '1994.49654', '940.23452', '425.24566',
        '1994.496541234566', '940.2345298765', '425.2456612334',
    ]

    for amount in amounts:
        precision = Decimal('0.1') ** digits

        rounded = Decimal(amount).quantize(precision)
        rounded2 = Decimal(amount).quantize(Decimal('0.01'))
        rounded3 = Decimal(amount).quantize(Decimal('0.001'))

        # test using the currency
        assert Money(amount, currency).as_rounded().value == rounded

        # test using digits
        assert Money(amount, currency).as_rounded(3).value == rounded3

        # test using not existent currency code
        assert Money(amount, "XTS").as_rounded().value == rounded2
Example #3
0
def setup_module(module):
    # uses the get_precision to avoid db hits
    set_precision_provider(babel_precision_provider.get_precision)

    global settings_overrider
    settings_overrider = override_settings(
        SHUUP_CALCULATE_TAXES_AUTOMATICALLY_IF_POSSIBLE=False)
    settings_overrider.__enter__()
Example #4
0
def setup_module(module):
    # uses the get_precision to avoid db hits
    set_precision_provider(babel_precision_provider.get_precision)

    global settings_overrider
    settings_overrider = override_settings(
        SHUUP_CALCULATE_TAXES_AUTOMATICALLY_IF_POSSIBLE=False)
    settings_overrider.__enter__()
Example #5
0
def test_as_rounded_returns_same_type():
    set_precision_provider(babel_precision_provider.get_precision)

    class CoolMoney(Money):
        is_cool = True

    amount = CoolMoney('0.4939389', 'USD')
    assert type(amount.as_rounded()) == CoolMoney
    assert amount.as_rounded().is_cool
Example #6
0
def test_set_precision_provider():
    def get_precision(currency):
        return None

    set_precision_provider(get_precision)
    assert money._precision_provider == get_precision

    set_precision_provider(babel_precision_provider.get_precision)
    assert money._precision_provider == babel_precision_provider.get_precision
Example #7
0
def test_set_precision_provider():
    def get_precision(currency):
        return None

    set_precision_provider(get_precision)
    assert money._precision_provider == get_precision

    set_precision_provider(babel_precision_provider.get_precision)
    assert money._precision_provider == babel_precision_provider.get_precision
Example #8
0
def test_as_rounded_returns_same_type():
    set_precision_provider(babel_precision_provider.get_precision)

    class CoolMoney(Money):
        is_cool = True

    amount = CoolMoney('0.4939389', 'USD')
    assert type(amount.as_rounded()) == CoolMoney
    assert amount.as_rounded().is_cool
Example #9
0
    def ready(self):
        from django.conf import settings
        if not getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None):
            raise MissingSettingException(
                "PARLER_DEFAULT_LANGUAGE_CODE must be set.")
        if not getattr(settings, "PARLER_LANGUAGES", None):
            raise MissingSettingException("PARLER_LANGUAGES must be set.")

        # set money precision provider function
        from .models import get_currency_precision
        money.set_precision_provider(get_currency_precision)
Example #10
0
    def ready(self):
        from django.conf import settings

        if not getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None):
            raise MissingSettingException("PARLER_DEFAULT_LANGUAGE_CODE must be set.")
        if not getattr(settings, "PARLER_LANGUAGES", None):
            raise MissingSettingException("PARLER_LANGUAGES must be set.")

        # set money precision provider function
        from .models import get_currency_precision

        money.set_precision_provider(get_currency_precision)
Example #11
0
    def ready(self):
        from django.conf import settings
        if not getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None):
            raise MissingSettingException("PARLER_DEFAULT_LANGUAGE_CODE must be set.")
        if not getattr(settings, "PARLER_LANGUAGES", None):
            raise MissingSettingException("PARLER_LANGUAGES must be set.")

        # set money precision provider function
        from .models import get_currency_precision
        money.set_precision_provider(get_currency_precision)

        if django.conf.settings.SHUUP_ERROR_PAGE_HANDLERS_SPEC:
            from .error_handling import install_error_handlers
            install_error_handlers()

        # connect signals
        import shuup.core.signal_handers    # noqa: F401
Example #12
0
    def ready(self):
        from django.conf import settings
        if not getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None):
            raise MissingSettingException("PARLER_DEFAULT_LANGUAGE_CODE must be set.")
        if not getattr(settings, "PARLER_LANGUAGES", None):
            raise MissingSettingException("PARLER_LANGUAGES must be set.")

        # set money precision provider function
        from .models import get_currency_precision
        money.set_precision_provider(get_currency_precision)

        if django.conf.settings.SHUUP_ERROR_PAGE_HANDLERS_SPEC:
            from .error_handling import install_error_handlers
            install_error_handlers()

        # connect signals
        import shuup.core.signal_handers    # noqa: F401
Example #13
0
def test_as_rounded_rounding_mode():
    set_precision_provider(babel_precision_provider.get_precision)

    prec2 = Decimal('0.01')
    m1 = Money('2.345', 'EUR')
    m2 = Money('2.344', 'EUR')

    assert m1.as_rounded(2).value == Decimal('2.34')
    assert m2.as_rounded(2).value == Decimal('2.34')

    from decimal import ROUND_HALF_DOWN, ROUND_HALF_UP, ROUND_FLOOR

    assert m1.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal('2.34')
    assert m2.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal('2.34')
    assert m1.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal('2.35')
    assert m2.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal('2.34')
    assert m1.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal('2.34')
    assert m2.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal('2.34')
Example #14
0
def test_as_rounded_rounding_mode():
    set_precision_provider(babel_precision_provider.get_precision)

    prec2 = Decimal("0.01")
    m1 = Money("2.345", "EUR")
    m2 = Money("2.344", "EUR")

    assert m1.as_rounded(2).value == Decimal("2.34")
    assert m2.as_rounded(2).value == Decimal("2.34")

    from decimal import ROUND_HALF_DOWN, ROUND_HALF_UP, ROUND_FLOOR

    assert m1.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal("2.34")
    assert m2.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal("2.34")
    assert m1.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal("2.35")
    assert m2.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal("2.34")
    assert m1.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal("2.34")
    assert m2.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal("2.34")
Example #15
0
def test_as_rounded_rounding_mode():
    set_precision_provider(babel_precision_provider.get_precision)

    prec2 = Decimal('0.01')
    m1 = Money('2.345', 'EUR')
    m2 = Money('2.344', 'EUR')

    assert m1.as_rounded(2).value == Decimal('2.34')
    assert m2.as_rounded(2).value == Decimal('2.34')

    from decimal import ROUND_HALF_DOWN, ROUND_HALF_UP, ROUND_FLOOR

    assert m1.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal('2.34')
    assert m2.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal('2.34')
    assert m1.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal('2.35')
    assert m2.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal('2.34')
    assert m1.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal('2.34')
    assert m2.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal('2.34')
Example #16
0
def test_as_rounded_rounding_mode():
    set_precision_provider(babel_precision_provider.get_precision)

    prec2 = Decimal("0.01")
    m1 = Money("2.345", "EUR")
    m2 = Money("2.344", "EUR")

    assert m1.as_rounded(2).value == Decimal("2.34")
    assert m2.as_rounded(2).value == Decimal("2.34")

    from decimal import ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_UP

    assert m1.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal("2.34")
    assert m2.as_rounded(2, rounding=ROUND_HALF_DOWN).value == Decimal("2.34")
    assert m1.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal("2.35")
    assert m2.as_rounded(2, rounding=ROUND_HALF_UP).value == Decimal("2.34")
    assert m1.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal("2.34")
    assert m2.as_rounded(2, rounding=ROUND_FLOOR).value == Decimal("2.34")
Example #17
0
def test_as_rounded_values(currency, digits):
    set_precision_provider(babel_precision_provider.get_precision)

    amounts = [
        "1",
        "2",
        "3",
        "4",
        "1.23223",
        "12.24442",
        "42.26233",
        "1223.46636",
        "13.24655",
        "411.234554",
        "101.74363",
        "12.99346",
        "4222.57422",
        "112.93549",
        "199.2446",
        "422.29234",
        "1994.49654",
        "940.23452",
        "425.24566",
        "1994.496541234566",
        "940.2345298765",
        "425.2456612334",
    ]

    for amount in amounts:
        precision = Decimal("0.1")**digits

        rounded = Decimal(amount).quantize(precision)
        rounded2 = Decimal(amount).quantize(Decimal("0.01"))
        rounded3 = Decimal(amount).quantize(Decimal("0.001"))

        # test using the currency
        assert Money(amount, currency).as_rounded().value == rounded

        # test using digits
        assert Money(amount, currency).as_rounded(3).value == rounded3

        # test using not existent currency code
        assert Money(amount, "XTS").as_rounded().value == rounded2
Example #18
0
def test_as_rounded_values(currency, digits):
    set_precision_provider(babel_precision_provider.get_precision)

    amounts = [
        "1",
        "2",
        "3",
        "4",
        "1.23223",
        "12.24442",
        "42.26233",
        "1223.46636",
        "13.24655",
        "411.234554",
        "101.74363",
        "12.99346",
        "4222.57422",
        "112.93549",
        "199.2446",
        "422.29234",
        "1994.49654",
        "940.23452",
        "425.24566",
        "1994.496541234566",
        "940.2345298765",
        "425.2456612334",
    ]

    for amount in amounts:
        precision = Decimal("0.1") ** digits

        rounded = Decimal(amount).quantize(precision)
        rounded2 = Decimal(amount).quantize(Decimal("0.01"))
        rounded3 = Decimal(amount).quantize(Decimal("0.001"))

        # test using the currency
        assert Money(amount, currency).as_rounded().value == rounded

        # test using digits
        assert Money(amount, currency).as_rounded(3).value == rounded3

        # test using not existent currency code
        assert Money(amount, "XTS").as_rounded().value == rounded2
Example #19
0
    def ready(self):
        from django.conf import settings
        if not getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None):
            raise MissingSettingException("PARLER_DEFAULT_LANGUAGE_CODE must be set.")
        if not getattr(settings, "PARLER_LANGUAGES", None):
            raise MissingSettingException("PARLER_LANGUAGES must be set.")

        # set money precision provider function
        from .models import get_currency_precision
        money.set_precision_provider(get_currency_precision)

        if django.conf.settings.SHUUP_ERROR_PAGE_HANDLERS_SPEC:
            from .error_handling import install_error_handlers
            install_error_handlers()

        from shuup.core.utils.context_cache import (
            bump_product_signal_handler, bump_shop_product_signal_handler
        )
        from shuup.core.models import Product, ShopProduct
        from django.db.models.signals import m2m_changed
        m2m_changed.connect(
            bump_shop_product_signal_handler,
            sender=ShopProduct.categories.through,
            dispatch_uid="shop_product:clear_shop_product_cache"
        )
        from django.db.models.signals import post_save
        post_save.connect(
            bump_product_signal_handler,
            sender=Product,
            dispatch_uid="product:bump_product_cache"
        )
        post_save.connect(
            bump_shop_product_signal_handler,
            sender=ShopProduct,
            dispatch_uid="shop_product:bump_shop_product_cache"
        )

        # extends SQLite with necessary functions
        from django.db.backends.signals import connection_created
        from shuup.core.utils.db import extend_sqlite_functions
        connection_created.connect(extend_sqlite_functions)
Example #20
0
    def ready(self):
        from django.conf import settings
        if not getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None):
            raise MissingSettingException(
                "PARLER_DEFAULT_LANGUAGE_CODE must be set.")
        if not getattr(settings, "PARLER_LANGUAGES", None):
            raise MissingSettingException("PARLER_LANGUAGES must be set.")

        # set money precision provider function
        from .models import get_currency_precision
        money.set_precision_provider(get_currency_precision)

        if django.conf.settings.SHUUP_ERROR_PAGE_HANDLERS_SPEC:
            from .error_handling import install_error_handlers
            install_error_handlers()

        from shuup.core.utils.context_cache import (
            bump_product_signal_handler, bump_shop_product_signal_handler)
        from shuup.core.models import Product, ShopProduct
        from django.db.models.signals import m2m_changed
        m2m_changed.connect(
            bump_shop_product_signal_handler,
            sender=ShopProduct.categories.through,
            dispatch_uid="shop_product:clear_shop_product_cache")
        from django.db.models.signals import post_save
        post_save.connect(bump_product_signal_handler,
                          sender=Product,
                          dispatch_uid="product:bump_product_cache")
        post_save.connect(bump_shop_product_signal_handler,
                          sender=ShopProduct,
                          dispatch_uid="shop_product:bump_shop_product_cache")

        # extends SQLite with necessary functions
        from django.db.backends.signals import connection_created
        from shuup.core.utils.db import extend_sqlite_functions
        connection_created.connect(extend_sqlite_functions)
Example #21
0
def test_currency_precision_provider_is_acceptable():
    money.set_precision_provider(get_currency_precision)
Example #22
0
def test_set_precision_provider_with_non_callable():
    with pytest.raises(AssertionError):
        set_precision_provider(3)
Example #23
0
def test_set_precision_provider_with_non_callable():
    with pytest.raises(AssertionError):
        set_precision_provider(3)
Example #24
0
def setup_module(module):
    # uses the get_precision to avoiding db hits
    set_precision_provider(babel_precision_provider.get_precision)
Example #25
0
def setup_module(module):
    settings.SHUUP_TAX_MODULE = "dummy_tax_module"
    tax_mod_overrider.__enter__()

    # uses the get_precision to avoid db hits
    set_precision_provider(babel_precision_provider.get_precision)
Example #26
0
def setup_module(module):
    settings.SHUUP_TAX_MODULE = "dummy_tax_module"
    tax_mod_overrider.__enter__()

    # uses the get_precision to avoid db hits
    set_precision_provider(babel_precision_provider.get_precision)
Example #27
0
def test_currency_precision_provider_is_acceptable():
    money.set_precision_provider(get_currency_precision)
Example #28
0
def setup_module(module):
    # uses the get_precision to avoiding db hits
    set_precision_provider(babel_precision_provider.get_precision)