1
0

test_distance.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import pytest
  2. import copy
  3. import finoex
  4. @pytest.mark.parametrize(('params', 'kwargs', 'expected_value', 'expected_unit'), [
  5. [[1.0, u'dm'], {}, 1.0, u'dm'],
  6. [[-4.0], {'unit': u'cm'}, -4.0, u'cm'],
  7. [[], {'value': 2.3, 'unit': u'cm'}, 2.3, u'cm'],
  8. ])
  9. def test_init(params, kwargs, expected_value, expected_unit):
  10. f = finoex.Distance(*params, **kwargs)
  11. assert type(f.value) == type(expected_value)
  12. assert type(f.unit) == type(expected_unit)
  13. assert f.value == expected_value
  14. assert f.unit == expected_unit
  15. @pytest.mark.parametrize(('params', 'kwargs'), [
  16. [[], {}],
  17. [[None], {}],
  18. [[None, u'kg'], {}],
  19. [[1, 'cm'], {}],
  20. [[], {'unit': u'cm'}],
  21. [[1], {'unit': 'cm'}],
  22. [[1, u'cm'], {}],
  23. [[1], {'unit': u'cm'}],
  24. [[(-1, 3), u'cm'], {}],
  25. [[], {'value': (3, -1), 'unit': u'cm'}],
  26. [[1.2], {'value': 2.3, 'unit': u'cm'}],
  27. ])
  28. def test_init_fail(params, kwargs):
  29. with pytest.raises(Exception):
  30. finoex.Distance(*params, **kwargs)
  31. @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
  32. [finoex.Distance(5.0, u'cm'), -0.5, finoex.Distance(-2.5, u'cm')],
  33. [finoex.Distance(1.0, u'kg'), 10, finoex.Distance(10.0, u'kg')],
  34. ])
  35. def test_scalar_figure_mul(factor_a, factor_b, product):
  36. factor_a_copy = copy.deepcopy(factor_a)
  37. factor_b_copy = copy.deepcopy(factor_b)
  38. assert (factor_a * factor_b) == product
  39. assert factor_a_copy == factor_a
  40. assert factor_b_copy == factor_b
  41. @pytest.mark.parametrize(('factor_a', 'factor_b'), [
  42. [finoex.Distance(5.0, u'cm'), finoex.Distance(2.0, u'cm')],
  43. [finoex.Distance(5.0, u'cm'), '23'],
  44. ])
  45. def test_figure_mul_fail(factor_a, factor_b):
  46. with pytest.raises(Exception):
  47. (factor_a * factor_b)
  48. @pytest.mark.parametrize(('dist', 'expected_metres'), [
  49. [finoex.Distance(12.3, 'm'), 12.3],
  50. [finoex.Distance(12.3, 'km'), 12300],
  51. ])
  52. def test_metres(dist, expected_metres):
  53. metres = dist.metres
  54. assert 'm' == metres.unit
  55. assert expected_metres == metres.value