source: exist/trunk/python/ndgUtils/models/testatom.py @ 4444

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/models/testatom.py@4444
Revision 4444, 13.0 KB checked in by cbyrom, 12 years ago (diff)

Add new xquery to lookup deployment atoms associated with an atom ID +
add code to models to allow the use of this to retrieve this information
+ update tests + avoid doubly escaping special characters.

Line 
1'''
2A test class for the GranuleAtom module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest, copy
8from ndgUtils.models.Atom import Atom, Link
9from ndgUtils.vocabtermdata import VocabTermData as VTD
10from utilities import getTripleData
11
12class testatom(unittest.TestCase):
13    validParams = ['OZONE MIXING RATIO (PPB ) | http://vocab.ndg.nerc.ac.uk/blah | OZONE', \
14                   'ERROR FLAG (OZONE MIXING RATIO ) | http://vocab.ndg.nerc.ac.uk/blah | ERROR', \
15                   'HYDROGEN PEROXIDE PPBV | http://vocab.ndg.nerc.ac.uk/blah | HYDROGEN',
16                   'CADMIUM >1.21 M DIAMETER (PMOL M-3) INSTRUMENTAL DETECTION LIMIT 0.132 NM']
17   
18    messyParams = ['OZONE MIXING RATIO &  (PPB     ) | http://vocab.ndg.nerc.ac.uk/blah | ozone', \
19                   'ERROR FLAG (OZONE MIXING > RATIO  ) | http://vocab.ndg.nerc.ac.uk/blah | error', \
20                   'Hydrogen Peroxide < ppbv | http://vocab.ndg.nerc.ac.uk/blah | hydrogen &']
21
22    summaryText = 'this is a summary of the granulite'
23
24    csmlFile = '/db/moles_support/csml/csml_famous_control_month.xml'
25   
26    logo = '/blah/blah/a_logo_icon.png'
27   
28    title = 'this is a test granulite'
29   
30    datasetID = 'famous_control_month'
31   
32    id = 'tag:localhost:5000,2008-09-17:/view/badc.nerc.ac.uk__ATOM__' + datasetID
33   
34    atomName = 'http://localhost:5000/view/badc.nerc.ac.uk__ATOM__' + datasetID
35   
36    name = 'famous_control_month.atom'
37   
38    xmlString = '<entry xmlns:georss="http://www.georss.org/georss/10" xmlns:gml="http://www.opengis.net/gml" ' + \
39        'xmlns:moles="http://ndg.nerc.ac.uk/schema/moles2beta">' + \
40        '<id>' + id + '</id>' + \
41        '<title>' + title + '</title>' + \
42            '<link href="' + atomName + '" rel="self"/>' + \
43            '<link href="/db/moles_support/granuleAtom/' + datasetID + '_granule.moles" rel="related"/>' + \
44            '<link href="http://badc.nerc.ac.uk/a-real-wms-endpoint" rel="http://vocab.ndg.nerc.ac.uk/term/P201/1/GCMDU026" title="WMS"/>' + \
45            '<link href="http://badc.nerc.ac.uk/alternate" rel="alternate" title="alternate page"/>' + \
46            '<link href="' + logo + '" title="' + VTD.TERM_DATA[VTD.LOGO_TERM].title + '" rel="logo"/>' + \
47            '<author><name>Tiddeman, David</name><uri>www.cb.org</uri></author>' + \
48            '<contributor><name>Brian Bandy</name><uri>www.cb.org</uri></contributor>' + \
49            '<moles:entity type="granule"><moles:molesISO><moles:responsibleParties>' + \
50                '<moles:responsibleParty><name>calum byrom</name><uri>www.cb.org</uri><role>Metadata maintainer</role></moles:responsibleParty>' + \
51                '<moles:responsibleParty><name>dom lowe</name><uri>www.badc.rl.ac.uk</uri><role>Metadata maintainer</role></moles:responsibleParty>' + \
52                '</moles:responsibleParties><moles:datasetLanguage>English</moles:datasetLanguage>' + \
53                '<moles:providerID>badc.nerc.ac.uk</moles:providerID>' + \
54                '<moles:metadataLanguage>English</moles:metadataLanguage>' + \
55                '</moles:molesISO>' + \
56            '</moles:entity>' + \
57            '<category term="OZONE" scheme="http://vocab.ndg.nerc.ac.uk/blah" label="OZONE MIXING RATIO (PPB )"/>' + \
58            '<category term="ERROR" scheme="http://vocab.ndg.nerc.ac.uk/blah" label="ERROR FLAG (OZONE MIXING RATIO )"/>' + \
59            '<category term="HYDROGEN" scheme="http://vocab.ndg.nerc.ac.uk/blah" label="HYDROGEN PEROXIDE PPBV"/>' + \
60            '<category term="" scheme="" label="CADMIUM &gt;1.21 M DIAMETER (PMOL M-3) INSTRUMENTAL DETECTION LIMIT 0.132 NM"/>' + \
61            '<summary>' + summaryText + '</summary>' + \
62            '<content src="' + csmlFile + '" type="application/xml"/>' + \
63            '<published>2008-06-17T10:15:19</published>' + \
64            '<moles:temporalRange>2792-12-16 0:0:0.0/2992-11-16 0:0:0.0</moles:temporalRange>' + \
65            '<georss:where><gml:Envelope><gml:lowerCorner>-90.0 -90.0</gml:lowerCorner><gml:upperCorner>360.0 5505.29980469</gml:upperCorner></gml:Envelope></georss:where>' + \
66        '</entry>'
67       
68    validAuthor = 'Tiddeman, David'
69    validAuthors = [validAuthor + ' | www.cb.org', \
70                        'Brian Bandy | www.cb.org']
71       
72    validGranuleAuthors = ['calum byrom | www.cb.org | Metadata maintainer', \
73                        'dom lowe | www.badc.rl.ac.uk | Metadata maintainer']
74
75    validLinks = ['http://badc.nerc.ac.uk/a-real-wms-endpoint | WMS | http://vocab.ndg.nerc.ac.uk/term/P201/1/GCMDU026',\
76                  'http://badc.nerc.ac.uk/alternate | alternate page | alternate',
77                  '/blah/blah/a_logo_icon.png | logo | LOGO - NOT YET SET UP',
78                  '/db/moles_support/granuleAtom/famous_control_month_granule.moles']
79   
80    minX = '-90.0'
81    minY = '-90.0'
82    maxX = '360.0'
83    maxY = '5505.29980469'
84    t1 = '2792-12-16 0:0:0.0'
85    t2 = '2992-11-16 0:0:0.0'
86    VTD = VTD()
87   
88    def setUp(self):
89        '''
90        set up data used in the tests.
91        '''
92        print "Setting up..."
93        self.atom = Atom(VTD.GRANULE_TERM, vocabTermData = self.VTD)
94        self.link = Link()
95        self.link.title = 'fred'
96        self.link.href = 'www.fred.com'
97        self.link.rel = 'related'
98        self.link2 = Link()
99        self.link2.title = 'pete'
100        self.link2.href = 'www.pete.com'
101        self.link2.rel = 'self'
102
103       
104       
105    def testAddValidParameters(self):
106        self.atom.addParameters(self.validParams)
107        self.assertEqual(self.atom.parameters[0].term, getTripleData(self.validParams[0])[2])
108        self.assertEqual(self.atom.parameters[1].term, getTripleData(self.validParams[1])[2])
109        self.assertEqual(self.atom.parameters[2].term, getTripleData(self.validParams[2])[2])
110       
111       
112    def testAddMessyParameters(self):
113        self.atom.addParameters(self.messyParams)
114        self.assertEqual(self.atom.parameters[0].term, 'OZONE')
115        self.assertEqual(self.atom.parameters[1].term, 'ERROR')
116        self.assertEqual(self.atom.parameters[2].term, 'HYDROGEN &AMP;')
117        self.assertEqual(self.atom.parameters[2].scheme, 'http://vocab.ndg.nerc.ac.uk/blah')
118        self.assertEqual(self.atom.parameters[2].label.lower(), 'Hydrogen Peroxide &lt; ppbv'.lower())
119       
120    def testFromString(self):
121        self.atom.fromString(self.xmlString)
122       
123        self.assertNotEqual(self.atom.author, None)
124        self.assertEqual(self.validAuthor, self.atom.author.name)
125       
126        self.assertEqual(len(self.atom.parameters), 4)
127       
128        for param in self.atom.parameters:
129            foundIt = False
130            for validParam in self.validParams:
131                if (validParam.startswith(param.term)):
132                    foundIt = True
133                    break
134           
135            self.assertEqual(True, foundIt, "Cound not find parameter (%s) in valid parameter list" %param)
136       
137        # currently only store max/min boundaries
138        #for spatial in self.atom.spatialData:
139        #    if not spatial in self.validSpatialData:
140        #        self.fail("Cound not find spatial data (%s) in valid coverage list" %spatial)
141        #
142        #for temporal in self.atom.temporalData:
143        #    if not temporal in self.validTemporalData:
144        #        self.fail("Cound not find temporal data (%s) in valid coverage list" %temporal)
145       
146        #self.assertEqual(len(self.atom.relatedLinks), 2)
147        for link in self.atom.relatedLinks:
148            foundIt = False
149            for validLink in self.validLinks:
150                if (validLink.startswith(link.href)):
151                    foundIt = True
152                    break
153           
154            self.assertEqual(True, foundIt, "Cound not find link (%s) in valid related link list" %link.href)
155
156        self.assertEqual(self.atom.summary[0], self.summaryText)
157        self.assertEqual(self.atom.csmlFile, self.csmlFile)
158        self.assertEqual(self.atom.cdmlFile, None)
159        self.assertEqual(self.atom.title, self.title)
160        self.assertEqual(self.atom.getLogos()[0].href, self.logo)
161        self.assertEqual(self.atom.datasetID, self.datasetID)
162        self.assertEqual(self.atom.minX, self.minX)
163        self.assertEqual(self.atom.minY, self.minY)
164        self.assertEqual(self.atom.maxX, self.maxX)
165        self.assertEqual(self.atom.maxY, self.maxY)
166        self.assertEqual(self.atom.t1, self.t1)
167        self.assertEqual(self.atom.t2, self.t2)
168       
169        # date when the granule was first ingested
170        if not self.atom.publishedDate:
171            self.fail("No published date found") 
172
173        self.assertEqual(self.atom.atomName,self.name)
174       
175
176    def testImportExport(self):
177        self.atom.fromString(self.xmlString)
178        xml = self.atom.toPrettyXML()
179        # NB, this is a naive test as ordering of sibling elements changes as does addition of new line characters
180        #self.assertEqual(xml, self.xmlString)
181
182    def testCreateWithInputDict(self):
183        inputs = {
184                  'atomTypeID': VTD.ACTIVITY_TERM,
185                  'title': self.title
186                  }
187        a = Atom(**inputs)
188        self.assertEqual(self.title, a.title)
189        self.assertEqual(VTD.ACTIVITY_TERM, a.atomTypeID)
190        self.assertEqual(VTD.TERM_DATA[VTD.ACTIVITY_TERM].title, a.atomTypeName)
191
192    def testaddUniqueRelatedLinks(self):
193        self.atom.fromString(self.xmlString)
194        linkNo = len(self.atom.relatedLinks)
195        self.atom.addUniqueRelatedLinks(self.link)
196        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
197        self.assertTrue(self.link in self.atom.relatedLinks)
198
199    def testAddMultipeDifferentRelatedLinks(self):
200        self.atom.fromString(self.xmlString)
201        linkNo = len(self.atom.relatedLinks)
202        self.atom.addUniqueRelatedLinks(self.link)
203        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
204        link2 = copy.deepcopy(self.link)
205        link2.title = "blabby"
206        self.atom.addUniqueRelatedLinks(link2)
207        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
208        self.atom.addUniqueRelatedLinks(link2)
209        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
210
211    def testAddMultipeDifferentRelatedLinksSimultaneously(self):
212        self.atom.fromString(self.xmlString)
213        linkNo = len(self.atom.relatedLinks)
214        link2 = copy.deepcopy(self.link)
215        link2.title = "blabby"
216        links = [self.link, link2]
217        self.atom.addUniqueRelatedLinks(links)
218        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
219        self.assertTrue(self.link in self.atom.relatedLinks)
220        self.assertTrue(link2 in self.atom.relatedLinks)
221
222        self.atom.addUniqueRelatedLinks(link2)
223        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
224       
225
226    def testAddMultipleUniqueRelatedLinks(self):
227        self.atom.fromString(self.xmlString)
228        linkNo = len(self.atom.relatedLinks)
229        self.atom.addUniqueRelatedLinks(self.link)
230        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
231        self.atom.addUniqueRelatedLinks(self.link)
232        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
233        self.atom.addUniqueRelatedLinks(self.link)
234        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
235
236    def testRemoveRelatedLinks(self):
237        self.atom.fromString(self.xmlString)
238        linkNo = len(self.atom.relatedLinks)
239        self.atom.addUniqueRelatedLinks(self.link)
240        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
241        self.assertTrue(self.link in self.atom.relatedLinks)
242        self.atom.removeRelatedLinks(self.link)
243        self.assertEqual(linkNo, len(self.atom.relatedLinks))
244        self.assertFalse(self.link in self.atom.relatedLinks)
245
246    def testRemoveMultipleRelatedLinks(self):
247        self.atom.fromString(self.xmlString)
248        linkNo = len(self.atom.relatedLinks)
249        link2 = copy.deepcopy(self.link)
250        link2.title = "blabby"
251        links = [self.link, link2]
252
253        self.atom.addUniqueRelatedLinks(links)
254        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
255        self.assertTrue(self.link in self.atom.relatedLinks)
256        self.assertTrue(link2 in self.atom.relatedLinks)
257
258        self.atom.removeRelatedLinks(links)
259        self.assertEqual(linkNo, len(self.atom.relatedLinks))
260        self.assertFalse(self.link in self.atom.relatedLinks)
261        self.assertFalse(link2 in self.atom.relatedLinks)
262
263    def testAddUniqueLinks(self):
264        data = []
265        links = [self.link, self.link2, self.link]
266        self.atom.addUniqueLinks(data, links)
267        self.assertEqual(2, len(data))
268
269    def testAddUniqueLinksWithInitialLinksDefined(self):
270        data = [self.link2]
271        links = [self.link, self.link2, self.link]
272        self.atom.addUniqueLinks(data, links)
273        self.assertEqual(2, len(data))
274       
275
276if __name__ == '__main__':
277
278    unittest.main()
Note: See TracBrowser for help on using the repository browser.