source: ndgCommon/trunk/ndg/common/unittests/clients/xmldb/eXist/ testrestclient.py @ 4978

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/unittests/clients/xmldb/eXist/ testrestclient.py@4978
Revision 4978, 7.3 KB checked in by cbyrom, 11 years ago (diff)

Add new test suite for RESTful client + update various other test classes to cope with changes to client + proxy uses.

Line 
1
2import unittest,os, xmlrpclib
3from ndg.common.src.clients.xmldb.eXist.restclient import *
4import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
5import ndg.common.unittests.testconstants as tc
6from ndg.common.unittests.testutils import testUtils as tu
7from ndg.common.src.lib.ndgresources import ndgResources
8import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
9from ndg.common.src.lib.utilities import loadConfigDetails
10
11class TestCase(unittest.TestCase): 
12
13
14    def setUp(self):
15        '''
16        set up data used in the tests.
17        '''
18        userid, password, host = loadConfigDetails(tc.EXIST_DBCONFIG_FILE, tc.EXIST_DB)
19        self.client = RESTClient(host, userid, password)
20
21        self.createdAtoms = []    # array to store paths to atoms created - in order to then delete them
22        self.utils = tu(tc.EXIST_DBCONFIG_FILE)
23
24
25    def testDescribeCollection(self):
26        info = self.client.describeCollection(dc.ATOM_COLLECTION_PATH)
27        self.assertEqual(dc.ATOM_COLLECTION_PATH, info['name'] + '/')
28        self.assertTrue(len(info['collections']) > 0)
29        self.assertTrue(info['owner'])
30        self.assertTrue(info['created'])
31        self.assertTrue(info['group'])
32        self.assertTrue(info['permissions'])
33
34    def testRunQuery(self):
35        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
36        self.createdAtoms.append(createdAtom.getFullPath())
37        res = ndgResources()
38        xquery = res.createXQuery('atomList', dc.ATOM_COLLECTION_PATH, "", "")
39        result = self.client.runQuery(xquery)[0]
40        self.assertTrue(len(result) > 0)
41        self.assertTrue(result.find('fileName') > 0)
42
43    def testInvalidRunQuery(self):
44        res = ndgResources()
45        result = self.client.runQuery(tc.INVALID_XQUERY)
46        self.assertEqual([], result)
47
48
49    def testValidRunQueryMultipleResults(self):
50        # NB, this test assumes there is at least one atom available in the exist db
51        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
52        self.createdAtoms.append(createdAtom.getFullPath())
53
54        res = ndgResources()
55        xquery = res.createXQuery('atom', dc.ATOM_COLLECTION_PATH, "", tc.datasetID)
56        docs = self.client.runQuery(xquery)
57        self.assertEqual(1, len(docs))
58        self.assertTrue(docs[0])
59       
60        atom2 = self.utils.createAtom(tc.xmlString)
61        atom2.ME.providerID = tc.GRANULITE_PROVIDER_ID2
62        atom2.setDatasetID(tc.datasetID)
63        path = atom2.getFullPath()
64        self.client.createDoc(atom2.toPrettyXML(), path)
65        self.createdAtoms.append(path)
66       
67        docs2 = self.client.runQuery(xquery)
68        self.assertEqual(2, len(docs2))
69        self.assertTrue(docs2[0])
70        self.assertTrue(docs2[1])
71        # NB, cannot guarantee order of results
72        self.assertTrue(docs[0].strip() == docs2[0].strip() or 
73                        docs[0].strip() == docs2[1].strip())
74
75
76    def testValidRunQueryMultipleResultsTwoResultsKeyword(self):
77        # NB, this test assumes there is at least one atom available in the exist db
78        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
79        self.createdAtoms.append(createdAtom.getFullPath())
80
81        res = ndgResources()
82        xquery = res.createXQuery('atom', dc.ATOM_COLLECTION_PATH, "", tc.datasetID)
83        docs = self.client.runQuery(xquery)
84        self.assertEqual(1, len(docs))
85        self.assertTrue(docs[0])
86       
87        atom2 = self.utils.createAtom(tc.xmlString)
88        atom2.ME.providerID = tc.GRANULITE_PROVIDER_ID2
89        atom2.setDatasetID(tc.datasetID)
90        path = atom2.getFullPath()
91        self.client.createDoc(atom2.toPrettyXML(), path)
92        self.createdAtoms.append(path)
93       
94        docs2 = self.client.runQuery(xquery)
95        self.assertEqual(2, len(docs2))
96        self.assertTrue(docs2[0])
97        self.assertTrue(docs2[1])
98        # NB, cannot guarantee order of results
99        self.assertTrue(docs[0].strip() == docs2[0].strip() or 
100                        docs[0].strip() == docs2[1].strip())
101
102        docs3 = self.client.runQuery(xquery, noResults = 2)
103        self.assertEqual(2, len(docs3))
104        self.assertTrue(docs3[0])
105        self.assertTrue(docs3[1])
106        # NB, cannot guarantee order of results
107        self.assertTrue(docs[0].strip() == docs3[0].strip() or 
108                        docs[0].strip() == docs3[1].strip())
109
110
111    def testValidRunQueryMultipleZeroResults(self):
112        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
113        self.createdAtoms.append(createdAtom.getFullPath())
114
115        res = ndgResources()
116        xquery = res.createXQuery('atom', dc.ATOM_COLLECTION_PATH, "", tc.datasetID)
117        docs = self.client.runQuery(xquery, noResults = 0)
118        self.assertEqual(0, len(docs))
119
120
121    def testValidRunQueryMultipleResultsOneResultKeyword(self):
122        # NB, this test assumes there is at least one atom available in the exist db
123        createdAtom = self.utils.createAtomInEXist(tc.xmlString)
124        self.createdAtoms.append(createdAtom.getFullPath())
125
126        res = ndgResources()
127        xquery = res.createXQuery('atom', dc.ATOM_COLLECTION_PATH, "", tc.datasetID)
128       
129        atom2 = self.utils.createAtom(tc.xmlString)
130        atom2.ME.providerID = tc.GRANULITE_PROVIDER_ID2
131        atom2.setDatasetID(tc.datasetID)
132        path = atom2.getFullPath()
133        self.client.createDoc(atom2.toPrettyXML(), path)
134        self.createdAtoms.append(path)
135       
136        docs2 = self.client.runQuery(xquery, noResults = 1)
137        self.assertEqual(1, len(docs2))
138        self.assertTrue(docs2[0])
139
140
141    def testCreateCollection(self):
142        self.client.removeCollection(tc.TEST_COLLECTION_PATH)
143        # NB this will create the actual collection; the createCollection() method
144        # for the REST client does nothing
145        self.assertTrue(self.client.createDoc(tc.xmlString, tc.TEST_FILE_PATH))
146        self.createdAtoms.append(tc.TEST_COLLECTION_PATH)
147       
148
149    def testRemoveCollection(self):
150        # NB this will create the actual collection; the createCollection() method
151        # for the REST client does nothing
152        self.assertTrue(self.client.createDoc(tc.xmlString, tc.TEST_FILE_PATH))
153        self.createdAtoms.append(tc.TEST_COLLECTION_PATH)
154
155    def testInvalidRemoveCollection(self):
156        self.assertFalse(self.client.removeCollection(tc.TEST_COLLECTION_PATH))
157   
158    def testCreateDoc(self):
159        self.assertTrue(self.client.createDoc(tc.xmlString, tc.TEST_FILE_PATH))
160        self.createdAtoms.append(tc.TEST_COLLECTION_PATH)
161   
162    def testCreateDocWithContentType(self):
163        self.assertTrue(self.client.createDoc(tc.xmlString, tc.TEST_FILE_PATH,
164                                              contentType = 'fred'))
165        self.createdAtoms.append(tc.TEST_COLLECTION_PATH)
166
167    def testRemoveDoc(self):
168        self.assertTrue(self.client.createDoc(tc.xmlString, tc.TEST_FILE_PATH))
169        self.assertTrue(self.client.removeDoc(tc.TEST_FILE_PATH))
170        self.assertTrue(self.client.removeCollection(tc.TEST_COLLECTION_PATH))
171
172    def testInvalidRemoveDoc(self):
173        self.assertFalse(self.client.removeDoc(tc.TEST_FILE_NAME))
174
175       
176    def tearDown(self):
177        for path  in self.createdAtoms:
178            # delete the test file, in case it has been used in a test
179            self.client.removeCollection(path)
180
181           
182if __name__=="__main__":
183    unittest.main()
Note: See TracBrowser for help on using the repository browser.