|
20 | 20 | import glob
|
21 | 21 | from osgeo import gdal, ogr
|
22 | 22 |
|
23 |
| -from qgis.core import QgsVectorLayer, QgsFeature, QgsGeometry, QgsFeatureRequest |
| 23 | +from qgis.core import QgsVectorLayer, QgsFeature, QgsGeometry, QgsFeatureRequest, QgsField |
24 | 24 | from qgis.testing import start_app, unittest
|
25 | 25 | from utilities import unitTestDataPath
|
26 | 26 |
|
@@ -129,6 +129,37 @@ def testFidSupport(self):
|
129 | 129 | got = [(f.attribute('fid'), f.attribute('intfield')) for f in vl.dataProvider().getFeatures(QgsFeatureRequest().setFilterExpression("fid = 12"))]
|
130 | 130 | self.assertEqual(got, [(12, 123)])
|
131 | 131 |
|
| 132 | + @unittest.expectedFailure(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(2, 0, 0)) |
| 133 | + def testNotNullConstraint(self): |
| 134 | + """ test detection of not null constraint on OGR layer """ |
| 135 | + |
| 136 | + tmpfile = os.path.join(self.basetestpath, 'testNotNullConstraint.sqlite') |
| 137 | + ds = ogr.GetDriverByName('SQLite').CreateDataSource(tmpfile) |
| 138 | + lyr = ds.CreateLayer('test', geom_type=ogr.wkbPoint, options=['FID=fid']) |
| 139 | + lyr.CreateField(ogr.FieldDefn('field1', ogr.OFTInteger)) |
| 140 | + fld2 = ogr.FieldDefn('field2', ogr.OFTInteger) |
| 141 | + fld2.SetNullable(False) |
| 142 | + lyr.CreateField(fld2) |
| 143 | + ds = None |
| 144 | + |
| 145 | + vl = QgsVectorLayer('{}'.format(tmpfile), 'test', 'ogr') |
| 146 | + self.assertTrue(vl.isValid()) |
| 147 | + |
| 148 | + # test some bad indexes |
| 149 | + self.assertEqual(vl.dataProvider().fieldConstraints(-1), QgsField.Constraints()) |
| 150 | + self.assertEqual(vl.dataProvider().fieldConstraints(1001), QgsField.Constraints()) |
| 151 | + |
| 152 | + self.assertFalse(vl.dataProvider().fieldConstraints(0) & QgsField.ConstraintNotNull) |
| 153 | + self.assertFalse(vl.dataProvider().fieldConstraints(1) & QgsField.ConstraintNotNull) |
| 154 | + self.assertTrue(vl.dataProvider().fieldConstraints(2) & QgsField.ConstraintNotNull) |
| 155 | + |
| 156 | + # test that constraints have been saved to fields correctly |
| 157 | + fields = vl.fields() |
| 158 | + self.assertFalse(fields.at(0).constraints() & QgsField.ConstraintNotNull) |
| 159 | + self.assertFalse(fields.at(1).constraints() & QgsField.ConstraintNotNull) |
| 160 | + self.assertTrue(fields.at(2).constraints() & QgsField.ConstraintNotNull) |
| 161 | + self.assertEqual(fields.at(2).constraintOrigin(QgsField.ConstraintNotNull), QgsField.ConstraintOriginProvider) |
| 162 | + |
132 | 163 |
|
133 | 164 | if __name__ == '__main__':
|
134 | 165 | unittest.main()
|
0 commit comments