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

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

Improve testing of existdbclient - adjust how new atoms are stored
and removed - so that the DB is left in a reliably consistent state.

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.createdAtoms = []    # array to store paths to atoms created - in order to then delete them
32        self.utils = tu(tc.DBCONFIG_FILE)
33
34    def testCreateOrUpdateEXistFile(self):
35        try:
36            self.utils.dbc.createOrUpdateEXistFile(data.testdata.xmlString, \
37                                             self.VALID_COLLECTION_PATH, \
38                                             self.VALID_FILE)
39            self.createdAtoms.append(self.VALID_FILE_PATH)
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        self.createdAtoms.append(atom.getFullPath())
62        atomPath = atom.getDefaultCollectionPath() + atom.atomName
63        self.assertEqual([], self.utils.dbc.checkAtomSchemaCompliance(atomPath))
64       
65    def testInvalidCheckAtomSchemaCompliance(self):
66        atom = self.utils.createAtomInEXist(tc.invalidXmlString)
67        self.createdAtoms.append(atom.getFullPath())
68        atomPath = atom.getFullPath()
69        errors = self.utils.dbc.checkAtomSchemaCompliance(atomPath)
70        self.assertEqual(1, len(errors))
71        self.assertTrue(errors[0].startswith(" Invalid content was found starting with element 'contributor'"))
72       
73    def testInvalidEmailCheckAtomSchemaCompliance(self):
74        atom = self.utils.createAtomInEXist(tc.invalidEmailXmlString)
75        self.createdAtoms.append(atom.getFullPath())
76        atomPath = atom.getFullPath()
77        errors = self.utils.dbc.checkAtomSchemaCompliance(atomPath)
78        self.assertEqual(2, len(errors))
79        self.assertTrue(errors[1].find("of element 'email' is not valid") > -1)
80
81    def testValidCheckAtomSchemaComplianceWithKeyword(self):
82        atom = self.utils.createAtom(tc.xmlString)
83        self.createdAtoms.append(atom.getFullPath())
84        self.assertEqual([], self.utils.dbc.checkAtomSchemaCompliance('', atom = atom))
85
86    def testInValidCheckAtomSchemaCompliance(self):
87        atom = self.utils.createAtom(tc.xmlString)
88        atom.author.name = ''
89        self.assertNotEqual([], self.utils.dbc.checkAtomSchemaCompliance('', atom = atom))
90       
91    def testCreateNoneAtomInExist(self):
92        self.assertRaises(ValueError,self.utils.dbc.createAtomInExist, None)
93
94    def testCreateNotAtomInExist(self):
95        self.assertRaises(ValueError,self.utils.dbc.createAtomInExist, [1,2,4])
96       
97    def testGetAtomFail(self):
98        self.assertRaises(ValueError, self.utils.dbc.getAtom, self.INVALID_PATH)
99
100    def testCreateAtomInExist(self):
101        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
102        self.createdAtoms.append(createdAtom.getFullPath())
103        self.assertNotEquals(None, createdAtom)
104
105    def testGetAtom(self):
106        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
107        self.createdAtoms.append(createdAtom.getFullPath())
108        self.assertNotEquals(None, createdAtom)
109        self.assertNotEquals(None, self.utils.dbc.getAtom(createdAtom.datasetID))
110
111
112    def testLoadCollectionData(self):
113        newClient = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE, loadCollectionData = True)
114        self.assertNotEquals(None, newClient.collections)
115
116
117    def testNotLoadCollectionData(self):
118        self.assertEquals(None, self.utils.dbc.collections)
119
120
121    def testGetSchema(self):
122        self.assertNotEquals(None, self.utils.dbc.AtomSchema) 
123       
124   
125    def testCreateCollections(self): 
126        self.assertEquals(False, self.utils.dbc.xmldb.removeCollection(self.DUMMY_COLLECTION))
127        self.utils.dbc.createCollections([self.DUMMY_COLLECTION]) 
128        self.assertEquals(True, self.utils.dbc.xmldb.removeCollection(self.DUMMY_COLLECTION))
129       
130
131    def testSetUpEXistDB(self):
132        molesSchema = self.VALID_COLLECTION_PATH + \
133                ndgXqueries.ATOM_MOLES_SCHEMA  + '.xsd'
134        self.utils.dbc.deleteEXistFile(molesSchema)
135        self.assertEquals(None, self.utils.dbc._eXistDBClient__lookupEXistFile(molesSchema))
136
137        newClient = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE, setUpDB = True)
138        self.assertNotEquals(None, newClient._eXistDBClient__lookupEXistFile(molesSchema))
139
140
141    def testNotSetUpEXistDB(self):
142        molesSchema = self.VALID_COLLECTION_PATH + \
143                ndgXqueries.ATOM_MOLES_SCHEMA  + '.xsd'
144        self.utils.dbc.deleteEXistFile(molesSchema)
145        self.assertEquals(None, self.utils.dbc._eXistDBClient__lookupEXistFile(molesSchema))
146
147        newClient = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE)
148        self.assertEquals(None, newClient._eXistDBClient__lookupEXistFile(molesSchema))
149       
150    def testSetUpEXistCollections(self):
151        try:
152            self.utils.dbc._eXistDBClient__setUpEXistAtomCollections()
153        except:
154            self.fail("Should not have thrown an exception")
155       
156    def testLoadDBDetailsValid(self):
157        self.utils.dbc._eXistDBClient__loadDBDetails(tc.DBCONFIG_FILE)
158        if not self.utils.dbc._pw or not self.utils.dbc.eXistDBHostname or not self.utils.dbc._username:
159            self.fail("Did not manage to read in db config data")
160       
161    def testLoadDBDetailsInvalid(self):
162        self.assertRaises(ValueError,self.utils.dbc._eXistDBClient__loadDBDetails, "blah" + tc.DBCONFIG_FILE)
163
164    def testLookupEXistFileFail(self):
165        self.assertEquals(self.utils.dbc._eXistDBClient__lookupEXistFile(self.VALID_FILE_PATH), None)
166
167    def testLookupEXistFileFind(self):
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.assertEquals(self.utils.dbc.isNewEXistFile(self.VALID_FILE_PATH), True)
176        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
177        self.createdAtoms.append(self.VALID_FILE_PATH)
178        self.assertEquals(self.utils.dbc.isNewEXistFile(self.VALID_FILE_PATH), False)
179           
180    def testCreateEXistFile(self):
181        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
182        self.createdAtoms.append(self.VALID_FILE_PATH)
183       
184    def testCreateEXistFileDuplicate(self):
185        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
186        self.createdAtoms.append(self.VALID_FILE_PATH)
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.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
199        self.createdAtoms.append(self.VALID_FILE_PATH)
200        self.assertNotEquals(self.utils.dbc.getEXistFile(self.VALID_FILE_PATH), None)
201   
202    def testBackupEXistFile(self):
203        self.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
204        self.createdAtoms.append(self.VALID_FILE_PATH)
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.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
216        self.createdAtoms.append(self.VALID_FILE_PATH)
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.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
224        self.createdAtoms.append(self.VALID_FILE_PATH)
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.assertEquals(self.utils.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
231        self.createdAtoms.append(self.VALID_FILE_PATH)
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
247    def testRestoreBackup(self):
248        atom = self.utils.createAtomInEXist(tc.xmlString)
249        self.createdAtoms.append(self.VALID_FILE_PATH)
250        atom = self.utils.createAtomInEXist(tc.xmlString)
251        backupPath = self.utils.dbc.backupName
252        self.utils.dbc.deleteAtomInExist(atom)
253        atomPath = atom.getDefaultCollectionPath() + atom.atomName
254        self.assertTrue(self.utils.dbc.isNewEXistFile(atomPath))
255        self.utils.dbc.restoreBackup(backupPath)
256        self.assertFalse(self.utils.dbc.isNewEXistFile(atomPath))
257        self.utils.dbc.deleteAtomInExist(atom)
258
259    def testInvalidRestoreBackup(self):
260        self.assertRaises(SystemError, self.utils.dbc.restoreBackup, self.DUMMY_COLLECTION)
261
262    def testCreateDIFDocumentFromAtom(self):
263        atom = self.utils.createAtom(tc.xmlString)
264        atom.atomTypeID = VTD.DE_TERM
265        atom = self.utils.dbc.createAtomInExist(atom)
266        self.createdAtoms.append(atom.getFullPath())
267        self.utils.dbc.createDIFDocumentFromAtom(atom)
268       
269    def tearDown(self):
270        for path  in self.createdAtoms:
271            # delete the test file, in case it has been used in a test
272            self.utils.deleteAtomInExist(path)
273
274if __name__ == '__main__':
275
276    unittest.main()
Note: See TracBrowser for help on using the repository browser.