Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ObjectNotFoundError exception on 404 #55

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions durga/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get(self, **kwargs):
if count > 1:
raise exceptions.MultipleObjectsReturnedError
elif count == 0:
raise exceptions.ObjectNotFoundError
raise exceptions.ObjectNotFoundError(self.request, self.response)
element = self.elements[0]
self._reset_request()
return element
Expand Down Expand Up @@ -133,12 +133,16 @@ def _reset_request(self):
def elements(self):
if not self._elements:
self.response = self.resource.dispatch(self.request)
self.data = self.resource.extract(self.response)
self.validated_data = self.resource.validate(self.data)
if self.as_dict or self.as_list:
self._elements = [self.get_values(data) for data in self.validated_data]
if self.response.status_code == 404:
self._elements = []
else:
self._elements = [self.get_element(data) for data in self.validated_data]
self.data = self.resource.extract(self.response)
self.validated_data = self.resource.validate(self.data)
if self.as_dict or self.as_list:
element_func = self.get_values
else:
element_func = self.get_element
self._elements = [element_func(data) for data in self.validated_data]
return self._elements

def get_element(self, data):
Expand Down
4 changes: 3 additions & 1 deletion durga/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class DurgaError(Exception):

class ObjectNotFoundError(DurgaError):
"""The requested object does not exist."""
pass
def __init__(self, request, response):
self.request = request
self.response = response


class MultipleObjectsReturnedError(DurgaError):
Expand Down
19 changes: 14 additions & 5 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@


@pytest.mark.httpretty
@pytest.mark.parametrize('content', ['{}', '[]'])
def test_get_object_not_found(content, resource):
httpretty.register_uri(httpretty.GET, resource.url, body=content,
content_type='application/json')
with pytest.raises(exceptions.ObjectNotFoundError):
@pytest.mark.parametrize('content, content_type, status', [
('{}', 'application/json', 200),
('[]', 'application/json', 200),
('{"status_code": 404, "message": "Page not found"}', 'application/json', 404),
("Page not found", 'text/plain', 404),
])
def test_get_object_not_found(content, content_type, status, resource):
httpretty.register_uri(httpretty.GET, resource.url, body=content, content_type=content_type,
status=status)
with pytest.raises(exceptions.ObjectNotFoundError) as excinfo:
resource.collection.get(year=1900)
assert excinfo.value.request.url == resource.url
assert excinfo.value.response.text == content
if content_type.endswith('json') and status == 200:
assert excinfo.value.response.json() == resource.collection.data


@pytest.mark.httpretty
Expand Down