def testString(self): """ Test calculation of aggregates on string fields""" layer = QgsVectorLayer("Point?field=fldstring:string", "layer", "memory") pr = layer.dataProvider() values = [ 'cc', 'aaaa', 'bbbbbbbb', 'aaaa', 'eeee', '', 'eeee', '', 'dddd' ] features = [] for v in values: f = QgsFeature() f.setFields(layer.fields()) f.setAttributes([v]) features.append(f) assert pr.addFeatures(features) tests = [ [QgsAggregateCalculator.Count, 'fldstring', 9], [QgsAggregateCalculator.CountDistinct, 'fldstring', 6], [QgsAggregateCalculator.CountMissing, 'fldstring', 2], [QgsAggregateCalculator.Min, 'fldstring', 'aaaa'], [QgsAggregateCalculator.Max, 'fldstring', 'eeee'], [QgsAggregateCalculator.StringMinimumLength, 'fldstring', 0], [QgsAggregateCalculator.StringMaximumLength, 'fldstring', 8], [QgsAggregateCalculator.ArrayAggregate, 'fldstring', values], ] agg = QgsAggregateCalculator(layer) for t in tests: val, ok = agg.calculate(t[0], t[1]) self.assertTrue(ok) self.assertEqual(val, t[2]) # test string concatenation agg.setDelimiter(',') self.assertEqual(agg.delimiter(), ',') val, ok = agg.calculate(QgsAggregateCalculator.StringConcatenate, 'fldstring') self.assertTrue(ok) self.assertEqual(val, 'cc,aaaa,bbbbbbbb,aaaa,eeee,,eeee,,dddd') val, ok = agg.calculate(QgsAggregateCalculator.StringConcatenateUnique, 'fldstring') self.assertTrue(ok) self.assertEqual(val, 'cc,aaaa,bbbbbbbb,eeee,,dddd') # bad tests - the following stats should not be calculatable for string fields for t in [ QgsAggregateCalculator.Sum, QgsAggregateCalculator.Mean, QgsAggregateCalculator.Median, QgsAggregateCalculator.StDev, QgsAggregateCalculator.StDevSample, QgsAggregateCalculator.Range, QgsAggregateCalculator.Minority, QgsAggregateCalculator.Majority, QgsAggregateCalculator.FirstQuartile, QgsAggregateCalculator.ThirdQuartile, QgsAggregateCalculator.InterQuartileRange ]: val, ok = agg.calculate(t, 'fldstring') self.assertFalse(ok)
def testParameters(self): """ Test setting parameters""" a = QgsAggregateCalculator(None) params = QgsAggregateCalculator.AggregateParameters() params.filter = 'string filter' params.delimiter = 'delim' a.setParameters(params) self.assertEqual(a.filter(), 'string filter') self.assertEqual(a.delimiter(), 'delim')
def testParameters(self): """ Test setting parameters""" a = QgsAggregateCalculator(None) params = QgsAggregateCalculator.AggregateParameters() params.filter = 'string filter' params.delimiter = 'delim' a.setParameters(params) self.assertEqual(a.filter(), 'string filter') self.assertEqual(a.delimiter(), 'delim')
def testParameters(self): """ Test setting parameters""" a = QgsAggregateCalculator(None) params = QgsAggregateCalculator.AggregateParameters() params.filter = "string filter" params.delimiter = "delim" a.setParameters(params) self.assertEqual(a.filter(), "string filter") self.assertEqual(a.delimiter(), "delim")
def testString(self): """ Test calculation of aggregates on string fields""" layer = QgsVectorLayer("Point?field=fldstring:string", "layer", "memory") pr = layer.dataProvider() values = ['cc', 'aaaa', 'bbbbbbbb', 'aaaa', 'eeee', '', 'eeee', '', 'dddd'] features = [] for v in values: f = QgsFeature() f.setFields(layer.fields()) f.setAttributes([v]) features.append(f) assert pr.addFeatures(features) tests = [[QgsAggregateCalculator.Count, 'fldstring', 9], [QgsAggregateCalculator.CountDistinct, 'fldstring', 6], [QgsAggregateCalculator.CountMissing, 'fldstring', 2], [QgsAggregateCalculator.Min, 'fldstring', 'aaaa'], [QgsAggregateCalculator.Max, 'fldstring', 'eeee'], [QgsAggregateCalculator.StringMinimumLength, 'fldstring', 0], [QgsAggregateCalculator.StringMaximumLength, 'fldstring', 8], [QgsAggregateCalculator.ArrayAggregate, 'fldstring', values], ] agg = QgsAggregateCalculator(layer) for t in tests: val, ok = agg.calculate(t[0], t[1]) self.assertTrue(ok) self.assertEqual(val, t[2]) # test string concatenation agg.setDelimiter(',') self.assertEqual(agg.delimiter(), ',') val, ok = agg.calculate(QgsAggregateCalculator.StringConcatenate, 'fldstring') self.assertTrue(ok) self.assertEqual(val, 'cc,aaaa,bbbbbbbb,aaaa,eeee,,eeee,,dddd') val, ok = agg.calculate(QgsAggregateCalculator.StringConcatenateUnique, 'fldstring') self.assertTrue(ok) self.assertEqual(val, 'cc,aaaa,bbbbbbbb,eeee,,dddd') # bad tests - the following stats should not be calculatable for string fields for t in [QgsAggregateCalculator.Sum, QgsAggregateCalculator.Mean, QgsAggregateCalculator.Median, QgsAggregateCalculator.StDev, QgsAggregateCalculator.StDevSample, QgsAggregateCalculator.Range, QgsAggregateCalculator.Minority, QgsAggregateCalculator.Majority, QgsAggregateCalculator.FirstQuartile, QgsAggregateCalculator.ThirdQuartile, QgsAggregateCalculator.InterQuartileRange ]: val, ok = agg.calculate(t, 'fldstring') self.assertFalse(ok)