test_sum.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import pytest
  2. import copy
  3. import finoex
  4. import ioex
  5. import re
  6. @pytest.mark.parametrize(('params', 'kwargs', 'expected_value', 'expected_currency'), [
  7. [[1.0, u'US$'], {}, 1.0, u'USD'],
  8. [[1.0, u'USD'], {}, 1.0, u'USD'],
  9. [[-4.0], {'unit': u'EUR'}, -4.0, u'EUR'],
  10. [[-4.0], {'unit': u'€'}, -4.0, u'EUR'],
  11. [[-4.0], {'currency': u'EUR'}, -4.0, u'EUR'],
  12. [[-4.0], {'currency': u'¥'}, -4.0, u'CNY'],
  13. [[], {'value': 2.3, 'unit': u'EUR'}, 2.3, u'EUR'],
  14. [[], {'value': 2.3, 'unit': u'€'}, 2.3, u'EUR'],
  15. [[], {'value': 2.3, 'currency': u'EUR'}, 2.3, u'EUR'],
  16. [[], {'value': 2.3, 'currency': u'€'}, 2.3, u'EUR'],
  17. ])
  18. def test_init(params, kwargs, expected_value, expected_currency):
  19. f = finoex.Sum(*params, **kwargs)
  20. assert type(f.value) == type(expected_value)
  21. assert type(f.unit) == type(expected_currency)
  22. assert f.value == expected_value
  23. assert f.unit == expected_currency
  24. assert f.currency == expected_currency
  25. @pytest.mark.parametrize(('params', 'kwargs'), [
  26. [[(-1, 3), u'EUR'], {}],
  27. [[1, 'EUR'], {}],
  28. [[1.0, ('EUR',)], {}],
  29. [[1.0, u'USD', u'EUR'], {}],
  30. [[1.2], {'value': 2.3, 'unit': u'EUR'}],
  31. [[1], {'unit': 'EUR'}],
  32. [[1], {'unit': u'EUR'}],
  33. [[None, u'EUR'], {}],
  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. finoex.Sum(*params, **kwargs)
  43. @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
  44. [finoex.Sum(5.0, u'USD'), 1.5, finoex.Sum(7.5, u'USD')],
  45. [finoex.Sum(5.0, u'USD'), 2, finoex.Sum(10.0, u'USD')],
  46. [finoex.Sum(5.0, u'EUR'), 1.5, finoex.Sum(7.5, u'EUR')],
  47. [finoex.Sum(5.0, u'EUR'), 2, finoex.Sum(10.0, u'EUR')],
  48. ])
  49. def test_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. [finoex.Sum(5.0, u'USD'), finoex.Sum(2.0, u'USD')],
  57. [finoex.Sum(5.0, u'EUR'), finoex.Sum(2.0, u'USD')],
  58. [finoex.Sum(5.0, u'USD'), '23'],
  59. ])
  60. def test_mul_fail(factor_a, factor_b):
  61. with pytest.raises(Exception):
  62. (factor_a * factor_b)
  63. @pytest.mark.parametrize(('dividend', 'divisor', 'quotient'), [
  64. [finoex.Sum(5.0, u'USD'), 2.0, finoex.Sum(2.5, u'USD')],
  65. [finoex.Sum(5.0, u'USD'), 2, finoex.Sum(2.5, u'USD')],
  66. [finoex.Sum(5.0, u'EUR'), -10, finoex.Sum(-0.5, u'EUR')],
  67. ])
  68. def test_mul(dividend, divisor, quotient):
  69. dividend_copy = copy.deepcopy(dividend)
  70. divisor_copy = copy.deepcopy(divisor)
  71. assert quotient == (dividend / divisor)
  72. assert dividend_copy == dividend
  73. assert divisor_copy == divisor
  74. @pytest.mark.parametrize(('loc', 'text', 'expected_sum'), [
  75. ['de_AT.UTF-8', "-1,23 USD", finoex.Sum(-1.23, 'USD')],
  76. ['de_AT.UTF-8', "2,50 EUR", finoex.Sum(2.5, 'EUR')],
  77. ['de_AT.UTF-8', "2,50 €", finoex.Sum(2.5, 'EUR')],
  78. ['de_AT.UTF-8', "EUR 1234,56", finoex.Sum(1234.56, 'EUR')],
  79. ['de_AT.UTF-8', "US$ 0,50", finoex.Sum(0.5, 'USD')],
  80. ['de_AT.UTF-8', "US$0,50", finoex.Sum(0.5, 'USD')],
  81. ['en_US.UTF-8', "$-1.23 USD", finoex.Sum(-1.23, 'USD')],
  82. ['en_US.UTF-8', "$1.23 USD", finoex.Sum(1.23, 'USD')],
  83. ['en_US.UTF-8', "-1.23 US$", finoex.Sum(-1.23, 'USD')],
  84. ['en_US.UTF-8', "-1.23 USD", finoex.Sum(-1.23, 'USD')],
  85. ['en_US.UTF-8', "1.23 ¥", finoex.Sum(1.23, 'CNY')],
  86. ['en_US.UTF-8', "2.2 US$", finoex.Sum(2.2, 'US$')],
  87. ['en_US.UTF-8', "2.50 EUR", finoex.Sum(2.5, 'EUR')],
  88. ['en_US.UTF-8', "2.50 €", finoex.Sum(2.5, 'EUR')],
  89. ['en_US.UTF-8', "US$-0.50", finoex.Sum(-0.5, 'USD')],
  90. ['en_US.UTF-8', "\u20ac10.26", finoex.Sum(10.26, 'EUR')],
  91. ['en_US.UTF-8', "¥1.23", finoex.Sum(1.23, 'CNY')],
  92. ])
  93. def test_parse_text(loc, text, expected_sum):
  94. with ioex.setlocale(loc):
  95. assert expected_sum == finoex.Sum.parse_text(text)
  96. @pytest.mark.parametrize(('text'), [
  97. 'pre$1.23 USD',
  98. '$1#23 USD',
  99. '1#23 USD',
  100. ])
  101. def test_parse_text_fail(text):
  102. with pytest.raises(Exception):
  103. finoex.Sum.parse_text(text)
  104. @pytest.mark.parametrize(('haystack', 'expected_needles'), [
  105. ["Preis: 0,50 US$", [{'currency': 'US$', 'value': '0,50'}]],
  106. ["Preis: 0,50 €", [{'currency': '€', 'value': '0,50'}]],
  107. ["Preis: 1234 ¥", [{'currency': '¥', 'value': '1234'}]],
  108. ["Preis: US$ 0,50", []],
  109. ["Preis: € 0,50", []],
  110. ["Preis: ¥1234", []],
  111. ["price: 1.23 US$", [{'currency': 'US$', 'value': '1.23'}]],
  112. ["price: 1.23 €", [{'currency': '€', 'value': '1.23'}]],
  113. ["price: US$ 1.23", []],
  114. ["price: €1.23", []],
  115. ])
  116. def test_sum_regex_value_first(haystack, expected_needles):
  117. matches = re.finditer(
  118. finoex.Sum.sum_regex_value_first,
  119. haystack,
  120. )
  121. assert expected_needles == [m.groupdict() for m in matches]
  122. @pytest.mark.parametrize(('haystack', 'expected_needles'), [
  123. ["Preis: US$ 0,50", [{'currency': 'US$', 'value': '0,50'}]],
  124. ["Preis: € 0,50", [{'currency': '€', 'value': '0,50'}]],
  125. ["Preis: ¥1234", [{'currency': '¥', 'value': '1234'}]],
  126. ["price: US$ 1.23", [{'currency': 'US$', 'value': '1.23'}]],
  127. ["price: €1.23", [{'currency': '€', 'value': '1.23'}]],
  128. ])
  129. def test_sum_regex_currency_first(haystack, expected_needles):
  130. matches = re.finditer(
  131. finoex.Sum.sum_regex_currency_first,
  132. haystack,
  133. )
  134. assert expected_needles == [m.groupdict() for m in matches]
  135. @pytest.mark.parametrize(('haystack', 'expected_needles'), [
  136. ["0,50 US$", [{'post_currency': 'US$', 'pre_value': '0,50'}]],
  137. ["0,50 €", [{'post_currency': '€', 'pre_value': '0,50'}]],
  138. ["1234 ¥", [{'post_currency': '¥', 'pre_value': '1234'}]],
  139. ["1.23 US$", [{'post_currency': 'US$', 'pre_value': '1.23'}]],
  140. ["1.23 €", [{'post_currency': '€', 'pre_value': '1.23'}]],
  141. ["US$ 0,50", [{'pre_currency': 'US$', 'post_value': '0,50'}]],
  142. ["€ 0,50", [{'pre_currency': '€', 'post_value': '0,50'}]],
  143. ["¥1234", [{'pre_currency': '¥', 'post_value': '1234'}]],
  144. ["US$ 1.23", [{'pre_currency': 'US$', 'post_value': '1.23'}]],
  145. ["€1.23", [{'pre_currency': '€', 'post_value': '1.23'}]],
  146. ])
  147. def test_sum_regex(haystack, expected_needles):
  148. matches = re.finditer(
  149. finoex.Sum.sum_regex,
  150. haystack,
  151. )
  152. attr = [{k: v for k, v in m.groupdict().items() if v is not None}
  153. for m in matches]
  154. assert expected_needles == attr