From 7e3d510e7134dc84b3872d1e50d6e6675560c8e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Muhl?= Date: Fri, 13 Feb 2015 16:25:56 +0100 Subject: [PATCH 1/3] Added test to show that current implementaion isn't correct This test shows that the extract method in Resource class returns only data if self.objects_path == tuple(). This behavior is wrong, because extract() should always return a list of data. --- tests/test_collection.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_collection.py b/tests/test_collection.py index 13e9a37..0d861a5 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -104,6 +104,14 @@ def test_missing_objects_path(fixture, resource): resource.collection.count() +@pytest.mark.httpretty +def test_without_objects_path_with_schema(fixture, resource): + resource.objects_path = tuple() + httpretty.register_uri(httpretty.GET, resource.get_url(), body=fixture('movie.json'), + content_type='application/json') + resource.collection.count() + + @pytest.mark.httpretty def test_create(resource, return_payload): httpretty.register_uri(httpretty.POST, resource.get_url(), body=return_payload, From 3680046249a2d18651c918a5b4763fcec376e90a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Muhl?= Date: Fri, 13 Feb 2015 16:30:45 +0100 Subject: [PATCH 2/3] Added fix for extract method in Resource class If the objects_path and object_path are only a tuple the data will be returned as list. --- durga/resource.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/durga/resource.py b/durga/resource.py index a30556e..e5767f3 100755 --- a/durga/resource.py +++ b/durga/resource.py @@ -54,6 +54,8 @@ def extract(self, response): """Returns a list of JSON data extracted from the response.""" try: data = response.json() + if self.objects_path == self.object_path == tuple(): + return [data] if len(data): for key in self.objects_path: data = data[key] From 06e4638e86d69fbb937f26a20cd0b522fb756342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Muhl?= Date: Fri, 13 Feb 2015 16:32:59 +0100 Subject: [PATCH 3/3] Corrected test Now test behavior has changed. When objects_path == tuple() durga won't throw an exeption. --- tests/test_collection.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_collection.py b/tests/test_collection.py index 0d861a5..a8919f9 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -100,8 +100,6 @@ def test_missing_objects_path(fixture, resource): resource.schema = None httpretty.register_uri(httpretty.GET, resource.get_url(), body=fixture('movies.json'), content_type='application/json') - with pytest.raises(exceptions.DurgaError): - resource.collection.count() @pytest.mark.httpretty