Skip to content
Browse files

Merge branch 'master' into no-content-response

  • Loading branch information...
2 parents 06432e8 + d9dca10 commit 6a1fdad986349cdbf99cf0055c2bcc9849d1c1ca @carljm carljm committed Dec 2, 2011
Showing with 28 additions and 3 deletions.
  1. +12 −0 remoteobjects/dataobject.py
  2. +2 −1 remoteobjects/promise.py
  3. +11 −0 tests/test_dataobject.py
  4. +3 −2 tests/test_http.py
View
12 remoteobjects/dataobject.py
@@ -187,11 +187,23 @@ def __iter__(self):
def to_dict(self):
"""Encodes the DataObject to a dictionary."""
+ # Start with the last set of data we got from the API
data = deepcopy(self.api_data)
+
+ # Now replace the data with what's actually in our object
for field_name, field in self.fields.iteritems():
value = getattr(self, field.attrname, None)
if value is not None:
data[field.api_name] = field.encode(value)
+ else:
+ data[field.api_name] = None
+
+ # Now delete any fields that ended up being None
+ # since we should exclude them in the resulting dict.
+ for k in data.keys():
+ if data[k] is None:
+ del data[k]
+
return data
@classmethod
View
3 remoteobjects/promise.py
@@ -157,6 +157,7 @@ def get(cls, url, http=None, **kwargs):
self._location = url
self._http = http
self._delivered = False
+ self._get_kwargs = kwargs
return self
@@ -210,7 +211,7 @@ def deliver(self):
if self._http is None:
http = remoteobjects.http.userAgent
- request = self.get_request()
+ request = self.get_request(**self._get_kwargs)
response, content = http.request(**request)
self.update_from_response(request['uri'], response, content)
View
11 tests/test_dataobject.py
@@ -61,6 +61,17 @@ class BasicMost(self.cls):
self.assertEquals(BasicMost.__name__, 'BasicMost',
"metaclass magic didn't break our class's name")
+ bm = BasicMost(name='fred', value=2)
+ bm.api_data = {"name": "fred", "value": 2}
+ bm_dict = bm.to_dict()
+ self.assertEquals({ 'name': 'fred', 'value': 2 }, bm_dict, 'First go-round has proper contents')
+ bm.name = 'tom'
+ bm_dict = bm.to_dict()
+ self.assertEquals({ 'name': 'tom', 'value': 2 }, bm_dict, 'Setting name to another string works')
+ bm.name = None
+ bm_dict = bm.to_dict()
+ self.assertEquals({ 'value': 2 }, bm_dict, 'Setting name to None works, and name is omitted in the dict')
+
def test_descriptorwise(self):
class BasicMost(self.cls):
View
5 tests/test_http.py
@@ -57,12 +57,13 @@ class BasicMost(self.cls):
request = {
'uri': 'http://example.com/ohhai',
- 'headers': {'accept': 'application/json'},
+ 'headers': {'accept': 'application/json', 'x-test': 'boo'},
}
content = """{"name": "Fred", "value": 7}"""
h = utils.mock_http(request, content)
- b = BasicMost.get('http://example.com/ohhai', http=h)
+ b = BasicMost.get('http://example.com/ohhai', http=h,
+ headers={"x-test": "boo"})
self.assertEquals(b.name, 'Fred')
self.assertEquals(b.value, 7)
mox.Verify(h)

0 comments on commit 6a1fdad

Please sign in to comment.
Something went wrong with that request. Please try again.