Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
elixir
metadata-tools
Commits
a795cb20
Commit
a795cb20
authored
Jul 21, 2020
by
Vilem Ded
Browse files
fix - testing JSON schemas
parent
29d485d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
tests/schemas/test_elx_schemas.py
View file @
a795cb20
from
unittest
import
TestCase
from
json
import
loads
from
jsonschema
import
validate
from
jsonschema.exceptions
import
SchemaError
import
os
# Fixtures
dirname
=
os
.
path
.
dirname
(
__file__
)
testdata_dir
=
os
.
path
.
abspath
(
os
.
path
.
join
(
dirname
,
"data/"
))
schema_dir
=
os
.
path
.
abspath
(
os
.
path
.
join
(
dirname
,
"../../metadata_tools/resources/"
))
schema_testdata_filename_map
=
[
(
'elu-dataset.json'
,
'datasets.json'
),
(
'elu-project.json'
,
'projects.json'
),
(
'elu-institution.json'
,
'partners.json'
)
]
class
TestParser
(
TestCase
):
class
TestJSONSchemas
(
TestCase
):
def
test_validation
(
self
):
print
(
os
.
getcwd
())
schemaFile
=
open
(
'../../metadata_tools/resources/elu-dataset.json'
,
encoding
=
'utf-8'
)
dataFile
=
open
(
'data/datasets.json'
,
encoding
=
'utf-8'
)
schema
=
loads
(
schemaFile
.
read
())
data
=
loads
(
dataFile
.
read
())
try
:
validate
(
data
,
schema
[
'schema'
])
self
.
assert_
(
True
)
except
:
self
.
fail
()
finally
:
dataFile
.
close
()
schemaFile
.
close
()
for
schema_filename
,
dataset_filename
in
schema_testdata_filename_map
:
schema_filepath
=
os
.
path
.
abspath
(
os
.
path
.
join
(
schema_dir
,
schema_filename
))
schemaFile
=
open
(
os
.
path
.
join
(
schema_dir
,
schema_filename
),
encoding
=
'utf-8'
)
data_filepath
=
os
.
path
.
abspath
(
os
.
path
.
join
(
testdata_dir
,
dataset_filename
))
dataFile
=
open
(
data_filepath
,
encoding
=
'utf-8'
)
schema
=
loads
(
schemaFile
.
read
())
data
=
loads
(
dataFile
.
read
())[
'items'
]
for
item
in
data
:
try
:
validate
(
item
,
schema
)
self
.
assert_
(
True
)
except
SchemaError
:
self
.
fail
(
f
"JSONSchema
{
schema_filepath
}
is not valid - SchemaError."
)
except
:
self
.
fail
(
f
"JSONSchema
{
schema_filepath
}
is not valid - failed on test data
{
data_filepath
}
"
)
finally
:
dataFile
.
close
()
schemaFile
.
close
()
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment