Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
engine
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
AURA
engine
Commits
8029288f
Commit
8029288f
authored
1 year ago
by
David Trattnig
Browse files
Options
Downloads
Patches
Plain Diff
test: improve test cases for API
parent
09e8f9a4
No related branches found
No related tags found
1 merge request
!20
Preparations for ORM-less scheduling
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_simple_api.py
+64
-23
64 additions, 23 deletions
tests/test_simple_api.py
with
64 additions
and
23 deletions
tests/test_simple_api.py
+
64
−
23
View file @
8029288f
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import
json
import
unittest
import
unittest
from
unittest
import
mock
from
unittest
import
mock
...
@@ -49,6 +50,8 @@ class TestApi(unittest.TestCase):
...
@@ -49,6 +50,8 @@ class TestApi(unittest.TestCase):
return
self
.
json_data
return
self
.
json_data
print
(
f
"
Calling mocked
'
requests.get
'
with
'
{
args
[
0
]
}
'"
)
print
(
f
"
Calling mocked
'
requests.get
'
with
'
{
args
[
0
]
}
'"
)
if
args
[
0
]
==
"
http://aura.test.available
"
:
return
MockResponse
({
"
foo
"
:
"
bar
"
},
200
)
if
args
[
0
]
==
"
http://aura.test.available/bad-request
"
:
if
args
[
0
]
==
"
http://aura.test.available/bad-request
"
:
return
MockResponse
({},
400
)
return
MockResponse
({},
400
)
if
args
[
0
]
==
"
http://aura.test.available/connection-error
"
:
if
args
[
0
]
==
"
http://aura.test.available/connection-error
"
:
...
@@ -57,6 +60,28 @@ class TestApi(unittest.TestCase):
...
@@ -57,6 +60,28 @@ class TestApi(unittest.TestCase):
raise
requests
.
exceptions
.
Timeout
raise
requests
.
exceptions
.
Timeout
if
args
[
0
]
==
"
http://aura.test.available/exception
"
:
if
args
[
0
]
==
"
http://aura.test.available/exception
"
:
raise
Exception
raise
Exception
if
args
[
0
]
==
"
http://aura.test.available/not-found
"
:
return
MockResponse
({},
404
)
if
args
[
0
]
==
"
https://some.website.no.api
"
:
return
MockResponse
({},
405
)
return
MockResponse
(
None
,
404
)
# Mock `requests.put` in `SimpleRestApi`
def
mocked_requests_put
(
*
args
,
**
kwargs
):
class
MockResponse
:
def
__init__
(
self
,
json_data
,
status_code
):
self
.
json_data
=
json_data
self
.
status_code
=
status_code
def
json
(
self
):
return
self
.
json_data
print
(
f
"
Calling mocked
'
requests.put
'
with
'
{
args
[
0
]
}
'"
)
if
args
[
0
]
==
"
http://aura.test.available/bad-request
"
:
return
MockResponse
({},
400
)
if
args
[
0
]
==
"
http://aura.test.available/not-found
"
:
return
MockResponse
({},
404
)
return
MockResponse
(
None
,
404
)
return
MockResponse
(
None
,
404
)
...
@@ -108,12 +133,14 @@ class TestApi(unittest.TestCase):
...
@@ -108,12 +133,14 @@ class TestApi(unittest.TestCase):
except
ValueError
:
except
ValueError
:
self
.
assertTrue
(
"
Value not found in dict
"
)
self
.
assertTrue
(
"
Value not found in dict
"
)
def
test_get
(
self
):
@mock.patch
(
"
aura_engine.base.api.requests.get
"
,
side_effect
=
mocked_requests_get
)
def
test_get
(
self
,
mock_get
):
print
(
self
.
_testMethodName
)
print
(
self
.
_testMethodName
)
result
=
self
.
api
.
get
(
"
http
s
://
o94.at/
"
)
result
=
self
.
api
.
get
(
"
http://
aura.test.available
"
)
# Success
# Success
self
.
assertEqual
(
200
,
result
.
response
.
status_code
)
self
.
assertEqual
(
200
,
result
.
response
.
status_code
)
self
.
assertEqual
(
"
bar
"
,
result
.
json
[
"
foo
"
])
@mock.patch
(
"
aura_engine.base.api.requests.get
"
,
side_effect
=
mocked_requests_get
)
@mock.patch
(
"
aura_engine.base.api.requests.get
"
,
side_effect
=
mocked_requests_get
)
def
test_get_bad_request
(
self
,
mock_get
):
def
test_get_bad_request
(
self
,
mock_get
):
...
@@ -131,43 +158,57 @@ class TestApi(unittest.TestCase):
...
@@ -131,43 +158,57 @@ class TestApi(unittest.TestCase):
# Bad Request
# Bad Request
self
.
assertEqual
(
400
,
result
.
response
.
status_code
)
self
.
assertEqual
(
400
,
result
.
response
.
status_code
)
@mock.patch
(
"
aura_engine.base.api.requests.get
"
,
side_effect
=
mocked_requests_get
)
@mock.patch
(
"
aura_engine.base.api.requests.post
"
)
def
test_get_exception
(
self
,
mock_get
):
def
test_post_success
(
self
,
mock_post
):
mock_post
.
return_value
.
status_code
=
201
mock_post
.
return_value
.
json
.
return_value
=
"
mock response
"
print
(
self
.
_testMethodName
)
print
(
self
.
_testMethodName
)
result
=
self
.
api
.
get
(
"
http://aura.test.available/exception
"
)
data
=
{
"
foo
"
:
"
bar
"
}
# Bad Request
result
=
self
.
api
.
post
(
"
http://aura.test.available/api
"
,
data
=
data
)
self
.
assertEqual
(
400
,
result
.
response
.
status_code
)
@mock.patch
(
"
aura_engine.base.api.requests.get
"
,
side_effect
=
mocked_requests_get
)
mock_post
.
assert_called_once_with
(
def
test_get_timeout
(
self
,
mock_get
):
"
http://aura.test.available/api
"
,
print
(
self
.
_testMethodName
)
data
=
json
.
dumps
({
"
foo
"
:
"
bar
"
},
indent
=
4
,
sort_keys
=
True
,
default
=
str
),
result
=
self
.
api
.
get
(
"
http://aura.test.available/timeout
"
)
headers
=
{
"
content-type
"
:
"
application/json
"
},
)
# Bad Request
# print(result)
self
.
assertEqual
(
400
,
result
.
response
.
status_code
)
self
.
assertEqual
(
201
,
result
.
response
.
status_code
)
@mock.patch
(
"
aura_engine.base.api.requests.put
"
)
def
test_put_success
(
self
,
mock_post
):
mock_post
.
return_value
.
status_code
=
200
mock_post
.
return_value
.
json
.
return_value
=
"
mock response
"
def
test_post
(
self
):
print
(
self
.
_testMethodName
)
print
(
self
.
_testMethodName
)
data
=
{
"
foo
"
:
"
bar
"
}
data
=
{
"
foo
"
:
"
bar
"
}
# Not allowed
result
=
self
.
api
.
put
(
"
http://aura.test.available/api
"
,
data
=
data
)
result
=
self
.
api
.
post
(
"
https://o94.at/
"
,
data
=
data
)
print
(
result
)
mock_post
.
assert_called_once_with
(
self
.
assertEqual
(
405
,
result
.
response
.
status_code
)
"
http://aura.test.available/api
"
,
data
=
json
.
dumps
({
"
foo
"
:
"
bar
"
},
indent
=
4
,
sort_keys
=
True
,
default
=
str
),
headers
=
{
"
content-type
"
:
"
application/json
"
},
)
# print(result)
self
.
assertEqual
(
200
,
result
.
response
.
status_code
)
def
test_put
(
self
):
@mock.patch
(
"
aura_engine.base.api.requests.put
"
,
side_effect
=
mocked_requests_put
)
def
test_put
(
self
,
mock_put
):
print
(
self
.
_testMethodName
)
print
(
self
.
_testMethodName
)
data
=
{
"
foo
"
:
"
bar
"
}
data
=
{
"
foo
"
:
"
bar
"
}
# Bad request: Invalid URL
# Bad request: Invalid URL
result
=
self
.
api
.
put
(
"
http:/
0.0.0.0/invalid-service
"
,
data
=
data
)
result
=
self
.
api
.
put
(
"
http:/
/aura.test.available/bad-request
"
,
data
=
data
)
print
(
result
)
#
print(result)
self
.
assertEqual
(
400
,
result
.
response
.
status_code
)
self
.
assertEqual
(
400
,
result
.
response
.
status_code
)
# Not found
# Not found
result
=
self
.
api
.
put
(
"
http
s
://
o94.at/invalid-service
"
,
data
=
data
)
result
=
self
.
api
.
put
(
"
http://
aura.test.available/not-found
"
,
data
=
data
)
print
(
result
)
#
print(result)
self
.
assertEqual
(
404
,
result
.
response
.
status_code
)
self
.
assertEqual
(
404
,
result
.
response
.
status_code
)
...
...
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