Skip to content
Permalink
Browse files

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

(cherry picked from commit 36a7d1b)
  • Loading branch information
troopa81 authored and nyalldawson committed Oct 23, 2020
1 parent 08a8592 commit 0d4f0a392831ac89792a0cbe75ba8f9b7256e148
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
@@ -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:
@@ -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.
You can’t perform that action at this time.