Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
tests_api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
hive
tests_api
Commits
e7402f41
Commit
e7402f41
authored
4 years ago
by
Krzysztof Leśniak
Browse files
Options
Downloads
Patches
Plain Diff
added pyresttest extension to generate json schema
parent
d03b5efa
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
hived/pyresttest_api_tests/database_api/database_api_test.yaml
+3
-0
3 additions, 0 deletions
.../pyresttest_api_tests/database_api/database_api_test.yaml
hived/pyresttest_api_tests/schema_generator.py
+56
-0
56 additions, 0 deletions
hived/pyresttest_api_tests/schema_generator.py
with
59 additions
and
0 deletions
hived/pyresttest_api_tests/database_api/database_api_test.yaml
+
3
−
0
View file @
e7402f41
...
@@ -16,6 +16,9 @@
...
@@ -16,6 +16,9 @@
-
validator_schema_json
:
&validator_schema_json
-
validator_schema_json
:
&validator_schema_json
-
json_schema
:
{
schema
:
{
file
:
{
template
:
'
$method.json.schema'
}}}
-
json_schema
:
{
schema
:
{
file
:
{
template
:
'
$method.json.schema'
}}}
-
validator_schema_gen
:
&validator_schema_gen
-
json_schema_generate
:
{
schema_file
:
'
$api/$method.json.schema'
,
output_file
:
'
$api/$method.json.out'
}
-
base_test
:
&base_test
-
base_test
:
&base_test
-
generator_binds
:
-
generator_binds
:
-
test_id
:
test_id
-
test_id
:
test_id
...
...
This diff is collapsed.
Click to expand it.
hived/pyresttest_api_tests/schema_generator.py
0 → 100644
+
56
−
0
View file @
e7402f41
import
string
import
json
from
genson
import
SchemaBuilder
from
pyresttest
import
validators
def
json_schema_generator
(
config
):
"""
Creates a dummy validator that generates json schema for api call.
This validator always fails.
:config: Dictionary containing
'
schema_file
'
and
'
output_file
'
keys.
Both are templated file names. Api call result will be saved to
'
output_file
'
, and generated schema in
'
schema_file
'
.
{schema_file:
'
$api/$method.json.schema
'
, output_file:
'
$api/$method.json.out
'
}
"""
output
=
JSONSchemaGenerator
()
output
.
schema_file
=
config
[
'
schema_file
'
]
output
.
output_file
=
config
[
'
output_file
'
]
return
output
def
gen_schema
(
json
):
"""
Returns schema for given json string
:json: json string
:returns: schema for given json
"""
builder
=
SchemaBuilder
()
builder
.
add_object
(
json
)
return
builder
.
to_json
(
indent
=
2
)
def
write
(
file_name
,
data
):
"""
Writes given data to file named filename
:file_name: name of the output file
:data: string to write
"""
with
open
(
file_name
,
"
w
"
)
as
f
:
f
.
write
(
data
)
class
JSONSchemaGenerator
(
validators
.
AbstractValidator
):
"""
Does extract response body and compare with given my_file_name.json.pat.
If comparison failed response is save into my_file_name.json.out file.
"""
def
validate
(
self
,
body
=
None
,
headers
=
None
,
context
=
None
):
extractor
=
validators
.
_get_extractor
({
'
jsonpath_mini
'
:
'
.
'
})
output
=
extractor
.
extract
(
body
=
body
,
headers
=
headers
,
context
=
context
)
schema_file
=
string
.
Template
(
self
.
schema_file
).
safe_substitute
(
context
.
get_values
())
output_file
=
string
.
Template
(
self
.
output_file
).
safe_substitute
(
context
.
get_values
())
write
(
output_file
,
json
.
dumps
(
output
,
indent
=
2
))
schema
=
gen_schema
(
output
)
write
(
schema_file
,
schema
)
return
False
VALIDATORS
=
{
'
json_schema_generate
'
:
json_schema_generator
}
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