Ejemplo n.º 1
0
    def test_gpkg_field_domain_set(self):
        """
        Test setting field domains
        """
        gpkg_domains_original_path = '{}/bug_17878.gpkg'.format(TEST_DATA_DIR)
        temp_domains_path = '{}/domain_set.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, '')

        # field doesn't exist
        with self.assertRaises(QgsProviderConnectionException):
            conn.setFieldDomainName('xxx', '', 'bug_17878', 'my new domain')

        conn.setFieldDomainName('int_field', '', 'bug_17878', 'my new domain')

        # try retrieving result
        del conn
        conn = md.createConnection(temp_domains_path, {})

        fields = conn.fields('', 'bug_17878')
        field = fields.field('int_field')
        self.assertEqual(field.constraints().domainName(), 'my new domain')
Ejemplo n.º 2
0
    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')])
Ejemplo n.º 3
0
    def testRangeDomainWidget(self):
        w = QgsFieldDomainWidget(Qgis.FieldDomainType.Range)

        domain = w.createFieldDomain()

        self.assertIsInstance(domain, QgsRangeFieldDomain)
        self.assertEqual(domain.fieldType(), QVariant.Double)
        self.assertEqual(domain.splitPolicy(),
                         Qgis.FieldDomainSplitPolicy.DefaultValue)
        self.assertEqual(domain.mergePolicy(),
                         Qgis.FieldDomainMergePolicy.DefaultValue)
        self.assertEqual(domain.minimum(), 0)
        self.assertTrue(domain.minimumIsInclusive())
        self.assertEqual(domain.maximum(), 100)
        self.assertTrue(domain.maximumIsInclusive())

        # set domain and test round trips
        domain = QgsRangeFieldDomain('name', 'desc', QVariant.Int, -10, False,
                                     -1, True)
        domain.setSplitPolicy(Qgis.FieldDomainSplitPolicy.GeometryRatio)
        domain.setMergePolicy(Qgis.FieldDomainMergePolicy.Sum)
        w.setFieldDomain(domain)

        domain2 = w.createFieldDomain()
        self.assertIsInstance(domain2, QgsRangeFieldDomain)
        self.assertEqual(domain2.fieldType(), QVariant.Int)
        self.assertEqual(domain2.splitPolicy(),
                         Qgis.FieldDomainSplitPolicy.GeometryRatio)
        self.assertEqual(domain2.mergePolicy(),
                         Qgis.FieldDomainMergePolicy.Sum)
        self.assertEqual(domain2.minimum(), -10.0)
        self.assertFalse(domain2.minimumIsInclusive())
        self.assertEqual(domain2.maximum(), -1.0)
        self.assertTrue(domain2.maximumIsInclusive())

        domain = QgsRangeFieldDomain('name', 'desc', QVariant.Int, -10.1, True,
                                     -1.1, False)
        w.setFieldDomain(domain)

        domain2 = w.createFieldDomain()
        self.assertIsInstance(domain2, QgsRangeFieldDomain)
        self.assertEqual(domain2.fieldType(), QVariant.Int)
        self.assertEqual(domain2.minimum(), -10.1)
        self.assertTrue(domain2.minimumIsInclusive())
        self.assertEqual(domain2.maximum(), -1.1)
        self.assertFalse(domain2.maximumIsInclusive())
Ejemplo n.º 4
0
    def testRangeFieldDomain(self):
        domain = QgsRangeFieldDomain('name', 'desc', QVariant.Int, 1, True, 5,
                                     True)

        self.assertEqual(str(domain), '<QgsRangeFieldDomain: name [1, 5]>')

        self.assertEqual(domain.type(), Qgis.FieldDomainType.Range)
        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.minimum(), 1)
        domain.setMinimum(-1)
        self.assertEqual(domain.minimum(), -1)

        self.assertEqual(domain.maximum(), 5)
        domain.setMaximum(55)
        self.assertEqual(domain.maximum(), 55)

        self.assertTrue(domain.minimumIsInclusive())
        domain.setMinimumIsInclusive(False)
        self.assertFalse(domain.minimumIsInclusive())

        self.assertEqual(str(domain), '<QgsRangeFieldDomain: n (-1, 55]>')

        self.assertTrue(domain.maximumIsInclusive())
        domain.setMaximumIsInclusive(False)
        self.assertFalse(domain.maximumIsInclusive())

        self.assertEqual(str(domain), '<QgsRangeFieldDomain: n (-1, 55)>')

        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.minimum(), -1)
        self.assertEqual(d2.maximum(), 55)
        self.assertFalse(d2.minimumIsInclusive())
        self.assertFalse(d2.maximumIsInclusive())
        self.assertEqual(d2.splitPolicy(),
                         Qgis.FieldDomainSplitPolicy.GeometryRatio)
        self.assertEqual(d2.mergePolicy(),
                         Qgis.FieldDomainMergePolicy.GeometryWeighted)