Skip to content
Permalink
Browse files

Fixes #31798 : don't convert bool to string in virtual layer

  • Loading branch information
troopa81 authored and nyalldawson committed Sep 22, 2020
1 parent 8dd75cc commit 36a7d1b8c468ae14ccde7a8334e30d8140f26e6e
Showing with 36 additions and 0 deletions.
  1. +1 −0 src/providers/virtual/qgsvirtuallayersqlitemodule.cpp
  2. +35 −0 tests/src/python/test_provider_virtual.py
@@ -696,6 +696,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:
@@ -1271,6 +1271,41 @@ def test_subset_string(self):
self.assertEqual(gpkg_virtual_layer.featureCount(), 1)
self.assertEqual(gpkg_virtual_layer.subsetString(), '"join_value" = \'twenty\'')

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 36a7d1b

Please sign in to comment.
You can’t perform that action at this time.