Skip to content

Commit

Permalink
validator: catch exceptions, allow absence of comment, bump version
Browse files Browse the repository at this point in the history
* Catch ParserError and IOError exceptions when loading YAML files.
* Allow a submission.yaml file without an initial "comment" key.
* Add tests to give 100% coverage.
* Bump version to 0.1.15.

Signed-off-by: Graeme Watt <graeme.watt@durham.ac.uk>
  • Loading branch information
GraemeWatt committed May 15, 2017
1 parent d892918 commit 1c74478
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
20 changes: 18 additions & 2 deletions hepdata_validator/data_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import os
import yaml
from yaml.scanner import ScannerError
from yaml.parser import ParserError

from hepdata_validator import Validator, ValidationMessage
from jsonschema import validate as json_validate, ValidationError
Expand Down Expand Up @@ -95,14 +96,29 @@ def validate(self, **kwargs):
self.add_validation_message(ValidationMessage(file=file_path, message=
'There was a problem parsing the file.\n' + str(se)))
return False
except ParserError as pe:
self.add_validation_message(ValidationMessage(file=file_path, message=
'There was a problem parsing the file.\n' + pe.__str__()))
return False
except IOError as ioe:
self.add_validation_message(ValidationMessage(file=file_path, message=
'There was a problem parsing the file.\n' + ioe.__str__()))
return False
except: #pragma: no cover
try: # pragma: no cover
data = yaml.load(open(file_path, 'r')) # pragma: no cover
except ScannerError as se: # pragma: no cover
self.add_validation_message(
ValidationMessage(file=file_path, message=
self.add_validation_message(ValidationMessage(file=file_path, message=
'There was a problem parsing the file.\n' + str(se))) # pragma: no cover
return False
except ParserError as pe: # pragma: no cover
self.add_validation_message(ValidationMessage(file=file_path, message=
'There was a problem parsing the file.\n' + pe.__str__()))
return False
except IOError as ioe:
self.add_validation_message(ValidationMessage(file=file_path, message=
'There was a problem parsing the file.\n' + ioe.__str__()))
return False

try:

Expand Down
11 changes: 10 additions & 1 deletion hepdata_validator/submission_file_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import yaml
from yaml.scanner import ScannerError
from yaml.parser import ParserError
from hepdata_validator import Validator, ValidationMessage

__author__ = 'eamonnmaguire'
Expand Down Expand Up @@ -48,7 +49,7 @@ def validate(self, **kwargs):
if data_item is None:
continue
try:
if 'comment' in data_item:
if 'name' not in data_item:
validate(data_item, additional_file_section_schema)
else:
validate(data_item, submission_file_schema)
Expand All @@ -70,4 +71,12 @@ def validate(self, **kwargs):
'This can be because you forgot spaces '
'after colons in your YAML file for instance. '
'Diagnostic information follows.\n' + str(se)))
return False

except ParserError as pe:
self.add_validation_message(ValidationMessage(file=file_path, message=pe.__str__()))
return False

except IOError as ioe:
self.add_validation_message(ValidationMessage(file=file_path, message=ioe.__str__()))
return False
2 changes: 1 addition & 1 deletion hepdata_validator/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@

from __future__ import absolute_import, print_function

__version__ = "0.1.14"
__version__ = "0.1.15"
20 changes: 20 additions & 0 deletions testsuite/test_data/invalid_parser_file.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
independent_variables:
- header: {name: SQRT(S), units: GEV}
values:
- value: 7000
dependent_variables:
- header: {name: SIG(total), units: FB}
qualifiers:
- {name: RE, value: P P --> Z0 Z0 X # note missing final "}"
values:
- value: 6.7
errors:
- {symerror: 0.45, label: stat}
- {asymerror: {plus: 0.4, minus: -0.3}, label: sys}
- {symerror: 0.34, label: "sys,lumi"}
- value: 5.7
errors:
- {symerror: 0.4, label: stat}
- {asymerror: {plus: 0.42, minus: 0.31}, label: sys}
- {symerror: 0.4, label: "sys,lumi"}
14 changes: 14 additions & 0 deletions testsuite/test_data/invalid_parser_submission.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: "Table 3"
location: Page 17 of preprint
description: The measured fiducial cross sections. The first systematic uncertainty is the combined systematic uncertainty excluding luminosity, the second is the luminosity
keywords: # used for searching, possibly multiple values for each keyword
- { name: reactions, value: [P P --> Z0 Z0 X] # note missing final "}"
- { name: observables, value: [SIG]}
- { name: energies, value: [7000]}
data_file: data3.yaml
additional_resources:
- location: "http:github.com/HEPData/hepdata"
description: "Full source code for creating this data"
- location: "http:github.com/HEPData/hepdata"
description: "Full source code for creating this data"
52 changes: 52 additions & 0 deletions testsuite/validation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def setUp(self):
self.valid_empty_file = 'test_data/valid_submission_empty.yaml'
self.invalid_file = 'test_data/invalid_submission.yaml'
self.invalid_syntax_file = 'test_data/invalid_syntax_submission.yaml'
self.invalid_parser_file = 'test_data/invalid_parser_submission.yaml'

def test_valid_submission_yaml(self):
print '___SUBMISSION_FILE_VALIDATION: Testing valid yaml submission___'
Expand Down Expand Up @@ -103,6 +104,32 @@ def test_invalid_submission_yaml(self):

self.validator.print_errors(invalid_sub_yaml)

def test_invalid_parser_submission_yaml(self):
print '___SUBMISSION_FILE_VALIDATION: ' \
'Testing invalid parser yaml submission___'
self.validator = None
self.validator = SubmissionFileValidator()
invalid_sub_yaml = os.path.join(self.base_dir, self.invalid_parser_file)

self.assertEqual(self.validator.validate(
file_path=invalid_sub_yaml), False
)

self.validator.print_errors(invalid_sub_yaml)

def test_ioerror_submission_yaml(self):
print '___SUBMISSION_FILE_VALIDATION: ' \
'Testing ioerror yaml submission___'
self.validator = None
self.validator = SubmissionFileValidator()
invalid_sub_yaml = os.path.join(self.base_dir, self.valid_file[:-1])

self.assertEqual(self.validator.validate(
file_path=invalid_sub_yaml), False
)

self.validator.print_errors(invalid_sub_yaml)


class DataValidationTest(unittest.TestCase):
validator = None
Expand Down Expand Up @@ -140,10 +167,21 @@ def setUp(self):
'test_data/invalid_data_file.yaml'
)

self.invalid_parser_file = os.path.join(
self.base_dir,
'test_data/invalid_parser_file.yaml'
)

self.valid_custom_file = os.path.join(
self.base_dir,
'test_data/valid_file_custom.yaml')

def test_no_file_path_supplied(self):
try:
self.validator.validate(file_path=None)
except LookupError as le:
assert (le)

def test_valid_yaml_file(self):
print '___DATA_VALIDATION: Testing valid yaml submission___'
is_valid = self.validator.validate(file_path=self.valid_file_yaml)
Expand Down Expand Up @@ -207,6 +245,20 @@ def test_load_invalid_data_file(self):
for message in self.validator.get_messages(self.invalid_syntax_data_file):
self.assertTrue(message.message.index("There was a problem parsing the file.") == 0)

def test_invalid_parser_yaml_file(self):
print '___DATA_VALIDATION: Testing invalid parser yaml submission___'
self.assertEqual(self.validator.validate(file_path=self.invalid_parser_file),
False)

self.validator.print_errors(self.invalid_parser_file)

def test_ioerror_yaml_file(self):
print '___DATA_VALIDATION: Testing ioerror yaml submission___'
self.assertEqual(self.validator.validate(file_path=self.valid_file_yaml[:-1]),
False)

self.validator.print_errors(self.valid_file_yaml[:-1])


if __name__ == '__main__':
unittest.main()

0 comments on commit 1c74478

Please sign in to comment.