Skip to content

Commit

Permalink
Unit test for creation of blob field in existing table
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 12, 2018
1 parent 17ced91 commit f3f1214
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -507,6 +507,43 @@ def testBinaryField(self):
self.assertIsInstance(features[2]['DATA'], QByteArray)
self.assertEqual(hashlib.md5(features[2]['DATA'].data()).hexdigest(), '4b952b80e4288ca5111be2f6dd5d6809')

def testBlobCreation(self):
"""
Test creating binary blob field in existing table
"""
tmpfile = os.path.join(self.basetestpath, 'newbinaryfield.sqlite')
ds = ogr.GetDriverByName('SQLite').CreateDataSource(tmpfile)
lyr = ds.CreateLayer('test', geom_type=ogr.wkbPoint, options=['FID=fid'])
lyr.CreateField(ogr.FieldDefn('strfield', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger))
f = None
ds = None

vl = QgsVectorLayer(tmpfile)
self.assertTrue(vl.isValid())

dp = vl.dataProvider()
f = QgsFeature(dp.fields())
f.setAttributes([1, 'str', 100])
self.assertTrue(dp.addFeature(f))

# add binary field
self.assertTrue(dp.addAttributes([QgsField('binfield', QVariant.ByteArray)]))

fields = dp.fields()
bin1_field = fields[fields.lookupField('binfield')]
self.assertEqual(bin1_field.type(), QVariant.ByteArray)
self.assertEqual(bin1_field.typeName(), 'Binary')

f = QgsFeature(fields)
bin_1 = b'xxx'
bin_val1 = QByteArray(bin_1)
f.setAttributes([2, 'str2', 200, bin_val1])
self.assertTrue(dp.addFeature(f))

f2 = [f for f in dp.getFeatures()][1]
self.assertEqual(f2.attributes(), [2, 'str2', 200, QByteArray(bin_1)])


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

0 comments on commit f3f1214

Please sign in to comment.