Skip to content
Permalink
Browse files

add test for locator model with groups

  • Loading branch information
3nids committed May 18, 2018
1 parent 1fe0fb7 commit ae389be4a98ae0f180fc2f4f2c853b7f0244aabc
Showing with 97 additions and 33 deletions.
  1. +97 −33 tests/src/python/test_qgslocator.py
@@ -20,6 +20,7 @@
QgsLocatorContext,
QgsLocatorResult,
QgsLocatorModel,
QgsLocatorProxyModel,
QgsLocatorAutomaticModel,
QgsSettings)
from qgis.PyQt.QtCore import QVariant, pyqtSignal, QCoreApplication
@@ -31,35 +32,52 @@

class test_filter(QgsLocatorFilter):

def __init__(self, identifier, prefix=None, parent=None):
def __init__(self, identifier, prefix=None, groupResult=False, parent=None):
super().__init__(parent)
self.identifier = identifier
self._prefix = prefix
self.groupResult = groupResult

def clone(self):
return test_filter(self.identifier)
return test_filter(self.identifier, self.prefix, self.groupResult)

def name(self):
return 'test_' + self.identifier

def displayName(self):
return 'test'
return 'test_' + self.identifier

def prefix(self):
return self._prefix

def fetchResults(self, string, context, feedback):
for i in range(3):
n = 3 if not self.groupResult else 9
for i in range(n):
if feedback.isCanceled():
return
sleep(0.001)
result = QgsLocatorResult()
result.displayString = self.identifier + str(i)
if self.groupResult:
if i < 6:
result.group = 'first group'
elif i < 8:
result.group = 'second group'
self.resultFetched.emit(result)

def triggerResult(self, result):
pass

def priority(self):
if self.identifier == 'a':
return QgsLocatorFilter.High
elif self.identifier == 'b':
return QgsLocatorFilter.Medium
elif self.identifier == 'c':
return QgsLocatorFilter.Low
else:
return QgsLocatorFilter.Medium


class TestQgsLocator(unittest.TestCase):

@@ -268,6 +286,8 @@ def got_hit(result):

def testModel(self):
m = QgsLocatorModel()
p = QgsLocatorProxyModel(m)
p.setSourceModel(m)
l = QgsLocator()

filter_a = test_filter('a')
@@ -282,40 +302,84 @@ def testModel(self):
QCoreApplication.processEvents()

# 4 results - one is locator name
self.assertEqual(m.rowCount(), 4)
self.assertEqual(m.data(m.index(0, 0)), 'test')
self.assertEqual(m.data(m.index(0, 0), QgsLocatorModel.ResultTypeRole), 0)
self.assertEqual(m.data(m.index(0, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(1, 0)), 'a0')
self.assertEqual(m.data(m.index(1, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(m.data(m.index(1, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(2, 0)), 'a1')
self.assertEqual(m.data(m.index(2, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(m.data(m.index(2, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(3, 0)), 'a2')
self.assertEqual(m.data(m.index(3, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(m.data(m.index(3, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(p.rowCount(), 4)
self.assertEqual(p.data(p.index(0, 0)), 'test_a')
self.assertEqual(p.data(p.index(0, 0), QgsLocatorModel.ResultTypeRole), 0)
self.assertEqual(p.data(p.index(0, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')
self.assertEqual(p.data(p.index(1, 0)), 'a0')
self.assertEqual(p.data(p.index(1, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(p.data(p.index(1, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')
self.assertEqual(p.data(p.index(2, 0)), 'a1')
self.assertEqual(p.data(p.index(2, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(p.data(p.index(2, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')
self.assertEqual(p.data(p.index(3, 0)), 'a2')
self.assertEqual(p.data(p.index(3, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(p.data(p.index(3, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')

m.clear()
self.assertEqual(m.rowCount(), 0)
self.assertEqual(p.rowCount(), 0)
l.fetchResults('b', context)

for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()

self.assertEqual(m.rowCount(), 4)
self.assertEqual(m.data(m.index(1, 0)), 'a0')
self.assertEqual(m.data(m.index(2, 0)), 'a1')
self.assertEqual(m.data(m.index(3, 0)), 'a2')
self.assertEqual(p.rowCount(), 4)
self.assertEqual(p.data(p.index(1, 0)), 'a0')
self.assertEqual(p.data(p.index(2, 0)), 'a1')
self.assertEqual(p.data(p.index(3, 0)), 'a2')

m.deferredClear()
# should not be immediately cleared!
self.assertEqual(m.rowCount(), 4)
self.assertEqual(p.rowCount(), 4)
for i in range(100):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(m.rowCount(), 0)
self.assertEqual(p.rowCount(), 0)
m.clear()

# test with groups
self.assertEqual(p.rowCount(), 0)
filter_b = test_filter('b', None, True)
l.registerFilter(filter_b)
l.fetchResults('c', context)
for i in range(200):
sleep(0.002)
QCoreApplication.processEvents()
self.assertEqual(p.rowCount(), 16) # 1 title a + 3 results + 1 title b + 2 groups + 9 results
self.assertEqual(p.data(p.index(0, 0)), 'test_a')
self.assertEqual(p.data(p.index(0, 0), QgsLocatorModel.ResultTypeRole), 0)
self.assertEqual(p.data(p.index(1, 0)), 'a0')
self.assertEqual(p.data(p.index(1, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(p.data(p.index(2, 0)), 'a1')
self.assertEqual(p.data(p.index(2, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(p.data(p.index(3, 0)), 'a2')
self.assertEqual(p.data(p.index(3, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(p.data(p.index(4, 0)), 'test_b')
self.assertEqual(p.data(p.index(4, 0), QgsLocatorModel.ResultTypeRole), 0)
self.assertEqual(p.data(p.index(4, 0), QgsLocatorModel.ResultFilterNameRole), 'test_b')
self.assertEqual(p.data(p.index(5, 0)).strip(), 'first group')
self.assertEqual(p.data(p.index(5, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(6, 0)), 'b0')
self.assertEqual(p.data(p.index(6, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(7, 0)), 'b1')
self.assertEqual(p.data(p.index(7, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(8, 0)), 'b2')
self.assertEqual(p.data(p.index(8, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(9, 0)), 'b3')
self.assertEqual(p.data(p.index(9, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(10, 0)), 'b4')
self.assertEqual(p.data(p.index(10, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(11, 0)), 'b5')
self.assertEqual(p.data(p.index(11, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(p.data(p.index(12, 0)).strip(), 'second group')
self.assertEqual(p.data(p.index(12, 0), QgsLocatorModel.ResultTypeRole), 2)
self.assertEqual(p.data(p.index(13, 0)), 'b6')
self.assertEqual(p.data(p.index(13, 0), QgsLocatorModel.ResultTypeRole), 2)
self.assertEqual(p.data(p.index(14, 0)), 'b7')
self.assertEqual(p.data(p.index(14, 0), QgsLocatorModel.ResultTypeRole), 2)
self.assertEqual(p.data(p.index(15, 0)), 'b8')
self.assertEqual(p.data(p.index(15, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)

def testAutoModel(self):
"""
@@ -336,18 +400,18 @@ def testAutoModel(self):

# 4 results - one is locator name
self.assertEqual(m.rowCount(), 4)
self.assertEqual(m.data(m.index(0, 0)), 'test')
self.assertEqual(m.data(m.index(0, 0)), 'test_a')
self.assertEqual(m.data(m.index(0, 0), QgsLocatorModel.ResultTypeRole), 0)
self.assertEqual(m.data(m.index(0, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(0, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')
self.assertEqual(m.data(m.index(1, 0)), 'a0')
self.assertEqual(m.data(m.index(1, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(m.data(m.index(1, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(1, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(m.data(m.index(1, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')
self.assertEqual(m.data(m.index(2, 0)), 'a1')
self.assertEqual(m.data(m.index(2, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(m.data(m.index(2, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(2, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(m.data(m.index(2, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')
self.assertEqual(m.data(m.index(3, 0)), 'a2')
self.assertEqual(m.data(m.index(3, 0), QgsLocatorModel.ResultTypeRole), 1)
self.assertEqual(m.data(m.index(3, 0), QgsLocatorModel.ResultFilterNameRole), 'test')
self.assertEqual(m.data(m.index(3, 0), QgsLocatorModel.ResultTypeRole), QgsLocatorModel.NoGroup)
self.assertEqual(m.data(m.index(3, 0), QgsLocatorModel.ResultFilterNameRole), 'test_a')

m.search('a')

@@ -357,7 +421,7 @@ def testAutoModel(self):

# 4 results - one is locator name
self.assertEqual(m.rowCount(), 4)
self.assertEqual(m.data(m.index(0, 0)), 'test')
self.assertEqual(m.data(m.index(0, 0)), 'test_a')
self.assertEqual(m.data(m.index(1, 0)), 'a0')
self.assertEqual(m.data(m.index(2, 0)), 'a1')
self.assertEqual(m.data(m.index(3, 0)), 'a2')

0 comments on commit ae389be

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