def test_sorted_impacted_cities(self): """Test getting impacted cities sorted by mmi then population.""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) table = shake_event.sorted_impacted_cities() file_path = unique_filename( prefix='test_sorted_impacted_cities', suffix='.txt', dir=temp_dir('test')) cities_file = file(file_path, 'w') cities_file.writelines(str(table)) cities_file.close() table = str(table).replace(', \'', ',\n\'') table += '\n' fixture_path = os.path.join( data_dir(), 'tests', 'test_sorted_impacted_cities.txt') cities_file = file(fixture_path) expected_string = cities_file.read() cities_file.close() expected_string = expected_string.replace(', \'', ',\n\'') self.max_diff = None self.assertEqual(expected_string, table)
def test_local_cities(self): """Test that we can retrieve the cities local to the event""" shake_id = '20120726022003' shake_event = ShakeEvent(shake_id) # Get teh mem layer cities_layer = shake_event.local_cities_memory_layer() provider = cities_layer.dataProvider() expected_feature_count = 6 self.assertEquals(provider.featureCount(), expected_feature_count) strings = [] request = QgsFeatureRequest() for feature in cities_layer.getFeatures(request): # fetch map of attributes attributes = cities_layer.dataProvider().attributeIndexes() for attribute_key in attributes: strings.append("%d: %s\n" % ( attribute_key, feature[attribute_key].toString())) strings.append('------------------\n') LOGGER.debug('Mem table:\n %s' % strings) file_path = unique_filename(prefix='test_local_cities', suffix='.txt', dir=temp_dir('test')) cities_file = file(file_path, 'w') cities_file.writelines(strings) cities_file.close() fixture_path = os.path.join(data_dir(), 'tests', 'test_local_cities.txt') cities_file = file(fixture_path) expected_string = cities_file.readlines() cities_file.close() diff = difflib.unified_diff(expected_string, strings) diff_list = list(diff) diff_string = '' for _, myLine in enumerate(diff_list): diff_string += myLine message = ('Diff is not zero length:\n' 'Control file: %s\n' 'Test file: %s\n' 'Diff:\n%s' % (fixture_path, file_path, diff_string)) self.assertEqual(diff_string, '', message)