Skip to content
Snippets Groups Projects
Commit 6f7d77b1 authored by Fynn Becker's avatar Fynn Becker :crab:
Browse files

Close #10 Add caching when using unbounded slices

parent 92891386
No related branches found
No related tags found
No related merge requests found
......@@ -130,10 +130,15 @@ class LazyPostgrestJsonResult:
if isinstance(key, slice) and key.step is not None:
raise ValueError("{self.__class__.__name__} does not support stepping".format(self=self))
# cache is not populated and unbounded slice is requested, i.e. res[:]
if isinstance(key, slice) and all(e is None for e in (self._result_cache, key.start, key.stop)):
self._fetch_all()
if self._result_cache is not None:
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 ''
......
......@@ -226,6 +226,29 @@ class TestPgrestClientFilterStrategyNone(TestCase):
self.assertListEqual(list(res), self.data) # should utilize cache
self.assertTrue(mock.called_once) # should not have been called again
def test_cache_fetching_unbounded_slice(self, mock):
mock.register_uri(
'GET',
'http://example.com/superhero',
request_headers=DEFAULT_HEADERS,
status_code=200,
reason='OK',
json=self.data
)
res = self.pgrest_client.filter('superhero')
self.assertIsInstance(res, LazyPostgrestJsonResult) # should return lazy object
self.assertFalse(mock.called) # no request should have been made yet
self.assertListEqual(res[:], self.data) # fetch data
self.assertTrue(mock.called_once) # should have been called once
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[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
@Mocker()
class TestPgrestClientFilterStrategyExact(TestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment