|
17 | 17 | import os
|
18 | 18 | import tempfile
|
19 | 19 | import shutil
|
| 20 | +import hashlib |
20 | 21 | from osgeo import gdal, ogr
|
21 | 22 |
|
22 | 23 | from qgis.core import (QgsVectorLayer,
|
|
27 | 28 | NULL,
|
28 | 29 | QgsRectangle)
|
29 | 30 | from qgis.testing import start_app, unittest
|
30 |
| -from qgis.PyQt.QtCore import QDate, QTime, QDateTime |
| 31 | +from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QByteArray |
31 | 32 |
|
32 | 33 | start_app()
|
33 | 34 |
|
@@ -384,6 +385,54 @@ def test_SplitFeature(self):
|
384 | 385 | self.assertEqual([f for f in layer.getFeatures()][0].geometry().asWkt(), 'Polygon ((0.5 0, 0.5 1, 1 1, 1 0, 0.5 0))')
|
385 | 386 | self.assertEqual([f for f in layer.getFeatures()][1].geometry().asWkt(), 'Polygon ((0.5 1, 0.5 0, 0 0, 0 1, 0.5 1))')
|
386 | 387 |
|
| 388 | + def testBlob(self): |
| 389 | + """ |
| 390 | + Test binary blob field |
| 391 | + """ |
| 392 | + tmpfile = os.path.join(self.basetestpath, 'binaryfield.sqlite') |
| 393 | + ds = ogr.GetDriverByName('SQLite').CreateDataSource(tmpfile) |
| 394 | + lyr = ds.CreateLayer('test', geom_type=ogr.wkbPoint, options=['FID=fid']) |
| 395 | + lyr.CreateField(ogr.FieldDefn('strfield', ogr.OFTString)) |
| 396 | + lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger)) |
| 397 | + lyr.CreateField(ogr.FieldDefn('binfield', ogr.OFTBinary)) |
| 398 | + lyr.CreateField(ogr.FieldDefn('binfield2', ogr.OFTBinary)) |
| 399 | + f = None |
| 400 | + ds = None |
| 401 | + |
| 402 | + vl = QgsVectorLayer(tmpfile) |
| 403 | + self.assertTrue(vl.isValid()) |
| 404 | + |
| 405 | + fields = vl.fields() |
| 406 | + bin1_field = fields[fields.lookupField('binfield')] |
| 407 | + self.assertEqual(bin1_field.type(), QVariant.ByteArray) |
| 408 | + self.assertEqual(bin1_field.typeName(), 'Binary') |
| 409 | + bin2_field = fields[fields.lookupField('binfield2')] |
| 410 | + self.assertEqual(bin2_field.type(), QVariant.ByteArray) |
| 411 | + self.assertEqual(bin2_field.typeName(), 'Binary') |
| 412 | + |
| 413 | + dp = vl.dataProvider() |
| 414 | + f = QgsFeature(fields) |
| 415 | + bin_1 = b'xxx' |
| 416 | + bin_2 = b'yyy' |
| 417 | + bin_val1 = QByteArray(bin_1) |
| 418 | + bin_val2 = QByteArray(bin_2) |
| 419 | + f.setAttributes([1, 'str', 100, bin_val1, bin_val2]) |
| 420 | + self.assertTrue(dp.addFeature(f)) |
| 421 | + |
| 422 | + f2 = next(dp.getFeatures()) |
| 423 | + self.assertEqual(f2.attributes(), [1, 'str', 100, QByteArray(bin_1), QByteArray(bin_2)]) |
| 424 | + |
| 425 | + bin_3 = b'zzz' |
| 426 | + bin_4 = b'aaa' |
| 427 | + bin_val3 = QByteArray(bin_3) |
| 428 | + bin_val4 = QByteArray(bin_4) |
| 429 | + self.assertTrue(dp.changeAttributeValues({f2.id(): {fields.lookupField('intfield'): 200, |
| 430 | + fields.lookupField('binfield'): bin_val4, |
| 431 | + fields.lookupField('binfield2'): bin_val3}})) |
| 432 | + |
| 433 | + f5 = next(dp.getFeatures()) |
| 434 | + self.assertEqual(f5.attributes(), [1, 'str', 200, QByteArray(bin_4), QByteArray(bin_3)]) |
| 435 | + |
387 | 436 |
|
388 | 437 | if __name__ == '__main__':
|
389 | 438 | unittest.main()
|
0 commit comments