Skip to content

Commit

Permalink
Add tests for README customization example.
Browse files Browse the repository at this point in the history
  • Loading branch information
dharvey-consbio committed Oct 27, 2016
1 parent 5b180f2 commit b9d79ff
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,21 @@ class CustomIsoParser(IsoParser):
self._data_map[lang_prop] = 'language/CharacterString'
self._data_map['_' + lang_prop] = 'language/LanguageCode/@codeListValue'

# Complex structure (reuse of contacts structure)
# Complex structure (reuse of contacts structure plus phone)
ct_prop = 'metadata_contacts'
ct_format = 'contact/CI_ResponsibleParty/{ct_path}'
ct_defintion = get_complex_definitions()[CONTACTS]
ct_defintion['phone'] = '{phone}'

# Reusing CONTACT structure definition to specify locations per prop
self._data_structures[ct_prop] = format_xpaths(
get_complex_definitions()[CONTACTS],
ct_defintion,
name=ct_format.format(ct_path='individualName/CharacterString'),
organization=ct_format.format(ct_path='organisationName/CharacterString'),
position=ct_format.format(ct_path='positionName/CharacterString'),
phone=ct_format.format(
ct_path='contactInfo/CI_Contact/phone/CI_Telephone/voice/CharacterString'
),
email=ct_format.format(
ct_path='contactInfo/CI_Contact/address/CI_Address/electronicMailAddress/CharacterString'
)
Expand Down
37 changes: 37 additions & 0 deletions gis_metadata/tests/data/iso_metadata.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
<?xml version='1.0' encoding='UTF-8'?>
<MD_Metadata xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gfc="http://www.isotc211.org/2005/gfc" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:srv="http://www.isotc211.org/2005/srv" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.isotc211.org/2005/gmd">
<language>
<LanguageCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#LanguageCode" codeListValue="eng">eng</LanguageCode>
</language>
<contact>
<CI_ResponsibleParty>
<individualName>
<gco:CharacterString>Custom Contact Name</gco:CharacterString>
</individualName>
<organisationName>
<gco:CharacterString>Custom Contact Organization</gco:CharacterString>
</organisationName>
<positionName>
<gco:CharacterString>Custom Contact Position</gco:CharacterString>
</positionName>
<contactInfo>
<CI_Contact>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>Custom Contact Phone</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
<address>
<CI_Address>
<electronicMailAddress>
<gco:CharacterString>Custom Contact Email</gco:CharacterString>
</electronicMailAddress>
</CI_Address>
</address>
</CI_Contact>
</contactInfo>
<role>
<CI_RoleCode codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_RoleCode" codeListValue="pointOfContact" codeSpace="ISOTC211/19115">pointOfContact</CI_RoleCode>
</role>
</CI_ResponsibleParty>
</contact>
<gmd:contentInfo>
<gmd:MD_FeatureCatalogueDescription>
<gmd:featureCatalogueCitation href="http://www.isotc211.org/2005/gfc/resources/example/G_3.xml">
Expand Down
69 changes: 67 additions & 2 deletions gis_metadata/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from gis_metadata.arcgis_metadata_parser import ArcGISParser, ARCGIS_NODES, ARCGIS_ROOTS
from gis_metadata.fgdc_metadata_parser import FgdcParser, FGDC_ROOT
from gis_metadata.iso_metadata_parser import IsoParser, ISO_ROOTS, _iso_tag_formats
from gis_metadata.metadata_parser import MetadataParser, get_metadata_parser, get_parsed_content
from gis_metadata.metadata_parser import MetadataParser, get_metadata_parser, get_parsed_content, convert_parser_to

from gis_metadata.exceptions import ParserError
from gis_metadata.utils import get_complex_definitions, get_supported_props
from gis_metadata.utils import format_xpaths, get_complex_definitions, get_supported_props
from gis_metadata.utils import DATE_TYPE, DATE_VALUES
from gis_metadata.utils import DATE_TYPE_SINGLE, DATE_TYPE_RANGE, DATE_TYPE_MISSING, DATE_TYPE_MULTIPLE
from gis_metadata.utils import ATTRIBUTES, CONTACTS, DIGITAL_FORMS, PROCESS_STEPS
Expand Down Expand Up @@ -510,6 +510,33 @@ def test_write_template(self):

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',
'position': 'Custom Contact Position', 'organization': 'Custom Contact Organization'
}],
'metadata_language': 'eng'
}

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

complex_val = {
'name': 'Changed Contact Name', 'email': 'Changed Contact Email', 'phone': 'Changed Contact Phone',
'position': 'Changed Contact Position', 'organization': 'Changed Contact Organization'
}
self.assert_reparsed_complex_for(custom_parser, 'metadata_contacts', complex_val, [complex_val])
self.assert_reparsed_simple_for(custom_parser, ['metadata_language'], 'es', 'es')

supported_props = get_supported_props().update(target_values.keys())
converted_parser = convert_parser_to(custom_parser, CustomIsoParser, supported_props)

self.assert_parsers_are_equal(custom_parser, converted_parser)

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

Expand Down Expand Up @@ -993,3 +1020,41 @@ def test_write_values_to_template(self):
self.assert_parser_after_write(ArcGISParser, self.arcgis_metadata, self.test_arcgis_file_path, True)
self.assert_parser_after_write(FgdcParser, self.fgdc_metadata, self.test_fgdc_file_path, True)
self.assert_parser_after_write(IsoParser, self.iso_metadata, self.test_iso_file_path, True)


class CustomIsoParser(IsoParser):

def _init_data_map(self):
super(CustomIsoParser, self)._init_data_map()

# Basic property: text or list (with default location)
lang_prop = 'metadata_language'
self._data_map[lang_prop] = 'language/CharacterString'
self._data_map['_' + lang_prop] = 'language/LanguageCode/@codeListValue'

# Complex structure (reuse of contacts structure plus phone)
ct_prop = 'metadata_contacts'
ct_format = 'contact/CI_ResponsibleParty/{ct_path}'
ct_defintion = get_complex_definitions()[CONTACTS]
ct_defintion['phone'] = '{phone}'

self._data_structures[ct_prop] = format_xpaths(
ct_defintion,
name=ct_format.format(ct_path='individualName/CharacterString'),
organization=ct_format.format(ct_path='organisationName/CharacterString'),
position=ct_format.format(ct_path='positionName/CharacterString'),
phone=ct_format.format(
ct_path='contactInfo/CI_Contact/phone/CI_Telephone/voice/CharacterString'
),
email=ct_format.format(
ct_path='contactInfo/CI_Contact/address/CI_Address/electronicMailAddress/CharacterString'
)
)

self._data_map['_{prop}_root'.format(prop=ct_prop)] = 'contact'
self._data_map[ct_prop] = ParserProperty(self._parse_complex_list, self._update_complex_list)
self._metadata_props.add(lang_prop)

# Ensure the parent logic knows about the two new properties
self._metadata_props.add(lang_prop)
self._metadata_props.add(ct_prop)

0 comments on commit b9d79ff

Please sign in to comment.