test_sum.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import pytest
  2. import copy
  3. import finoex
  4. import ioex
  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'CNY'],
  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 = finoex.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.0, ('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], {}],
  34. [[], {'unit': u'EUR'}],
  35. [[], {'value': (3, -1), 'unit': u'EUR'}],
  36. [[], {'value': 2.3, 'unit': u'EUR', 'currency': u'EUR'}],
  37. [[], {}],
  38. ])
  39. def test_init_fail(params, kwargs):
  40. with pytest.raises(Exception):
  41. finoex.Sum(*params, **kwargs)
  42. @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
  43. [finoex.Sum(5.0, u'USD'), 1.5, finoex.Sum(7.5, u'USD')],
  44. [finoex.Sum(5.0, u'USD'), 2, finoex.Sum(10.0, u'USD')],
  45. [finoex.Sum(5.0, u'EUR'), 1.5, finoex.Sum(7.5, u'EUR')],
  46. [finoex.Sum(5.0, u'EUR'), 2, finoex.Sum(10.0, u'EUR')],
  47. ])
  48. def test_mul(factor_a, factor_b, product):
  49. factor_a_copy = copy.deepcopy(factor_a)
  50. factor_b_copy = copy.deepcopy(factor_b)
  51. assert (factor_a * factor_b) == product
  52. assert factor_a_copy == factor_a
  53. assert factor_b_copy == factor_b
  54. @pytest.mark.parametrize(('factor_a', 'factor_b'), [
  55. [finoex.Sum(5.0, u'USD'), finoex.Sum(2.0, u'USD')],
  56. [finoex.Sum(5.0, u'EUR'), finoex.Sum(2.0, u'USD')],
  57. [finoex.Sum(5.0, u'USD'), '23'],
  58. ])
  59. def test_mul_fail(factor_a, factor_b):
  60. with pytest.raises(Exception):
  61. (factor_a * factor_b)
  62. @pytest.mark.parametrize(('dividend', 'divisor', 'quotient'), [
  63. [finoex.Sum(5.0, u'USD'), 2.0, finoex.Sum(2.5, u'USD')],
  64. [finoex.Sum(5.0, u'USD'), 2, finoex.Sum(2.5, u'USD')],
  65. [finoex.Sum(5.0, u'EUR'), -10, finoex.Sum(-0.5, u'EUR')],
  66. ])
  67. def test_mul(dividend, divisor, quotient):
  68. dividend_copy = copy.deepcopy(dividend)
  69. divisor_copy = copy.deepcopy(divisor)
  70. assert quotient == (dividend / divisor)
  71. assert dividend_copy == dividend
  72. assert divisor_copy == divisor
  73. @pytest.mark.parametrize(('loc', 'text', 'expected_sum'), [
  74. ['de_AT.UTF-8', "-1,23 USD", finoex.Sum(-1.23, 'USD')],
  75. ['de_AT.UTF-8', "2,50 EUR", finoex.Sum(2.5, 'EUR')],
  76. ['de_AT.UTF-8', "2,50 €", finoex.Sum(2.5, 'EUR')],
  77. ['de_AT.UTF-8', "EUR 1234,56", finoex.Sum(1234.56, 'EUR')],
  78. ['de_AT.UTF-8', "US$ 0,50", finoex.Sum(0.5, 'USD')],
  79. ['de_AT.UTF-8', "US$0,50", finoex.Sum(0.5, 'USD')],
  80. ['en_US.UTF-8', "$-1.23 USD", finoex.Sum(-1.23, 'USD')],
  81. ['en_US.UTF-8', "$1.23 USD", finoex.Sum(1.23, 'USD')],
  82. ['en_US.UTF-8', "-1.23 US$", finoex.Sum(-1.23, 'USD')],
  83. ['en_US.UTF-8', "-1.23 USD", finoex.Sum(-1.23, 'USD')],
  84. ['en_US.UTF-8', "1.23 ¥", finoex.Sum(1.23, 'CNY')],
  85. ['en_US.UTF-8', "2.2 US$", finoex.Sum(2.2, 'US$')],
  86. ['en_US.UTF-8', "2.50 EUR", finoex.Sum(2.5, 'EUR')],
  87. ['en_US.UTF-8', "2.50 €", finoex.Sum(2.5, 'EUR')],
  88. ['en_US.UTF-8', "US$-0.50", finoex.Sum(-0.5, 'USD')],
  89. ['en_US.UTF-8', "\u20ac10.26", finoex.Sum(10.26, 'EUR')],
  90. ['en_US.UTF-8', "¥1.23", finoex.Sum(1.23, 'CNY')],
  91. ])
  92. def test_parse_text(loc, text, expected_sum):
  93. with ioex.setlocale(loc):
  94. assert expected_sum == finoex.Sum.parse_text(text)
  95. @pytest.mark.parametrize(('text'), [
  96. 'pre$1.23 USD',
  97. '$1#23 USD',
  98. '1#23 USD',
  99. ])
  100. def test_parse_text_fail(text):
  101. with pytest.raises(Exception):
  102. finoex.Sum.parse_text(text)