importpytest
fromsqlobjectimport ForeignKey, SQLMultipleJoin, SQLObject, SQLRelatedJoin, \
StringCol
fromsqlobject.tests.dbtestimport inserts, setupClass
# Tests retrieving objects through a join/fk on a selectResults
[docs]
classSRThrough1(SQLObject):
three = ForeignKey('SRThrough3')
twos = SQLMultipleJoin('SRThrough2', joinColumn='oneID')
[docs]
classSRThrough2(SQLObject):
one = ForeignKey('SRThrough1')
threes = SQLRelatedJoin('SRThrough3', addRemoveName='Three')
[docs]
classSRThrough3(SQLObject):
name = StringCol()
ones = SQLMultipleJoin('SRThrough1', joinColumn='threeID')
twos = SQLRelatedJoin('SRThrough2')
[docs]
defsetup_module(mod):
global ones, twos, threes
setupClass([SRThrough3, SRThrough1, SRThrough2])
threes = inserts(SRThrough3,
[('a',), ('b',), ('c',)],
'name')
ones = inserts(SRThrough1,
[(threes[0].id,), (threes[0].id,), (threes[2].id,)],
'threeID')
twos = inserts(SRThrough2,
[(ones[0].id,), (ones[1].id,), (ones[2].id,)],
'oneID')
twos[0].addThree(threes[0])
twos[0].addThree(threes[1])
[docs]
deftestBadRef():
with pytest.raises(AttributeError):
threes[0].throughTo.four
[docs]
deftestThroughFK():
assert list(threes[0].ones.throughTo.three) == [threes[0]]
[docs]
deftestThroughMultipleJoin():
assert list(threes[0].ones.throughTo.twos) == [twos[0], twos[1]]
[docs]
deftestThroughFKAndJoin():
assert list(threes[0].ones.throughTo.three.throughTo.twos) == [twos[0]]