source: ndgCommon/trunk/ndg/common/unittests/lib/testgranulite.py @ 4970

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/unittests/lib/testgranulite.py@4970
Revision 4970, 8.0 KB checked in by cbyrom, 11 years ago (diff)

Various fixes, tidy ups and simplications to ndgCommon codebase.

Line 
1'''
2A test class for the Granulite module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest, logging
8from ndg.common.src.lib.granulite import granulite
9import ndg.common.unittests.testconstants as tc
10from ndg.common.unittests.testutils import testUtils as tu
11from ndg.common.src.models.Atom import Atom
12from ndg.common.src.models.vocabtermdata import VocabTermData as VTD
13import ndg.common.src.lib.utilities as utilities
14import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
15from ndg.common.src.models.ndgObject import ndgObject as no
16
17class testGranulite(unittest.TestCase):
18
19    def setUp(self):
20        '''
21        set up data used in the tests.
22        '''
23        self.utils = tu(tc.EXIST_DBCONFIG_FILE)
24        self.g = granulite(tc.VALID_TEST_GRANULITE, eXistClient = self.utils.ac)
25
26    def createGranuliteDEs(self):
27        '''
28        Create the DEs referenced in the granulite file
29        '''
30        inputs = self.g._granulite__getGranuliteDetails()
31        self.DEs = []
32        for de in inputs['data_entity_id']:
33            data = utilities.getTripleData(de)
34            id = data[0]
35            atom = self.utils.createAtom(tc.xmlString)
36            atom.atomTypeID = VTD.DE_TERM
37            atom.setDatasetID(id)
38            atom = self.utils.ac.createAtom(atom)
39            self.DEs.append(atom)
40
41    def deleteGranuliteDEs(self):
42        '''
43        Delete the DEs referenced in the granulite file
44        '''
45        for de in self.DEs:
46            self.utils.deleteDoc(de.getFullPath())
47           
48       
49       
50    def testProcessGranulite(self):
51        atom = None
52        try:
53            # firstly, create the DEs referenced in the granulite
54            self.createGranuliteDEs()
55            atom = self.g.processGranulite()
56            self.assertNotEqual(None, atom)
57            self.assertEqual('-8.26171875', atom.minX)
58            self.assertEqual('-11.25', atom.minY)
59            self.assertEqual('-3.33984375', atom.maxX)
60            self.assertEqual('-17.578125', atom.maxY)
61
62        except Exception, e:
63            self.fail("An exception should not have been thrown")
64        finally:
65            if atom is not None:
66                self.utils.deleteDoc(atom.getFullPath())
67            self.deleteGranuliteDEs()
68       
69    def testProcessCSMLOrCDMLFile(self):
70        try:
71            atom = self.g.processCSMLOrCDMLFile()
72            self.assertNotEqual(None, atom)
73        except:
74            self.fail("An exception should not have been thrown")
75       
76       
77    def testGetSectionName(self):
78        sectionName = "parameters"
79        foundName = self.g._granulite__getSectionName(sectionName + "::")
80        self.assertEqual(sectionName, foundName)
81       
82    def testGetInvalidSectionName(self):
83        sectionName = "parameters"
84        foundName = self.g._granulite__getSectionName(sectionName)
85        self.assertEqual(None, foundName)
86       
87    def testInitialiseGranule(self):
88        self.g._granulite__initialiseGranule()
89        self.assertNotEqual(None, self.g._atom)
90       
91    def _initGranule(self):
92        self.g._granulite__initialiseGranule()
93        return self.g._granulite__getGranuliteDetails()
94       
95
96    def testGetGranuliteDetails(self):
97        details = self._initGranule()
98        self.assertEqual(tc.GRANULITE_TITLE, details['granule_info'][0].split('|')[2].strip())
99        self.assertEqual(tc.GRANULITE_TIME_AXIS, details['cdml_file'][0].split('|')[2].strip())
100        self.assertEqual(tc.GRANULITE_CDML_DATASETID, details['cdml_file'][0].split('|')[1].strip())
101
102    def testApplyCoreGranuliteDetails(self):
103        details = self._initGranule()
104        self.g.ingestGranuliteFiles = False # don't worry about reading in CSML/CDML data
105        self.g._granulite__applyCoreGranuliteDetails(details)
106        self.assertEqual(tc.GRANULITE_TITLE, self.g._atom.title)
107        self.assertEqual(tc.GRANULITE_DATASETID, self.g._atom.datasetID)
108        self.assertEqual(tc.GRANULITE_PROVIDER_ID, self.g._atom.ME.providerID)
109
110    def testApplyGranuliteDetails(self):
111        details = self._initGranule()
112        self.g.ingestGranuliteFiles = False # don't worry about reading in CSML/CDML data
113        self.g._granulite__applyGranuliteDetails(details)
114        self.assertEqual(tc.GRANULITE_LOGO, self.g._atom.getLogos()[0].href)
115        self.assertEquals(tc.GRANULITE_AUTHOR_NAME, self.g._atom.ME.responsibleParties[0].name)
116        self.assertEqual(self.g._atom.minX, '-8.26171875')
117        self.assertEqual(self.g._atom.minY, '-11.25')
118        self.assertEqual(self.g._atom.maxX, '-3.33984375')
119        self.assertEqual(self.g._atom.maxY, '-17.578125')
120        self.assertEqual(self.g._atom.t1, tc.GRANULITE_T1)
121        self.assertEqual(self.g._atom.t2, tc.GRANULITE_T2)
122       
123
124    def testExtractSpatialData(self):
125        self._initGranule()
126        self.g._extractSpatialData("POLYGON((-8.26171875 -11.25,-9.140625 -17.40234375,-3.33984375 -17.578125,-3.1640625 -12.65625,-8.26171875 -11.25))")
127        self.assertEqual(self.g._atom.minX, '-8.26171875')
128        self.assertEqual(self.g._atom.minY, '-11.25')
129        self.assertEqual(self.g._atom.maxX, '-3.33984375')
130        self.assertEqual(self.g._atom.maxY, '-17.578125')
131
132
133    def testUpdateDataEntity(self):
134        # firstly create a DE to use
135        try:
136            atom = Atom(VTD.DE_TERM)
137            atom.fromString(tc.xmlString)
138            atom.atomTypeID = VTD.DE_TERM
139            atom = self.utils.ac.createAtom(atom)
140   
141            # now set up the granulite
142            details = self._initGranule()
143            self.g.ingestGranuliteFiles = False # don't worry about reading in CSML/CDML data
144            self.g._granulite__applyCoreGranuliteDetails(details)
145           
146            # and now update DE
147            self.g._granulite__updateDataEntity(atom.datasetID, atom.ME.providerID)
148            atomPath = atom.getDefaultCollectionPath() + atom.atomName
149            self.assertEqual([], self.utils.ac.checkAtomSchemaCompliance(atomPath))
150   
151            xml = self.utils.ac.getNDGDoc(atom.ME.providerID, no.ATOM_DOC_TYPE,
152                                          tc.datasetID, 
153                                          targetCollection = dc.ATOM_COLLECTION_PATH)
154            updatedAtom = Atom(xmlString = xml)
155           
156            self.assertNotEqual(atom, updatedAtom)
157           
158            # there should be one extra link on the new atom
159            self.assertEqual(len(atom.relatedLinks) + 1, len(updatedAtom.relatedLinks))
160        except Exception, e:
161            self.fail("An exception shouldn't have been thrown")
162        finally:
163            self.utils.deleteDoc(atom.getFullPath())
164
165
166    def testAddGranuleToDataEntityRecords(self):
167        # firstly create a DE to use
168        atom = Atom(VTD.DE_TERM)
169        atom.fromString(tc.xmlString)
170        atom.atomTypeID = VTD.DE_TERM
171        atom = self.utils.ac.createAtom(atom)
172
173        # now set up the granulite
174        details = self._initGranule()
175        self.g.ingestGranuliteFiles = False # don't worry about reading in CSML/CDML data
176        self.g._granulite__applyCoreGranuliteDetails(details)
177       
178        # and now update DE
179        self.g._dataEntityIDs = [atom.datasetID]
180        self.g._granulite__addGranuleToDataEntityRecords()
181        atomPath = atom.getDefaultCollectionPath() + atom.atomName
182        self.assertEqual([], self.utils.ac.checkAtomSchemaCompliance(atomPath))
183
184        xml = self.utils.ac.getNDGDoc(atom.ME.providerID, no.ATOM_DOC_TYPE,
185                                      tc.datasetID, 
186                                      targetCollection = dc.ATOM_COLLECTION_PATH)
187        updatedAtom = Atom(xmlString = xml)
188        self.assertNotEqual(atom, updatedAtom)
189       
190        # there should be one extra link on the new atom
191        self.assertEqual(len(atom.relatedLinks) + 1, len(updatedAtom.relatedLinks))
192        self.utils.deleteDoc(atomPath)
193       
194       
195if __name__ == '__main__':
196
197    unittest.main()
Note: See TracBrowser for help on using the repository browser.