File: test_getdotattr.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 (85 lines) | stat: -rw-r--r-- 2,839 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
77
78
79
80
81
82
83
84
85
import sqlalchemy as sa

from sqlalchemy_utils.functions import getdotattr
from tests import TestCase


class TestGetDotAttr(TestCase):
    def create_models(self):
        class Document(self.Base):
            __tablename__ = 'document'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))

        class Section(self.Base):
            __tablename__ = 'section'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))

            document_id = sa.Column(
                sa.Integer, sa.ForeignKey(Document.id)
            )

            document = sa.orm.relationship(Document, backref='sections')

        class SubSection(self.Base):
            __tablename__ = 'subsection'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))

            section_id = sa.Column(
                sa.Integer, sa.ForeignKey(Section.id)
            )

            section = sa.orm.relationship(Section, backref='subsections')

        class SubSubSection(self.Base):
            __tablename__ = 'subsubsection'
            id = sa.Column(sa.Integer, primary_key=True)
            name = sa.Column(sa.Unicode(255))
            locale = sa.Column(sa.String(10))

            subsection_id = sa.Column(
                sa.Integer, sa.ForeignKey(SubSection.id)
            )

            subsection = sa.orm.relationship(
                SubSection, backref='subsubsections'
            )

        self.Document = Document
        self.Section = Section
        self.SubSection = SubSection
        self.SubSubSection = SubSubSection

    def test_simple_objects(self):
        document = self.Document(name=u'some document')
        section = self.Section(document=document)
        subsection = self.SubSection(section=section)

        assert getdotattr(
            subsection,
            'section.document.name'
        ) == u'some document'

    def test_with_instrumented_lists(self):
        document = self.Document(name=u'some document')
        section = self.Section(document=document)
        subsection = self.SubSection(section=section)
        subsubsection = self.SubSubSection(subsection=subsection)

        assert getdotattr(document, 'sections') == [section]
        assert getdotattr(document, 'sections.subsections') == [
            subsection
        ]
        assert getdotattr(document, 'sections.subsections.subsubsections') == [
            subsubsection
        ]

    def test_class_paths(self):
        assert getdotattr(self.Section, 'document') is self.Section.document
        assert (
            getdotattr(self.SubSection, 'section.document') is
            self.Section.document
        )
        assert getdotattr(self.Section, 'document.name') is self.Document.name