Skip to content

Commit

Permalink
Hone custom parser validation and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dharvey-consbio committed Oct 27, 2016
1 parent b9d79ff commit dd9223d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
16 changes: 3 additions & 13 deletions gis_metadata/metadata_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
from gis_metadata.utils import DATES, DATE_TYPE, DATE_VALUES
from gis_metadata.utils import DATE_TYPE_RANGE, DATE_TYPE_RANGE_BEGIN, DATE_TYPE_RANGE_END
from gis_metadata.utils import parse_complex, parse_complex_list, parse_dates, parse_property
from gis_metadata.utils import update_complex, update_complex_list, update_property
from gis_metadata.utils import validate_any, validate_complex_list, validate_properties
from gis_metadata.utils import _complex_definitions, _supported_props, ParserError
from gis_metadata.utils import update_complex, update_complex_list, update_property, validate_any, validate_properties
from gis_metadata.utils import _supported_props, ParserError


# Place holders for lazy, one-time FGDC & ISO imports
Expand Down Expand Up @@ -395,15 +394,6 @@ def validate(self):
validate_properties(self._data_map, self._metadata_props)

for prop in self._data_map:
try:
validate_any(prop, getattr(self, prop))
except ParserError:
if prop in _complex_definitions:
raise # Enforce basic validation for established structures
if prop not in self._data_structures:
raise # Enforce validation for simple properties

# Validate custom data structures according to configured structure
validate_complex_list(prop, getattr(self, prop), self._data_structures[prop])
validate_any(prop, getattr(self, prop), self._data_structures.get(prop))

return self
22 changes: 21 additions & 1 deletion gis_metadata/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ class MetadataParserTests(MetadataParserTestCase):
def test_custom_parser(self):
""" Covers support for custom parsers """

custom_parser = CustomIsoParser(self.iso_metadata)
target_values = {
'metadata_contacts': [{
'name': 'Custom Contact Name', 'email': 'Custom Contact Email', 'phone': 'Custom Contact Phone',
Expand All @@ -522,6 +521,7 @@ def test_custom_parser(self):
'metadata_language': 'eng'
}

custom_parser = CustomIsoParser(self.iso_metadata)
for prop in target_values:
self.assertEqual(getattr(custom_parser, prop), target_values[prop], 'Custom parser values were not parsed')

Expand All @@ -537,6 +537,26 @@ def test_custom_parser(self):

self.assert_parsers_are_equal(custom_parser, converted_parser)

# Test invalid custom complex structure value

metadata_contacts = custom_parser.metadata_contacts
custom_parser.metadata_contacts = u'None'

with self.assertRaises(ParserError):
custom_parser.validate()

custom_parser.metadata_contacts = metadata_contacts

# Test invalid custom simple value

metadata_language = custom_parser.metadata_language
custom_parser.metadata_language = {}

with self.assertRaises(ParserError):
custom_parser.validate()

custom_parser.metadata_language = metadata_language

def test_generic_parser(self):
""" Covers code that enforces certain behaviors for custom parsers """

Expand Down
8 changes: 6 additions & 2 deletions gis_metadata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,13 @@ def validate_any(prop, value, xpath_map=None):
elif prop == PROCESS_STEPS:
validate_process_steps(prop, value)

elif prop not in _supported_props and xpath_map is not None:
# Validate custom data structures as complex lists by default
validate_complex_list(prop, value, xpath_map)

else:
for val in wrap_value(value):
validate_type(prop, val, string_types)
for val in wrap_value(value, include_empty=True):
validate_type(prop, val, (string_types, list))


def validate_complex(prop, value, xpath_map=None):
Expand Down

0 comments on commit dd9223d

Please sign in to comment.