File: test_get_type.py

package info (click to toggle)
python-sqlalchemy-utils 0.30.12-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,056 kB
  • sloc: python: 10,350; makefile: 160
file content (42 lines) | stat: -rw-r--r-- 1,294 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
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy_utils import get_type


class TestGetType(object):
    def setup_method(self, method):
        Base = declarative_base()

        class User(Base):
            __tablename__ = 'user'
            id = sa.Column(sa.Integer, primary_key=True)

        class Article(Base):
            __tablename__ = 'article'
            id = sa.Column(sa.Integer, primary_key=True)

            author_id = sa.Column(sa.Integer, sa.ForeignKey(User.id))
            author = sa.orm.relationship(User)

            some_property = sa.orm.column_property(
                sa.func.coalesce(id, 1)
            )

        self.Article = Article
        self.User = User

    def test_instrumented_attribute(self):
        assert isinstance(get_type(self.Article.id), sa.Integer)

    def test_column_property(self):
        assert isinstance(get_type(self.Article.id.property), sa.Integer)

    def test_column(self):
        assert isinstance(get_type(self.Article.__table__.c.id), sa.Integer)

    def test_calculated_column_property(self):
        assert isinstance(get_type(self.Article.some_property), sa.Integer)

    def test_relationship_property(self):
        assert get_type(self.Article.author) == self.User