def validate_detail_columns(self, columns): from corehq.apps.app_manager.suite_xml.const import FIELD_TYPE_LOCATION from corehq.apps.locations.util import parent_child from corehq.apps.locations.fixtures import should_sync_hierarchical_fixture hierarchy = None for column in columns: if column.field_type == FIELD_TYPE_LOCATION: domain = self.module.get_app().domain domain_obj = Domain.get_by_name(domain) try: if not should_sync_hierarchical_fixture(domain_obj, self.module.get_app()): # discontinued feature on moving to flat fixture format raise LocationXpathValidationError( _('That format is no longer supported. To reference the location hierarchy you need to' ' use the "Custom Calculations in Case List" feature preview. For more information ' 'see: https://confluence.dimagi.com/pages/viewpage.action?pageId=38276915')) hierarchy = hierarchy or parent_child(domain) LocationXpath('').validate(column.field_property, hierarchy) except LocationXpathValidationError as e: yield { 'type': 'invalid location xpath', 'details': str(e), 'module': self.get_module_info(), 'column': column, }
def testStructureErrorCases(self): test_cases = [ 'unknowntype:outlet/location_type', 'outlet:unknowntype/location_type', 'state:outlet/location_type', # can only point to parents ] for input in test_cases: self.assertRaises( LocationXPathValidationError, LocationXpath('commtrack:locations').validate, input, self.hierarchy, ) self.assertRaises( LocationXPathValidationError, LocationXpath('commtrack:locations').location, input, self.hierarchy, )
def testFormatErrorCases(self): test_cases = [ 'badformatalloneword', 'badformat:noslash', 'badformat/nocolon' 'badformat:too:many/colons' 'badformat:too/many/slashes' ] for input in test_cases: self.assertRaises( LocationXpathValidationError, LocationXpath('commtrack:locations').validate, input, self.hierarchy, ) self.assertRaises( LocationXpathValidationError, LocationXpath('commtrack:locations').location, input, self.hierarchy, )
def testValidXpathExpressions(self): test_cases = [ ("outlet:outlet/location_type", "instance('commtrack:locations')/states/state/districts/district/blocks/block/outlets/outlet[@id = current()/location_id]/location_type"), ("outlet:block/name", "instance('commtrack:locations')/states/state/districts/district/blocks/block[count(outlets/outlet[@id = current()/location_id]) > 0]/name"), ("outlet:district/name", "instance('commtrack:locations')/states/state/districts/district[count(blocks/block/outlets/outlet[@id = current()/location_id]) > 0]/name"), ("district:district/location_type", "instance('commtrack:locations')/states/state/districts/district[@id = current()/location_id]/location_type"), ("district:state/name", "instance('commtrack:locations')/states/state[count(districts/district[@id = current()/location_id]) > 0]/name"), ] for input, expected in test_cases: self.assertEqual(expected, LocationXpath('commtrack:locations').location(input, self.hierarchy))
def xpath(self): from corehq.apps.locations.util import parent_child hierarchy = parent_child(self.app.domain) return LocationXpath('commtrack:locations').location( self.column.field_property, hierarchy)