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 (60 lines) | stat: -rw-r--r-- 1,535 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
import pytest
import sqlalchemy as sa

from sqlalchemy_utils import Currency, CurrencyType, i18n
from sqlalchemy_utils.compat import _select_args


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


@pytest.fixture
def User(Base):
    class User(Base):
        __tablename__ = 'user'
        id = sa.Column(sa.Integer, primary_key=True)
        currency = sa.Column(CurrencyType)

        def __repr__(self):
            return 'User(%r)' % self.id

    return User


@pytest.fixture
def init_models(User):
    pass


@pytest.mark.skipif('i18n.babel is None')
class TestCurrencyType:
    def test_parameter_processing(self, session, User, set_get_locale):
        user = User(
            currency=Currency('USD')
        )

        session.add(user)
        session.commit()

        user = session.query(User).first()
        assert user.currency.name == 'US Dollar'

    def test_scalar_attributes_get_coerced_to_objects(
        self,
        User,
        set_get_locale
    ):
        user = User(currency='USD')
        assert isinstance(user.currency, Currency)

    def test_literal_param(self, session, User):
        clause = User.currency == 'USD'
        compiled = str(clause.compile(compile_kwargs={'literal_binds': True}))
        assert compiled == '"user".currency = \'USD\''

    def test_compilation(self, User, session):
        query = sa.select(*_select_args(User.currency))
        # the type should be cacheable and not throw exception
        session.execute(query)