File: test_cast_if.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (51 lines) | stat: -rw-r--r-- 1,375 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
import sqlalchemy.orm

from sqlalchemy_utils import cast_if
from sqlalchemy_utils.compat import (
    _declarative_base,
    _select_args,
    get_scalar_subquery
)


@pytest.fixture(scope='class')
def base():
    return _declarative_base()


@pytest.fixture(scope='class')
def article_cls(base):
    class Article(base):
        __tablename__ = 'article'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.String)
        name_synonym = sa.orm.synonym('name')

    return Article


class TestCastIf:
    def test_column(self, article_cls):
        expr = article_cls.__table__.c.name
        assert cast_if(expr, sa.String) is expr

    def test_column_property(self, article_cls):
        expr = article_cls.name.property
        assert cast_if(expr, sa.String) is expr

    def test_instrumented_attribute(self, article_cls):
        expr = article_cls.name
        assert cast_if(expr, sa.String) is expr

    def test_synonym(self, article_cls):
        expr = article_cls.name_synonym
        assert cast_if(expr, sa.String) is expr

    def test_scalar_selectable(self, article_cls):
        expr = get_scalar_subquery(sa.select(*_select_args(article_cls.id)))
        assert cast_if(expr, sa.Integer) is expr

    def test_scalar(self):
        assert cast_if('something', sa.String) == 'something'