Skip to content

Commit

Permalink
fixed issue with setting value on lazy loaded reference directly with…
Browse files Browse the repository at this point in the history
…out the resolve() explicit call
  • Loading branch information
joamag committed Mar 29, 2016
1 parent a553469 commit 01d031e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/quorum/test/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Person(quorum.Model):
type = int
)

father = quorum.field(
type = quorum.reference(
"Person",
name = "identifier"
)
)

cats = quorum.field(
type = quorum.references(
"Cat",
Expand Down
22 changes: 22 additions & 0 deletions src/quorum/test/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,25 @@ def test_references(self):
self.assertEqual(isinstance(person, dict), True)
self.assertEqual(isinstance(person["cats"], list), True)
self.assertEqual(len(person["cats"]), 0)

father = mock.Person()
father.name = "father"
father.save()

person = mock.Person.get(identifier = 1)
person.father = father
person.save()

person = mock.Person.get(identifier = 1)

self.assertEqual(isinstance(person.father, quorum.Reference), True)
self.assertEqual(person.father.name, "father")

person = mock.Person.get(identifier = 1)

person.father.name = "father_changed"
person.father.save()

person = mock.Person.get(identifier = 1)

self.assertEqual(person.father.name, "father_changed")
6 changes: 6 additions & 0 deletions src/quorum/typesf.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def reference(target, name = None, eager = False):
name = name or "id"
target_t = type(target)
is_reference = target_t in legacy.STRINGS
reserved = ("id", "_target", "_object", "_type", "__dict__")

class _Reference(Reference):

Expand Down Expand Up @@ -405,6 +406,11 @@ def __getattr__(self, name):
raise AttributeError("'%s' not found" % name)

def __setattr__(self, name, value):
# in case the name that is being set is not part of the reserved
# names for the reference underlying structure the object resolution
# is triggered to make sure the underlying object exists and is loaded
if not name in reserved: self.resolve()

# verifies if the reference object exists in the current
# reference instance, that's the case if the object name is
# defined in the dictionary and the referenced object contains
Expand Down

0 comments on commit 01d031e

Please sign in to comment.