source: exist/trunk/python/ndgutilstests/lib/testexistdbclient.py @ 4783

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

Extend test suites to test new feed and DIF creation functionality.

Line 
1'''
2A test class for the eXistDBClient module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest, time
8import ndgUtils.lib.existdbclient as dbc
9from ndgUtils.eXistConnector import eXistConnector as ec
10import unittests.testdata as data
11from ndgUtils.ndgXqueries import ndgXqueries
12from ndgUtils.models.Atom import Atom
13from ndgUtils.models import AtomState
14from ndgUtils.models.vocabtermdata import VocabTermData as VTD
15import ndgutilstests.testconstants as tc
16from ndgutilstests.testutils import testUtils as tu
17
18class testEXistDBClient(unittest.TestCase):
19   
20     # collection path for CSML docs
21    VALID_COLLECTION_PATH = ec.BASE_COLLECTION_PATH
22    VALID_FILE = "tst.xml"
23    VALID_FILE_PATH = VALID_COLLECTION_PATH + VALID_FILE
24    INVALID_PATH = 'blah'
25    DUMMY_COLLECTION = VALID_COLLECTION_PATH + INVALID_PATH
26   
27    def setUp(self):
28        '''
29        set up data used in the tests.
30        '''
31        self.tidyUp = False    #     switch to indicate to tearDown that we need to delete the created eXist file
32        self.utils = tu(tc.DBCONFIG_FILE)
33
34    def testCreateOrUpdateEXistFile(self):
35        self.tidyUp = True
36        try:
37            self.utils.dbc.createOrUpdateEXistFile(data.testdata.xmlString, \
38                                             self.VALID_COLLECTION_PATH, \
39                                             self.VALID_FILE)
40            self.utils.dbc.createOrUpdateEXistFile(data.testdata.xmlString, \
41                                             self.VALID_COLLECTION_PATH, \
42                                             self.VALID_FILE)
43        except:
44            self.fail("An exception should not have been thrown here")
45       
46
47    def testAddTimeStamp(self):
48        file = self.utils.dbc._eXistDBClient__addTimeStamp(self.VALID_FILE)
49        self.assertTrue(file.startswith(self.VALID_FILE.split('.')[0]))
50        self.assertTrue(file.endswith(self.VALID_FILE.split('.')[1]))
51        self.assertNotEqual(file, self.VALID_FILE)
52       
53
54    def testRemoveTimeStamp(self):
55        file = self.utils.dbc._eXistDBClient__addTimeStamp(self.VALID_FILE)
56        file = self.utils.dbc._eXistDBClient__removeTimeStamp(file)
57        self.assertEqual(self.VALID_FILE, file)
58       
59    def testValidCheckAtomSchemaCompliance(self):
60        atom = self.utils.createAtomInEXist(tc.xmlString)
61        atomPath = atom.getDefaultCollectionPath() + atom.atomName
62        self.assertEqual([], self.utils.dbc.checkAtomSchemaCompliance(atomPath))
63        self.utils.dbc.deleteAtomInExist(atom)
64       
65    def testInvalidCheckAtomSchemaCompliance(self):
66        atom = self.utils.createAtomInEXist(tc.invalidXmlString)
67        atomPath = atom.getDefaultCollectionPath() + atom.atomName
68        errors = self.utils.dbc.checkAtomSchemaCompliance(atomPath)
69        self.assertEqual(1, len(errors))
70        self.assertTrue(errors[0].startswith(" Invalid content was found starting with element 'contributor'"))
71        self.utils.dbc.deleteAtomInExist(atom)
72       
73    def testInvalidEmailCheckAtomSchemaCompliance(self):
74        atom = self.utils.createAtomInEXist(tc.invalidEmailXmlString)
75        atomPath = atom.getDefaultCollectionPath() + atom.atomName
76        errors = self.utils.dbc.checkAtomSchemaCompliance(atomPath)
77        self.assertEqual(2, len(errors))
78        self.assertTrue(errors[1].find("of element 'email' is not valid") > -1)
79        self.utils.dbc.deleteAtomInExist(atom)
80
81    def testValidCheckAtomSchemaComplianceWithKeyword(self):
82        atom = self.utils.createAtom(tc.xmlString)
83        self.assertEqual([], self.utils.dbc.checkAtomSchemaCompliance('', atom = atom))
84
85    def testInValidCheckAtomSchemaCompliance(self):
86        atom = self.utils.createAtom(tc.xmlString)
87        atom.author.name = ''
88        self.assertNotEqual([], self.utils.dbc.checkAtomSchemaCompliance('', atom = atom))
89       
90    def testCreateNoneAtomInExist(self):
91        self.assertRaises(ValueError,self.utils.dbc.createAtomInExist, None)
92
93    def testCreateNotAtomInExist(self):
94        self.assertRaises(ValueError,self.utils.dbc.createAtomInExist, [1,2,4])
95       
96    def testGetAtomFail(self):
97        self.assertRaises(ValueError, self.utils.dbc.getAtom, self.INVALID_PATH)
98
99    def testCreateAtomInExist(self):
100        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
101        self.assertNotEquals(None, createdAtom)
102        self.utils.dbc.deleteAtomInExist(createdAtom)
103
104    def testGetAtom(self):
105        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
106        self.assertNotEquals(None, createdAtom)
107        self.assertNotEquals(None, self.utils.dbc.getAtom(createdAtom.datasetID))
108        self.utils.dbc.deleteAtomInExist(createdAtom)
109
110
111    def testLoadCollectionData(self):
112        newClient = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE, loadCollectionData = True)
113        self.assertNotEquals(None, newClient.collections)
114
115
116    def testNotLoadCollectionData(self):
117        self.assertEquals(None, self.utils.dbc.collections)
118
119
120    def testGetSchema(self):
121        self.assertNotEquals(None, self.utils.dbc.AtomSchema) 
122       
123   
124    def testCreateCollections(self): 
125        self.assertEquals(False, self.utils.dbc.xmldb.removeCollection(self.DUMMY_COLLECTION))
126        self.utils.dbc.createCollections([self.DUMMY_COLLECTION]) 
127        self.assertEquals(True, self.utils.dbc.xmldb.removeCollection(self.DUMMY_COLLECTION))
128       
129
130    def testSetUpEXistDB(self):
131        molesSchema = self.VALID_COLLECTION_PATH + \
132                ndgXqueries.ATOM_MOLES_SCHEMA  + '.xsd'
133        self.utils.dbc.deleteEXistFile(molesSchema)
134        self.assertEquals(None, self.utils.dbc._eXistDBClient__lookupEXistFile(molesSchema))
135
136        newClient = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE, setUpDB = True)
137        self.assertNotEquals(None, newClient._eXistDBClient__lookupEXistFile(molesSchema))
138
139
140    def testNotSetUpEXistDB(self):
141        molesSchema = self.VALID_COLLECTION_PATH + \
142                ndgXqueries.ATOM_MOLES_SCHEMA  + '.xsd'
143        self.utils.dbc.deleteEXistFile(molesSchema)
144        self.assertEquals(None, self.utils.dbc._eXistDBClient__lookupEXistFile(molesSchema))
145
146        newClient = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE)
147        self.assertEquals(None, newClient._eXistDBClient__lookupEXistFile(molesSchema))
148       
149    def testSetUpEXistCollections(self):
150        try:
151            self.utils.dbc._eXistDBClient__setUpEXistAtomCollections()
152        except:
153            self.fail("Should not have thrown an exception")
154       
155    def testLoadDBDetailsValid(self):
156        self.utils.dbc._eXistDBClient__loadDBDetails(tc.DBCONFIG_FILE)
157        if not self.utils.dbc._pw or not self.utils.dbc.eXistDBHostname or not self.utils.dbc._username:
158            self.fail("Did not manage to read in db config data")
159       
160    def testLoadDBDetailsInvalid(self):
161        self.assertRaises(ValueError,self.utils.dbc._eXistDBClient__loadDBDetails, "blah" + tc.DBCONFIG_FILE)
162
163    def testLookupEXistFileFail(self):
164        self.assertEquals(self.utils.dbc._eXistDBClient__lookupEXistFile(self.VALID_FILE_PATH), None)
165
166    def testLookupEXistFileFind(self):
167        self.tidyUp = True
168        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
169        self.assertNotEquals(self.utils.dbc._eXistDBClient__lookupEXistFile(self.VALID_FILE_PATH), None)
170       
171    def testIsNewEXistFile(self):
172        self.assertEquals(self.utils.dbc.isNewEXistFile(self.VALID_FILE_PATH), True)
173
174    def testIsNewEXistFileBeforeAndAfterCreateFile(self):
175        self.tidyUp = True
176        self.assertEquals(self.utils.dbc.isNewEXistFile(self.VALID_FILE_PATH), True)
177        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
178        self.assertEquals(self.utils.dbc.isNewEXistFile(self.VALID_FILE_PATH), False)
179           
180    def testCreateEXistFile(self):
181        self.tidyUp = True
182        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
183       
184    def testCreateEXistFileDuplicate(self):
185        self.tidyUp = True
186        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
187        # NB, this just overwrites the file - so is valid here
188        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
189       
190    def testDeleteEXistFile(self):
191        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
192        self.assertEquals(self.utils.dbc.deleteEXistFile(self.VALID_FILE_PATH), True)
193   
194    def testGetEXistFileFail(self):
195        self.assertEquals(self.utils.dbc.getEXistFile(self.VALID_FILE_PATH), None)
196   
197    def testGetEXistFileSucceed(self):
198        self.tidyUp = True
199        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
200        self.assertNotEquals(self.utils.dbc.getEXistFile(self.VALID_FILE_PATH), None)
201   
202    def testBackupEXistFile(self):
203        self.tidyUp = True
204        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
205        backupFile = self.utils.dbc.backupEXistFile(self.VALID_COLLECTION_PATH, self.VALID_FILE)
206        time.sleep(2)
207        self.assertNotEquals(backupFile, None)
208        self.assertNotEquals(self.utils.dbc.getEXistFile(backupFile), None)
209        self.utils.dbc.deleteEXistFile(backupFile)
210   
211    def testBackupEXistFileMissing(self):
212        self.assertRaises(SystemError, self.utils.dbc.backupEXistFile, self.VALID_COLLECTION_PATH, self.VALID_FILE)
213           
214    def testGetAtomFileCollectionPath(self):
215        self.tidyUp = True
216        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
217        self.utils.dbc.getAtomFileCollectionPath(data.testdata.id)
218           
219    def testGetAtomFileCollectionPath(self):
220        self.assertEquals(None, self.utils.dbc.getAtomFileCollectionPath(self.INVALID_PATH))
221           
222    def testGetAllAtomIDs(self):
223        self.tidyUp = True
224        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
225        ids = self.utils.dbc.getAllAtomIDs()
226        self.assertNotEquals(0, len(ids))
227        self.assertNotEquals(None, ids[0])
228           
229    def testGetAllAtomCollections(self):
230        self.tidyUp = True
231        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
232        ids = self.utils.dbc.getAllAtomCollections()
233        self.assertNotEquals(0, len(ids))
234        self.assertNotEquals({}, ids)
235           
236    def testRunAsynchAtomPublish(self):
237        atom = self.utils.createAtom(tc.xmlString)
238        atom.state = AtomState.PUBLISHED_STATE
239        providerCollection = ec.PROVIDER_FEED_PATH + atom.ME.providerID
240        if self.utils.dbc.isNewEXistCollection(providerCollection):
241            self.utils.dbc.createCollections([providerCollection])
242            self.utils.dbc.feedClient.createAtomFeed(providerCollection,
243                           self.utils.dbc.feedClient.PROVIDERLEVEL_ATOM_FEED_TITLE %atom.ME.providerID)
244        self.utils.dbc.runAsynchAtomPublish(atom)
245
246    def testRestoreBackup(self):
247        atom = self.utils.createAtomInEXist(tc.xmlString)
248        atom = self.utils.createAtomInEXist(tc.xmlString)
249        backupPath = self.utils.dbc.backupName
250        self.utils.dbc.deleteAtomInExist(atom)
251        atomPath = atom.getDefaultCollectionPath() + atom.atomName
252        self.assertTrue(self.utils.dbc.isNewEXistFile(atomPath))
253        self.utils.dbc.restoreBackup(backupPath)
254        self.assertFalse(self.utils.dbc.isNewEXistFile(atomPath))
255        self.utils.dbc.deleteAtomInExist(atom)
256
257    def testInvalidRestoreBackup(self):
258        self.assertRaises(SystemError, self.utils.dbc.restoreBackup, self.DUMMY_COLLECTION)
259
260    def testCreateDIFDocumentFromAtom(self):
261        atom = self.utils.createAtom(tc.xmlString)
262        atom.atomTypeID = VTD.DE_TERM
263        atom = self.utils.dbc.createAtomInExist(atom)
264        self.utils.dbc.createDIFDocumentFromAtom(atom)
265        self.utils.dbc.deleteAtomInExist(atom)
266       
267    def tearDown(self):
268        if self.tidyUp:
269            # delete the test file, in case it has been used in a test
270            self.utils.dbc.deleteEXistFile(self.VALID_FILE_PATH)
271
272if __name__ == '__main__':
273
274    unittest.main()
Note: See TracBrowser for help on using the repository browser.