bia-fitness/bia/test_units.py

59 lines
1.4 KiB
Python
Raw Normal View History

2015-03-23 21:41:55 +00:00
from .units import Mass
def test_basic_conversions():
a = Mass(2, 'kg')
kg = a.as_unit('kg')
assert kg.scalar == 2
assert kg.unit == 'kg'
lb = a.as_unit('lb')
assert abs(lb.scalar - 4.40925) < 0.0001
assert lb.unit == 'lb'
g = a.as_unit('g')
assert abs(g.scalar - 2000) < 0.0001
assert g.unit == 'g'
assert g.as_unit('lb').scalar == a.as_unit('lb').scalar
2015-03-24 03:45:53 +00:00
def test_complex_conversion():
a = Mass(2, ['kg', 'kg'])
b = a.as_unit('lb')
2015-03-24 03:45:53 +00:00
def test_basic_cmp():
assert Mass(1, 'kg') < Mass(2, 'kg')
assert Mass(1, 'kg') <= Mass(2, 'kg')
assert Mass(2, 'kg') <= Mass(2, 'kg')
assert Mass(2, 'kg') == Mass(2, 'kg')
assert Mass(2, 'kg') >= Mass(2, 'kg')
assert Mass(2, 'kg') > Mass(1, 'kg')
assert Mass(2, 'kg') >= Mass(1, 'kg')
def test_conversion_cmp():
assert Mass(1, 'kg') < Mass(100, 'lb')
2015-03-24 04:07:01 +00:00
assert Mass(1000000, 'g') > Mass(100, 'lb')
2015-03-24 03:45:53 +00:00
2015-03-24 04:07:01 +00:00
def test_addition():
2015-03-24 03:45:53 +00:00
assert Mass(1, 'kg') + Mass(2, 'kg') == Mass(3, 'kg')
2015-03-24 04:07:01 +00:00
assert Mass(1, 'kg') + Mass(1, 'lb') > Mass(1.4, 'kg')
def test_subtraction():
2015-03-24 03:45:53 +00:00
assert Mass(2, 'kg') - Mass(1, 'kg') == Mass(1, 'kg')
2015-03-24 04:07:01 +00:00
assert Mass(1, 'kg') - Mass(1, 'lb') < Mass(0.55, 'kg')
def test_multiplication():
assert Mass(2, 'kg') * 2 == Mass(4, 'kg')
assert Mass(2, 'kg') * Mass(1, 'kg') == Mass(2, ['kg', 'kg'])
def test_division():
assert Mass(8, 'kg') / 2 == Mass(4, 'kg')
assert Mass(2, 'kg') / Mass(1, 'kg') == Mass(2, [])