Skip to content

Commit

Permalink
bugs with_parent, repr, column type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johnk committed Dec 4, 2015
1 parent 6bf7dbd commit 9b4a611
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
18 changes: 11 additions & 7 deletions parse_rest/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ def __init__(self, **kw):

# 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
Expand Down Expand Up @@ -198,9 +197,13 @@ def _create_new_relation(self, obj):
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

Expand Down Expand Up @@ -240,8 +243,6 @@ def query(self):
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
Expand Down Expand Up @@ -618,6 +619,9 @@ def manageRelation(self, action, key, className, objectsId):

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)
30 changes: 30 additions & 0 deletions parse_rest/test_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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
Expand Down Expand Up @@ -130,6 +131,35 @@ def testNoTypeSet(self):
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"""
Expand Down

0 comments on commit 9b4a611

Please sign in to comment.