source: exist/trunk/python/ndgUtils/models/testexistdbclient.py @ 4220

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

Move the atom specific eXist db client code to ndgutils for re-use + move
associated tests.

Line 
1'''
2A test class for the eXistDBClient module.
3
4C Byrom Jun 2008
5Tessella
6'''
7import unittest
8import existdbclient as dbc
9from ndgUtils.eXistConnector import eXistConnector
10import unittests.testdata as data
11import time
12
13class testEXistDBClient(unittest.TestCase):
14   
15     # collection path for CSML docs
16    VALID_COLLECTION_PATH = eXistConnector.NDG_A_COLLECTION_PATH
17    VALID_FILE = "tst.xml"
18    VALID_FILE_PATH = VALID_COLLECTION_PATH + VALID_FILE
19    DBCONFIG_FILE = "exist.config"
20   
21    def setUp(self):
22        '''
23        set up data used in the tests.
24        '''
25        print "Setting up..."
26        self.tidyUp = False    #     switch to indicate to tearDown that we need to delete the created eXist file
27        self.dbc = dbc.eXistDBClient(self.DBCONFIG_FILE)
28       
29       
30    def testSetUpEXistCollections(self):
31        self.dbc._setUpEXistCollections()
32       
33    def testLoadDBDetailsValid(self):
34        self.dbc._loadDBDetails(self.DBCONFIG_FILE)
35        if not self.dbc._pw or not self.dbc.eXistDBHostname or not self.dbc._username:
36            fail("Did not manage to read in db config data")
37       
38    def testLoadDBDetailsInvalid(self):
39        self.assertRaises(ValueError,self.dbc._loadDBDetails, "blah" + self.DBCONFIG_FILE)
40
41    def testLookupEXistFileFail(self):
42        self.assertEquals(self.dbc._lookupEXistFile(self.VALID_FILE_PATH), None)
43
44    def testLookupEXistFileFind(self):
45        self.tidyUp = True
46        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
47        self.assertNotEquals(self.dbc._lookupEXistFile(self.VALID_FILE_PATH), None)
48       
49    def testIsNewEXistFile(self):
50        self.assertEquals(self.dbc.isNewEXistFile(self.VALID_FILE_PATH), True)
51
52    def testIsNewEXistFileBeforeAndAfterCreateFile(self):
53        self.tidyUp = True
54        self.assertEquals(self.dbc.isNewEXistFile(self.VALID_FILE_PATH), True)
55        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
56        self.assertEquals(self.dbc.isNewEXistFile(self.VALID_FILE_PATH), False)
57           
58    def testCreateEXistFile(self):
59        self.tidyUp = True
60        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
61       
62    def testCreateEXistFileDuplicate(self):
63        self.tidyUp = True
64        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
65        # NB, this just overwrites the file - so is valid here
66        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
67       
68    def testDeleteEXistFile(self):
69        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
70        self.assertEquals(self.dbc.deleteEXistFile(self.VALID_FILE_PATH), True)
71   
72    def testGetEXistFileFail(self):
73        self.assertEquals(self.dbc.getEXistFile(self.VALID_FILE_PATH), None)
74   
75    def testGetEXistFileSucceed(self):
76        self.tidyUp = True
77        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
78        self.assertNotEquals(self.dbc.getEXistFile(self.VALID_FILE_PATH), None)
79   
80    def testBackupEXistFile(self):
81        self.tidyUp = True
82        self.assertEquals(self.dbc.createEXistFile(data.testdata.xmlString, self.VALID_COLLECTION_PATH, self.VALID_FILE), True)
83        time.sleep(2)
84        backupFile = self.dbc.backupEXistFile(self.VALID_COLLECTION_PATH, self.VALID_FILE)
85        self.assertNotEquals(backupFile, None)
86        self.assertNotEquals(self.dbc.getEXistFile(backupFile), None)
87        self.dbc.deleteEXistFile(backupFile)
88   
89    def testBackupEXistFileMissing(self):
90        self.assertRaises(SystemError, self.dbc.backupEXistFile, self.VALID_COLLECTION_PATH, self.VALID_FILE)
91   
92    def tearDown(self):
93        if self.tidyUp:
94            # delete the test file, in case it has been used in a test
95            self.dbc.deleteEXistFile(self.VALID_FILE_PATH)
96
97if __name__ == '__main__':
98
99    unittest.main()
Note: See TracBrowser for help on using the repository browser.