def testGettersAndSetters(self): """Test behavior of concept, slice, and table setters and getters.""" self.dspl_dataset.AddImport( dspl_model.Import(namespace_id='import1', namespace_url='import1_url')) self.dspl_dataset.AddTopic( dspl_model.Topic(topic_id='topic1', children=[dspl_model.Topic(topic_id='topic2')])) self.dspl_dataset.AddTopic(dspl_model.Topic(topic_id='topic3')) self.dspl_dataset.AddConcept(dspl_model.Concept(concept_id='concept1')) self.dspl_dataset.AddConcept(dspl_model.Concept(concept_id='concept2')) self.dspl_dataset.AddSlice(dspl_model.Slice(slice_id='slice1')) self.dspl_dataset.AddTable(dspl_model.Table(table_id='table1')) self.assertEqual( self.dspl_dataset.GetImport('import1').namespace_url, 'import1_url') self.assertEqual( self.dspl_dataset.GetTopic('topic1').topic_id, 'topic1') self.assertEqual( self.dspl_dataset.GetTopic('topic2').topic_id, 'topic2') self.assertEqual( self.dspl_dataset.GetTopic('topic3').topic_id, 'topic3') self.assertEqual( self.dspl_dataset.GetConcept('concept2').concept_id, 'concept2') self.assertEqual( self.dspl_dataset.GetSlice('slice1').slice_id, 'slice1') self.assertEqual( self.dspl_dataset.GetTable('table1').table_id, 'table1') self.assertEqual(self.dspl_dataset.GetConcept('concept3'), None) self.assertEqual(self.dspl_dataset.GetSlice('slice3'), None) self.assertEqual(self.dspl_dataset.GetTable('table3'), None)
def ElementToConcept(concept_element): """Convert an ElementTree concept element into a Concept object. Args: concept_element: ElementTree element having data from <concept>...</concept> section in an XML file Returns: dspl_model.Concept object """ dspl_concept = dspl_model.Concept() dspl_concept.concept_id = concept_element.get('id') dspl_concept.concept_extension_reference = (concept_element.get( 'extends', default='')) concept_info_element = concept_element.find(_DSPL_SCHEMA_PREFIX + 'info') if concept_info_element is not None: dspl_concept.concept_name = _GetValue( concept_info_element.find(_DSPL_SCHEMA_PREFIX + 'name')) dspl_concept.concept_description = _GetValue( concept_info_element.find(_DSPL_SCHEMA_PREFIX + 'description')) concept_topic_elements = concept_element.findall(_DSPL_SCHEMA_PREFIX + 'topic') if concept_topic_elements is not None: dspl_concept.topic_references = [] for concept_topic_element in concept_topic_elements: dspl_concept.topic_references.append(concept_topic_element.get('ref')) concept_type_element = concept_element.find(_DSPL_SCHEMA_PREFIX + 'type') if concept_type_element is not None: dspl_concept.data_type = concept_type_element.get('ref') concept_table_element = concept_element.find(_DSPL_SCHEMA_PREFIX + 'table') dspl_concept.attributes = ElementsToAttributes(concept_element) dspl_concept.properties = ElementsToProperties(concept_element) if concept_table_element is not None: dspl_concept.table_ref = concept_table_element.get('ref') return dspl_concept
def testDatasetXMLCreation(self): """Create dataset using models, then compare output to expected XML.""" self.dspl_dataset.name = 'My Dataset' self.dspl_dataset.description = 'My Dataset Description' self.dspl_dataset.url = 'url1' self.dspl_dataset.provider_name = 'Googler' self.dspl_dataset.provider_url = 'url2' self.dspl_dataset.AddImport( dspl_model.Import(namespace_id='imported_namespace1', namespace_url='http://imported_namespace1_url')) self.dspl_dataset.AddImport( dspl_model.Import(namespace_id='imported_namespace2', namespace_url='http://imported_namespace2_url')) topic1 = dspl_model.Topic(topic_id='topic1', topic_name='topic1_name') topic2 = dspl_model.Topic(topic_id='topic2', topic_name='topic2_name') topic3 = dspl_model.Topic(topic_id='topic3', topic_name='topic3_name') topic4 = dspl_model.Topic(topic_id='topic4', topic_name='topic4_name') topic1.children = [topic2, topic3] self.dspl_dataset.AddTopic(topic1) self.dspl_dataset.AddTopic(topic4) self.dspl_dataset.AddConcept( dspl_model.Concept( concept_id='concept1', concept_name='Concept 1', concept_description='Concept 1 Description', table_ref='table2', concept_extension_reference='entity:entity', data_type='string', attributes=[ dspl_model.Attribute(concept_ref='attribute_concept', value='attribute_value') ], properties=[ dspl_model.Property(concept_ref='property_concept'), dspl_model.Property(concept_ref='another_property_concept', is_parent=True) ])) self.dspl_dataset.AddConcept( dspl_model.Concept(concept_id='concept2', concept_name='Concept 2', concept_description='Concept 2 Description', data_type='integer', topic_references=['topic1', 'topic2'])) self.dspl_dataset.AddConcept( dspl_model.Concept(concept_id='geo:country', concept_reference='geo:country', data_type='string')) self.dspl_dataset.AddSlice( dspl_model.Slice(slice_id='data_slice', dimension_refs=['concept1', 'geo:country'], metric_refs=['concept2'], dimension_map={ 'concept1': 'concept_column1', 'geo:country': 'concept_column3' }, metric_map={'concept2': 'concept_column2'}, table_ref='table3')) self.dspl_dataset.AddTable( dspl_model.Table( table_id='table', columns=[ dspl_model.TableColumn('col1', 'string', '', ''), dspl_model.TableColumn('col2', 'integer', '', '1234') ], file_name='mydata.csv', verbose=False)) xml_output = self.dspl_dataset.ToXMLElement() for element_tuple in itertools.izip_longest( xml_output.getiterator(), xml.etree.ElementTree.fromstring(TEST_DSPL_XML).getiterator()): constructed_element = element_tuple[0] expected_element = element_tuple[1] # Compare tag names constructed_tag_name = constructed_element.tag # Remove namespace prefixes from expected tag expected_tag_name = re.search('^(?:\{.*\})?(.*)$', expected_element.tag).group(1) self.assertEqual(constructed_tag_name, expected_tag_name) # Compare tag attributes, ignoring these for dspl and value tags if (constructed_element.tag != 'dspl' and constructed_element.tag != 'value'): self.assertEqual(sorted(constructed_element.items()), sorted(expected_element.items())) # Compare tag text constructed_text = constructed_element.text expected_text = expected_element.text # Handle differences in how test and expected results handle text if expected_text: expected_text = expected_text.strip() if expected_text == '': expected_text = None self.assertEqual(constructed_text, expected_text)
def ElementToSlice(slice_element, dspl_dataset): """Convert an ElementTree slice element into a Slice object. Args: slice_element: ElementTree element having data from <slice>...</slice> section in an XML file dspl_dataset: The dataset that this slice is a member of Returns: dspl_model.Slice object """ dspl_slice = dspl_model.Slice() dspl_slice.slice_id = slice_element.get('id') # Parse dimensions dimension_elements = slice_element.findall(_DSPL_SCHEMA_PREFIX + 'dimension') dspl_dimension_refs = [] for dimension_element in dimension_elements: dimension_id = dimension_element.get('concept') dspl_concept = dspl_dataset.GetConcept(dimension_id) if not dspl_concept and ':' in dimension_id: # Dimension refers to an externally-defined concept dspl_concept = dspl_model.Concept() dspl_concept.concept_id = dimension_id dspl_concept.concept_reference = dimension_id dspl_dataset.AddConcept(dspl_concept) dspl_dimension_refs.append(dimension_id) # Parse metrics metric_elements = slice_element.findall(_DSPL_SCHEMA_PREFIX + 'metric') dspl_metric_refs = [] for metric_element in metric_elements: metric_id = metric_element.get('concept') dspl_concept = dspl_dataset.GetConcept(metric_id) if not dspl_concept and ':' in metric_id: # Metric refers to an externally-defined concept dspl_concept = dspl_model.Concept() dspl_concept.concept_id = metric_id dspl_concept.concept_reference = metric_id dspl_dataset.AddConcept(dspl_concept) dspl_metric_refs.append(metric_id) dspl_slice.dimension_refs = dspl_dimension_refs dspl_slice.metric_refs = dspl_metric_refs slice_table_element = slice_element.find(_DSPL_SCHEMA_PREFIX + 'table') if slice_table_element is not None: dspl_slice.table_ref = slice_table_element.get('ref') # Parse mapDimension and mapMetric elements dimension_map_elements = slice_table_element.findall( _DSPL_SCHEMA_PREFIX + 'mapDimension') for dimension_map_element in dimension_map_elements: dspl_slice.dimension_map[dimension_map_element.get('concept')] = ( dimension_map_element.get('toColumn')) metric_map_elements = slice_table_element.findall(_DSPL_SCHEMA_PREFIX + 'mapMetric') for metric_map_element in metric_map_elements: dspl_slice.metric_map[metric_map_element.get('concept')] = ( metric_map_element.get('toColumn')) # Add 'implicit' dimension and/or metric maps for external concepts for dimension_id in dspl_slice.dimension_refs: if (':' in dimension_id) and (dimension_id not in dspl_slice.dimension_map): dimension_name = dimension_id.split(':')[1] dspl_slice.dimension_map[dimension_id] = dimension_name for metric_id in dspl_slice.metric_refs: if (':' in metric_id) and (metric_id not in dspl_slice.metric_map): metric_name = metric_id.split(':')[1] dspl_slice.metric_map[metric_id] = metric_name return dspl_slice