Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
tests: json files validated against schema
Browse files Browse the repository at this point in the history
* Validates the JSON files used for configuration (`claimstore initdb`)
  and populating (`claimstore populatedb`). (closes #64)

Signed-off-by: Jose Benito Gonzalez Lopez <jose.benito.gonzalez@cern.ch>
  • Loading branch information
jbenito3 committed Sep 14, 2015
1 parent 5325fb3 commit 304f5a9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
known_third_party=webtest
46 changes: 21 additions & 25 deletions claimstore/core/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,25 @@ def validate_json(json_input, schema):
:param schema: JSON schema to use in the validation. It must be a string
with the format module.schema_name (e.g. claims.claimants).
:type schema: str.
:returns: True if `json_input` follows the schema. False otherwise.
:rtype: bool.
:raises: :exc:`ValidationError` if the instance is invalid.
"""
if schema:
schema_content = get_json_schema(schema)
module_name, schema_name = schema.split('.')
resolver = jsonschema.RefResolver('{}/'.format(
pathlib.Path(
os.path.join(
current_app.config['BASE_DIR'],
'claimstore',
'modules',
module_name,
'static',
'json',
'schemas'
)
).as_uri()),
schema_content
)
jsonschema.Draft4Validator(
json.loads(schema_content),
resolver=resolver
).validate(json_input)
return True
return False
schema_content = get_json_schema(schema)
module_name, schema_name = schema.split('.')
resolver = jsonschema.RefResolver('{}/'.format(
pathlib.Path(
os.path.join(
current_app.config['BASE_DIR'],
'claimstore',
'modules',
module_name,
'static',
'json',
'schemas'
)
).as_uri()),
schema_content
)
jsonschema.Draft4Validator(
json.loads(schema_content),
resolver=resolver
).validate(json_input)
4 changes: 2 additions & 2 deletions claimstore/modules/claims/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def initdb(config):
load_all_predicates(config)
load_all_pids(config)
load_all_claimants(config)
click.echo('Database successfully initialised.')
click.echo('Database initialisation completed.')


@click.command()
Expand Down Expand Up @@ -95,7 +95,7 @@ def populatedb(data):
)
try:
load_all_claims(config_path=data)
click.echo('Database successfully populated.')
click.echo('Database populate completed.')
except Exception:
click.echo(
'Claims could not be loaded. Try `claimstore initdb` first.'
Expand Down
37 changes: 21 additions & 16 deletions claimstore/modules/claims/fixtures/claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,39 @@ def load_all_claims(test_app=None, config_path=None):
)
for claim_fp in glob.glob("{}/*.json".format(claims_filepath)):
with open(claim_fp) as f:
test_app.post_json(
resp = test_app.post_json(
'/claims',
json.loads(f.read()))
json.load(f),
expect_errors=True
)
if resp.status_code != 200:
print('{} could not be loaded. {}.'.format(
claim_fp,
resp.json['message']
))


@pytest.fixture
def dummy_claim():
"""Fixture that creates a dummy claim."""
return json.loads("""
{
"claimant": "dummy_claimant",
"subject": {
return {
"claimant": "dummy_claimant",
"subject": {
"type": "CDS_RECORD_ID",
"value": "2001192"
},
"predicate": "is_same_as",
"certainty": 1.0,
"object": {
},
"predicate": "is_same_as",
"certainty": 1.0,
"object": {
"type": "CDS_REPORT_NUMBER",
"value": "CMS-PAS-HIG-14-008"
},
"arguments": {
},
"arguments": {
"human": 0,
"actor": "CDS_submission"
},
"created": "2015-03-25T11:00:00Z"
}
""")
},
"created": "2015-03-25T11:00:00Z"
}


def _remove_all_claims():
Expand Down
35 changes: 22 additions & 13 deletions claimstore/modules/claims/fixtures/claimant.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import pytest
from flask import current_app
from jsonschema import ValidationError

from claimstore.core.json import validate_json
from claimstore.ext.sqlalchemy import db
from claimstore.modules.claims.models import Claimant

Expand Down Expand Up @@ -56,34 +58,41 @@ def load_all_claimants(config_path=None):
)
for claimant_fp in glob.glob("{}/*.json".format(claimants_filepath)):
with open(claimant_fp) as f:
create_claimant(json.loads(f.read()))
json_data = json.load(f)
try:
validate_json(json_data, 'claims.claimant')
except ValidationError:
print(
'`{}` could not be loaded. It does not follow the proper '
'JSON schema specification.'.format(claimant_fp)
)
continue
create_claimant(json_data)


@pytest.fixture
def dummy_claimant():
"""Fixture that creates a dummy claimant."""
return json.loads("""
{
"name": "dummy_claimant",
"url": "http://dummy.net",
"persistent_identifiers": [
{
return {
"name": "dummy_claimant",
"url": "http://dummy.net",
"persistent_identifiers": [
{
"type": "CDS_RECORD_ID",
"description": "CDS record",
"url": "http://cds.cern.ch/record/<CDS_RECORD_ID>",
"example_value": "123",
"example_url": "http://cds.cern.ch/record/123"
},
{
},
{
"type": "CDS_REPORT_NUMBER",
"description": "CDS report number",
"url": "http://cds.cern.ch/report/<CDS_REPORT_NUMBER>",
"example_value": "CMS-PAS-HIG-14-008",
"example_url": "http://cds.cern.ch/report/CMS-PAS-HIG-14-008"
}
]
}
""")
}
]
}


@pytest.fixture
Expand Down
11 changes: 10 additions & 1 deletion claimstore/modules/claims/fixtures/pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import pytest
from flask import current_app
from jsonschema import ValidationError

from claimstore.core.json import validate_json
from claimstore.ext.sqlalchemy import db
from claimstore.modules.claims.models import IdentifierType

Expand Down Expand Up @@ -57,7 +59,14 @@ def load_all_pids(config_path=None):
)
for pid_fp in glob.glob("{}/*.json".format(pids_filepath)):
with open(pid_fp) as f:
create_pid(json.loads(f.read()))
json_data = json.load(f)
try:
validate_json(json_data, 'claims.persistent_id')
except ValidationError:
print('`{}` could not be loaded. It does not follow the proper'
' JSON schema specification.'.format(pid_fp))
continue
create_pid(json_data)


@pytest.fixture
Expand Down
15 changes: 12 additions & 3 deletions claimstore/modules/claims/fixtures/predicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import pytest
from flask import current_app
from jsonschema import ValidationError

from claimstore.core.json import validate_json
from claimstore.ext.sqlalchemy import db
from claimstore.modules.claims.models import Predicate

Expand Down Expand Up @@ -54,9 +56,16 @@ def load_all_predicates(config_path=None):
'config',
'predicates'
)
for predicate_fp in glob.glob("{}/*.json".format(predicates_filepath)):
with open(predicate_fp) as f:
create_predicate(json.load(f))
for pred_fp in glob.glob("{}/*.json".format(predicates_filepath)):
with open(pred_fp) as f:
json_data = json.load(f)
try:
validate_json(json_data, 'claims.predicate')
except ValidationError:
print('`{}` could not be loaded. It does not follow the proper'
' JSON schema specification.'.format(pred_fp))
continue
create_predicate(json_data)


@pytest.fixture
Expand Down
5 changes: 3 additions & 2 deletions claimstore/modules/claims/restful.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import isodate # noqa
from flask import Blueprint, current_app, request
from flask_restful import Api, Resource, abort, inputs, reqparse
from jsonschema import ValidationError
from sqlalchemy import or_

from claimstore.core.datetime import loc_date_utc
Expand Down Expand Up @@ -100,11 +101,11 @@ def validate_json(self, json_data):
"""Validate that json_data follows the appropiate JSON schema.
:param json_data: JSON data to be validated.
:raises: InvalidJSONData
:raises: :exc:`InvalidJSONData` if the instance is invalid.
"""
try:
validate_json(json_data, self.json_schema)
except Exception as e:
except ValidationError as e:
raise InvalidJSONData('JSON data is not valid', extra=str(e))


Expand Down

0 comments on commit 304f5a9

Please sign in to comment.