Skip to content

Commit

Permalink
Update label tests to ensure setup happens only once and load/delete …
Browse files Browse the repository at this point in the history
…only layer to test
  • Loading branch information
dakcarto committed Mar 6, 2014
1 parent a49b9a7 commit 6a1b706
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 38 deletions.
50 changes: 35 additions & 15 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -84,6 +84,7 @@ class TestQgsPalLabeling(TestCase):
""":type: QgsPalLabeling"""
_PalEngine = None
""":type: QgsLabelingEngineInterface"""
_BaseSetup = False

@classmethod
def setUpClass(cls):
Expand All @@ -103,6 +104,7 @@ def setUpClass(cls):
cls._TestGroup = ''
cls._TestGroupPrefix = ''
cls._TestGroupAbbr = ''
cls._TestGroupCanvasAbbr = ''
cls._TestImage = ''

# initialize class MapRegistry, Canvas, MapRenderer, Map and PAL
Expand Down Expand Up @@ -133,6 +135,8 @@ def setUpClass(cls):
'SKIPPING TEST SUITE')
assert cls._PalEngine, msg

cls._BaseSetup = True

@classmethod
def setDefaultEngineSettings(cls):
"""Restore default settings for pal labelling"""
Expand All @@ -143,28 +147,35 @@ def setDefaultEngineSettings(cls):
@classmethod
def tearDownClass(cls):
"""Run after all tests"""
cls.removeAllLayers()
pass
# cls.removeAllLayers()

@classmethod
def removeAllLayers(cls):
cls._MapRegistry.removeAllMapLayers()
cls._MapSettings.setLayers([])

@classmethod
def getTestFont(cls):
return QFont(cls._TestFont)

@classmethod
def loadFeatureLayer(cls, table):
def loadFeatureLayer(cls, table, chk=False):
if chk and cls._MapRegistry.mapLayersByName(table):
return
uri = QgsDataSourceURI()
uri.setDatabase(cls._PalFeaturesDb)
uri.setDataSource('', table, 'geometry')
vlayer = QgsVectorLayer(uri.uri(), table, 'spatialite')
# .qml should contain only style for symbology
vlayer.loadNamedStyle(os.path.join(cls._PalDataDir,
'{0}.qml'.format(table)))
# qDebug('render_lyr = {0}'.format(repr(vlayer)))
cls._MapRegistry.addMapLayer(vlayer)
# place new layer on top of render stack
render_lyrs = [vlayer.id()] + list(cls._MapSettings.layers())
render_lyrs = [vlayer.id()]
render_lyrs.extend(cls._MapSettings.layers())
# qDebug('render_lyrs = {0}'.format(repr(render_lyrs)))
cls._MapSettings.setLayers(render_lyrs)

# zoom to aoi
Expand Down Expand Up @@ -199,10 +210,10 @@ def configTest(self, prefix, abbr):
self._Test = '{0}_{1}'.format(self._TestGroupAbbr,
self._TestFunction.replace('test_', ''))

def defaultSettings(self):
def defaultLayerSettings(self):
lyr = QgsPalLayerSettings()
lyr.enabled = True
lyr.fieldName = 'text' # default in data sources
lyr.fieldName = 'text' # default in test data sources
font = self.getTestFont()
font.setPointSize(48)
lyr.textFont = font
Expand All @@ -226,25 +237,31 @@ def settingsDict(lyr):
res[attr] = value
return res

def controlImagePath(self, grpprefix=''):
if not grpprefix:
grpprefix = self._TestGroupPrefix
return os.path.join(self._TestDataDir, 'control_images',
'expected_' + grpprefix,
self._Test, self._Test + '.png')

def saveContolImage(self, tmpimg=''):
# don't save control images for RenderVsOtherOutput (Vs) tests, since
# those control images belong to a different test result
if ('PAL_CONTROL_IMAGE' not in os.environ
or 'Vs' in self._TestGroup):
return
testgrpdir = 'expected_' + self._TestGroupPrefix
testdir = os.path.join(self._TestDataDir, 'control_images',
testgrpdir, self._Test)
imgpath = self.controlImagePath()
testdir = os.path.dirname(imgpath)
if not os.path.exists(testdir):
os.makedirs(testdir)
imgbasepath = os.path.join(testdir, self._Test)
imgpath = imgbasepath + '.png'
imgbasepath = \
os.path.join(testdir,
os.path.splitext(os.path.basename(imgpath))[0])
for f in glob.glob(imgbasepath + '.*'):
if os.path.exists(f):
os.remove(f)
if tmpimg:
if os.path.exists(tmpimg):
shutil.copyfile(tmpimg, imgpath)
if tmpimg and os.path.exists(tmpimg):
shutil.copyfile(tmpimg, imgpath)
else:
self._Map.render()
self._Canvas.saveAsImage(imgpath)
Expand All @@ -262,6 +279,9 @@ def renderCheck(self, mismatch=0, imgpath='', grpprefix=''):
"""
if not grpprefix:
grpprefix = self._TestGroupPrefix
ctl_path = self.controlImagePath(grpprefix)
if not os.path.exists(ctl_path):
raise OSError('Missing control image: {0}'.format(ctl_path))
chk = QgsRenderChecker()
chk.setControlPathPrefix('expected_' + grpprefix)
chk.setControlName(self._Test)
Expand Down Expand Up @@ -310,15 +330,15 @@ def test_settings_enable_pal(self):

def test_layer_pal_activated(self):
# Verify, via engine, that PAL labeling can be activated for layer
lyr = self.defaultSettings()
lyr = self.defaultLayerSettings()
lyr.writeToLayer(self.layer)
msg = '\nLayer labeling not activated, as reported by labelingEngine'
self.assertTrue(self._PalEngine.willUseLayer(self.layer), msg)

def test_write_read_settings(self):
# Verify written PAL settings are same when read from layer
# load and write default test settings
lyr1 = self.defaultSettings()
lyr1 = self.defaultLayerSettings()
lyr1dict = self.settingsDict(lyr1)
# print lyr1dict
lyr1.writeToLayer(self.layer)
Expand Down
17 changes: 11 additions & 6 deletions tests/src/python/test_qgspallabeling_canvas.py
Expand Up @@ -41,20 +41,25 @@

class TestCanvasPoint(TestQgsPalLabeling, TestPointBase):

layer = None

@classmethod
def setUpClass(cls):
TestQgsPalLabeling.setUpClass()
if not cls._BaseSetup:
TestQgsPalLabeling.setUpClass()
cls.layer = TestQgsPalLabeling.loadFeatureLayer('point')

@classmethod
def tearDownClass(cls):
TestQgsPalLabeling.tearDownClass()
cls._MapRegistry.removeMapLayer(cls.layer.id())
cls.layer = None

def setUp(self):
"""Run before each test."""
self.configTest('pal_canvas', 'sp')
TestQgsPalLabeling.setDefaultEngineSettings()
self.lyr = self.defaultSettings()

def tearDown(self):
"""Run after each test."""
pass
self.lyr = self.defaultLayerSettings()

def checkTest(self, **kwargs):
self.lyr.writeToLayer(self.layer)
Expand Down
30 changes: 18 additions & 12 deletions tests/src/python/test_qgspallabeling_server.py
Expand Up @@ -62,7 +62,8 @@ class TestServerBase(TestQgsPalLabeling):

@classmethod
def setUpClass(cls):
TestQgsPalLabeling.setUpClass()
if not cls._BaseSetup:
TestQgsPalLabeling.setUpClass()
MAPSERV.startup()
MAPSERV.web_dir_install(glob.glob(cls._PalDataDir + os.sep + '*.qml'))

Expand All @@ -73,9 +74,8 @@ def setUpClass(cls):
os.path.join(MAPSERV.web_dir(), cls._TestProjName))

# the blue background (set via layer style) to match renderchecker's
cls._BkgrdLayer = TestQgsPalLabeling.loadFeatureLayer('background')
TestQgsPalLabeling.loadFeatureLayer('background', True)
cls._CheckMismatch = 200 # default for server tests; mismatch expected
cls._CheckGroup = '' # default '' will check against server control

settings = QSettings()
# noinspection PyArgumentList
Expand All @@ -87,7 +87,7 @@ def tearDownClass(cls):
"""Run after all tests"""
TestQgsPalLabeling.tearDownClass()
# layers removed, save empty project file
cls._TestProj.write()
# cls._TestProj.write()
if "PAL_SERVER_TEMP" in os.environ:
MAPSERV.stop_processes()
MAPSERV.open_temp_dir()
Expand Down Expand Up @@ -132,16 +132,25 @@ def defaultWmsParams(self, layername):

class TestServerPoint(TestServerBase, TestPointBase):

layer = None
""":type: QgsVectorLayer"""

@classmethod
def setUpClass(cls):
TestServerBase.setUpClass()
cls.layer = TestQgsPalLabeling.loadFeatureLayer('point')

@classmethod
def tearDownClass(cls):
TestServerBase.tearDownClass()
cls._MapRegistry.removeMapLayer(cls.layer.id())
cls.layer = None

def setUp(self):
"""Run before each test."""
self.configTest('pal_server', 'sp')
TestQgsPalLabeling.setDefaultEngineSettings()
self.lyr = self.defaultSettings()
self.lyr = self.defaultLayerSettings()
self.params = self.defaultWmsParams('point')
self._TestImage = ''

Expand All @@ -161,18 +170,15 @@ def checkTest(self, **kwargs):
# print self._TestImage.__repr__()
self.saveContolImage(self._TestImage)
self.assertTrue(res_m, 'Failed to retrieve/save image from test server')
# gp = kwargs['grpprefix'] if 'grpprefix' in kwargs else ''
self.assertTrue(*self.renderCheck(mismatch=self._CheckMismatch,
imgpath=self._TestImage,
grpprefix=self._CheckGroup))
imgpath=self._TestImage))


class TestServerVsCanvasPoint(TestServerPoint):

@classmethod
def setUpClass(cls):
TestServerPoint.setUpClass()
cls._CheckGroup = 'pal_canvas'
def setUp(self):
super(TestServerVsCanvasPoint, self).setUp()
self.configTest('pal_canvas', 'sp')


if __name__ == '__main__':
Expand Down
23 changes: 18 additions & 5 deletions tests/src/python/test_qgspallabeling_tests.py
Expand Up @@ -30,7 +30,7 @@
class TestPointBase(object):

def __init__(self):
"""Dummy assignments, intended to be overriden in subclasses"""
"""Dummy assignments, intended to be overridden in subclasses"""
self.lyr = QgsPalLayerSettings()
""":type: QgsPalLayerSettings"""
# noinspection PyArgumentList
Expand All @@ -42,7 +42,7 @@ def __init__(self):
""":type: QgsMapCanvas"""

def checkTest(self, **kwargs):
"""Intended to be overriden in subclasses"""
"""Intended to be overridden in subclasses"""
pass

def test_default_label(self):
Expand Down Expand Up @@ -151,13 +151,26 @@ def suiteTests():
"""
return {
'sp_suite': [
# 'test_default_label',
# 'test_text_size_map_unit',
# 'test_text_color',
# 'test_background_rect',
# 'test_background_rect_w_offset',
# 'test_background_svg',
'test_background_svg_w_offset',
# 'test_background_svg_w_offset',
# 'test_partials_labels_enabled',
# 'test_partials_labels_disabled',
],
'sp_vs_suite': [
# 'test_background_svg',
'test_background_svg_w_offset',
# 'test_default_label',
# 'test_text_size_map_unit',
# 'test_text_color',
# 'test_background_rect',
# 'test_background_rect_w_offset',
# 'test_background_svg',
# 'test_background_svg_w_offset',
# 'test_partials_labels_enabled',
# 'test_partials_labels_disabled',
]
}

Expand Down

0 comments on commit 6a1b706

Please sign in to comment.