Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
postgrestutils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tools
postgrestutils
Commits
6f7d77b1
Commit
6f7d77b1
authored
5 years ago
by
Fynn Becker
Browse files
Options
Downloads
Patches
Plain Diff
Close
#10
Add caching when using unbounded slices
parent
92891386
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
postgrestutils/client/postgrestclient.py
+5
-0
5 additions, 0 deletions
postgrestutils/client/postgrestclient.py
tests/test_postgrestclient.py
+23
-0
23 additions, 0 deletions
tests/test_postgrestclient.py
with
28 additions
and
0 deletions
postgrestutils/client/postgrestclient.py
+
5
−
0
View file @
6f7d77b1
...
...
@@ -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
''
...
...
This diff is collapsed.
Click to expand it.
tests/test_postgrestclient.py
+
23
−
0
View file @
6f7d77b1
...
...
@@ -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
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment