File: test_get_tables.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 (76 lines) | stat: -rw-r--r-- 2,336 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
69
70
71
72
73
74
75
76
import sqlalchemy as sa

from sqlalchemy_utils import get_tables
from tests import TestCase


class TestGetTables(TestCase):
    def create_models(self):
        class TextItem(self.Base):
            __tablename__ = 'text_item'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            type = sa.Column(sa.Unicode(255))

            __mapper_args__ = {
                'polymorphic_on': type,
                'with_polymorphic': '*'
            }

        class Article(TextItem):
            __tablename__ = 'article'
            id = sa.Column(
                sa.Integer, sa.ForeignKey(TextItem.id), primary_key=True
            )
            __mapper_args__ = {
                'polymorphic_identity': u'article'
            }

        self.TextItem = TextItem
        self.Article = Article

    def test_child_class_using_join_table_inheritance(self):
        assert get_tables(self.Article) == [
            self.TextItem.__table__,
            self.Article.__table__
        ]

    def test_entity_using_with_polymorphic(self):
        assert get_tables(self.TextItem) == [
            self.TextItem.__table__,
            self.Article.__table__
        ]

    def test_instrumented_attribute(self):
        assert get_tables(self.TextItem.name) == [
            self.TextItem.__table__,
        ]

    def test_polymorphic_instrumented_attribute(self):
        assert get_tables(self.Article.id) == [
            self.TextItem.__table__,
            self.Article.__table__
        ]

    def test_column(self):
        assert get_tables(self.Article.__table__.c.id) == [
            self.Article.__table__
        ]

    def test_mapper_entity_with_class(self):
        query = self.session.query(self.Article)
        assert get_tables(query._entities[0]) == [
            self.TextItem.__table__, self.Article.__table__
        ]

    def test_mapper_entity_with_mapper(self):
        query = self.session.query(sa.inspect(self.Article))
        assert get_tables(query._entities[0]) == [
            self.TextItem.__table__, self.Article.__table__
        ]

    def test_column_entity(self):
        query = self.session.query(self.Article.id)
        assert get_tables(query._entities[0]) == [
            self.TextItem.__table__, self.Article.__table__
        ]