1
0

test_sum.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ['de_AT.UTF-8', "US$\xa00,50", finoex.Sum(0.5, 'USD')],
  82. ['en_US.UTF-8', "$-1.23 USD", 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 US$", finoex.Sum(-1.23, 'USD')],
  85. ['en_US.UTF-8', "-1.23 USD", finoex.Sum(-1.23, 'USD')],
  86. ['en_US.UTF-8', "-1.23\xa0USD", finoex.Sum(-1.23, 'USD')],
  87. ['en_US.UTF-8', "1.23 ¥", finoex.Sum(1.23, 'CNY')],
  88. ['en_US.UTF-8', "2.2 US$", finoex.Sum(2.2, 'US$')],
  89. ['en_US.UTF-8', "2.50 EUR", finoex.Sum(2.5, 'EUR')],
  90. ['en_US.UTF-8', "2.50 €", finoex.Sum(2.5, 'EUR')],
  91. ['en_US.UTF-8', "US$-0.50", finoex.Sum(-0.5, 'USD')],
  92. ['en_US.UTF-8', "\u20ac10.26", finoex.Sum(10.26, 'EUR')],
  93. ['en_US.UTF-8', "¥1.23", finoex.Sum(1.23, 'CNY')],
  94. ])
  95. def test_parse_text(loc, text, expected_sum):
  96. with ioex.setlocale(loc):
  97. assert expected_sum == finoex.Sum.parse_text(text)
  98. @pytest.mark.parametrize(('text'), [
  99. 'pre$1.23 USD',
  100. '$1#23 USD',
  101. '1#23 USD',
  102. ])
  103. def test_parse_text_fail(text):
  104. with pytest.raises(Exception):
  105. finoex.Sum.parse_text(text)
  106. @pytest.mark.parametrize(('haystack', 'expected_needles'), [
  107. ["Preis: 0,50 US$", [{'currency': 'US$', 'value': '0,50'}]],
  108. ["Preis: 0,50 €", [{'currency': '€', 'value': '0,50'}]],
  109. ["Preis: 1234 ¥", [{'currency': '¥', 'value': '1234'}]],
  110. ["Preis: US$ 0,50", []],
  111. ["Preis: € 0,50", []],
  112. ["Preis: ¥1234", []],
  113. ["price: 1.23 US$", [{'currency': 'US$', 'value': '1.23'}]],
  114. ["price: 1.23 €", [{'currency': '€', 'value': '1.23'}]],
  115. ["price: US$ 1.23", []],
  116. ["price: €1.23", []],
  117. ])
  118. def test_sum_regex_value_first(haystack, expected_needles):
  119. matches = re.finditer(
  120. finoex.Sum.sum_regex_value_first,
  121. haystack,
  122. )
  123. assert expected_needles == [m.groupdict() for m in matches]
  124. @pytest.mark.parametrize(('haystack', 'expected_needles'), [
  125. ["Preis: US$ 0,50", [{'currency': 'US$', 'value': '0,50'}]],
  126. ["Preis: € 0,50", [{'currency': '€', 'value': '0,50'}]],
  127. ["Preis: ¥1234", [{'currency': '¥', 'value': '1234'}]],
  128. ["price: US$ 1.23", [{'currency': 'US$', 'value': '1.23'}]],
  129. ["price: €1.23", [{'currency': '€', 'value': '1.23'}]],
  130. ])
  131. def test_sum_regex_currency_first(haystack, expected_needles):
  132. matches = re.finditer(
  133. finoex.Sum.sum_regex_currency_first,
  134. haystack,
  135. )
  136. assert expected_needles == [m.groupdict() for m in matches]
  137. @pytest.mark.parametrize(('haystack', 'expected_needles'), [
  138. ["0,50 US$", [{'post_currency': 'US$', 'pre_value': '0,50'}]],
  139. ["0,50 €", [{'post_currency': '€', 'pre_value': '0,50'}]],
  140. ["1234 ¥", [{'post_currency': '¥', 'pre_value': '1234'}]],
  141. ["1.23 US$", [{'post_currency': 'US$', 'pre_value': '1.23'}]],
  142. ["1.23 €", [{'post_currency': '€', 'pre_value': '1.23'}]],
  143. ["US$ 0,50", [{'pre_currency': 'US$', 'post_value': '0,50'}]],
  144. ["€ 0,50", [{'pre_currency': '€', 'post_value': '0,50'}]],
  145. ["¥1234", [{'pre_currency': '¥', 'post_value': '1234'}]],
  146. ["US$ 1.23", [{'pre_currency': 'US$', 'post_value': '1.23'}]],
  147. ["€1.23", [{'pre_currency': '€', 'post_value': '1.23'}]],
  148. ])
  149. def test_sum_regex(haystack, expected_needles):
  150. matches = re.finditer(
  151. finoex.Sum.sum_regex,
  152. haystack,
  153. )
  154. attr = [{k: v for k, v in m.groupdict().items() if v is not None}
  155. for m in matches]
  156. assert expected_needles == attr