def test_gpkg_field_domain_create(self): """ Test creating field domains """ gpkg_domains_original_path = '{}/domains.gpkg'.format(TEST_DATA_DIR) temp_domains_path = '{}/domains_create.gpkg'.format(self.temp_dir.path()) shutil.copy(gpkg_domains_original_path, temp_domains_path) md = QgsProviderRegistry.instance().providerMetadata('ogr') conn = md.createConnection(temp_domains_path, {}) domain = QgsRangeFieldDomain('my new domain', 'my new domain desc', QVariant.Int, 5, True, 15, True) conn.addFieldDomain(domain, '') # try retrieving result del conn conn = md.createConnection(temp_domains_path, {}) res = conn.fieldDomain('my new domain') self.assertEqual(res.type(), Qgis.FieldDomainType.Range) self.assertEqual(res.name(), 'my new domain') self.assertEqual(res.minimum(), 5) self.assertEqual(res.maximum(), 15) # try adding another with a duplicate name, should fail with self.assertRaises(QgsProviderConnectionException) as e: conn.addFieldDomain(domain, '') self.assertEqual(str(e.exception), 'Could not create field domain: A domain of identical name already exists') domain = QgsGlobFieldDomain('my new glob domain', 'my new glob desc', QVariant.String, '*aaabc*') conn.addFieldDomain(domain, '') # try retrieving result del conn conn = md.createConnection(temp_domains_path, {}) res = conn.fieldDomain('my new glob domain') self.assertEqual(res.type(), Qgis.FieldDomainType.Glob) self.assertEqual(res.name(), 'my new glob domain') self.assertEqual(res.description(), 'my new glob desc') self.assertEqual(res.glob(), '*aaabc*') # coded value domain = QgsCodedFieldDomain('my new coded domain', 'my new coded desc', QVariant.String, [QgsCodedValue('a', 'aa'), QgsCodedValue('b', 'bb')]) conn.addFieldDomain(domain, '') # try retrieving result del conn conn = md.createConnection(temp_domains_path, {}) res = conn.fieldDomain('my new coded domain') self.assertEqual(res.type(), Qgis.FieldDomainType.Coded) self.assertEqual(res.name(), 'my new coded domain') self.assertEqual(res.description(), 'my new coded desc') self.assertCountEqual(res.values(), [QgsCodedValue('a', 'aa'), QgsCodedValue('b', 'bb')])
def testCodedFieldDomain(self): domain = QgsCodedFieldDomain( 'name', 'desc', QVariant.Int, [QgsCodedValue(5, 'a'), QgsCodedValue(6, 'b')]) self.assertEqual(str(domain), '<QgsCodedFieldDomain: name>') self.assertEqual(domain.type(), Qgis.FieldDomainType.Coded) self.assertEqual(domain.name(), 'name') domain.setName('n') self.assertEqual(domain.name(), 'n') self.assertEqual(domain.description(), 'desc') domain.setDescription('desc 2') self.assertEqual(domain.description(), 'desc 2') self.assertEqual(domain.fieldType(), QVariant.Int) domain.setFieldType(QVariant.Double) self.assertEqual(domain.fieldType(), QVariant.Double) self.assertEqual(domain.values(), [QgsCodedValue(5, 'a'), QgsCodedValue(6, 'b')]) domain.setValues([QgsCodedValue(51, 'aa'), QgsCodedValue(61, 'bb')]) self.assertEqual(domain.values(), [QgsCodedValue(51, 'aa'), QgsCodedValue(61, 'bb')]) domain.setSplitPolicy(Qgis.FieldDomainSplitPolicy.GeometryRatio) self.assertEqual(domain.splitPolicy(), Qgis.FieldDomainSplitPolicy.GeometryRatio) domain.setMergePolicy(Qgis.FieldDomainMergePolicy.GeometryWeighted) self.assertEqual(domain.mergePolicy(), Qgis.FieldDomainMergePolicy.GeometryWeighted) d2 = domain.clone() self.assertEqual(d2.name(), 'n') self.assertEqual(d2.description(), 'desc 2') self.assertEqual(d2.fieldType(), QVariant.Double) self.assertEqual(d2.values(), [QgsCodedValue(51, 'aa'), QgsCodedValue(61, 'bb')]) self.assertEqual(d2.splitPolicy(), Qgis.FieldDomainSplitPolicy.GeometryRatio) self.assertEqual(d2.mergePolicy(), Qgis.FieldDomainMergePolicy.GeometryWeighted)
def testCodedValue(self): c = QgsCodedValue(5, 'a') self.assertEqual(c.code(), 5) self.assertEqual(c.value(), 'a') self.assertEqual(str(c), '<QgsCodedValue: 5 (a)>') self.assertEqual(c, QgsCodedValue(5, 'a')) self.assertNotEqual(c, QgsCodedValue(5, 'aa')) self.assertNotEqual(c, QgsCodedValue(55, 'a'))
def testCodedValueWidget(self): w = QgsFieldDomainWidget(Qgis.FieldDomainType.Coded) domain = w.createFieldDomain() self.assertIsInstance(domain, QgsCodedFieldDomain) self.assertEqual(domain.fieldType(), QVariant.String) self.assertEqual(domain.splitPolicy(), Qgis.FieldDomainSplitPolicy.DefaultValue) self.assertEqual(domain.mergePolicy(), Qgis.FieldDomainMergePolicy.DefaultValue) self.assertFalse(domain.values()) # set domain and test round trips domain = QgsCodedFieldDomain('name', 'desc', QVariant.Int, [ QgsCodedValue('1', 'aa'), QgsCodedValue('2', 'bb'), QgsCodedValue('3', 'cc') ]) domain.setSplitPolicy(Qgis.FieldDomainSplitPolicy.GeometryRatio) domain.setMergePolicy(Qgis.FieldDomainMergePolicy.Sum) w.setFieldDomain(domain) domain2 = w.createFieldDomain() self.assertIsInstance(domain2, QgsCodedFieldDomain) self.assertEqual(domain2.fieldType(), QVariant.Int) self.assertEqual(domain2.splitPolicy(), Qgis.FieldDomainSplitPolicy.GeometryRatio) self.assertEqual(domain2.mergePolicy(), Qgis.FieldDomainMergePolicy.Sum) self.assertEqual(domain2.values(), [ QgsCodedValue('1', 'aa'), QgsCodedValue('2', 'bb'), QgsCodedValue('3', 'cc') ])