File: test_currency.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (68 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest

from sqlalchemy_utils import Currency, i18n


@pytest.fixture
def set_get_locale():
    i18n.get_locale = lambda: i18n.babel.Locale('en')


@pytest.mark.skipif('i18n.babel is None')
@pytest.mark.usefixtures('set_get_locale')
class TestCurrency:

    def test_init(self):
        assert Currency('USD') == Currency(Currency('USD'))

    def test_hashability(self):
        assert len({Currency('USD'), Currency('USD')}) == 1

    def test_invalid_currency_code(self):
        with pytest.raises(ValueError):
            Currency('Unknown code')

    def test_invalid_currency_code_type(self):
        with pytest.raises(TypeError):
            Currency(None)

    @pytest.mark.parametrize(
        ('code', 'name'),
        (
            ('USD', 'US Dollar'),
            ('EUR', 'Euro')
        )
    )
    def test_name_property(self, code, name):
        assert Currency(code).name == name

    @pytest.mark.parametrize(
        ('code', 'symbol'),
        (
            ('USD', '$'),
            ('EUR', '€')
        )
    )
    def test_symbol_property(self, code, symbol):
        assert Currency(code).symbol == symbol

    def test_equality_operator(self):
        assert Currency('USD') == 'USD'
        assert 'USD' == Currency('USD')
        assert Currency('USD') == Currency('USD')

    def test_non_equality_operator(self):
        assert Currency('USD') != 'EUR'
        assert not (Currency('USD') != 'USD')

    def test_unicode(self):
        currency = Currency('USD')
        assert str(currency) == 'USD'

    def test_str(self):
        currency = Currency('USD')
        assert str(currency) == 'USD'

    def test_representation(self):
        currency = Currency('USD')
        assert repr(currency) == "Currency('USD')"