Commit 60755bf6 by David Robinson

Added ability to access a parse_rest.Object like a dictionary or an iterable,…

Added ability to access a parse_rest.Object like a dictionary or an iterable, along with test cases for that functionality.
parent bde3cfc1
......@@ -204,6 +204,26 @@ class Object(ParseResource):
def remove(self, attr):
self.__dict__.pop(attr)
# dictionary and list-like methods
def __contains__(self, k):
return not k.startswith("_") and k in self.__dict__
def __iter__(self):
return (k for k in self.__dict__ if not k.startswith("_"))
def __getitem__(self, key):
if key.startswith("_"):
raise KeyError("Cannot access this value in this way")
return self.__dict__[key]
def __setitem__(self, key, value):
if key.startswith("_"):
raise KeyError("Cannot change this value in this way")
self.__dict__[key] = value
def items(self):
return self._attributes.items()
def refresh(self):
uri = '/%s/%s' % (self._class_name, self._object_id)
response_dict = self.__class__.execute(uri, 'GET')
......
......@@ -88,6 +88,17 @@ class TestObjectAndQuery(unittest.TestCase):
self.assertGreater(obj1.updatedAt(), current_updated)
self.assertEqual(obj1.score, 1000)
# test accessing like a dictionary
self.assertTrue("playerName" in obj1)
self.assertTrue("score" in obj1)
self.assertEqual(obj1["score"], 1000)
self.assertEqual(obj1["playerName"], "Sean Plott")
obj1["playerName"] = "Sean Scott"
self.assertEqual(obj1.playerName, "Sean Scott")
# non-existent or forbidden lookup
self.assertRaises(KeyError, obj1.__getitem__, "nosuchkey")
self.assertRaises(KeyError, obj1.__getitem__, "_class_name")
# re-retrieve it
obj2 = query.get(obj1.objectId())
self.assertEqual(obj2.score, 1000)
......@@ -239,6 +250,10 @@ class TestUser(unittest.TestCase):
self.assertEqual(u.username, u_retrieved.username)
self.assertEqual(u_retrieved.phone, "555-5555")
# test accessing like a dictionary
self.assertEqual(u_retrieved["username"], "dhelmet@spaceballs.com")
self.assertEqual(u_retrieved["phone"], "555-5555")
# try creating another account with the same user
u2 = parse_rest.User("dhelmet@spaceballs.com", "12345")
self.assertRaises(parse_rest.ParseError, u2.signup)
......
......@@ -22,7 +22,7 @@ class TestCommand(Command):
setup(
name='parse_rest',
version='0.5.2013',
version='0.6.2013',
description='A client library for Parse.com\'.s REST API',
url='https://github.com/dgrtwo/ParsePy',
packages=['parse_rest'],
......
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