File: maptest.py

package info (click to toggle)
mapserver 5.6.5-2%2Bsqueeze3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 15,900 kB
  • ctags: 25,593
  • sloc: ansic: 201,813; cpp: 49,629; cs: 11,792; python: 5,233; perl: 3,249; sh: 1,199; makefile: 884; lex: 592; java: 466; xml: 373; yacc: 334; tcl: 158; ruby: 53
file content (312 lines) | stat: -rw-r--r-- 13,829 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# $Id: maptest.py 8389 2009-01-06 15:27:12Z aboudreault $
#
# Project:  MapServer
# Purpose:  xUnit style Python mapscript tests of Map
# Author:   Sean Gillies, sgillies@frii.com
#
# ===========================================================================
# Copyright (c) 2004, Sean Gillies
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ===========================================================================
#
# Execute this module as a script from mapserver/mapscript/python
#
#     python tests/cases/maptest.py -v
#
# ===========================================================================

import os, sys
import unittest

# the testing module helps us import the pre-installed mapscript
from testing import mapscript, MapTestCase, TESTMAPFILE

# ===========================================================================
# Test begins now

class MapConstructorTestCase(unittest.TestCase):

    def testMapConstructorNoArg(self):
        """MapConstructorTestCase.testMapConstructorNoArg: test map constructor with no argument"""
        test_map = mapscript.mapObj()
        maptype = type(test_map)
        assert str(maptype) == "<class 'mapscript.mapObj'>", maptype
        assert test_map.thisown == 1
    
    def testMapConstructorEmptyStringArg(self):
        """MapConstructorTestCase.testMapConstructorEmptyStringArg: test map constructor with old-style empty string argument"""
        test_map = mapscript.mapObj('')
        maptype = type(test_map)
        assert str(maptype) == "<class 'mapscript.mapObj'>", maptype
        assert test_map.thisown == 1
        
    def testMapConstructorFilenameArg(self):
        """MapConstructorTestCasetest.testMapConstructorEmptyStringArg: map constructor with filename argument"""
        test_map = mapscript.mapObj(TESTMAPFILE)
        maptype = type(test_map)
        assert str(maptype) == "<class 'mapscript.mapObj'>", maptype
        assert test_map.thisown == 1

class MapExtentTestCase(MapTestCase):
    def testSetExtent(self):
        """MapExtentTestCase.testSetExtent: test the setting of a mapObj's extent"""
        test_map = mapscript.mapObj(TESTMAPFILE)
        e = test_map.extent
        result = test_map.setExtent(e.minx, e.miny, e.maxx, e.maxy)
        self.assertAlmostEqual(test_map.scaledenom, 14.24445829)
        assert result == mapscript.MS_SUCCESS, result
        
    def testSetExtentBadly(self):
        """MapExtentTestCase.testSetExtentBadly: test that mapscript raises an error for an invalid mapObj extent"""
        test_map = mapscript.mapObj(TESTMAPFILE)
        self.assertRaises(mapscript.MapServerError, test_map.setExtent,
                          1.0, -2.0, -3.0, 4.0)
        
    def testReBindingExtent(self):
        """test the rebinding of a map's extent"""
        test_map = mapscript.mapObj(TESTMAPFILE)
        rect1 = mapscript.rectObj(-10.0, -10.0, 10.0, 10.0)
        rect2 = mapscript.rectObj(-10.0, -10.0, 10.0, 10.0)
        test_map.extent = rect1
        assert repr(test_map.extent) != repr(rect1), (test_map.extent, rect1)
        del rect1
        self.assertRectsEqual(test_map.extent, rect2)
        
class MapLayersTestCase(MapTestCase):

    def testMapInsertLayer(self):
        """MapLayersTestCase.testMapInsertLayer: test insertion of a new layer at default (last) index"""
        n = self.map.numlayers
        layer = mapscript.layerObj()
        layer.name = 'new'
	assert layer.map == None, layer.map
        index = self.map.insertLayer(layer)
	assert layer.map != None, layer.map
        assert index == n, index
        assert self.map.numlayers == n + 1
        names = [self.map.getLayer(i).name for i in range(self.map.numlayers)]
        assert names == ['RASTER', 'POLYGON', 'LINE', 'POINT', 'INLINE', 
                         'INLINE-PIXMAP-RGBA', 'INLINE-PIXMAP-PCT', 'new']
        order = self.map.getLayerOrder()
        assert order == (0, 1, 2, 3, 4, 5, 6, 7), order
        
    def testMapInsertLayerAtZero(self):
        """MapLayersTestCase.testMapInsertLayerAtZero: test insertion of a new layer at first index"""
        n = self.map.numlayers
        layer = mapscript.layerObj()
        layer.name = 'new'
	assert layer.map == None, layer.map
        index = self.map.insertLayer(layer, 0)
	assert layer.map != None, layer.map
        assert index == 0, index
        assert self.map.numlayers == n + 1
        names = [self.map.getLayer(i).name for i in range(self.map.numlayers)]
        assert names == ['new', 'RASTER', 'POLYGON', 'LINE', 'POINT', 'INLINE',
                         'INLINE-PIXMAP-RGBA', 'INLINE-PIXMAP-PCT'], names 
        order = self.map.getLayerOrder()
        assert order == (0, 1, 2, 3, 4, 5, 6, 7), order

    def testMapInsertLayerDrawingOrder(self):
        """MapLayersTestCase.testMapInsertLayerDrawingOrder: test affect of insertion of a new layer at index 1 on drawing order"""
        n = self.map.numlayers
        # reverse layer drawing order
        o_start = (6, 5, 4, 3, 2, 1, 0)
        self.map.setLayerOrder(o_start)
        # insert Layer
        layer = mapscript.layerObj()
        layer.name = 'new'
        index = self.map.insertLayer(layer, 1)
        assert index == 1, index 
        # We expect our new layer to be at index 1 in drawing order as well
        order = self.map.getLayerOrder()
        assert order == (7, 1, 6, 5, 4, 3, 2, 0), order

    def testMapInsertLayerBadIndex(self):
        """MapLayersTestCase.testMapInsertLayerBadIndex: expect an exception when index is too large"""
        layer = mapscript.layerObj()
        self.assertRaises(mapscript.MapServerChildError, self.map.insertLayer, 
                          layer, 1000)
       
    def testMapInsertNULLLayer(self):
        """expect an exception on attempt to insert a NULL Layer"""
        self.assertRaises(mapscript.MapServerChildError, self.map.insertLayer, 
                          None)

    def testMapRemoveLayerAtTail(self):
        """removal of highest index (tail) layer"""
        n = self.map.numlayers
        layer = self.map.removeLayer(n-1)
        assert self.map.numlayers == n-1
        assert layer.name == 'INLINE-PIXMAP-PCT'
        assert layer.thisown == 1
        del layer
        order = self.map.getLayerOrder()
        assert order == (0, 1, 2, 3, 4, 5), order

    def testMapRemoveLayerAtZero(self):
        """removal of lowest index (0) layer"""
        n = self.map.numlayers
        layer = self.map.removeLayer(0)
        assert self.map.numlayers == n-1
        assert layer.name == 'RASTER'
        order = self.map.getLayerOrder()
        assert order == (0, 1, 2, 3, 4, 5), order
        
    def testMapRemoveLayerDrawingOrder(self):
        """test affect of layer removal on drawing order"""
        n = self.map.numlayers
        # reverse layer drawing order
        o_start = (6, 5, 4, 3, 2, 1, 0)
        self.map.setLayerOrder(o_start)
        layer = self.map.removeLayer(1)
        assert self.map.numlayers == n-1
        assert layer.name == 'POLYGON'
        order = self.map.getLayerOrder()
        assert order == (5, 4, 3, 2, 1, 0), order
        names = [self.map.getLayer(i).name for i in range(self.map.numlayers)]
        assert names == ['RASTER', 'LINE', 'POINT', 'INLINE', 
                         'INLINE-PIXMAP-RGBA', 'INLINE-PIXMAP-PCT'], names  
        
class MapExceptionTestCase(MapTestCase):
    def testDrawBadData(self):
        """MapExceptionTestCase.testDrawBadData: a bad data descriptor in a layer returns proper error"""
        self.map.getLayerByName('POLYGON').data = 'foo'
        self.assertRaises(mapscript.MapServerError, self.map.draw)

class EmptyMapExceptionTestCase(unittest.TestCase):
    def setUp(self):
        self.map = mapscript.mapObj('')
    def tearDown(self):
        self.map = None
    def testDrawEmptyMap(self):
        """EmptyMapExceptionTestCase.testDrawEmptyMap: drawing an empty map returns proper error"""
        self.assertRaises(mapscript.MapServerError, self.map.draw)

class MapMetaDataTestCase(MapTestCase):
    def testInvalidKeyAccess(self):
        """MapMetaDataTestCase.testInvalidKeyAccess: an invalid map metadata key returns proper error"""
        self.assertRaises(mapscript.MapServerError, self.map.getMetaData, 'foo')
    def testFirstKeyAccess(self):
        """MapMetaDataTestCase.testFirstKeyAccess: first metadata key is correct value"""
        key = self.map.getFirstMetaDataKey()
        assert key == 'key1'
        val = self.map.getMetaData(key)
        assert val == 'value1'
    def testLastKeyAccess(self):
        """MapMetaDataTestCase.testLastKeyAccess: last metadata key is correct value"""
        key = self.map.getFirstMetaDataKey()
        for i in range(3):
            key = self.map.getNextMetaDataKey(key)
            assert key is not None
        key = self.map.getNextMetaDataKey(key)
        assert key is None
    def testMapMetaData(self):
        """MapMetaDataTestCase.testMapMetaData: map metadata keys are correct values"""
        keys = []
        key = self.map.getFirstMetaDataKey()
        if key is not None: keys.append(key)
        while 1:
            key = self.map.getNextMetaDataKey(key)
            if not key:
                break
            keys.append(key)
        assert keys == ['key1', 'key2', 'key3', 'key4'], keys
    def testLayerMetaData(self):
        """MapMetaDataTestCase.testLayerMetaData: layer metadata keys are correct values"""
        keys = []
        key = self.map.getLayer(1).getFirstMetaDataKey()
        if key is not None: keys.append(key)
        while 1:
            key = self.map.getLayer(1).getNextMetaDataKey(key)
            if not key:
                break
            keys.append(key)
        assert keys == ['key1', 'key2', 'key3', 'key4'], keys
    def testClassMetaData(self):
        """MapMetaDataTestCase.testClassMetaData: class metadata keys are correct values"""
        keys = []
        key = self.map.getLayer(1).getClass(0).getFirstMetaDataKey()
        if key is not None: keys.append(key)
        while 1:
            key = self.map.getLayer(1).getClass(0).getNextMetaDataKey(key)
            if not key:
                break
            keys.append(key)
        assert keys == ['key1', 'key2', 'key3', 'key4'], keys

class NoFontSetTestCase(unittest.TestCase):

    def testNoGetFontSetFile(self):
        """an empty map should have fontset filename == None"""
        self.map = mapscript.mapObj()
        assert self.map.fontset.filename == None


class MapFontSetTestCase(MapTestCase):

    def testGetFontSetFile(self):
        """expect fontset file to be 'fonts.txt'"""
        file = self.map.fontset.filename
        assert file == 'fonts.txt', file
   

class MapSizeTestCase(MapTestCase):

    def testDefaultSize(self):
        assert self.map.width == 200
        assert self.map.height == 200

    def testSetSize(self):
        retval = self.map.setSize(480, 480)
        assert retval == mapscript.MS_SUCCESS, retval
        assert self.map.width == 480
        assert self.map.height == 480

class MapSetWKTTestCase(MapTestCase):

    def testOGCWKT(self):
        self.map.setWKTProjection('PROJCS["unnamed",PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["standard_parallel_1",65],PARAMETER["standard_parallel_2",55],PARAMETER["latitude_of_center",0],PARAMETER["longitude_of_center",-153],PARAMETER["false_easting",-4943910.68],PARAMETER["false_northing",0]]')
        proj4 = self.map.getProjection()

        assert proj4.find( '+proj=aea' ) != -1
        assert proj4.find( '+ellps=WGS84' ) != -1
        assert (mapscript.projectionObj(proj4)).getUnits() != mapscript.MS_DD

    def testESRIWKT(self):
        self.map.setWKTProjection('ESRI::PROJCS["Pulkovo_1995_GK_Zone_2",GEOGCS["GCS_Pulkovo_1995",DATUM["D_Pulkovo_1995",SPHEROID["Krasovsky_1940",6378245,298.3]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Gauss_Kruger"],PARAMETER["False_Easting",2500000],PARAMETER["False_Northing",0],PARAMETER["Central_Meridian",9],PARAMETER["Scale_Factor",1],PARAMETER["Latitude_Of_Origin",0],UNIT["Meter",1]]')
        proj4 = self.map.getProjection()

        assert proj4.find( '+proj=tmerc' ) != -1
        assert proj4.find( '+ellps=krass' ) != -1
        assert (mapscript.projectionObj(proj4)).getUnits() != mapscript.MS_DD

    def testWellKnownGEOGCS(self):
        self.map.setWKTProjection('WGS84')
        proj4 = self.map.getProjection()
        assert proj4.find( '+proj=longlat' ) != -1, proj4
        assert proj4.find( '+ellps=WGS84' ) != -1, proj4
        assert (mapscript.projectionObj(proj4)).getUnits() != mapscript.MS_METERS

# ===========================================================================
# Run the tests outside of the main suite

if __name__ == '__main__':
    unittest.main()