diff --git a/postgrestutils/client/postgrestclient.py b/postgrestutils/client/postgrestclient.py index 0c087535a9a6fb2638a8c8982906dabf076c4052..8291c0e76b7f695c03027fca9bfe6c0f6fcb588d 100644 --- a/postgrestutils/client/postgrestclient.py +++ b/postgrestutils/client/postgrestclient.py @@ -138,11 +138,10 @@ class LazyPostgrestJsonResult: return self._result_cache[key] if isinstance(key, slice): - - range = '{start}-{stop}'.format( - start=key.start or 0, - stop=key.stop and key.stop - 1 or '' - ) + start = key.start if key.start is not None else 0 + if key.stop is not None and key.stop <= start: + return list() + range = '{start}-{stop}'.format(start=start, stop=key.stop - 1 if key.stop is not None else '') else: range = '{0}-{0}'.format(key) return self._fetch_range(range) diff --git a/tests/test_postgrestclient.py b/tests/test_postgrestclient.py index 99e2d48d09f32676e14e0997de6861f42bbb830c..f682e2e1352f3c01156b0795330bb506952cf9b5 100644 --- a/tests/test_postgrestclient.py +++ b/tests/test_postgrestclient.py @@ -199,6 +199,9 @@ class TestPgrestClientFilterStrategyNone(TestCase): self.assertListEqual(res._result_cache, self.data) # fetched data should be cached self.assertEqual(res._len_cache, len(self.data)) # len of fetched data should be cached self.assertListEqual(list(res), self.data) # should utilize cache + self.assertListEqual(res[:1], self.data[:1]) # should utilize cache + self.assertListEqual(res[:0], self.data[:0]) # should return empty list + self.assertListEqual(res[4:2], self.data[4:2]) # should return empty list self.assertListEqual(res[2:], self.data[2:]) # should utilize cache self.assertDictEqual(res[0], self.data[0]) # should utilize cache self.assertTrue(mock.called_once) # should not have been called again @@ -221,6 +224,9 @@ class TestPgrestClientFilterStrategyNone(TestCase): self.assertTrue(mock.called_once) # should have been called once self.assertEqual(res._len_cache, len(self.data)) # len of fetched data should be cached self.assertListEqual(res._result_cache, self.data) # results should be cached (counting strategy none) + self.assertListEqual(res[:1], self.data[:1]) # should utilize cache + self.assertListEqual(res[:0], self.data[:0]) # should return empty list + self.assertListEqual(res[4:2], self.data[4:2]) # should return empty list self.assertListEqual(res[2:], self.data[2:]) # should utilize cache self.assertDictEqual(res[0], self.data[0]) # should utilize cache self.assertListEqual(list(res), self.data) # should utilize cache @@ -245,6 +251,8 @@ class TestPgrestClientFilterStrategyNone(TestCase): self.assertListEqual(res._result_cache, self.data) # fetched data should be cached self.assertEqual(res._len_cache, len(self.data)) # len of fetched data should be cached self.assertListEqual(res[:], self.data) # should utilize cache + self.assertListEqual(res[:0], self.data[:0]) # should return empty list + self.assertListEqual(res[4:2], self.data[4:2]) # should return empty list self.assertListEqual(res[2:], self.data[2:]) # should utilize cache self.assertDictEqual(res[0], self.data[0]) # should utilize cache self.assertTrue(mock.called_once) # should not have been called again @@ -280,6 +288,9 @@ class TestPgrestClientFilterStrategyExact(TestCase): self.assertListEqual(res._result_cache, self.data) # fetched data should be cached self.assertEqual(res._len_cache, len(self.data)) # len of fetched data should also be cached self.assertListEqual(list(res), self.data) # should utilize cache + self.assertListEqual(res[:1], self.data[:1]) # should utilize cache + self.assertListEqual(res[:0], self.data[:0]) # should return empty list + self.assertListEqual(res[4:2], self.data[4:2]) # should return empty list self.assertListEqual(res[2:], self.data[2:]) # should utilize cache self.assertDictEqual(res[0], self.data[0]) # should utilize cache self.assertTrue(mock.called_once) # should not have been called again @@ -302,7 +313,7 @@ class TestPgrestClientFilterStrategyExact(TestCase): status_code=200, reason='OK', headers={'Content-Range': '0-0/*'}, - json=self.data[0] + json=[self.data[0]] ) # in order to fetch range since index 2 mock.register_uri( @@ -332,9 +343,12 @@ class TestPgrestClientFilterStrategyExact(TestCase): self.assertEqual(len(res), len(self.data)) # should fetch len self.assertTrue(mock.called_once) # should have been called once self.assertEqual(res._len_cache, len(self.data)) # len of fetched data should be cached + self.assertListEqual(res[:1], self.data[:1]) # should fetch first element as range + self.assertListEqual(res[:0], self.data[:0]) # should return empty list + self.assertListEqual(res[4:2], self.data[4:2]) # should return empty list self.assertListEqual(res[2:], self.data[2:]) # should fetch range starting at index 2 - self.assertDictEqual(res[0], self.data[0]) # should fetch first element as range + self.assertDictEqual(res[0], self.data[0]) # should fetch first element as range but return dict self.assertListEqual(list(res), self.data) # should fetch all elements self.assertListEqual(res._result_cache, self.data) # should cache all elements self.assertTrue(mock.called) # should have been called at least once - self.assertEqual(mock.call_count, 4) # should have only been called 4 times (fetch len, range, first and all) + self.assertEqual(mock.call_count, 5) # should have only been called 5 times (fetch len, range, first and all)