File: resultcachetest.py

package info (click to toggle)
mapserver 6.0.1-3.2%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,940 kB
  • sloc: ansic: 209,190; cpp: 53,949; cs: 12,101; python: 5,323; perl: 3,332; makefile: 841; lex: 706; yacc: 609; java: 466; xml: 379; sh: 253; tcl: 158; ruby: 53
file content (162 lines) | stat: -rw-r--r-- 6,008 bytes parent folder | download | duplicates (3)
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
# $Id: resultcachetest.py 3886 2004-10-26 17:18:34Z sean $
#
# Project:  MapServer
# Purpose:  xUnit style Python mapscript tests of ResultCache
# 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/resultcachetest.py -v
#
# ===========================================================================

import os, sys
import unittest

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

# Base class
class LayerQueryTestCase(MapTestCase):

    def setUp(self):
        MapTestCase.setUp(self)
        self.layer = self.map.getLayer(1)
        self.layer.template = 'some day i will fix this!'
  
    def pointquery(self):
        p = mapscript.pointObj(0.0, 51.5)
        self.layer.queryByPoint(self.map, p, mapscript.MS_MULTIPLE, -1)
        return self.layer.getResults()

    def indexquery(self):
        self.layer.queryByIndex(self.map, -1, 0, mapscript.MS_TRUE)
        return self.layer.getResults()


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

class ResultCacheTestCase(LayerQueryTestCase):

    def testNoDirectAccess(self):
        """denying direct access to layer's resultcache"""
        self.assertRaises(AttributeError, getattr, self.layer, 'resultcache')

    def testNullResultCache(self):
        """before a query layer's resultcache should be NULL"""
        results = self.layer.getResults()
        assert results == None

class PointQueryResultsTestCase(LayerQueryTestCase):

    def testCacheAfterQuery(self):
        """simple point query returns one result"""
        results = self.pointquery()
        assert results.numresults == 1

    def testCacheAfterFailedQuery(self):
        """point query with no results returns NULL"""
        p = mapscript.pointObj(0.0, 0.0)
        self.layer.queryByPoint(self.map, p, mapscript.MS_MULTIPLE, -1)
        results = self.layer.getResults()
        assert results == None

    def testDeletionOfResults(self):
        """deleting results should not harm the layer"""
        results = self.pointquery()
        assert results.thisown == 0
        del results
        results = self.layer.getResults()
        assert results.numresults == 1

    def testQueryResultBounds(self):
        """result bounds should equal layer bounds"""
        results = self.pointquery()
        e = self.layer.getExtent() 
        self.assertRectsEqual(results.bounds, e)

    def testQueryResultMembers(self):
        """get the single result member"""
        results = self.pointquery()
        self.layer.open()
        res = results.getResult(0)
        feature = self.layer.getFeature(res.shapeindex, res.tileindex)
        self.layer.close()
        self.assertRectsEqual(results.bounds, feature.bounds)
        assert feature.getValue(1) == 'A Polygon'

    def testQueryResultsListComp(self):
        """get all results using list comprehension"""
        results = self.pointquery()
        result_list = [results.getResult(i) for i in range(results.numresults)]
        assert len(result_list) == 1
        assert result_list[0].shapeindex == 0

    def testQueryByIndex(self):
        """pop a result into the result set"""
        self.layer.queryByIndex(self.map, -1, 0, mapscript.MS_FALSE)
        results = self.layer.getResults()
        assert results.numresults == 1
        self.layer.queryByIndex(self.map, -1, 0, mapscript.MS_TRUE)
        results = self.layer.getResults()
        assert results.numresults == 2
        
class DumpAndLoadTestCase(LayerQueryTestCase):
    
    def testSaveAndLoadQuery(self):
        """test saving query to a file"""
        results = self.pointquery()
        self.map.saveQuery('test.qry')
        self.map.freeQuery()
        results = self.layer.getResults()
        assert results == None
        self.map.loadQuery('test.qry')
        results = self.layer.getResults()
        assert results is not None
      
class LayerOffQueryTestCase(LayerQueryTestCase):

    def testPointQueryOffLayer(self):
        """simple point query returns one result, even if layer is off"""
        self.layer.status = mapscript.MS_OFF
        results = self.pointquery()
        assert results.numresults == 1

    def testIndexQueryOffLayer(self):
        """simple index query returns one result, even if layer is off"""
        self.layer.status = mapscript.MS_OFF
        results = self.indexquery()
        assert results.numresults == 1



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

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