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
287c51ca
Commit
287c51ca
authored
4 years ago
by
Fynn Becker
Browse files
Options
Downloads
Patches
Plain Diff
Group similar tests
parent
802e3892
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_postgrestclient.py
+59
-232
59 additions, 232 deletions
tests/test_postgrestclient.py
with
59 additions
and
232 deletions
tests/test_postgrestclient.py
+
59
−
232
View file @
287c51ca
...
...
@@ -258,104 +258,15 @@ class TestPgrestClientFilterStrategyNone(TestCase):
@Mocker
()
class
TestPgrestClientFilterStrateg
yExact
(
TestCase
):
class
TestPgrestClientFilter
Counting
Strateg
ies
(
TestCase
):
def
setUp
(
self
):
super
().
setUp
()
self
.
data
=
SUPERHERO_TEST_DATA
def
test_fetch_all_first
(
self
,
mock
):
# in order to fetch all
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
DEFAULT_HEADERS
,
status_code
=
200
,
reason
=
'
OK
'
,
json
=
self
.
data
self
.
counting_strategies
=
(
postgrestutils
.
Count
.
EXACT
,
postgrestutils
.
Count
.
PLANNED
,
postgrestutils
.
Count
.
ESTIMATED
)
with
default_session
(
count
=
postgrestutils
.
Count
.
EXACT
)
as
s
:
res
=
s
.
filter
(
'
superhero
'
)
self
.
assertIsInstance
(
res
,
postgrestutils
.
JsonResultSet
)
# should return lazy object
self
.
assertFalse
(
mock
.
called
)
# no request should have been made yet
self
.
assertEqual
(
list
(
res
),
self
.
data
)
# fetch data
self
.
assertTrue
(
mock
.
called_once
)
# should have been called once
self
.
assertEqual
(
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
.
assertEqual
(
list
(
res
),
self
.
data
)
# should utilize cache
self
.
assertEqual
(
res
[:
1
],
self
.
data
[:
1
])
# should utilize cache
self
.
assertEqual
(
res
[:
0
],
self
.
data
[:
0
])
# should return empty list
self
.
assertEqual
(
res
[
4
:
2
],
self
.
data
[
4
:
2
])
# should return empty list
self
.
assertEqual
(
res
[
2
:],
self
.
data
[
2
:])
# should utilize cache
self
.
assertEqual
(
res
[
0
],
self
.
data
[
0
])
# should utilize cache
self
.
assertTrue
(
mock
.
called_once
)
# should not have been called again
def
test_fetch_len_first
(
self
,
mock
):
# in order to fetch all
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
DEFAULT_HEADERS
,
status_code
=
200
,
reason
=
'
OK
'
,
json
=
self
.
data
)
# in order to fetch first
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
0-0
'
}},
status_code
=
200
,
reason
=
'
OK
'
,
headers
=
{
'
Content-Range
'
:
'
0-0/*
'
},
json
=
[
self
.
data
[
0
]]
)
# in order to fetch range since index 2
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
2-
'
}},
status_code
=
200
,
reason
=
'
OK
'
,
headers
=
{
'
Content-Range
'
:
'
2-4/*
'
},
json
=
self
.
data
[
2
:]
)
# in order to fetch length
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
0-0
'
,
'
Prefer
'
:
'
count=exact
'
}},
status_code
=
206
,
reason
=
'
Partial Content
'
,
headers
=
{
'
Content-Range
'
:
'
0-0/5
'
},
json
=
self
.
data
[
0
]
)
with
default_session
(
count
=
postgrestutils
.
Count
.
EXACT
)
as
s
:
res
=
s
.
filter
(
'
superhero
'
)
self
.
assertIsInstance
(
res
,
postgrestutils
.
JsonResultSet
)
# should return lazy object
self
.
assertFalse
(
mock
.
called
)
# no request should have been made yet
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
.
assertEqual
(
res
[:
1
],
self
.
data
[:
1
])
# should fetch first element as range
self
.
assertEqual
(
res
[:
0
],
self
.
data
[:
0
])
# should return empty list
self
.
assertEqual
(
res
[
4
:
2
],
self
.
data
[
4
:
2
])
# should return empty list
self
.
assertEqual
(
res
[
2
:],
self
.
data
[
2
:])
# should fetch range starting at index 2
self
.
assertEqual
(
res
[
0
],
self
.
data
[
0
])
# should fetch first element as range but return dict
self
.
assertEqual
(
list
(
res
),
self
.
data
)
# should fetch all elements
self
.
assertEqual
(
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
,
5
)
# should have been called 5 times (fetch len, range, first and all)
@Mocker
()
class
TestPgrestClientFilterStrategyPlanned
(
TestCase
):
def
setUp
(
self
):
super
().
setUp
()
self
.
data
=
SUPERHERO_TEST_DATA
def
test_fetch_all_first
(
self
,
mock
):
# in order to fetch all
...
...
@@ -367,7 +278,9 @@ class TestPgrestClientFilterStrategyPlanned(TestCase):
reason
=
'
OK
'
,
json
=
self
.
data
)
with
default_session
(
count
=
postgrestutils
.
Count
.
PLANNED
)
as
s
:
for
strategy
in
self
.
counting_strategies
:
mock
.
reset
()
with
default_session
(
count
=
strategy
)
as
s
:
res
=
s
.
filter
(
'
superhero
'
)
self
.
assertIsInstance
(
res
,
postgrestutils
.
JsonResultSet
)
# should return lazy object
...
...
@@ -376,101 +289,8 @@ class TestPgrestClientFilterStrategyPlanned(TestCase):
self
.
assertEqual
(
list
(
res
),
self
.
data
)
# fetch data
self
.
assertTrue
(
mock
.
called_once
)
# should have been called once
self
.
assertEqual
(
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
.
assertEqual
(
list
(
res
),
self
.
data
)
# should utilize cache
self
.
assertEqual
(
res
[:
1
],
self
.
data
[:
1
])
# should utilize cache
self
.
assertEqual
(
res
[:
0
],
self
.
data
[:
0
])
# should return empty list
self
.
assertEqual
(
res
[
4
:
2
],
self
.
data
[
4
:
2
])
# should return empty list
self
.
assertEqual
(
res
[
2
:],
self
.
data
[
2
:])
# should utilize cache
self
.
assertEqual
(
res
[
0
],
self
.
data
[
0
])
# should utilize cache
self
.
assertTrue
(
mock
.
called_once
)
# should not have been called again
def
test_fetch_len_first
(
self
,
mock
):
# in order to fetch all
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
DEFAULT_HEADERS
,
status_code
=
200
,
reason
=
'
OK
'
,
json
=
self
.
data
)
# in order to fetch first
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
0-0
'
}},
status_code
=
200
,
reason
=
'
OK
'
,
headers
=
{
'
Content-Range
'
:
'
0-0/*
'
},
json
=
[
self
.
data
[
0
]]
)
# in order to fetch range since index 2
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
2-
'
}},
status_code
=
200
,
reason
=
'
OK
'
,
headers
=
{
'
Content-Range
'
:
'
2-4/*
'
},
json
=
self
.
data
[
2
:]
)
# in order to fetch length
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
0-0
'
,
'
Prefer
'
:
'
count=planned
'
}},
status_code
=
206
,
reason
=
'
Partial Content
'
,
headers
=
{
'
Content-Range
'
:
'
0-0/5
'
},
json
=
self
.
data
[
0
]
)
with
default_session
(
count
=
postgrestutils
.
Count
.
PLANNED
)
as
s
:
res
=
s
.
filter
(
'
superhero
'
)
self
.
assertIsInstance
(
res
,
postgrestutils
.
JsonResultSet
)
# should return lazy object
self
.
assertFalse
(
mock
.
called
)
# no request should have been made yet
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
.
assertEqual
(
res
[:
1
],
self
.
data
[:
1
])
# should fetch first element as range
self
.
assertEqual
(
res
[:
0
],
self
.
data
[:
0
])
# should return empty list
self
.
assertEqual
(
res
[
4
:
2
],
self
.
data
[
4
:
2
])
# should return empty list
self
.
assertEqual
(
res
[
2
:],
self
.
data
[
2
:])
# should fetch range starting at index 2
self
.
assertEqual
(
res
[
0
],
self
.
data
[
0
])
# should fetch first element as range but return dict
self
.
assertEqual
(
list
(
res
),
self
.
data
)
# should fetch all elements
self
.
assertEqual
(
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
,
5
)
# should have been called 5 times (fetch len, range, first and all)
@Mocker
()
class
TestPgrestClientFilterStrategyEstimated
(
TestCase
):
def
setUp
(
self
):
super
().
setUp
()
self
.
data
=
SUPERHERO_TEST_DATA
def
test_fetch_all_first
(
self
,
mock
):
# in order to fetch all
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
DEFAULT_HEADERS
,
status_code
=
200
,
reason
=
'
OK
'
,
json
=
self
.
data
)
with
default_session
(
count
=
postgrestutils
.
Count
.
ESTIMATED
)
as
s
:
res
=
s
.
filter
(
'
superhero
'
)
self
.
assertIsInstance
(
res
,
postgrestutils
.
JsonResultSet
)
# should return lazy object
self
.
assertFalse
(
mock
.
called
)
# no request should have been made yet
self
.
assertEqual
(
list
(
res
),
self
.
data
)
# fetch data
self
.
assertTrue
(
mock
.
called_once
)
# should have been called once
self
.
assertEqual
(
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
.
assertEqual
(
list
(
res
),
self
.
data
)
# should utilize cache
self
.
assertEqual
(
res
[:
1
],
self
.
data
[:
1
])
# should utilize cache
self
.
assertEqual
(
res
[:
0
],
self
.
data
[:
0
])
# should return empty list
...
...
@@ -509,20 +329,23 @@ class TestPgrestClientFilterStrategyEstimated(TestCase):
headers
=
{
'
Content-Range
'
:
'
2-4/*
'
},
json
=
self
.
data
[
2
:]
)
# in order to fetch length
# in order to fetch length using different strategies
for
strategy
in
self
.
counting_strategies
:
mock
.
register_uri
(
'
GET
'
,
'
http://example.com/superhero
'
,
request_headers
=
{
**
DEFAULT_HEADERS
,
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
0-0
'
,
'
Prefer
'
:
'
count=
estimated
'
}
**
{
'
Range-Unit
'
:
'
items
'
,
'
Range
'
:
'
0-0
'
,
'
Prefer
'
:
'
count=
{}
'
.
format
(
strategy
.
value
)
}
},
status_code
=
206
,
reason
=
'
Partial Content
'
,
headers
=
{
'
Content-Range
'
:
'
0-0/5
'
},
json
=
self
.
data
[
0
]
)
with
default_session
(
count
=
postgrestutils
.
Count
.
ESTIMATED
)
as
s
:
mock
.
reset
()
with
default_session
(
count
=
strategy
)
as
s
:
res
=
s
.
filter
(
'
superhero
'
)
self
.
assertIsInstance
(
res
,
postgrestutils
.
JsonResultSet
)
# should return lazy object
...
...
@@ -531,15 +354,19 @@ class TestPgrestClientFilterStrategyEstimated(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
.
assertEqual
(
res
[:
1
],
self
.
data
[:
1
])
# should fetch first element as range
self
.
assertEqual
(
res
[:
0
],
self
.
data
[:
0
])
# should return empty list
self
.
assertEqual
(
res
[
4
:
2
],
self
.
data
[
4
:
2
])
# should return empty list
self
.
assertEqual
(
res
[
2
:],
self
.
data
[
2
:])
# should fetch range starting at index 2
self
.
assertEqual
(
res
[
0
],
self
.
data
[
0
])
# should fetch first element as range but return dict
self
.
assertEqual
(
list
(
res
),
self
.
data
)
# should fetch all elements
self
.
assertEqual
(
res
.
_result_cache
,
self
.
data
)
# should fetch all elements
self
.
assertEqual
(
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
,
5
)
# should have been called 5 times (fetch len, range, first and all)
# should have been called 5 times (fetch len, fetch 2 ranges,
# fetch first and fetch all)
self
.
assertEqual
(
mock
.
call_count
,
5
)
@Mocker
()
...
...
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