File: test_country.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 (51 lines) | stat: -rw-r--r-- 1,367 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
import pytest
import sqlalchemy as sa

from sqlalchemy_utils import Country, CountryType, i18n  # noqa
from sqlalchemy_utils.compat import _select_args


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

        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 TestCountryType:

    def test_parameter_processing(self, session, User):
        user = User(
            country=Country('FI')
        )

        session.add(user)
        session.commit()

        user = session.query(User).first()
        assert user.country.name == 'Finland'

    def test_scalar_attributes_get_coerced_to_objects(self, User):
        user = User(country='FI')
        assert isinstance(user.country, Country)

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

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