Merge "Replace _transpose_component with _filter_component"

This commit is contained in:
Jenkins 2016-07-22 17:20:13 +00:00 committed by Gerrit Code Review
commit 0713525a8d
3 changed files with 19 additions and 23 deletions

View File

@ -92,7 +92,7 @@ class Limits(resource2.Resource):
body = response.json() body = response.json()
body = body[self.resource_key] body = body[self.resource_key]
absolute_body = self._transpose_component( absolute_body = self._filter_component(
body["absolute"], AbsoluteLimits._body_mapping()) body["absolute"], AbsoluteLimits._body_mapping())
self.absolute = AbsoluteLimits.existing(**absolute_body) self.absolute = AbsoluteLimits.existing(**absolute_body)
@ -100,7 +100,7 @@ class Limits(resource2.Resource):
rates = [] rates = []
for rate_body in rates_body: for rate_body in rates_body:
rate_body = self._transpose_component(rate_body, rate_body = self._filter_component(rate_body,
RateLimit._body_mapping()) RateLimit._body_mapping())
rates.append(RateLimit(**rate_body)) rates.append(RateLimit(**rate_body))

View File

@ -463,18 +463,13 @@ class Resource(object):
return _Request(uri, body, headers) return _Request(uri, body, headers)
def _transpose_component(self, component, mapping): def _filter_component(self, component, mapping):
"""Transpose the keys in component based on a mapping """Filter the keys in component based on a mapping
This method converts a dict of server-side data to have This method converts a dict of server-side data to contain
the appropriate keys for attributes on this instance. only the appropriate keys for attributes on this instance.
""" """
result = {} return {k: v for k, v in component.items() if k in mapping.values()}
for key, value in mapping.items():
if value in component:
result[key] = component[value]
return result
def _translate_response(self, response, has_body=True): def _translate_response(self, response, has_body=True):
"""Given a KSA response, inflate this instance with its data """Given a KSA response, inflate this instance with its data
@ -490,11 +485,11 @@ class Resource(object):
if self.resource_key and self.resource_key in body: if self.resource_key and self.resource_key in body:
body = body[self.resource_key] body = body[self.resource_key]
body = self._transpose_component(body, self._body_mapping()) body = self._filter_component(body, self._body_mapping())
self._body.attributes.update(body) self._body.attributes.update(body)
self._body.clean() self._body.clean()
headers = self._transpose_component(response.headers, headers = self._filter_component(response.headers,
self._header_mapping()) self._header_mapping())
self._header.attributes.update(headers) self._header.attributes.update(headers)
self._header.clean() self._header.clean()

View File

@ -668,19 +668,20 @@ class TestResource(base.TestCase):
self.assertEqual({key: {"x": body_value}}, result.body) self.assertEqual({key: {"x": body_value}}, result.body)
self.assertEqual({"y": header_value}, result.headers) self.assertEqual({"y": header_value}, result.headers)
def test__transpose_component(self): def test__filter_component(self):
client_name = "client_name" client_name = "client_name"
server_name = "serverName" server_name = "serverName"
value = "value" value = "value"
# Include something in the mapping that we don't receive # Include something in the mapping that we don't receive
# so the branch that looks at existence in the compoment is checked. # so the branch that looks at existence in the compoment is checked.
mapping = {client_name: server_name, "other": "blah"} mapping = {client_name: server_name, "other": "blah"}
component = {server_name: value} component = {server_name: value, "something": "else"}
sot = resource2.Resource() sot = resource2.Resource()
result = sot._transpose_component(component, mapping) result = sot._filter_component(component, mapping)
self.assertEqual({client_name: value}, result) # The something:else mapping should not make it into here.
self.assertEqual({server_name: value}, result)
def test__translate_response_no_body(self): def test__translate_response_no_body(self):
class Test(resource2.Resource): class Test(resource2.Resource):
@ -690,7 +691,7 @@ class TestResource(base.TestCase):
response.headers = dict() response.headers = dict()
sot = Test() sot = Test()
sot._transpose_component = mock.Mock(return_value={"attr": "value"}) sot._filter_component = mock.Mock(return_value={"attr": "value"})
sot._translate_response(response, has_body=False) sot._translate_response(response, has_body=False)
@ -707,7 +708,7 @@ class TestResource(base.TestCase):
response.json.return_value = body response.json.return_value = body
sot = Test() sot = Test()
sot._transpose_component = mock.Mock(side_effect=[body, dict()]) sot._filter_component = mock.Mock(side_effect=[body, dict()])
sot._translate_response(response, has_body=True) sot._translate_response(response, has_body=True)
@ -728,7 +729,7 @@ class TestResource(base.TestCase):
response.json.return_value = {key: body} response.json.return_value = {key: body}
sot = Test() sot = Test()
sot._transpose_component = mock.Mock(side_effect=[body, dict()]) sot._filter_component = mock.Mock(side_effect=[body, dict()])
sot._translate_response(response, has_body=True) sot._translate_response(response, has_body=True)