More tests for attr observers
This commit is contained in:
@@ -90,12 +90,13 @@ class AttributeValueGenerator(object):
|
|||||||
)
|
)
|
||||||
elif index == len(path) - 1:
|
elif index == len(path) - 1:
|
||||||
inversed_path = ~path[0:-1]
|
inversed_path = ~path[0:-1]
|
||||||
entities = list(
|
entities = getdotattr(
|
||||||
getdotattr(
|
|
||||||
target,
|
target,
|
||||||
str(inversed_path)
|
str(inversed_path)
|
||||||
)
|
)
|
||||||
)
|
if entities:
|
||||||
|
if not isinstance(entities, list):
|
||||||
|
entities = [entities]
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
if isinstance(entity, list):
|
if isinstance(entity, list):
|
||||||
for e in entity:
|
for e in entity:
|
||||||
@@ -112,12 +113,13 @@ class AttributeValueGenerator(object):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
inversed_path = ~path[0:-1]
|
inversed_path = ~path[0:-1]
|
||||||
entities = list(
|
entities = getdotattr(
|
||||||
getdotattr(
|
|
||||||
target,
|
target,
|
||||||
str(inversed_path[index:])
|
str(inversed_path[index:])
|
||||||
)
|
)
|
||||||
)
|
if entities:
|
||||||
|
if not isinstance(entities, list):
|
||||||
|
entities = [entities]
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
setattr(
|
setattr(
|
||||||
entity,
|
entity,
|
||||||
|
@@ -251,6 +251,8 @@ def getdotattr(obj_or_class, dot_path):
|
|||||||
last = tmp
|
last = tmp
|
||||||
elif isinstance(last, InstrumentedAttribute):
|
elif isinstance(last, InstrumentedAttribute):
|
||||||
last = getter(last.property.mapper.class_)
|
last = getter(last.property.mapper.class_)
|
||||||
|
elif last is None:
|
||||||
|
return None
|
||||||
else:
|
else:
|
||||||
last = getter(last)
|
last = getter(last)
|
||||||
return last
|
return last
|
||||||
|
@@ -85,6 +85,9 @@ class AttrPath(object):
|
|||||||
prop.parent.class_.__name__
|
prop.parent.class_.__name__
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
if isinstance(backref, tuple):
|
||||||
|
return backref[0]
|
||||||
|
else:
|
||||||
return backref
|
return backref
|
||||||
|
|
||||||
if isinstance(self.parts[-1].property, sa.orm.ColumnProperty):
|
if isinstance(self.parts[-1].property, sa.orm.ColumnProperty):
|
||||||
|
@@ -170,9 +170,86 @@ class TestInstantAttributeValueGeneration(TestCase):
|
|||||||
document = self.Document(name=u'Document 1', locale='en')
|
document = self.Document(name=u'Document 1', locale='en')
|
||||||
section = self.Section(name=u'Section 1', document=document)
|
section = self.Section(name=u'Section 1', document=document)
|
||||||
subsection = self.SubSection(name=u'Section 1', section=section)
|
subsection = self.SubSection(name=u'Section 1', section=section)
|
||||||
assert subsection.locale == 'en'
|
|
||||||
document.locale = 'fi'
|
document.locale = 'fi'
|
||||||
assert subsection.locale == 'fi'
|
assert subsection.locale == 'fi'
|
||||||
|
|
||||||
|
def test_simple_assignment(self):
|
||||||
|
document = self.Document(name=u'Document 1', locale='en')
|
||||||
|
section = self.Section(name=u'Section 1', document=document)
|
||||||
|
subsection = self.SubSection(name=u'Section 1', section=section)
|
||||||
|
assert subsection.locale == 'en'
|
||||||
|
|
||||||
|
def test_intermediate_object_reference(self):
|
||||||
|
document = self.Document(name=u'Document 1', locale='en')
|
||||||
|
section = self.Section(name=u'Section 1', document=document)
|
||||||
|
subsection = self.SubSection(name=u'Section 1', section=section)
|
||||||
section.document = self.Document(name=u'Document 2', locale='sv')
|
section.document = self.Document(name=u'Document 2', locale='sv')
|
||||||
assert subsection.locale == 'sv'
|
assert subsection.locale == 'sv'
|
||||||
|
|
||||||
|
|
||||||
|
class TestInstantAttrGenerationWithScalars(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))
|
||||||
|
locale = sa.Column(sa.String(10))
|
||||||
|
|
||||||
|
class Section(self.Base):
|
||||||
|
__tablename__ = 'section'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column(sa.Unicode(255))
|
||||||
|
locale = sa.Column(sa.String(10))
|
||||||
|
|
||||||
|
document_id = sa.Column(
|
||||||
|
sa.Integer, sa.ForeignKey(Document.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
document = sa.orm.relationship(
|
||||||
|
Document,
|
||||||
|
backref=sa.orm.backref('section', uselist=False)
|
||||||
|
)
|
||||||
|
|
||||||
|
class SubSection(self.Base):
|
||||||
|
__tablename__ = 'subsection'
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column(sa.Unicode(255))
|
||||||
|
locale = sa.Column(sa.String(10))
|
||||||
|
|
||||||
|
section_id = sa.Column(
|
||||||
|
sa.Integer, sa.ForeignKey(Section.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
section = sa.orm.relationship(
|
||||||
|
Section,
|
||||||
|
backref=sa.orm.backref('subsection', uselist=False)
|
||||||
|
)
|
||||||
|
|
||||||
|
@generates(locale, source='section.document.locale')
|
||||||
|
def copy_locale(self, value):
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
self.Document = Document
|
||||||
|
self.Section = Section
|
||||||
|
self.SubSection = SubSection
|
||||||
|
|
||||||
|
def test_change_parent_attribute(self):
|
||||||
|
document = self.Document(name=u'Document 1', locale='en')
|
||||||
|
section = self.Section(name=u'Section 1', document=document)
|
||||||
|
subsection = self.SubSection(name=u'Section 1', section=section)
|
||||||
|
document.locale = 'fi'
|
||||||
|
assert subsection.locale == 'fi'
|
||||||
|
|
||||||
|
def test_simple_assignment(self):
|
||||||
|
document = self.Document(name=u'Document 1', locale='en')
|
||||||
|
section = self.Section(name=u'Section 1', document=document)
|
||||||
|
subsection = self.SubSection(name=u'Section 1', section=section)
|
||||||
|
assert subsection.locale == 'en'
|
||||||
|
|
||||||
|
def test_intermediate_object_reference(self):
|
||||||
|
document = self.Document(name=u'Document 1', locale='en')
|
||||||
|
section = self.Section(name=u'Section 1', document=document)
|
||||||
|
subsection = self.SubSection(name=u'Section 1', section=section)
|
||||||
|
section.document = self.Document(name=u'Document 2', locale='sv')
|
||||||
|
assert subsection.locale == 'sv'
|
||||||
|
Reference in New Issue
Block a user