def testCreateExpression(self): """ Test creating an expression using the widget""" layer = QgsVectorLayer( "Point?field=fldtxt:string&field=fldint:integer&field=flddate:datetime", "test", "memory" ) parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 0) w.initWidget(parent) line_edit = w.lineEdit() line_edit.setText("test") case_sensitive = w.caseSensitiveCheckBox() case_sensitive.setChecked(False) self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.IsNull), '"fldtxt" IS NULL') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.IsNotNull), '"fldtxt" IS NOT NULL') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.EqualTo), "lower(\"fldtxt\")=lower('test')") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), "lower(\"fldtxt\")<>lower('test')") case_sensitive.setChecked(True) self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.EqualTo), "\"fldtxt\"='test'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), "\"fldtxt\"<>'test'") case_sensitive.setChecked(False) self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.Contains), "\"fldtxt\" ILIKE '%test%'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.DoesNotContain), "NOT (\"fldtxt\" ILIKE '%test%')") case_sensitive.setChecked(True) self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.Contains), "\"fldtxt\" LIKE '%test%'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.DoesNotContain), "NOT (\"fldtxt\" LIKE '%test%')") case_sensitive.setChecked(False) # numeric field parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 1) w.initWidget(parent) # may need updating if widget layout changes: line_edit = w.lineEdit() line_edit.setText("5.5") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.EqualTo), '"fldint"=5.5') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), '"fldint"<>5.5') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.GreaterThan), '"fldint">5.5') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.LessThan), '"fldint"<5.5') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.GreaterThanOrEqualTo), '"fldint">=5.5') self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.LessThanOrEqualTo), '"fldint"<=5.5') # date/time/datetime parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 2) w.initWidget(parent) # may need updating if widget layout changes: line_edit = w.lineEdit() line_edit.setText("2015-06-03") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.EqualTo), "\"flddate\"='2015-06-03'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), "\"flddate\"<>'2015-06-03'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.GreaterThan), "\"flddate\">'2015-06-03'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.LessThan), "\"flddate\"<'2015-06-03'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.GreaterThanOrEqualTo), "\"flddate\">='2015-06-03'") self.assertEquals(w.createExpression(QgsSearchWidgetWrapper.LessThanOrEqualTo), "\"flddate\"<='2015-06-03'")
def testCreateExpression(self): """ Test creating an expression using the widget""" layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer&field=flddate:datetime", "test", "memory") parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 0) w.initWidget(parent) line_edit = w.lineEdit() line_edit.setText('test') case_sensitive = w.caseSensitiveCheckBox() case_sensitive.setChecked(False) self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.IsNull), '"fldtxt" IS NULL') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.IsNotNull), '"fldtxt" IS NOT NULL') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.EqualTo), 'lower("fldtxt")=lower(\'test\')') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), 'lower("fldtxt")<>lower(\'test\')') case_sensitive.setChecked(True) self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.EqualTo), '"fldtxt"=\'test\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), '"fldtxt"<>\'test\'') case_sensitive.setChecked(False) self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.Contains), '"fldtxt" ILIKE \'%test%\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.DoesNotContain), 'NOT ("fldtxt" ILIKE \'%test%\')') case_sensitive.setChecked(True) self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.Contains), '"fldtxt" LIKE \'%test%\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.DoesNotContain), 'NOT ("fldtxt" LIKE \'%test%\')') case_sensitive.setChecked(False) # numeric field parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 1) w.initWidget(parent) # may need updating if widget layout changes: line_edit = w.lineEdit() line_edit.setText('5.5') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.EqualTo), '"fldint"=5.5') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), '"fldint"<>5.5') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.GreaterThan), '"fldint">5.5') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.LessThan), '"fldint"<5.5') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.GreaterThanOrEqualTo), '"fldint">=5.5') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.LessThanOrEqualTo), '"fldint"<=5.5') # date/time/datetime parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 2) w.initWidget(parent) # may need updating if widget layout changes: line_edit = w.lineEdit() line_edit.setText('2015-06-03') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.EqualTo), '"flddate"=\'2015-06-03\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.NotEqualTo), '"flddate"<>\'2015-06-03\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.GreaterThan), '"flddate">\'2015-06-03\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.LessThan), '"flddate"<\'2015-06-03\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.GreaterThanOrEqualTo), '"flddate">=\'2015-06-03\'') self.assertEqual(w.createExpression(QgsSearchWidgetWrapper.LessThanOrEqualTo), '"flddate"<=\'2015-06-03\'')
def testCurrentFilterExpression(self): """ Test creating an expression using the widget""" layer = QgsVectorLayer("Point?field=fldint:integer", "test", "memory") parent = QWidget() w = QgsDefaultSearchWidgetWrapper(layer, 0, parent) af = QgsAttributeFormEditorWidget(None, None) af.setSearchWidgetWrapper(w) # test that filter combines both current value in search widget wrapper and flags from search tool button w.lineEdit().setText('5.5') af.searchWidgetToolButton().setActiveFlags(QgsSearchWidgetWrapper.EqualTo) self.assertEqual(af.currentFilterExpression(), '"fldint"=5.5') af.searchWidgetToolButton().setActiveFlags(QgsSearchWidgetWrapper.NotEqualTo) self.assertEqual(af.currentFilterExpression(), '"fldint"<>5.5')