8000 Add serialize method to base resource and tests for serialize · tyree88/twilio-python@fd30438 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd30438

Browse files
author
Evan Fossier
committed
Add serialize method to base resource and tests for serialize
1 parent 7441999 commit fd30438

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

tests/test_base_resource.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def test_resource_init():
2424
assert_equal(r.base_uri, base_uri)
2525
assert_equal(r.auth, auth)
2626
assert_equal(r.uri, uri)
27+
assert_equal(r.content, {})
2728

2829

2930
def test_equivalence():
@@ -82,6 +83,13 @@ def testInstanceLoading(self):
8283

8384
assert_true(isinstance(instance, InstanceResource))
8485
assert_equal(instance.sid, "foo")
86+
assert_equal(instance.content, {"sid": "foo"})
87+
88+
def testInstanceSerialize(self):
89+
instance = self.r.load_instance({"sid": "foo"})
90+
91+
assert_true(isinstance(instance, InstanceResource))
92+
assert_equal(instance.serialize(), {"sid": "foo"})
8593

8694
def testListResourceCreateResponse200(self):
8795
"""We should accept 200 OK in response to a POST creating a resource."""
@@ -142,6 +150,7 @@ def test_instance_loading(self):
142150

143151
assert_true(isinstance(instance, NextGenInstanceResource))
144152
assert_equal(instance.sid, "foo")
153+
assert_equal(instance.serialize(), {"sid": "foo"})
145154

146155

147156
class testInstanceResourceInit(unittest.TestCase):
@@ -153,15 +162,18 @@ def setUp(self):
153162

154163
def testInit(self):
155164
assert_equal(self.r.uri, self.uri)
165+
assert_equal(self.r.content, {})
156166

157167
def testLoad(self):
158168
self.r.load({"hey": "you"})
159169
assert_equal(self.r.hey, "you")
170+
assert_equal(self.r.content, {"hey": "you"})
160171

161172
def testLoadWithUri(self):
162173
self.r.load({"hey": "you", "uri": "foobar"})
163174
assert_equal(self.r.hey, "you")
164175
assert_equal(self.r.uri, self.uri)
176+
assert_equal(self.r.content, {"hey": "you"})
165177

166178
def testLoadDateCreated(self):
167179
self.r.load({"date_created": "Sat, 29 Sep 2012 12:47:54 +0000",
@@ -186,6 +198,11 @@ def testLoadSubresources(self):
186198
self.r.load_subresources()
187199
m.assert_called_with(self.r.uri, self.r.auth, self.r.timeout)
188200

201+
def testSerialize(self):
202+
self.r.load({"hey": "you", "uri": "foobar"})
203+
assert_equal(self.r.content, {"hey": "you"})
204+
assert_equal(self.r.serialize(), {"hey": "you"})
205+
189206

190207
class NextGenInstanceResourceTest(unittest.TestCase):
191208
def setUp(self):
@@ -195,6 +212,12 @@ def setUp(self):
195212
def test_load(self):
196213
self.r.load({"hey": "you"})
197214
assert_equal(self.r.hey, "you")
215+
assert_equal(self.r.content, {"hey": "you"})
216+
217+
def test_serialize(self):
218+
self.r.load({"hey": "you"})
219+
assert_equal(self.r.content, {"hey": "you"})
220+
assert_equal(self.r.serialize(), {"hey": "you"})
198221

199222
def test_iso_date_parser(self):
200223
self.r.load({"date_created": "2015-01-01T00:00:00Z"})

twilio/rest/resources/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def __init__(self, base_uri, auth, timeout=UNSET_TIMEOUT):
176176
self.base_uri = base_uri
177177
self.auth = auth
178178
self.timeout = timeout
179+
self.content = {}
179180

180181
def __eq__(self, other):
181182
return (isinstance(other, self.__class__) and
@@ -211,6 +212,9 @@ def uri(self):
211212
format = (self.base_uri, self.name)
212213
return "%s/%s" % format
213214

215+
def serialize(self):
216+
return self.content
217+
214218

215219
class InstanceResource(Resource):
216220
""" The object representation of an instance response from the Twilio API
@@ -249,6 +253,7 @@ def load(self, entries):
249253
entries[key] = self._parse_date(entries[key])
250254

251255
self.__dict__.update(entries)
256+
self.content.update(entries)
252257

253258
def load_subresources(self):
254259
"""
@@ -261,6 +266,7 @@ def load_subresources(self):
261266
self.parent.timeout
262267
)
263268
self.__dict__[list_resource.key] = list_resource
269+
self.content[list_resource.key] = list_resource
264270

265271
def update_instance(self, **kwargs):
266272
""" Make a POST request to the API to update an object's properties
@@ -269,7 +275,7 @@ def update_instance(self, **kwargs):
269275
:raises: a :class:`~twilio.rest.RestException` on failure
270276
"""
271277
a = self.parent.update(self.name, **kwargs)
272-
self.load(a.__dict__)
278+
self.load(a.content)
273279

274280
def delete_instance(self):
275281
""" Make a DELETE request to the API to delete the object

0 commit comments

Comments
 (0)
0