Skip to content

Commit

Permalink
Fixes #31798 : don't convert bool to string in virtual layer
Browse files Browse the repository at this point in the history
(cherry picked from commit 36a7d1b)
  • Loading branch information
troopa81 authored and nyalldawson committed Oct 23, 2020
1 parent 08a8592 commit 0d4f0a3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/providers/virtual/qgsvirtuallayersqlitemodule.cpp
Expand Up @@ -692,6 +692,7 @@ int vtableColumn( sqlite3_vtab_cursor *cursor, sqlite3_context *ctxt, int idx )
{
case QVariant::Int:
case QVariant::UInt:
case QVariant::Bool:
sqlite3_result_int( ctxt, v.toInt() );
break;
case QVariant::LongLong:
Expand Down
35 changes: 35 additions & 0 deletions tests/src/python/test_provider_virtual.py
Expand Up @@ -1207,6 +1207,41 @@ def test_feature_count_on_error(self):

QgsProject.instance().removeMapLayer(l1.id())

def test_bool_fields(self):

ml = QgsVectorLayer("NoGeometry?field=a:int&field=b:boolean", "mem", "memory")
self.assertEqual(ml.isValid(), True)
QgsProject.instance().addMapLayer(ml)

ml.startEditing()
f1 = QgsFeature(ml.fields())
f1.setAttribute('a', 1)
f1.setAttribute('b', True)
f2 = QgsFeature(ml.fields())
f2.setAttribute('a', 2)
f2.setAttribute('b', False)
ml.addFeatures([f1, f2])
ml.commitChanges()

self.assertEqual([(f['a'], f['b']) for f in ml.getFeatures()], [(1, True), (2, False)])

df = QgsVirtualLayerDefinition()
df.setQuery('select * from mem')
vl = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual([(f['a'], f['b']) for f in vl.getFeatures()], [(1, True), (2, False)])

df = QgsVirtualLayerDefinition()
df.setQuery('select * from mem where b')
vl = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual([(f['a'], f['b']) for f in vl.getFeatures()], [(1, True)])

df = QgsVirtualLayerDefinition()
df.setQuery('select * from mem where not b')
vl = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual([(f['a'], f['b']) for f in vl.getFeatures()], [(2, False)])

QgsProject.instance().removeMapLayer(ml.id())


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

0 comments on commit 0d4f0a3

Please sign in to comment.