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 = body[self.resource_key]
absolute_body = self._transpose_component(
absolute_body = self._filter_component(
body["absolute"], AbsoluteLimits._body_mapping())
self.absolute = AbsoluteLimits.existing(**absolute_body)
@ -100,8 +100,8 @@ class Limits(resource2.Resource):
rates = []
for rate_body in rates_body:
rate_body = self._transpose_component(rate_body,
RateLimit._body_mapping())
rate_body = self._filter_component(rate_body,
RateLimit._body_mapping())
rates.append(RateLimit(**rate_body))
self.rate = rates

View File

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

View File

@ -668,19 +668,20 @@ class TestResource(base.TestCase):
self.assertEqual({key: {"x": body_value}}, result.body)
self.assertEqual({"y": header_value}, result.headers)
def test__transpose_component(self):
def test__filter_component(self):
client_name = "client_name"
server_name = "serverName"
value = "value"
# Include something in the mapping that we don't receive
# so the branch that looks at existence in the compoment is checked.
mapping = {client_name: server_name, "other": "blah"}
component = {server_name: value}
component = {server_name: value, "something": "else"}
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):
class Test(resource2.Resource):
@ -690,7 +691,7 @@ class TestResource(base.TestCase):
response.headers = dict()
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)
@ -707,7 +708,7 @@ class TestResource(base.TestCase):
response.json.return_value = body
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)
@ -728,7 +729,7 @@ class TestResource(base.TestCase):
response.json.return_value = {key: body}
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)