Fix find() interface when attr is not specified

It seems to use None as the attr key and throws TypeError.
Let's not do the secondary search when attr is not specified.

Change-Id: Ic240d19f48d362ab74a91bace5eab31542823718
Closes-Bug: #1739882
This commit is contained in:
rabi 2017-12-23 19:01:50 +05:30
parent a8e71b0565
commit a9893be624
2 changed files with 39 additions and 13 deletions

View File

@ -387,6 +387,10 @@ class BaseAPI(object):
Headers dictionary to pass to requests
"""
def raise_not_found():
msg = _("%s not found") % value
raise exceptions.NotFound(msg)
try:
ret = self._request(
'GET', "/%s/%s" % (path, value),
@ -400,18 +404,20 @@ class BaseAPI(object):
ksa_exceptions.NotFound,
ksa_exceptions.BadRequest,
):
kwargs = {attr: value}
try:
ret = self.find_one(
path,
headers=headers,
**kwargs
)
except (
exceptions.NotFound,
ksa_exceptions.NotFound,
):
msg = _("%s not found") % value
raise exceptions.NotFound(msg)
if attr:
kwargs = {attr: value}
try:
ret = self.find_one(
path,
headers=headers,
**kwargs
)
except (
exceptions.NotFound,
ksa_exceptions.NotFound,
):
raise_not_found()
else:
raise_not_found()
return ret

View File

@ -269,6 +269,26 @@ class TestBaseAPIFind(api_fakes.TestSession):
endpoint=self.BASE_URL,
)
def test_baseapi_find(self):
self.requests_mock.register_uri(
'GET',
self.BASE_URL + '/qaz/1',
json={'qaz': api_fakes.RESP_ITEM_1},
status_code=200,
)
ret = self.api.find('qaz', '1')
self.assertEqual(api_fakes.RESP_ITEM_1, ret)
self.requests_mock.register_uri(
'GET',
self.BASE_URL + '/qaz/1',
status_code=404,
)
self.assertRaises(
exceptions.NotFound,
self.api.find,
'qaz',
'1')
def test_baseapi_find_attr_by_id(self):
# All first requests (by name) will fail in this test