source: exist/trunk/python/ndgUtils/test_xqueries.py @ 3361

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/test_xqueries.py@4025
Revision 3361, 7.5 KB checked in by lawrence, 12 years ago (diff)

BrowseServer? removed from xquery. Bug fix in listing query?

Line 
1
2##
3## This test file has the more sophisticated, data aware tests
4##
5
6import unittest
7from ndgXqueries import ndgXqueries
8from eXistInterface import ndg_eXist
9from ndgObject import ndgObject
10import ConfigParser
11from xmlrpclib import Fault
12from ndgUtils.elementtree import ElementTree as ET
13
14class TestCase(unittest.TestCase):
15   
16    def setUp(self): 
17        ''' Automatically run before test cases '''
18        f='test.config'
19        self.c=ConfigParser.ConfigParser()
20        self.c.read(f)
21        self.xq=ndgXqueries()
22        self.xmldb1=ndg_eXist(db='glue.badc.rl.ac.uk')
23        self.xmldb2=ndg_eXist(db='chinook.badc.rl.ac.uk')
24        self.testDElist=['badc.nerc.ac.uk__NDG-B1__dataent_COAPEC']
25   
26    def _logToFile(self,fname,xml):
27        ''' Writes some xml to a log file '''
28        f=file(fname,'w')
29        f.write(xml)
30        f.close()
31   
32    def _getmoles(self,uri,targetCollection,db):
33        ''' Returns a moles document by URI from targetCollection '''
34        n=ndgObject(uri)
35        xq=self.xq.actual('moles',targetCollection,n.repository,n.localID)
36        id,s=db.executeQuery(xq)
37        self.assertEqual(s['hits'],1,'Unable to obtain from database the document for unique id %s (hits=%s)'%(uri,s['hits']))
38        return db.retrieve(id,0)
39       
40    def test_DIFlist(self):
41        ''' Test the DIF listing query on glue '''
42        xq=self.xq['DIFList']
43        xq=xq.replace('TargetCollection','/db/discovery/original/DIF')
44        r=self.xmldb1.executeChunkedQuery(xq,1,1)
45        print r
46       
47    def test_molesList(self,dtype=None):
48        ''' Test the MOLES listing query on chinook '''
49       
50        def _getType(r):
51            moles='{http://ndg.nerc.ac.uk/moles}'
52            tree=ET.fromstring(str(r))
53            rid=tree.findtext('*/%srepositoryIdentifier'%moles)
54            lid=tree.findtext('*/%slocalIdentifier'%moles)
55            xq=self.xq.actual('molesObjectType',tc,rid,lid)
56            r=self.xmldb2.executeChunkedQuery(xq,1,1)
57            tree=ET.fromstring(str(r))
58            return int(tree.findtext('%sobjectType'%moles))
59       
60        xq=self.xq['molesList']
61        tc='/db/ndg_B_metadata/badc.nerc.ac.uk'
62        moles='{http://ndg.nerc.ac.uk/moles}'
63        xq=xq.replace('TargetCollection',tc)
64        print xq
65        #this returns the first one of
66        r=self.xmldb2.executeChunkedQuery(xq,1,1)
67        print r
68        #make sure it has got a dgDataEntity record (that's the default)
69        ot=_getType(r)
70        self.assertEqual(ot,4,'Data Entity should be 4 (is %s)'%ot)
71        # now let's convince ourselves that another sort of
72        # listing query should work, start by constructing the query
73        xq=self.xq['molesList']
74        xq=xq.replace('TargetCollection',tc)
75        xq=xq.replace('dgDataEntity','dgActivity')
76        # and deliver it
77        r=self.xmldb2.executeChunkedQuery(xq,1,1)
78        #and make sure it has the right type of entity
79        ot=_getType(r)
80        self.assertEqual(ot,1,'Activity should be 1 (is %s)'%ot)
81       
82    def test_moles2dif(self):
83        ''' Test the creation of a DIF file from rich moles content '''
84        uri=self.testDElist[0]
85        uri=uri.replace('NDG-B1','NDG-B0')
86        n=ndgObject(uri)
87        targetCollection='/db/ndg_B_metadata/%s'%n.repository
88       
89        #check it's there ...
90        xml=self._getmoles(uri,targetCollection,self.xmldb2)
91       
92        #ok, now let's try a moles2dif xquery
93        xquery=self.xq.actual('moles2dif',targetCollection,n.repository,n.localID)
94        # decided not to do this any more, so xquery should not have this any longer
95        #xquery=xquery.replace('BrowseServer',self.c.get('NDG_B_SERVICE',n.repository))
96        id,s=self.xmldb2.executeQuery(xquery)
97        print '%s hit in %s ms'%(s['hits'],s['queryTime']) 
98        self.assertEqual(1,s['hits'],'Should have had one dif document returned!')
99        dif_from_moles=self.xmldb2.retrieve(id,0)
100        self._logToFile('dif_from_moles.xml',dif_from_moles)
101        #now test that the b link does something sensible
102        etdif=ET.fromstring(dif_from_moles)
103        rurl=etdif.findall('Related_URL')
104        for url in rurl:
105            utext=uurl.find('URL').text or ''
106            self.failUnless('BrowseServer' not in utext)
107       
108
109    def atest_dif2molesRoundTrip(self):
110        # Actually we now know that the dif2moles code was very lightweight and doesn't
111        # do much, so expectations of round tripping have been given up ...
112        ''' Tests the dif 2 moles round tripping. Note that an issue is that the
113        personnel and organisation material gets handled oddly '''
114       
115        # start by choosing an original DIF
116        moles1=self.testDElist[0]
117        dif1=moles1.replace('NDG-B1','DIF')
118        n=ndgObject(dif1)
119       
120        # handling the DIF
121        targetCollection='/db/discovery/original/DIF/%s'%n.repository
122        difName=n.uri+'.xml'
123        dif1_original=self.xmldb1.getDoc(targetCollection,difName)
124        self.assertEqual(dif1_original[0:4],'<DIF','unable to load raw dif file %s'%difName)
125       
126        # now sort out the output ID stuff ...
127        xquery=self.xq.actual('dif2moles',targetCollection,n.repository,n.localID)
128        # and then sort out the input ID stuff
129        difid=n.uri.replace('__',':')
130        xquery=xquery.replace('Input_Entry_ID',difid)
131        xquery=xquery.replace('repository_localid','badc')
132        #xquery='''for $s in xmldb:get-child-collections('%s') return <collection>{$s}</collection>'''%targetCollection
133       
134        # and deliver the xquery ...
135        id,s=self.xmldb1.executeQuery(xquery)
136        self.assertEqual(1,s['hits'],'Should have had one moles document returned!')
137        moles_from_dif1=self.xmldb.retrieve(id,0)
138        self.xmldb1.release(id)
139       
140        # now load it back into exist
141        dummyCollection='/db/scratchUtilTest'
142        # but first check the dummyCollection doesn't exist, and if it does, get rid of it
143        xq='''for $s in xmldb:get-child-collections('/db') return <collection>/db/{$s}</collection>'''
144        id,s=self.xmldb1.executeQuery(xq)
145        collections=[self.xmldb1.retrieveNext(id,i) for i in range(s['hits'])]
146        self.xmldb1.release(id)
147        if '<collection>%s</collection>'%dummyCollection in collections: 
148            ok=self.xmldb1.removeCollection(dummyCollection)
149            self.assertEqual(True,ok,'We needed to delete the scratch collection!')
150        #ok, now we can goahed and create the collection and stuff the file into it
151        ok=self.xmldb1.createCollection(dummyCollection)
152        self.assertEqual(True,ok,'Unable to createCollection')
153        moles2=ndgObject(n.uri.replace('DIF','NDG-B0'))
154        dummyFile='%s/%s'%(dummyCollection,moles2.uri)
155        ok=self.xmldb1.storeXML(moles_from_dif1,dummyFile)
156        self.assertEqual(True,ok,'Unable to upload xml file')
157       
158        # now let's see what we get for a dif back from that moles document!
159        xquery=self.xq.actual('moles2dif',dummyCollection,moles2.repository,moles2.localID)
160        id,s=self.xmldb1.executeQuery(xquery)
161        self.assertEqual(1,s['hits'],'Should have had one dif document returned!')
162        dif1_from_moles=self.xmldb.retrieve(id,0)
163       
164        # are they the same? well, there is no guarantee that they should be
165        # in the same order if the schema used xsd:any rather than xsd:sequence ...
166        # so this simple test would fail
167        # self.assertEqual(dif1_from_moles,dif1_original)
168
169        self._logToFile('original_dif.xml',dif1_original)
170        self._logToFile('moles_dif.xml',dif1_from_moles)
171
172
173if __name__=="__main__":
174    unittest.main()
Note: See TracBrowser for help on using the repository browser.