1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- # -*- coding: utf-8 -*-
- import pytest
- import copy
- import dingguo
- @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'EUR'],
- [[], {'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 = dingguo.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, u'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, u'kg'], {}],
- [[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):
- dingguo.Sum(*params, **kwargs)
- @pytest.mark.parametrize(('factor_a', 'factor_b', 'product'), [
- [dingguo.Sum(5.0, u'USD'), 1.5, dingguo.Sum(7.5, u'USD')],
- [dingguo.Sum(5.0, u'USD'), 2, dingguo.Sum(10.0, u'USD')],
- [dingguo.Sum(5.0, u'EUR'), 1.5, dingguo.Sum(7.5, u'EUR')],
- [dingguo.Sum(5.0, u'EUR'), 2, dingguo.Sum(10.0, u'EUR')],
- ])
- def test_scalar_figure_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'), [
- [dingguo.ScalarFigure(5.0, u'USD'), dingguo.ScalarFigure(2.0, u'USD')],
- [dingguo.ScalarFigure(5.0, u'EUR'), dingguo.ScalarFigure(2.0, u'USD')],
- [dingguo.ScalarFigure(5.0, u'USD'), '23'],
- ])
- def test_figure_mul_fail(factor_a, factor_b):
- with pytest.raises(Exception):
- (factor_a * factor_b)
|