Files
deb-python-sqlalchemy-utils/tests/functions/test_getdotattr.py
Jacob Magnusson 815f07d6c1 Use pytest fixtures to reduce complexity and repetition
Also:

Allow override of database name and user in tests (important for me as I would have to mess with my PSQL and MySQL database users otherwise)
Use dict.items instead of six.iteritems as it sporadically caused RuntimeError: dictionary changed size during iteration in Python 2.6 tests.
Fix typo DNS to DSN
Adds Python 3.5 to tox.ini
Added an .editorconfig
Import babel.dates in sqlalchemy_utils.i18n as an exception would be raised when using the latest versions of babel.
2016-01-19 10:52:30 +01:00

108 lines
2.9 KiB
Python

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(object):
def test_simple_objects(self, Document, Section, SubSection):
document = Document(name=u'some document')
section = Section(document=document)
subsection = SubSection(section=section)
assert getdotattr(
subsection,
'section.document.name'
) == u'some document'
def test_with_instrumented_lists(
self,
Document,
Section,
SubSection,
SubSubSection
):
document = Document(name=u'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