bia-fitness/bia/test_units.py

83 lines
2.0 KiB
Python
Raw Permalink Normal View History

2015-03-24 21:16:45 +00:00
import pytest
from .units import Unit, ConversionError
2015-03-24 20:22:32 +00:00
from .units import UnitValue as V
def test_unit_basics():
assert Unit('kg') == Unit(['kg'])
assert Unit(['kg'], ['m']) == Unit(['kg'], ['m'])
2015-03-23 21:41:55 +00:00
def test_basic_conversions():
2015-03-24 20:22:32 +00:00
a = V(2, 'kg')
2015-03-23 21:41:55 +00:00
kg = a.as_unit('kg')
assert kg.scalar == 2
2015-03-24 20:22:32 +00:00
assert kg.unit == Unit('kg')
2015-03-23 21:41:55 +00:00
lb = a.as_unit('lb')
assert abs(lb.scalar - 4.40925) < 0.0001
2015-03-24 20:22:32 +00:00
assert lb.unit == Unit('lb')
2015-03-23 21:41:55 +00:00
g = a.as_unit('g')
assert abs(g.scalar - 2000) < 0.0001
2015-03-24 20:22:32 +00:00
assert g.unit == Unit('g')
2015-03-23 21:41:55 +00:00
assert g.as_unit('lb').scalar == a.as_unit('lb').scalar
2015-03-24 03:45:53 +00:00
2015-03-24 20:27:42 +00:00
def test_complex_conversions():
a = V(2, ['m', 'm'])
cm2 = a.as_unit(['cm', 'cm'])
assert cm2.scalar == 20000
assert cm2.unit == Unit(['cm', 'cm'])
g = V(9.8, Unit(['m'], ['s', 's']))
g_ft = g.as_unit(Unit(['ft'], ['s', 's']))
assert g_ft.scalar - 32 < 1
assert g_ft.unit == Unit(['ft'], ['s', 's'])
2015-03-24 21:16:45 +00:00
def test_invalid_conversion_basic():
with pytest.raises(ConversionError):
V(1, 'm').as_unit('s')
def test_invalid_conversion_dimension():
with pytest.raises(ConversionError):
V(1, ['m']).as_unit(['m', 'm'])
2015-03-24 20:27:42 +00:00
2015-03-24 03:45:53 +00:00
def test_basic_cmp():
2015-03-24 20:22:32 +00:00
assert V(1, 'kg') < V(2, 'kg')
assert V(1, 'kg') <= V(2, 'kg')
assert V(2, 'kg') <= V(2, 'kg')
assert V(2, 'kg') == V(2, 'kg')
assert V(2, 'kg') >= V(2, 'kg')
assert V(2, 'kg') > V(1, 'kg')
assert V(2, 'kg') >= V(1, 'kg')
2015-03-24 03:45:53 +00:00
def test_conversion_cmp():
2015-03-24 20:22:32 +00:00
assert V(1, 'kg') < V(100, 'lb')
assert V(1000000, 'g') > V(100, 'lb')
2015-03-24 03:45:53 +00:00
2015-03-24 04:07:01 +00:00
def test_addition():
2015-03-24 20:22:32 +00:00
assert V(1, 'kg') + V(2, 'kg') == V(3, 'kg')
assert V(1, 'kg') + V(1, 'lb') > V(1.4, 'kg')
2015-03-24 04:07:01 +00:00
def test_subtraction():
2015-03-24 20:22:32 +00:00
assert V(2, 'kg') - V(1, 'kg') == V(1, 'kg')
assert V(1, 'kg') - V(1, 'lb') < V(0.55, 'kg')
def test_multiplication():
2015-03-24 20:22:32 +00:00
assert V(2, 'kg') * 2 == V(4, 'kg')
assert V(2, 'kg') * V(1, 'kg') == V(2, ['kg', 'kg'])
def test_division():
2015-03-24 20:22:32 +00:00
assert V(8, 'kg') / 2 == V(4, 'kg')
assert V(2, 'kg') / V(1, 'kg') == V(2, [])