Skip to content

Commit

Permalink
Merge pull request #13 from DurhamARC/master
Browse files Browse the repository at this point in the history
Check that the given schema version is valid on creating the validator
  • Loading branch information
GraemeWatt authored Feb 27, 2020
2 parents ee168b1 + 3745fe7 commit 7a5b43b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
17 changes: 16 additions & 1 deletion hepdata_validator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
# as an Intergovernmental Organization or submit itself to any jurisdiction.

import abc
import os

from .version import __version__

__all__ = ('__version__', )

LATEST_SCHEMA_VERSION = '1.0.0'
VALID_SCHEMA_VERSIONS = ['1.0.0', '0.1.0']
LATEST_SCHEMA_VERSION = VALID_SCHEMA_VERSIONS[0]

RAW_SCHEMAS_URL = 'https://raw.githubusercontent.com/HEPData/hepdata-validator/' \
+ __version__ + '/hepdata_validator/schemas'
Expand All @@ -46,6 +48,19 @@ def __init__(self, *args, **kwargs):
self.default_schema_file = ''
self.schemas = kwargs.get('schemas', {})
self.schema_version = kwargs.get('schema_version', LATEST_SCHEMA_VERSION)
if self.schema_version not in VALID_SCHEMA_VERSIONS:
raise ValueError('Invalid schema version ' + self.schema_version)

def _get_schema_filepath(self, schema_filename):
full_filepath = os.path.join(self.base_path,
'schemas',
self.schema_version,
schema_filename)

if not os.path.isfile(full_filepath):
raise ValueError('Invalid schema file ' + full_filepath)

return full_filepath

@abc.abstractmethod
def validate(self, **kwargs):
Expand Down
5 changes: 1 addition & 4 deletions hepdata_validator/data_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ class DataFileValidator(Validator):

def __init__(self, *args, **kwargs):
super(DataFileValidator, self).__init__(*args, **kwargs)
self.default_schema_file = os.path.join(self.base_path,
'schemas',
self.schema_version,
self.schema_name)
self.default_schema_file = self._get_schema_filepath(self.schema_name)

def load_custom_schema(self, type, schema_file_path=None):
"""
Expand Down
10 changes: 2 additions & 8 deletions hepdata_validator/submission_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@ class SubmissionFileValidator(Validator):

def __init__(self, *args, **kwargs):
super(SubmissionFileValidator, self).__init__(*args, **kwargs)
self.default_schema_file = os.path.join(self.base_path,
'schemas',
self.schema_version,
self.submission_filename)
self.additional_info_schema = os.path.join(self.base_path,
'schemas',
self.schema_version,
self.additional_info_filename)
self.default_schema_file = self._get_schema_filepath(self.submission_filename)
self.additional_info_schema = self._get_schema_filepath(self.additional_info_filename)

def validate(self, **kwargs):
"""
Expand Down
20 changes: 20 additions & 0 deletions testsuite/test_data_validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pytest
from hepdata_validator import VALID_SCHEMA_VERSIONS
from hepdata_validator.data_file_validator import DataFileValidator
from hepdata_validator.data_file_validator import UnsupportedDataSchemaException

Expand Down Expand Up @@ -255,3 +256,22 @@ def test_file_with_inconsistent_values_v1(validator_v1, data_path):
validator_v1.print_errors(file)

assert is_valid is False


def test_invalid_schema_version():
with pytest.raises(ValueError) as excinfo:
validator = DataFileValidator(schema_version='0.9999.99')

assert "Invalid schema version 0.9999.99" == str(excinfo.value)


def test_invalid_schema_file():
# Fudge the schema versions constant so we can check the file check works
VALID_SCHEMA_VERSIONS.append('0.9999.9999')
try:
with pytest.raises(ValueError) as excinfo:
validator = DataFileValidator(schema_version='0.9999.9999')

assert "Invalid schema file" in str(excinfo.value)
finally:
VALID_SCHEMA_VERSIONS.pop()
20 changes: 20 additions & 0 deletions testsuite/test_submission_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pytest
import yaml
from hepdata_validator import VALID_SCHEMA_VERSIONS
from hepdata_validator.submission_file_validator import SubmissionFileValidator

# We try to load using the CSafeLoader for speed improvements.
Expand Down Expand Up @@ -196,3 +197,22 @@ def test_io_error_submission_yaml_v1(validator_v1, data_path):
validator_v1.print_errors(file)

assert is_valid is False


def test_invalid_schema_version():
with pytest.raises(ValueError) as excinfo:
validator = SubmissionFileValidator(schema_version='0.9999.99')

assert "Invalid schema version 0.9999.99" == str(excinfo.value)


def test_invalid_schema_file():
# Fudge the schema versions constant so we can check the file check works
VALID_SCHEMA_VERSIONS.append('0.9999.9999')
try:
with pytest.raises(ValueError) as excinfo:
validator = SubmissionFileValidator(schema_version='0.9999.9999')

assert "Invalid schema file" in str(excinfo.value)
finally:
VALID_SCHEMA_VERSIONS.pop()

0 comments on commit 7a5b43b

Please sign in to comment.