source: exist/trunk/python/ndgutilstests/models/testatom.py @ 4641

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

Improve test suite, allowing referenced artefacts to be properly set
up and removed before and after tests + set global logging + adjust
file references + add required data files + simplify and fix some tests.

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 ndgUtils.lib.utilities import getTripleData
11import ndgutilstests.testconstants as tc
12
13class testatom(unittest.TestCase):
14    VTD = VTD()
15   
16    def setUp(self):
17        '''
18        set up data used in the tests.
19        '''
20        print "Setting up..."
21        self.atom = Atom(VTD.GRANULE_TERM, vocabTermData = self.VTD)
22        self.link = Link()
23        self.link.title = 'fred'
24        self.link.href = 'www.fred.com'
25        self.link.rel = 'related'
26        self.link2 = Link()
27        self.link2.title = 'pete'
28        self.link2.href = 'www.pete.com'
29        self.link2.rel = 'self'
30
31               
32    def testAddValidParameters(self):
33        self.atom.addParameters(tc.validParams)
34        self.assertEqual(self.atom.parameters[0].term, getTripleData(tc.validParams[0])[2])
35        self.assertEqual(self.atom.parameters[1].term, getTripleData(tc.validParams[1])[2])
36        self.assertEqual(self.atom.parameters[2].term, getTripleData(tc.validParams[2])[2])
37       
38       
39    def testAddMessyParameters(self):
40        self.atom.addParameters(tc.messyParams)
41        self.assertEqual(self.atom.parameters[0].term, 'OZONE')
42        self.assertEqual(self.atom.parameters[1].term, 'ERROR')
43        self.assertEqual(self.atom.parameters[2].term, 'HYDROGEN &')
44        self.assertEqual(self.atom.parameters[2].scheme, 'http://vocab.ndg.nerc.ac.uk/blah')
45        self.assertEqual(self.atom.parameters[2].label.lower(), 'Hydrogen Peroxide < ppbv'.lower())
46       
47    def testFromString(self):
48        self.atom.fromString(tc.xmlString)
49       
50        self.assertNotEqual(self.atom.author, None)
51        self.assertEqual(tc.validAuthor, self.atom.author.name)
52        self.assertEqual(3, len(self.atom.parameters))
53       
54        for param in self.atom.parameters:
55            foundIt = False
56            for validParam in tc.validParams:
57                if (validParam.startswith(param.term)):
58                    foundIt = True
59                    break
60           
61            self.assertEqual(True, foundIt, "Cound not find parameter (%s) in valid parameter list" %param)
62       
63        # currently only store max/min boundaries
64        #for spatial in self.atom.spatialData:
65        #    if not spatial in self.validSpatialData:
66        #        self.fail("Cound not find spatial data (%s) in valid coverage list" %spatial)
67        #
68        #for temporal in self.atom.temporalData:
69        #    if not temporal in self.validTemporalData:
70        #        self.fail("Cound not find temporal data (%s) in valid coverage list" %temporal)
71       
72        #self.assertEqual(len(self.atom.relatedLinks), 2)
73        for link in self.atom.relatedLinks:
74            foundIt = False
75            for validLink in tc.validLinks:
76                if (validLink.startswith(link.href)):
77                    foundIt = True
78                    break
79           
80            self.assertEqual(True, foundIt, "Cound not find link (%s) in valid related link list" %link.href)
81
82        self.assertEqual(tc.summaryText, self.atom.summary[0])
83        self.assertEqual(tc.csmlFile, self.atom.csmlFile)
84        self.assertEqual(None, self.atom.cdmlFile)
85        self.assertEqual(tc.title, self.atom.title)
86        self.assertEqual(tc.logo, self.atom.getLogos()[0].href)
87        self.assertEqual(tc.datasetID, self.atom.datasetID)
88        self.assertEqual(tc.minX, self.atom.minX)
89        self.assertEqual(tc.minY, self.atom.minY)
90        self.assertEqual(tc.maxX, self.atom.maxX)
91        self.assertEqual(tc.maxY, self.atom.maxY)
92        self.assertEqual(tc.T1, self.atom.t1)
93        self.assertEqual(tc.T2, self.atom.t2)
94       
95        # date when the granule was first ingested
96        if not self.atom.publishedDate:
97            self.fail("No published date found") 
98
99        self.assertEqual(tc.name, self.atom.atomName)
100       
101
102    def testImportExport(self):
103        self.atom.fromString(tc.xmlString)
104        xml = self.atom.toPrettyXML()
105        # NB, this is a naive test as ordering of sibling elements changes as does addition of new line characters
106        #self.assertEqual(xml, self.xmlString)
107
108    def testCreateWithInputDict(self):
109        inputs = {
110                  'atomTypeID': VTD.ACTIVITY_TERM,
111                  'title': tc.title
112                  }
113        a = Atom(**inputs)
114        self.assertEqual(tc.title, a.title)
115        self.assertEqual(VTD.ACTIVITY_TERM, a.atomTypeID)
116        self.assertEqual(VTD.TERM_DATA[VTD.ACTIVITY_TERM].title, a.atomTypeName)
117
118    def testaddUniqueRelatedLinks(self):
119        self.atom.fromString(tc.xmlString)
120        linkNo = len(self.atom.relatedLinks)
121        self.atom.addUniqueRelatedLinks(self.link)
122        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
123        self.assertTrue(self.link in self.atom.relatedLinks)
124
125    def testAddMultipeDifferentRelatedLinks(self):
126        self.atom.fromString(tc.xmlString)
127        linkNo = len(self.atom.relatedLinks)
128        self.atom.addUniqueRelatedLinks(self.link)
129        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
130        link2 = copy.deepcopy(self.link)
131        link2.title = "blabby"
132        self.atom.addUniqueRelatedLinks(link2)
133        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
134        self.atom.addUniqueRelatedLinks(link2)
135        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
136
137    def testAddMultipeDifferentRelatedLinksSimultaneously(self):
138        self.atom.fromString(tc.xmlString)
139        linkNo = len(self.atom.relatedLinks)
140        link2 = copy.deepcopy(self.link)
141        link2.title = "blabby"
142        links = [self.link, link2]
143        self.atom.addUniqueRelatedLinks(links)
144        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
145        self.assertTrue(self.link in self.atom.relatedLinks)
146        self.assertTrue(link2 in self.atom.relatedLinks)
147
148        self.atom.addUniqueRelatedLinks(link2)
149        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
150       
151
152    def testAddMultipleUniqueRelatedLinks(self):
153        self.atom.fromString(tc.xmlString)
154        linkNo = len(self.atom.relatedLinks)
155        self.atom.addUniqueRelatedLinks(self.link)
156        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
157        self.atom.addUniqueRelatedLinks(self.link)
158        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
159        self.atom.addUniqueRelatedLinks(self.link)
160        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
161
162    def testRemoveRelatedLinks(self):
163        self.atom.fromString(tc.xmlString)
164        linkNo = len(self.atom.relatedLinks)
165        self.atom.addUniqueRelatedLinks(self.link)
166        self.assertEqual(linkNo + 1, len(self.atom.relatedLinks))
167        self.assertTrue(self.link in self.atom.relatedLinks)
168        self.atom.removeRelatedLinks(self.link)
169        self.assertEqual(linkNo, len(self.atom.relatedLinks))
170        self.assertFalse(self.link in self.atom.relatedLinks)
171
172    def testRemoveMultipleRelatedLinks(self):
173        self.atom.fromString(tc.xmlString)
174        linkNo = len(self.atom.relatedLinks)
175        link2 = copy.deepcopy(self.link)
176        link2.title = "blabby"
177        links = [self.link, link2]
178
179        self.atom.addUniqueRelatedLinks(links)
180        self.assertEqual(linkNo + 2, len(self.atom.relatedLinks))
181        self.assertTrue(self.link in self.atom.relatedLinks)
182        self.assertTrue(link2 in self.atom.relatedLinks)
183
184        self.atom.removeRelatedLinks(links)
185        self.assertEqual(linkNo, len(self.atom.relatedLinks))
186        self.assertFalse(self.link in self.atom.relatedLinks)
187        self.assertFalse(link2 in self.atom.relatedLinks)
188
189    def testAddUniqueLinks(self):
190        data = []
191        links = [self.link, self.link2, self.link]
192        self.atom.addUniqueLinks(data, links)
193        self.assertEqual(2, len(data))
194
195    def testAddUniqueLinksWithInitialLinksDefined(self):
196        data = [self.link2]
197        links = [self.link, self.link2, self.link]
198        self.atom.addUniqueLinks(data, links)
199        self.assertEqual(2, len(data))
200       
201
202if __name__ == '__main__':
203
204    unittest.main()
Note: See TracBrowser for help on using the repository browser.