123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import pytest
- import copy
- import finoex
- import ioex
- import re
- @pytest.mark.parametrize(('params', 'kwargs', 'expected_value', 'expected_currency'), [
- [[1.0, u'US$'], {}, 1.0, u'USD'],
- [[1.0, u'USD'], {}, 1.0, u'USD'],
- [[-4.0], {'unit': u'EUR'}, -4.0, u'EUR'],
- [[-4.0], {'unit': u'€'}, -4.0, u'EUR'],
- [[-4.0], {'currency': u'EUR'}, -4.0, u'EUR'],
- [[-4.0], {'currency': u'¥'}, -4.0, u'CNY'],
- [[], {'value': 2.3, 'unit': u'EUR'}, 2.3, u'EUR'],
- [[], {'value': 2.3, 'unit': u'€'}, 2.3, u'EUR'],
- [[], {'value': 2.3, 'currency': u'EUR'}, 2.3, u'EUR'],
- [[], {'value': 2.3, 'currency': u'€'}, 2.3, u'EUR'],
- ])
- def test_init(params, kwargs, expected_value, expected_currency):
- f = finoex.Sum(*params, **kwargs)
- assert type(f.value) == type(expected_value)
- assert type(f.unit) == type(expected_currency)
- assert f.value == expected_value
- assert f.unit == expected_currency
- assert f.currency == expected_currency
- @pytest.mark.parametrize(('params', 'kwargs'), [
- [[(-1, 3), u'EUR'], {}],
- [[1, 'EUR'], {}],
- [[1.0, ('EUR',)], {}],
- [[1.0, u'USD', u'EUR'], {}],
- [[1.2], {'value': 2.3, 'unit': u'EUR'}],
- [[1], {'unit': 'EUR'}],
- [[1], {'unit': u'EUR'}],
- [[None, u'EUR'], {}],
- [[None], {}],
- [[], {'unit': u'EUR'}],
- [[], {'value': (3, -1), 'unit': u'EUR'}],
- [[], {'value': 2.3, 'unit': u'EUR', 'currency': u'EUR'}],
- [[], {}],
- ])
- def test_init_fail(params, kwargs):
- with pytest.raises(Exception):
- finoex.Sum(*params, **kwargs)
- @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
- [finoex.Sum(5.0, u'USD'), 1.5, finoex.Sum(7.5, u'USD')],
- [finoex.Sum(5.0, u'USD'), 2, finoex.Sum(10.0, u'USD')],
- [finoex.Sum(5.0, u'EUR'), 1.5, finoex.Sum(7.5, u'EUR')],
- [finoex.Sum(5.0, u'EUR'), 2, finoex.Sum(10.0, u'EUR')],
- ])
- def test_mul(factor_a, factor_b, product):
- factor_a_copy = copy.deepcopy(factor_a)
- factor_b_copy = copy.deepcopy(factor_b)
- assert (factor_a * factor_b) == product
- assert factor_a_copy == factor_a
- assert factor_b_copy == factor_b
- @pytest.mark.parametrize(('factor_a', 'factor_b'), [
- [finoex.Sum(5.0, u'USD'), finoex.Sum(2.0, u'USD')],
- [finoex.Sum(5.0, u'EUR'), finoex.Sum(2.0, u'USD')],
- [finoex.Sum(5.0, u'USD'), '23'],
- ])
- def test_mul_fail(factor_a, factor_b):
- with pytest.raises(Exception):
- (factor_a * factor_b)
- @pytest.mark.parametrize(('dividend', 'divisor', 'quotient'), [
- [finoex.Sum(5.0, u'USD'), 2.0, finoex.Sum(2.5, u'USD')],
- [finoex.Sum(5.0, u'USD'), 2, finoex.Sum(2.5, u'USD')],
- [finoex.Sum(5.0, u'EUR'), -10, finoex.Sum(-0.5, u'EUR')],
- ])
- def test_mul(dividend, divisor, quotient):
- dividend_copy = copy.deepcopy(dividend)
- divisor_copy = copy.deepcopy(divisor)
- assert quotient == (dividend / divisor)
- assert dividend_copy == dividend
- assert divisor_copy == divisor
- @pytest.mark.parametrize(('loc', 'text', 'expected_sum'), [
- ['de_AT.UTF-8', "-1,23 USD", finoex.Sum(-1.23, 'USD')],
- ['de_AT.UTF-8', "2,50 EUR", finoex.Sum(2.5, 'EUR')],
- ['de_AT.UTF-8', "2,50 €", finoex.Sum(2.5, 'EUR')],
- ['de_AT.UTF-8', "EUR 1234,56", finoex.Sum(1234.56, 'EUR')],
- ['de_AT.UTF-8', "US$ 0,50", finoex.Sum(0.5, 'USD')],
- ['de_AT.UTF-8', "US$0,50", finoex.Sum(0.5, 'USD')],
- ['de_AT.UTF-8', "US$\xa00,50", finoex.Sum(0.5, 'USD')],
- ['en_US.UTF-8', "$-1.23 USD", finoex.Sum(-1.23, 'USD')],
- ['en_US.UTF-8', "$1.23 USD", finoex.Sum(1.23, 'USD')],
- ['en_US.UTF-8', "-1.23 US$", finoex.Sum(-1.23, 'USD')],
- ['en_US.UTF-8', "-1.23 USD", finoex.Sum(-1.23, 'USD')],
- ['en_US.UTF-8', "-1.23\xa0USD", finoex.Sum(-1.23, 'USD')],
- ['en_US.UTF-8', "1.23 ¥", finoex.Sum(1.23, 'CNY')],
- ['en_US.UTF-8', "2.2 US$", finoex.Sum(2.2, 'US$')],
- ['en_US.UTF-8', "2.50 EUR", finoex.Sum(2.5, 'EUR')],
- ['en_US.UTF-8', "2.50 €", finoex.Sum(2.5, 'EUR')],
- ['en_US.UTF-8', "US$-0.50", finoex.Sum(-0.5, 'USD')],
- ['en_US.UTF-8', "\u20ac10.26", finoex.Sum(10.26, 'EUR')],
- ['en_US.UTF-8', "¥1.23", finoex.Sum(1.23, 'CNY')],
- ])
- def test_parse_text(loc, text, expected_sum):
- with ioex.setlocale(loc):
- assert expected_sum == finoex.Sum.parse_text(text)
- @pytest.mark.parametrize(('text'), [
- 'pre$1.23 USD',
- '$1#23 USD',
- '1#23 USD',
- ])
- def test_parse_text_fail(text):
- with pytest.raises(Exception):
- finoex.Sum.parse_text(text)
- @pytest.mark.parametrize(('haystack', 'expected_needles'), [
- ["Preis: 0,50 US$", [{'currency': 'US$', 'value': '0,50'}]],
- ["Preis: 0,50 €", [{'currency': '€', 'value': '0,50'}]],
- ["Preis: 1234 ¥", [{'currency': '¥', 'value': '1234'}]],
- ["Preis: US$ 0,50", []],
- ["Preis: € 0,50", []],
- ["Preis: ¥1234", []],
- ["price: 1.23 US$", [{'currency': 'US$', 'value': '1.23'}]],
- ["price: 1.23 €", [{'currency': '€', 'value': '1.23'}]],
- ["price: US$ 1.23", []],
- ["price: €1.23", []],
- ])
- def test_sum_regex_value_first(haystack, expected_needles):
- matches = re.finditer(
- finoex.Sum.sum_regex_value_first,
- haystack,
- )
- assert expected_needles == [m.groupdict() for m in matches]
- @pytest.mark.parametrize(('haystack', 'expected_needles'), [
- ["Preis: US$ 0,50", [{'currency': 'US$', 'value': '0,50'}]],
- ["Preis: € 0,50", [{'currency': '€', 'value': '0,50'}]],
- ["Preis: ¥1234", [{'currency': '¥', 'value': '1234'}]],
- ["price: US$ 1.23", [{'currency': 'US$', 'value': '1.23'}]],
- ["price: €1.23", [{'currency': '€', 'value': '1.23'}]],
- ])
- def test_sum_regex_currency_first(haystack, expected_needles):
- matches = re.finditer(
- finoex.Sum.sum_regex_currency_first,
- haystack,
- )
- assert expected_needles == [m.groupdict() for m in matches]
- @pytest.mark.parametrize(('haystack', 'expected_needles'), [
- ["0,50 US$", [{'post_currency': 'US$', 'pre_value': '0,50'}]],
- ["0,50 €", [{'post_currency': '€', 'pre_value': '0,50'}]],
- ["1234 ¥", [{'post_currency': '¥', 'pre_value': '1234'}]],
- ["1.23 US$", [{'post_currency': 'US$', 'pre_value': '1.23'}]],
- ["1.23 €", [{'post_currency': '€', 'pre_value': '1.23'}]],
- ["US$ 0,50", [{'pre_currency': 'US$', 'post_value': '0,50'}]],
- ["€ 0,50", [{'pre_currency': '€', 'post_value': '0,50'}]],
- ["¥1234", [{'pre_currency': '¥', 'post_value': '1234'}]],
- ["US$ 1.23", [{'pre_currency': 'US$', 'post_value': '1.23'}]],
- ["€1.23", [{'pre_currency': '€', 'post_value': '1.23'}]],
- ])
- def test_sum_regex(haystack, expected_needles):
- matches = re.finditer(
- finoex.Sum.sum_regex,
- haystack,
- )
- attr = [{k: v for k, v in m.groupdict().items() if v is not None}
- for m in matches]
- assert expected_needles == attr
|