Commit 9b4a6119 by johnk

bugs with_parent, repr, column type

Code coverage revealed some untested paths
related to retrieving rows from Parse,
rehydrating objects, and then using relation(),
and using relation() before it has a type.
parent 6bf7dbd1
......@@ -168,9 +168,8 @@ class Relation(ParseType):
# If we're called from from_native, we only know the related class.
# There is no way to know the parent object at this time, so we return.
if 'ClassName' in kw:
if 'className' in kw:
self.relatedClassName = kw['className']
return self
# If we're called to create a new Relation, the parentObject must
# be specified. We also defer creation of the relation until the
......@@ -198,9 +197,13 @@ class Relation(ParseType):
self.parentObject.__dict__[self.key] = self
def __repr__(self):
className = objectId = None
if self.parentObject is not None:
className = self.parentObject.className
objectId = self.parentObject.objectId
repr = u'<Relation where %s:%s for %s>' % \
(self.parentObject.className,
self.parentObject.objectId,
(className,
objectId,
self.relatedClassName)
return repr
......@@ -240,8 +243,6 @@ class Relation(ParseType):
self._probe_for_relation_class()
key = '%s__relatedTo' % (self.key,)
kw = {key: self.parentObject}
if not hasattr(self, 'relatedClassName'):
self._probe_for_relation_class()
relatedClass = Object.factory(self.relatedClassName)
q = relatedClass.Query.all().filter(**kw)
return q
......@@ -618,6 +619,9 @@ class Object(six.with_metaclass(ObjectMetaclass, ParseResource)):
def relation(self, key):
if hasattr(self, key):
return getattr(self, key).with_parent(parentObject=self, key=key)
try:
return getattr(self, key).with_parent(parentObject=self, key=key)
except:
raise ParseError("Column '%s' is not a Relation." % (key,))
else:
return Relation(parentObject=self, key=key)
......@@ -16,6 +16,7 @@ from itertools import chain
from parse_rest.core import ResourceRequestNotFound
from parse_rest.core import ResourceRequestBadRequest
from parse_rest.core import ParseError
from parse_rest.connection import register, ParseBatcher
from parse_rest.datatypes import GeoPoint, Object, Function, Pointer, Relation
from parse_rest.user import User
......@@ -130,6 +131,35 @@ class TestRelation(unittest.TestCase):
scores = self.rel.query()
self.assertEqual(len(scores), 0)
def testWrongColumnForRelation(self):
"""Should get a ParseError if we specify a relation on
a column that is not a relation.
"""
with self.assertRaises(ParseError):
rel = self.game.relation("name")
bad = rel.query()
def testWrongColumnForRelation(self):
"""Should get a ParseError if we specify a relation on
a column that is not a relation.
"""
with self.assertRaises(KeyError):
rel = self.game.relation("nonexistent")
bad = rel.query()
def testRepr(self):
s = "*** %s ***" % (self.rel)
self.assertRegex(s, '<Relation where .+ for .+>')
def testWithParent(self):
"""Rehydrating a relation from an instance on the server.
With_parent is called by relation() when the object was
retrieved from the server. This test is for code coverage.
"""
game2 = Game.Query.get(objectId=self.game.objectId)
# self.assertTrue(hasattr(game2, 'scores'))
rel2 = game2.relation('scores')
# self.assertIsInstance(rel2, Relation)
def run_tests():
"""Run all tests in the parse_rest package"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment