Beispiel #1
0
def test_add():
    total = Quantity(0, '$')
    for contribution in [1.23, 4.56, 7.89]:
        total = total.add(contribution)
    assert total.render() == '$13.68'
    for contribution in [1.23, 4.56, 8.89]:
        total = total.add(contribution, check_units=True)
    assert total.render() == '$28.36'
    for contribution in [1.23, 4.56, 9.89]:
        total = total.add(Quantity(contribution, '$'), check_units=True)
    assert total.render() == '$44.04'
    try:
        total = total.add(Quantity(contribution, 'lbs'), check_units=True)
        assert False
    except TypeError:
        assert True
Beispiel #2
0
def test_add():
    Quantity.set_prefs(spacer=None,
                       show_label=None,
                       label_fmt=None,
                       label_fmt_full=None)
    q1 = Quantity('1ns')
    q2 = Quantity('2ns')
    assert str(q1.add(q2)) == '3 ns'
    assert repr(q1.add(q2)) == "Quantity('3 ns')"
    assert str(q2.add(q1)) == '3 ns'
    assert repr(q2.add(q1)) == "Quantity('3 ns')"
    assert str(q1.add(q2, check_units=False)) == '3 ns'
    assert str(q1.add(q2, check_units=True)) == '3 ns'
    assert str(q1.add(q2, check_units='strict')) == '3 ns'
    assert str(q1.add(2e-9, check_units=False)) == '3 ns'
    assert str(q1.add(2e-9, check_units=True)) == '3 ns'
    with pytest.raises(TypeError) as exception:
        q1.add(2e-9, check_units='strict')
    q3 = Quantity('2nm')
    assert str(q1.add(q3, check_units=False)) == '3 ns'
    with pytest.raises(TypeError) as exception:
        q1.add(q3, check_units=True)
    with pytest.raises(TypeError) as exception:
        q1.add(q3, check_units='strict')

    q1.name = 'period'
    assert q1.add(q2).name == 'period'
    q1.desc = 'duration of one cycle'
    assert q1.add(q2).name == 'period'
    assert q1.add(q2).desc == 'duration of one cycle'

    class Dollars(Quantity):
        units = '$'
        prec = 2
        form = 'fixed'
        show_commas = True
        if sys.version_info.major == 3:
            minus = Quantity.minus_sign
        strip_zeros = False

    total = Dollars(0)
    assert str(total) == '$0.00'
    total = total.add(1000000)
    assert str(total) == '$1,000,000.00'
    total = total.add(Quantity('-2MHz'), check_units=False)
    if sys.version_info.major == 3:
        assert str(total) == '−$1,000,000.00'
    else:
        assert str(total) == '-$1,000,000.00'
    with pytest.raises(IncompatibleUnits) as exception:
        total.add(Quantity('-2MHz'), check_units=True)
    total = total.add(Quantity('$6M'), check_units=True)
    assert str(total) == '$5,000,000.00'

    class WholeDollars(Dollars):
        prec = 0

    total = WholeDollars(0)
    total.name = 'total'
    assert str(total) == '$0'
    total = total.add(1000000)
    assert str(total) == '$1,000,000'
    assert total.name == 'total'
    total = total.add(Quantity('-2MHz'), check_units=False)
    if sys.version_info.major == 3:
        assert str(total) == '−$1,000,000'
    else:
        assert str(total) == '-$1,000,000'
    assert total.name == 'total'
    with pytest.raises(IncompatibleUnits) as exception:
        total.add(Quantity('-2MHz'), check_units=True)
    total = total.add(Quantity('$6M'), check_units=True)
    assert str(total) == '$5,000,000'
    assert total.name == 'total'