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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import pytest
import sqlalchemy as sa
from sqlalchemy_utils.functions import getdotattr
@pytest.fixture
def Document(Base):
class Document(Base):
__tablename__ = 'document'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Unicode(255))
return Document
@pytest.fixture
def Section(Base, Document):
class Section(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')
return Section
@pytest.fixture
def SubSection(Base, Section):
class SubSection(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')
return SubSection
@pytest.fixture
def SubSubSection(Base, SubSection):
class SubSubSection(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'
)
return SubSubSection
@pytest.fixture
def init_models(Document, Section, SubSection, SubSubSection):
pass
class TestGetDotAttr:
def test_simple_objects(self, Document, Section, SubSection):
document = Document(name='some document')
section = Section(document=document)
subsection = SubSection(section=section)
assert getdotattr(
subsection,
'section.document.name'
) == 'some document'
def test_with_instrumented_lists(
self,
Document,
Section,
SubSection,
SubSubSection
):
document = Document(name='some document')
section = Section(document=document)
subsection = SubSection(section=section)
subsubsection = 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, Document, Section, SubSection):
assert getdotattr(Section, 'document') is Section.document
assert (
getdotattr(SubSection, 'section.document') is
Section.document
)
assert getdotattr(Section, 'document.name') is Document.name
|