source: mauRepo/CedaMarkup/trunk/ceda_markup/tests/gml.py @ 8418

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/CedaMarkup/trunk/ceda_markup/tests/gml.py
Revision 8418, 9.7 KB checked in by mnagni, 7 years ago (diff)

Incomplete - # 22513: missing ValidTime? in the GML tag library
 http://team.ceda.ac.uk/trac/ceda/ticket/22513

Even if this release adds the required tag, the most important difference with the version 0.0.2 is the library itself which has been deeply changed to add more flexibility for future development.
The main difference is that now the class is not composed by classes per markup type but from createXXX function per markup type which directly return an Element instance.

Another major add is a rich collection of unittests

  • Property svn:mime-type set to text/plain
Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 28 Jun 2012
30
31@author: mnagni
32'''
33import unittest
34from xml.etree.ElementTree import tostring, Element
35from ceda_markup.gml.gml import createGML, createPosList, createLinearRing,\
36    createExterior, createPolygon, createBeginPosition, createEndPosition,\
37    createTimePeriod, createValidTime
38
39
40class Test(unittest.TestCase):
41
42
43    def setUp(self):
44        pass
45
46
47    def tearDown(self):
48        pass
49
50
51    def testGML(self):
52        #gml tag as root
53        gml = createGML()       
54        self.assertEqual(tostring(gml), '<metadata xmlns="http://www.opengis.net/gml" />')
55       
56        #gml tag as SubElement of another root element
57        root = Element('myCustomTag')
58        gml = createGML(root)
59        root.append(gml)       
60        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml"><gml:metadata />\
61</myCustomTag>')
62
63    def testPosList(self):   
64        postList = createTestPosList()         
65        self.assertEqual(tostring(postList), '<posList xmlns="http://www.opengis.net/gml">\
6645.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</posList>')
67       
68        root = Element('myCustomTag')
69        postList = createTestPosList(root)
70        root.append(postList)       
71        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml">\
72<gml:posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</gml:posList></myCustomTag>')
73       
74    def testLineraRing(self):
75        root = createGML()
76        linearRing = createTestLinearRing(root)
77        root.append(linearRing)
78        self.assertEqual(tostring(root), '<metadata xmlns="http://www.opengis.net/gml">\
79<LinearRing><posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</posList></LinearRing></metadata>')
80       
81        root = Element('myCustomTag')
82        linearRing = createTestLinearRing(root)
83        root.append(linearRing)               
84        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml">\
85<gml:LinearRing><gml:posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</gml:posList>\
86</gml:LinearRing></myCustomTag>')
87       
88    def testExterior(self):
89        iroot = createGML()
90        exterior = createTestExterior(iroot)
91        iroot.append(exterior)
92        self.assertEqual(tostring(iroot), '<metadata xmlns="http://www.opengis.net/gml"><exterior>\
93<LinearRing><posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</posList></LinearRing></exterior></metadata>')
94       
95        iroot = Element('myCustomTag')
96        exterior = createTestExterior(iroot)
97        iroot.append(exterior)                       
98        self.assertEqual(tostring(iroot), '<myCustomTag xmlns:gml="http://www.opengis.net/gml">\
99<gml:exterior><gml:LinearRing><gml:posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</gml:posList>\
100</gml:LinearRing></gml:exterior></myCustomTag>')       
101       
102    def testPolygon(self):
103        root = createGML()
104        polygon = createTestPolygon(root = root)
105        root.append(polygon)         
106        self.assertEqual(tostring(root), '<metadata xmlns="http://www.opengis.net/gml"><Polygon><exterior>\
107<LinearRing><posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</posList></LinearRing></exterior></Polygon></metadata>')
108       
109        root = Element('myCustomTag')
110        polygon = createTestPolygon(root)
111        root.append(polygon)                 
112        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml"><gml:Polygon><gml:exterior>\
113<gml:LinearRing><gml:posList>45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45</gml:posList></gml:LinearRing>\
114</gml:exterior></gml:Polygon></myCustomTag>')       
115       
116    def testBeginPosition(self):               
117        root = createGML()
118        bp = createTestBeginPosition(root)
119        root.append(bp)         
120        self.assertEqual(tostring(root), '<metadata xmlns="http://www.opengis.net/gml"><beginPosition>2011-03-20T12:31:15.451Z</beginPosition></metadata>')       
121               
122        root = Element('myCustomTag')
123        bp = createTestBeginPosition(root)
124        root.append(bp)         
125        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml"><gml:beginPosition>2011-03-20T12:31:15.451Z</gml:beginPosition></myCustomTag>')               
126               
127    def testEndPosition(self):               
128        root = createGML()
129        bp = createTestEndPosition(root)
130        root.append(bp)         
131        self.assertEqual(tostring(root), '<metadata xmlns="http://www.opengis.net/gml"><endPosition>2011-03-20T12:31:15.451Z</endPosition></metadata>')       
132               
133        root = Element('myCustomTag')
134        bp = createTestEndPosition(root)
135        root.append(bp)         
136        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml"><gml:endPosition>2011-03-20T12:31:15.451Z</gml:endPosition></myCustomTag>')               
137               
138    def testTimePeriod(self):               
139        root = createGML()
140        tp = createTestTimePeriod(root)
141        root.append(tp)                         
142        self.assertEqual(tostring(root), '<metadata xmlns="http://www.opengis.net/gml"><TimePeriod>\
143<beginPosition>2011-03-20T12:31:15.451Z</beginPosition><endPosition>2011-03-20T12:31:15.451Z</endPosition>\
144</TimePeriod></metadata>')       
145               
146        root = Element('myCustomTag')
147        tp = createTestTimePeriod(root)
148        root.append(tp)         
149        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml"><gml:TimePeriod>\
150<gml:beginPosition>2011-03-20T12:31:15.451Z</gml:beginPosition><gml:endPosition>2011-03-20T12:31:15.451Z</gml:endPosition>\
151</gml:TimePeriod></myCustomTag>')     
152       
153    def testValidTime(self):
154        root = createGML()
155        vt = createTestValidTime(root)
156        root.append(vt)                 
157        self.assertEqual(tostring(root), '<metadata xmlns="http://www.opengis.net/gml"><validTime>\
158<TimePeriod><beginPosition>2011-03-20T12:31:15.451Z</beginPosition><endPosition>2011-03-20T12:31:15.451Z</endPosition>\
159</TimePeriod></validTime></metadata>')       
160               
161        root = Element('myCustomTag')
162        vt = createTestValidTime(root)
163        root.append(vt)         
164        self.assertEqual(tostring(root), '<myCustomTag xmlns:gml="http://www.opengis.net/gml"><gml:validTime>\
165<gml:TimePeriod><gml:beginPosition>2011-03-20T12:31:15.451Z</gml:beginPosition>\
166<gml:endPosition>2011-03-20T12:31:15.451Z</gml:endPosition></gml:TimePeriod></gml:validTime></myCustomTag>')                 
167               
168def createTestPosList(root = None, body = '45.256 -110.45 46.46 -109.48 43.84 -109.86 45.256 -110.45'):
169    return createPosList(root, body = body)
170
171def createTestLinearRing(root = None, body = None):
172    if body == None:
173        return createLinearRing(root, body = createTestPosList(root))
174    return createLinearRing(root, body = body)
175
176def createTestExterior(root = None, body = None):
177    if body == None:       
178        return createExterior(root, body = createTestLinearRing(root))         
179    return createExterior(root, body)
180
181def createTestPolygon(root = None, body = None):
182    if body == None:       
183        return createPolygon(root, body = createTestExterior(root))         
184    return createPolygon(root, body = body)
185
186def createTestBeginPosition(root = None, body = '2011-03-20T12:31:15.451Z'):
187    return createBeginPosition(root, body = body)
188
189def createTestEndPosition(root = None, body = '2011-03-20T12:31:15.451Z'):
190    return createEndPosition(root, body = body)   
191
192def createTestTimePeriod(root = None, begin = None, end = None): 
193    if begin == None or end == None:
194        begin = createTestBeginPosition(root)       
195        end = createTestEndPosition(root)           
196        return createTimePeriod(root, begin = begin, end = end)         
197    return createTimePeriod(root, begin = begin, end = end) 
198
199def createTestValidTime(root = None, body = None):
200    if body == None:       
201        return createValidTime(root, body = createTestTimePeriod(root))         
202    return createValidTime(root, body = body) 
Note: See TracBrowser for help on using the repository browser.