Skip to content

Commit 62bdc28

Browse files
committed
Unit tests for updating OGR binary column
1 parent de497a3 commit 62bdc28

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

tests/src/python/test_provider_ogr_sqlite.py

+50-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import tempfile
1919
import shutil
20+
import hashlib
2021
from osgeo import gdal, ogr
2122

2223
from qgis.core import (QgsVectorLayer,
@@ -27,7 +28,7 @@
2728
NULL,
2829
QgsRectangle)
2930
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
3132

3233
start_app()
3334

@@ -384,6 +385,54 @@ def test_SplitFeature(self):
384385
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))')
385386
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))')
386387

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+
387436

388437
if __name__ == '__main__':
389438
unittest.main()

0 commit comments

Comments
 (0)