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

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

Upgrade the various 'list' xqueries - to nest all results in a single
root element - so that only one document need be retrieved to get all
results + adjust ndgDirectory to cope with processing the new results +
fix the various namespaces mentioned in the codebase to map to the
current atom/moles ones.

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
99       
100       
101    def testAddValidParameters(self):
102        self.atom.addParameters(self.validParams)
103        self.assertEqual(self.atom.parameters[0].term, getTripleData(self.validParams[0])[2])
104        self.assertEqual(self.atom.parameters[1].term, getTripleData(self.validParams[1])[2])
105        self.assertEqual(self.atom.parameters[2].term, getTripleData(self.validParams[2])[2])
106       
107       
108    def testAddMessyParameters(self):
109        self.atom.addParameters(self.messyParams)
110        self.assertEqual(self.atom.parameters[0].term, 'OZONE')
111        self.assertEqual(self.atom.parameters[1].term, 'ERROR')
112        self.assertEqual(self.atom.parameters[2].term, 'HYDROGEN &amp;')
113        self.assertEqual(self.atom.parameters[2].scheme, 'http://vocab.ndg.nerc.ac.uk/blah')
114        self.assertEqual(self.atom.parameters[2].label.lower(), 'Hydrogen Peroxide &lt; ppbv'.lower())
115       
116    def testFromString(self):
117        self.atom.fromString(self.xmlString)
118       
119        self.assertNotEqual(self.atom.author, None)
120        self.assertEqual(self.validAuthor, self.atom.author.name)
121       
122        self.assertEqual(len(self.atom.parameters), 4)
123       
124        for param in self.atom.parameters:
125            foundIt = False
126            for validParam in self.validParams:
127                if (validParam.startswith(param.term)):
128                    foundIt = True
129                    break
130           
131            self.assertEqual(True, foundIt, "Cound not find parameter (%s) in valid parameter list" %param)
132       
133        # currently only store max/min boundaries
134        #for spatial in self.atom.spatialData:
135        #    if not spatial in self.validSpatialData:
136        #        self.fail("Cound not find spatial data (%s) in valid coverage list" %spatial)
137        #
138        #for temporal in self.atom.temporalData:
139        #    if not temporal in self.validTemporalData:
140        #        self.fail("Cound not find temporal data (%s) in valid coverage list" %temporal)
141       
142        #self.assertEqual(len(self.atom.relatedLinks), 2)
143        for link in self.atom.relatedLinks:
144            foundIt = False
145            for validLink in self.validLinks:
146                if (validLink.startswith(link.href)):
147                    foundIt = True
148                    break
149           
150            self.assertEqual(True, foundIt, "Cound not find link (%s) in valid related link list" %link.href)
151
152        self.assertEqual(self.atom.summary[0], self.summaryText)
153        self.assertEqual(self.atom.csmlFile, self.csmlFile)
154        self.assertEqual(self.atom.cdmlFile, None)
155        self.assertEqual(self.atom.title, self.title)
156        print self.atom.logos
157        self.assertEqual(self.atom.logos[0].href, self.logo)
158        self.assertEqual(self.atom.datasetID, self.datasetID)
159        self.assertEqual(self.atom.minX, self.minX)
160        self.assertEqual(self.atom.minY, self.minY)
161        self.assertEqual(self.atom.maxX, self.maxX)
162        self.assertEqual(self.atom.maxY, self.maxY)
163        self.assertEqual(self.atom.t1, self.t1)
164        self.assertEqual(self.atom.t2, self.t2)
165       
166        # date when the granule was first ingested
167        if not self.atom.publishedDate:
168            self.fail("No published date found") 
169
170        self.assertEqual(self.atom.atomName,self.name)
171       
172
173    def testImportExport(self):
174        self.atom.fromString(self.xmlString)
175        xml = self.atom.toPrettyXML()
176        # NB, this is a naive test as ordering of sibling elements changes as does addition of new line characters
177        #self.assertEqual(xml, self.xmlString)
178
179    def testCreateWithInputDict(self):
180        inputs = {
181                  'atomTypeID': VTD.ACTIVITY_TERM,
182                  'title': self.title
183                  }
184        a = Atom(**inputs)
185        self.assertEqual(self.title, a.title)
186        self.assertEqual(VTD.ACTIVITY_TERM, a.atomTypeID)
187        self.assertEqual(VTD.TERM_DATA[VTD.ACTIVITY_TERM].title, a.atomTypeName)
188
189    def testaddUniqueRelatedLinks(self):
190        self.atom.fromString(self.xmlString)
191        linkNo = len(self.atom.relatedLinks)
192        self.atom.addUniqueRelatedLinks(self.link)
193        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
194        self.assertTrue(self.link in self.atom.relatedLinks)
195
196    def testAddMultipeDifferentRelatedLinks(self):
197        self.atom.fromString(self.xmlString)
198        linkNo = len(self.atom.relatedLinks)
199        self.atom.addUniqueRelatedLinks(self.link)
200        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
201        link2 = copy.deepcopy(self.link)
202        link2.title = "blabby"
203        self.atom.addUniqueRelatedLinks(link2)
204        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
205        self.atom.addUniqueRelatedLinks(link2)
206        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
207
208    def testAddMultipeDifferentRelatedLinksSimultaneously(self):
209        self.atom.fromString(self.xmlString)
210        linkNo = len(self.atom.relatedLinks)
211        link2 = copy.deepcopy(self.link)
212        link2.title = "blabby"
213        links = [self.link, link2]
214        self.atom.addUniqueRelatedLinks(links)
215        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
216        self.assertTrue(self.link in self.atom.relatedLinks)
217        self.assertTrue(link2 in self.atom.relatedLinks)
218
219        self.atom.addUniqueRelatedLinks(link2)
220        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
221       
222
223    def testAddMultipleUniqueRelatedLinks(self):
224        self.atom.fromString(self.xmlString)
225        linkNo = len(self.atom.relatedLinks)
226        self.atom.addUniqueRelatedLinks(self.link)
227        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
228        self.atom.addUniqueRelatedLinks(self.link)
229        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
230        self.atom.addUniqueRelatedLinks(self.link)
231        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
232
233    def testRemoveRelatedLinks(self):
234        self.atom.fromString(self.xmlString)
235        linkNo = len(self.atom.relatedLinks)
236        self.atom.addUniqueRelatedLinks(self.link)
237        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
238        self.assertTrue(self.link in self.atom.relatedLinks)
239        self.atom.removeRelatedLinks(self.link)
240        self.assertEqual(linkNo, len(self.atom.relatedLinks))
241        self.assertFalse(self.link in self.atom.relatedLinks)
242
243    def testRemoveMultipleRelatedLinks(self):
244        self.atom.fromString(self.xmlString)
245        linkNo = len(self.atom.relatedLinks)
246        link2 = copy.deepcopy(self.link)
247        link2.title = "blabby"
248        links = [self.link, link2]
249
250        self.atom.addUniqueRelatedLinks(links)
251        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
252        self.assertTrue(self.link in self.atom.relatedLinks)
253        self.assertTrue(link2 in self.atom.relatedLinks)
254
255        self.atom.removeRelatedLinks(links)
256        self.assertEqual(linkNo, len(self.atom.relatedLinks))
257        self.assertFalse(self.link in self.atom.relatedLinks)
258        self.assertFalse(link2 in self.atom.relatedLinks)
259       
260       
261
262if __name__ == '__main__':
263
264    unittest.main()
Note: See TracBrowser for help on using the repository browser.