Exemple #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
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(
        E-Commerce_CALCULATE_TAXES_AUTOMATICALLY_IF_POSSIBLE=False)
    settings_overrider.__enter__()
Exemple #3
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
Exemple #4
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
Exemple #5
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')
Exemple #6
0
def setup_module(module):
    # uses the get_precision to avoiding db hits
    set_precision_provider(babel_precision_provider.get_precision)
Exemple #7
0
def test_set_precision_provider_with_non_callable():
    with pytest.raises(AssertionError):
        set_precision_provider(3)
Exemple #8
0
def setup_module(module):
    settings.E-Commerce_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)