Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Condense labeling test class inheritance and set up local server para…
…ms from map settings
  • Loading branch information
dakcarto committed Mar 6, 2014
1 parent 53cfa9e commit 803bc29
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 58 deletions.
23 changes: 21 additions & 2 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -126,8 +126,11 @@ def setUpClass(cls):
@classmethod
def tearDownClass(cls):
"""Run after all tests"""
pass
# cls.removeAllLayers()

def setUp(self):
"""Run before each test."""
TestQgsPalLabeling.setDefaultEngineSettings()
self.lyr = self.defaultLayerSettings()

@classmethod
def setDefaultEngineSettings(cls):
Expand All @@ -141,6 +144,18 @@ def removeAllLayers(cls):
cls._MapRegistry.removeAllMapLayers()
cls._MapSettings.setLayers([])

@classmethod
def removeMapLayer(cls, layer):
if layer is None:
return
lyr_id = layer.id()
cls._MapRegistry.removeMapLayer(lyr_id)
ms_layers = cls._MapSettings.layers()
""":type: QStringList"""
if ms_layers.contains(lyr_id):
ms_layers.removeAt(ms_layers.indexOf(lyr_id))
cls._MapSettings.setLayers(ms_layers)

@classmethod
def getTestFont(cls):
return QFont(cls._TestFont)
Expand Down Expand Up @@ -379,6 +394,10 @@ def renderCheck(self, mismatch=0, imgpath='', grpprefix=''):
msg = '\nRender check failed for "{0}"'.format(self._Test)
return res, msg

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


class TestPALConfig(TestQgsPalLabeling):

Expand Down
26 changes: 20 additions & 6 deletions tests/src/python/test_qgspallabeling_canvas.py
Expand Up @@ -39,34 +39,48 @@
)


class TestCanvasPoint(TestQgsPalLabeling, TestPointBase):
class TestCanvasBase(TestQgsPalLabeling):

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

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

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

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

def checkTest(self, **kwargs):
self.lyr.writeToLayer(self.layer)
self.saveControlImage()
self.assertTrue(*self.renderCheck())


class TestCanvasBasePoint(TestCanvasBase):

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


class TestCanvasPoint(TestCanvasBasePoint, TestPointBase):

def setUp(self):
"""Run before each test."""
super(TestCanvasPoint, self).setUp()
self.configTest('pal_canvas', 'sp')


if __name__ == '__main__':
# NOTE: unless PAL_SUITE env var is set all test class methods will be run
# SEE: test_qgspallabeling_tests.suiteTests() to define suite
Expand Down
7 changes: 3 additions & 4 deletions tests/src/python/test_qgspallabeling_composer.py
Expand Up @@ -76,19 +76,18 @@ def setUpClass(cls):
TestQgsPalLabeling.loadFeatureLayer('background', True)
cls._CheckMismatch = 0 # mismatch expected for crosscheck
cls._TestImage = ''
cls._TestKind = '' # img|svg|pdf
cls._TestKind = 0 # OutputKind.(Img|Svg|Pdf)

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
TestQgsPalLabeling.tearDownClass()
cls._MapRegistry.removeMapLayer(cls.layer.id())
cls.removeMapLayer(cls.layer)
cls.layer = None

def setUp(self):
"""Run before each test."""
TestQgsPalLabeling.setDefaultEngineSettings()
self.lyr = self.defaultLayerSettings()
super(TestComposerBase, self).setUp()
self._TestImage = ''
# ensure per test map settings stay encapsulated
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
Expand Down
92 changes: 50 additions & 42 deletions tests/src/python/test_qgspallabeling_server.py
Expand Up @@ -59,6 +59,9 @@ class TestServerBase(TestQgsPalLabeling):
_TestProj = None
""":type: QgsProject"""
_TestProjName = ''
layer = None
""":type: QgsVectorLayer"""
params = dict()

@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -86,8 +89,10 @@ def setUpClass(cls):
def tearDownClass(cls):
"""Run after all tests"""
TestQgsPalLabeling.tearDownClass()
cls.removeMapLayer(cls.layer)
cls.layer = None
# 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 All @@ -99,64 +104,47 @@ def setUp(self):
# web server stays up across all tests
# MAPSERV.fcgi_server_process().stop()
# self.deleteCache()
super(TestServerBase, self).setUp()
self._TestImage = ''
# ensure per test map settings stay encapsulated
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)

# noinspection PyPep8Naming
def deleteCache(self):
def delete_cache(self):
for item in os.listdir(self._CacheDir):
shutil.rmtree(os.path.join(self._CacheDir, item),
ignore_errors=True)

# noinspection PyPep8Naming
def defaultWmsParams(self, layername):
return {
def get_wms_params(self):
ms = self._TestMapSettings
osize = ms.outputSize()
dpi = str(ms.outputDpi())
lyrs = [str(self._MapRegistry.mapLayer(i).name()) for i in ms.layers()]
lyrs.reverse()
params = {
'SERVICE': 'WMS',
'VERSION': '1.3.0',
'REQUEST': 'GetMap',
'MAP': self._TestProjName,
# layer stacking order for rendering: bottom,to,top
'LAYERS': ['background', str(layername).strip()], # or 'name,name'
'LAYERS': lyrs, # or 'name,name'
'STYLES': ',',
# authid str or QgsCoordinateReferenceSystem obj
'CRS': 'EPSG:32613', # self._CRS
'BBOX': '606510,4823130,612510,4827130', # self.aoiExtent(),
'CRS': str(ms.destinationCrs().authid()),
# self.aoiExtent(),
'BBOX': str(ms.extent().toString(True).replace(' : ', ',')),
'FORMAT': 'image/png', # or: 'image/png; mode=8bit'
'WIDTH': '600',
'HEIGHT': '400',
'DPI': '72',
'MAP_RESOLUTION': '72',
'FORMAT_OPTIONS': 'dpi:72',
'WIDTH': str(osize.width()),
'HEIGHT': str(osize.height()),
'DPI': dpi,
'MAP_RESOLUTION': dpi,
'FORMAT_OPTIONS': 'dpi:{0}'.format(dpi),
'TRANSPARENT': 'FALSE',
'IgnoreGetMapUrl': '1'
}


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.defaultLayerSettings()
self.params = self.defaultWmsParams('point')
self._TestImage = ''

def tearDown(self):
"""Run after each test."""
pass
print params
return params

def checkTest(self, **kwargs):
self.lyr.writeToLayer(self.layer)
Expand All @@ -174,7 +162,27 @@ def checkTest(self, **kwargs):
imgpath=self._TestImage))


class TestServerVsCanvasPoint(TestServerPoint):
class TestServerBasePoint(TestServerBase):

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

def setUp(self):
super(TestServerBasePoint, self).setUp()
# self._TestMapSettings defines the params
self.params = self.get_wms_params()


class TestServerPoint(TestServerBasePoint, TestPointBase):

def setUp(self):
super(TestServerPoint, self).setUp()
self.configTest('pal_server', 'sp')


class TestServerVsCanvasPoint(TestServerBasePoint, TestPointBase):

def setUp(self):
super(TestServerVsCanvasPoint, self).setUp()
Expand Down
4 changes: 0 additions & 4 deletions tests/src/python/test_qgspallabeling_tests.py
Expand Up @@ -41,10 +41,6 @@ def __init__(self):
self._Canvas = None
""":type: QgsMapCanvas"""

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

def test_default_label(self):
# Default label placement, with text size in points
self.checkTest()
Expand Down

0 comments on commit 803bc29

Please sign in to comment.