Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Setra Client
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
it-bott-integrasjoner
Setra Client
Commits
d8472146
Commit
d8472146
authored
4 years ago
by
rha104
Browse files
Options
Downloads
Patches
Plain Diff
Updated tests
parent
7cc31562
No related branches found
No related tags found
1 merge request
!4
Added lots of tests. And fixed some issues with the client, discovered while testing
Pipeline
#51073
passed
4 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/conftest.py
+1
-1
1 addition, 1 deletion
tests/conftest.py
tests/test_client.py
+107
-47
107 additions, 47 deletions
tests/test_client.py
with
108 additions
and
48 deletions
tests/conftest.py
+
1
−
1
View file @
d8472146
...
...
@@ -21,7 +21,7 @@ def load_json_file(name):
@pytest.fixture
def
baseurl
():
return
'
http://localhost
'
return
'
http
s
://localhost
'
@pytest.fixture
...
...
This diff is collapsed.
Click to expand it.
tests/test_client.py
+
107
−
47
View file @
d8472146
import
pytest
import
requests
import
requests_mock
from
requests
import
HTTPError
from
setra_client.client
import
SetraEndpoints
from
setra_client.client
import
SetraClient
from
json.decoder
import
JSONDecodeError
@pytest.fixture
...
...
@@ -45,58 +44,119 @@ def test_init_modify_defaults(client_cls, baseurl, header_name):
assert
client
.
default_headers
[
header_name
]
!=
headers
[
header_name
]
# def test_get_call(client, requests_mock):
# """Ensure getting update schema works"""
# requests_mock.get('https://localhost/_webservices/?ws=contacts/upsert/1.0',
# json={'foo': 'bar'})
# response = client.call(method_name='GET', url=)
# assert response == {'foo': 'bar'}
def
test_get_batch_call
(
client
,
requests_mock
,
baseurl
):
def
test_get_successful_batch_with_json_content
(
client
,
requests_mock
,
baseurl
):
"""
A working GET call should return HTTP 200, with json content
"""
requests_mock
.
get
(
'
http://localhost/api/batch/3
'
,
json
=
{
'
foo
'
:
'
bar
'
})
response
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
SetraEndpoints
(
baseurl
).
batch
(
batch_id
=
'
3
'
))
url
=
SetraEndpoints
(
baseurl
).
batch
(
batch_id
=
'
3
'
)
# https://localhost/api/batch/3
requests_mock
.
get
(
url
,
json
=
{
'
foo
'
:
'
bar
'
},
status_code
=
200
)
response
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
)
assert
response
.
status_code
==
200
assert
response
.
json
()
==
{
'
foo
'
:
'
bar
'
}
def
test_get_batch_call4
(
client
,
baseurl
):
def
test_get_successful_batch_with_text_content
(
client
,
requests_mock
,
baseurl
):
"""
A working GET call should return HTTP 200, with json content
"""
session
=
requests
.
Session
()
adapter
=
requests_mock
.
Adapter
()
session
.
mount
(
'
http://
'
,
adapter
)
adapter
.
register_uri
(
'
GET
'
,
'
http://test.com/1
'
,
json
=
{
'
a
'
:
'
b
'
},
status_code
=
200
)
resp
=
session
.
get
(
'
http://test.com/1
'
)
assert
resp
.
status_code
==
200
assert
resp
.
json
()
==
{
'
a
'
:
'
b
'
}
url
=
SetraEndpoints
(
baseurl
).
batch
(
batch_id
=
'
3
'
)
# https://localhost/api/batch/3
requests_mock
.
get
(
url
,
text
=
"
some content
"
,
status_code
=
200
)
response
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
)
assert
response
.
status_code
==
200
assert
response
.
text
==
"
some content
"
def
test_get_failing_batch_with_json_content
(
client
,
requests_mock
,
baseurl
):
"""
A failing GET call with 404 should pass through the same response from the client, and return HTTP 404,
and the request with json content
"""
def
test_get_batch_call5
(
client
,
baseurl
):
"""
A failing GET call with 404 should return HTTP 200, and the request with json content
"""
session
=
requests
.
Session
()
adapter
=
requests_mock
.
Adapter
()
session
.
mount
(
'
http://
'
,
adapter
)
adapter
.
register_uri
(
'
GET
'
,
'
http://localhost/api/batch
'
,
json
=
{
'
a
'
:
'
b
'
},
status_code
=
404
)
resp
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
'
http://localhost/api/batch
'
)
assert
resp
.
status_code
==
200
assert
resp
.
text
==
"
test
"
# assert resp.json() == {'a': 'b'}
def
test_get_batch_call6
(
client
,
baseurl
):
"""
A failing GET call with 401 should return custom HTTP status
"""
session
=
requests
.
Session
()
adapter
=
requests_mock
.
Adapter
()
session
.
mount
(
'
http://
'
,
adapter
)
adapter
.
register_uri
(
'
GET
'
,
'
http://test.com/1
'
,
text
=
""
,
status_code
=
401
)
resp
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
'
http://test.com/1
'
)
assert
resp
.
status_code
==
200
print
(
"
TExt: ------------
"
)
print
(
resp
.
text
)
print
(
"
JSON: ------------
"
)
print
(
resp
.
json
())
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
get
(
url
,
json
=
{
'
a
'
:
'
b
'
},
status_code
=
404
)
resp
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
)
assert
resp
.
status_code
==
404
assert
resp
.
json
()
==
{
'
a
'
:
'
b
'
}
def
test_get_failing_batch_with_text_content
(
client
,
requests_mock
,
baseurl
):
"""
A failing GET call with 404 should pass through the same response from the client, and return HTTP 404,
and the request with json content
"""
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
get
(
url
,
text
=
"
some content
"
,
status_code
=
404
)
resp
=
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
)
assert
resp
.
status_code
==
404
assert
resp
.
text
==
"
some content
"
def
test_get_failing_batch_without_return_response_text
(
client
,
requests_mock
,
baseurl
):
"""
A failing GET call with 404 should raise HTTPError from the client,
and return HTTP 404, and the request with text content
"""
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
get
(
url
,
text
=
"
some content
"
,
status_code
=
404
)
with
pytest
.
raises
(
HTTPError
)
as
err
:
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
,
return_response
=
False
)
assert
err
.
type
==
requests
.
exceptions
.
HTTPError
assert
err
.
value
.
response
.
status_code
==
404
assert
err
.
value
.
response
.
text
==
"
some content
"
def
test_get_failing_batch_without_return_response_json
(
client
,
requests_mock
,
baseurl
):
"""
A failing GET call with 404 should raise HTTPError from the client,
and return HTTP 404, and the request with json content
"""
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
get
(
url
,
json
=
{
'
a
'
:
'
b
'
},
status_code
=
404
)
with
pytest
.
raises
(
HTTPError
)
as
err
:
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
,
return_response
=
False
)
assert
err
.
type
==
requests
.
exceptions
.
HTTPError
assert
err
.
value
.
response
.
status_code
==
404
assert
err
.
value
.
response
.
json
()
==
{
'
a
'
:
'
b
'
}
def
test_get_failing_batch_without_return_response2
(
client
,
requests_mock
,
baseurl
):
"""
A failing GET call with 404 returning text content, with return_response=False
should raise for HTTPError from the client
(because we expect json from setra)
"""
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
get
(
url
,
text
=
"
some content
"
,
status_code
=
200
)
with
pytest
.
raises
(
JSONDecodeError
)
as
err
:
client
.
call
(
method_name
=
'
GET
'
,
url
=
url
,
return_response
=
False
)
assert
err
.
type
==
JSONDecodeError
def
test_get_failing_batch_with_http500
(
client
,
requests_mock
,
baseurl
):
"""
A failing POST call with return_response=False and http 500,
should raise HTTPError from the client,
and return HTTP 500, and the request with json content
"""
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
post
(
url
,
json
=
{
'
a
'
:
'
b
'
},
status_code
=
500
)
with
pytest
.
raises
(
HTTPError
)
as
err
:
tt
=
client
.
post
(
url
=
url
,
return_response
=
False
)
assert
err
.
type
==
requests
.
exceptions
.
HTTPError
assert
err
.
value
.
response
.
status_code
==
500
assert
err
.
value
.
response
.
json
()
==
{
'
a
'
:
'
b
'
}
def
test_get_failing_batch_with_http5002
(
client
,
requests_mock
,
baseurl
):
"""
A failing POST call with 500 with return_response=True,
should just return the response, with 500
"""
url
=
SetraEndpoints
(
baseurl
).
batch
()
# https://localhost/api/batch
requests_mock
.
post
(
url
,
json
=
{
'
a
'
:
'
b
'
},
status_code
=
500
)
response
=
client
.
post
(
url
=
url
)
assert
response
.
status_code
==
500
assert
response
.
json
()
==
{
'
a
'
:
'
b
'
}
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