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 ElementToTable(table_element, csv_path, load_all_data): """Convert an ElementTree table element into a Table object. Args: table_element: ElementTree element having data from <table>...</table> section in an XML file csv_path: Path to directory where CSV file associated with this table can be found load_all_data: Boolean indicating whether all CSV data should be loaded Returns: dspl_model.Table object """ dspl_table = dspl_model.Table() dspl_table.table_id = table_element.get('id') column_elements = table_element.findall(_DSPL_SCHEMA_PREFIX + 'column') dspl_columns = [] for column_element in column_elements: column_value_element = column_element.find(_DSPL_SCHEMA_PREFIX + 'value') if column_value_element is not None: constant_value = column_value_element.text else: constant_value = '' dspl_column = dspl_model.TableColumn( column_id=column_element.get('id'), data_type=column_element.get('type'), data_format=column_element.get('format', default=''), constant_value=constant_value) dspl_columns.append(dspl_column) dspl_table.columns = dspl_columns data_element = table_element.find(_DSPL_SCHEMA_PREFIX + 'data') if data_element is not None: file_element = data_element.find(_DSPL_SCHEMA_PREFIX + 'file') if file_element is not None: dspl_table.file_name = file_element.text.strip() if dspl_table.file_name: csv_file_path = os.path.join(csv_path, dspl_table.file_name) dspl_table.table_data = _ReadCSVData(csv_file_path, load_all_data) return dspl_table
def testTableData(self): """Test that Table objects materialize their data to CSV correctly.""" table_data = [['col1', 'col2', 'col3'], ['1/1/2010', 'blue', 'california'], ['1/2/2010', 'red', 'maine']] dspl_table = dspl_model.Table(table_id='table', file_name=self.csv_file_path, table_data=table_data, verbose=False) dspl_table.MaterializeData('') output_csv_file = open(self.csv_file_path, 'r') output_csv_reader = csv.reader(output_csv_file) self.assertEqual(next(output_csv_reader), ['col1', 'col2', 'col3']) self.assertEqual(next(output_csv_reader), ['1/1/2010', 'blue', 'california']) self.assertEqual(next(output_csv_reader), ['1/2/2010', 'red', 'maine']) output_csv_file.close()
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)