Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
api-client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Arkindex
api-client
Commits
e6860005
Commit
e6860005
authored
2 years ago
by
Manon Blanco
Committed by
Erwan Rouchet
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Support error responses in MockApiClient
parent
eac0506b
No related branches found
No related tags found
1 merge request
!212
Support error responses in MockApiClient
Pipeline
#29392
passed
2 years ago
Stage: release
Changes
2
Pipelines
49
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
arkindex/mock.py
+22
-0
22 additions, 0 deletions
arkindex/mock.py
tests/test_mock.py
+17
-1
17 additions, 1 deletion
tests/test_mock.py
with
39 additions
and
1 deletion
arkindex/mock.py
+
22
−
0
View file @
e6860005
...
...
@@ -24,6 +24,26 @@ class MockApiClient(object):
self
.
responses
.
append
((
request
,
response
))
return
(
request
,
response
)
def
add_error_response
(
self
,
operation_id
:
str
,
status_code
:
int
,
title
=
"
Mock error response
"
,
content
=
"
Mock error response
"
,
body
=
None
,
*
args
,
**
kwargs
,
):
"""
Store a new mock error response for a request on an endpoint
"""
request
=
MockRequest
(
operation_id
,
body
,
args
,
kwargs
)
error
=
apistar
.
exceptions
.
ErrorResponse
(
title
=
title
,
status_code
=
status_code
,
content
=
content
,
)
self
.
responses
.
append
((
request
,
error
))
return
(
request
,
error
)
def
next_response
(
self
,
request
:
MockRequest
):
"""
Find the next available response for a request, and remove it from the stack
"""
for
request_cmp
,
response
in
self
.
responses
:
...
...
@@ -46,6 +66,8 @@ class MockApiClient(object):
response
=
next
(
self
.
next_response
(
request
),
None
)
if
response
is
not
None
:
logger
.
info
(
f
"
Sending mock response for
{
operation_id
}
"
)
if
isinstance
(
response
,
Exception
):
raise
response
return
response
# Throw exception when no response is found
...
...
This diff is collapsed.
Click to expand it.
tests/test_mock.py
+
17
−
1
View file @
e6860005
...
...
@@ -54,6 +54,14 @@ def test_mock_client():
"
id
"
:
"
5678-dog
"
,
},
)
mock
.
add_error_response
(
"
DeleteTag
"
,
name
=
"
Henri
"
,
body
=
{
"
tag
"
:
"
yyy
"
},
status_code
=
403
,
title
=
"
Forbidden
"
,
content
=
"
You cannot perform this action
"
,
)
# Call the endpoints - this simulates business code
animals
=
mock
.
paginate
(
"
ListAnimals
"
)
...
...
@@ -76,16 +84,24 @@ def test_mock_client():
"
id
"
:
"
1234-cat
"
,
}
with
pytest
.
raises
(
apistar
.
exceptions
.
ErrorResponse
)
as
e_info
:
mock
.
request
(
"
DeleteTag
"
,
name
=
"
Henri
"
,
body
=
{
"
tag
"
:
"
yyy
"
})
error_response
=
e_info
.
value
assert
error_response
.
status_code
==
403
assert
error_response
.
title
==
"
Forbidden
"
assert
error_response
.
content
==
"
You cannot perform this action
"
# A request not mocked should raise an exception
with
pytest
.
raises
(
apistar
.
exceptions
.
ErrorResponse
):
mock
.
request
(
"
DoSomething
"
,
parameter
=
"
value
"
)
# Check the mockup history
assert
len
(
mock
.
history
)
==
4
assert
len
(
mock
.
history
)
==
5
assert
[
req
.
operation
for
req
in
mock
.
history
]
==
[
"
ListAnimals
"
,
"
CreateTag
"
,
"
CreateTag
"
,
"
DeleteTag
"
,
"
DoSomething
"
,
]
...
...
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