test_sum.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. import copy
  4. import dingguo
  5. @pytest.mark.parametrize(('params', 'kwargs', 'expected_value', 'expected_currency'), [
  6. [[1.0, u'US$'], {}, 1.0, u'USD'],
  7. [[1.0, u'USD'], {}, 1.0, u'USD'],
  8. [[-4.0], {'unit': u'EUR'}, -4.0, u'EUR'],
  9. [[-4.0], {'unit': u'€'}, -4.0, u'EUR'],
  10. [[-4.0], {'currency': u'EUR'}, -4.0, u'EUR'],
  11. [[-4.0], {'currency': u'€'}, -4.0, u'EUR'],
  12. [[], {'value': 2.3, 'unit': u'EUR'}, 2.3, u'EUR'],
  13. [[], {'value': 2.3, 'unit': u'€'}, 2.3, u'EUR'],
  14. [[], {'value': 2.3, 'currency': u'EUR'}, 2.3, u'EUR'],
  15. [[], {'value': 2.3, 'currency': u'€'}, 2.3, u'EUR'],
  16. ])
  17. def test_init(params, kwargs, expected_value, expected_currency):
  18. f = dingguo.Sum(*params, **kwargs)
  19. assert type(f.value) == type(expected_value)
  20. assert type(f.unit) == type(expected_currency)
  21. assert f.value == expected_value
  22. assert f.unit == expected_currency
  23. assert f.currency == expected_currency
  24. @pytest.mark.parametrize(('params', 'kwargs'), [
  25. [[(-1, 3), u'EUR'], {}],
  26. [[1, 'EUR'], {}],
  27. [[1, u'EUR'], {}],
  28. [[1.0, u'USD', u'EUR'], {}],
  29. [[1.2], {'value': 2.3, 'unit': u'EUR'}],
  30. [[1], {'unit': 'EUR'}],
  31. [[1], {'unit': u'EUR'}],
  32. [[None, u'EUR'], {}],
  33. [[None, u'kg'], {}],
  34. [[None], {}],
  35. [[], {'unit': u'EUR'}],
  36. [[], {'value': (3, -1), 'unit': u'EUR'}],
  37. [[], {'value': 2.3, 'unit': u'EUR', 'currency': u'EUR'}],
  38. [[], {}],
  39. ])
  40. def test_init_fail(params, kwargs):
  41. with pytest.raises(Exception):
  42. dingguo.Sum(*params, **kwargs)
  43. @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
  44. [dingguo.Sum(5.0, u'USD'), 1.5, dingguo.Sum(7.5, u'USD')],
  45. [dingguo.Sum(5.0, u'USD'), 2, dingguo.Sum(10.0, u'USD')],
  46. [dingguo.Sum(5.0, u'EUR'), 1.5, dingguo.Sum(7.5, u'EUR')],
  47. [dingguo.Sum(5.0, u'EUR'), 2, dingguo.Sum(10.0, u'EUR')],
  48. ])
  49. def test_scalar_figure_mul(factor_a, factor_b, product):
  50. factor_a_copy = copy.deepcopy(factor_a)
  51. factor_b_copy = copy.deepcopy(factor_b)
  52. assert (factor_a * factor_b) == product
  53. assert factor_a_copy == factor_a
  54. assert factor_b_copy == factor_b
  55. @pytest.mark.parametrize(('factor_a', 'factor_b'), [
  56. [dingguo.ScalarFigure(5.0, u'USD'), dingguo.ScalarFigure(2.0, u'USD')],
  57. [dingguo.ScalarFigure(5.0, u'EUR'), dingguo.ScalarFigure(2.0, u'USD')],
  58. [dingguo.ScalarFigure(5.0, u'USD'), '23'],
  59. ])
  60. def test_figure_mul_fail(factor_a, factor_b):
  61. with pytest.raises(Exception):
  62. (factor_a * factor_b)