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

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

The dif2moles round tripping is in place (but the comparison doesn't
work and needs analysis).

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
12
13class TestCase(unittest.TestCase):
14   
15    def setUp(self): 
16        f='test.config'
17        self.c=ConfigParser.ConfigParser()
18        self.c.read(f)
19        self.xq=ndgXqueries()
20        self.xmldb=ndg_eXist(db='glue.badc.rl.ac.uk')
21        self.testDElist=['badc.nerc.ac.uk__NDG-B1__dataent_COAPEC']
22   
23    def _getmoles(uri,targetCollection):
24        ''' Returns a moles document by URI from targetCollection '''
25        n=ndgObject(uri)
26        xq=self.xq.actual('moles',targetCollection,n.repository,n.localID)
27        id,s=self.xmldb.executeQuery(xq)
28        self.assertEqual(s['hits'],1,'Unable to get unique %s'%uri)
29        return self.retrieve(id,1)
30       
31    def test_DIFlist(self):
32        ''' Test the DIF listing query on glue '''
33        xq=self.xq['DIFList']
34        xq=xq.replace('TargetCollection','/db/discovery/original/DIF')
35        r=self.xmldb.executeChunkedQuery(xq,1,1)
36        print r
37
38    def test_moles2difRoundTrip(self):
39        ''' Tests the moles 2 dif round tripping '''
40        # start by choosing an existing NDG moles data entity
41        moles1=self.testDElist[0]
42        dif1=moles1.replace('NDG-B1','DIF')
43        n=ndgObject(dif1)
44       
45        # handling the DIF
46        targetCollection='/db/discovery/original/DIF/%s'%n.repository
47        difName=n.uri+'.xml'
48        dif1_original=self.xmldb.getDoc(targetCollection,difName)
49        self.assertEqual(dif1_original[0:4],'<DIF','unable to load raw dif file %s'%difName)
50       
51        # now sort out the output ID stuff ...
52        xquery=self.xq.actual('dif2moles',targetCollection,n.repository,n.localID)
53        # and then sort out the input ID stuff
54        difid=n.uri.replace('__',':')
55        xquery=xquery.replace('Input_Entry_ID',difid)
56        xquery=xquery.replace('repository_localid','badc')
57        #xquery='''for $s in xmldb:get-child-collections('%s') return <collection>{$s}</collection>'''%targetCollection
58       
59        # and deliver the xquery ...
60        id,s=self.xmldb.executeQuery(xquery)
61        self.assertEqual(1,s['hits'],'Should have had one moles document returned!')
62        moles_from_dif1=self.xmldb.retrieve(id,0)
63        self.xmldb.release(id)
64       
65        # now load it back into exist
66        dummyCollection='/db/scratchUtilTest'
67        # but first check the dummyCollection doesn't exist, and if it does, get rid of it
68        xq='''for $s in xmldb:get-child-collections('/db') return <collection>/db/{$s}</collection>'''
69        id,s=self.xmldb.executeQuery(xq)
70        collections=[self.xmldb.retrieveNext(id,i) for i in range(s['hits'])]
71        self.xmldb.release(id)
72        if '<collection>%s</collection>'%dummyCollection in collections: 
73            ok=self.xmldb.removeCollection(dummyCollection)
74            self.assertEqual(True,ok,'We needed to delete the scratch collection!')
75        #ok, now we can goahed and create the collection and stuff the file into it
76        ok=self.xmldb.createCollection(dummyCollection)
77        self.assertEqual(True,ok,'Unable to createCollection')
78        moles2=ndgObject(n.uri.replace('DIF','NDG-B0'))
79        dummyFile='%s/%s'%(dummyCollection,moles2.uri)
80        ok=self.xmldb.storeXML(moles_from_dif1,dummyFile)
81        self.assertEqual(True,ok,'Unable to upload xml file')
82       
83        # now let's see what we get for a dif back from that moles document!
84        xquery=self.xq.actual('moles2dif',dummyCollection,moles2.repository,moles2.localID)
85        id,s=self.xmldb.executeQuery(xquery)
86        self.assertEqual(1,s['hits'],'Should have had one dif document returned!')
87        dif1_from_moles=self.xmldb.retrieve(id,0)
88       
89        #are they the same?
90        self.assertEqual(dif1_from_moles,dif1_original)
91       
92       
93     
94if __name__=="__main__":
95    unittest.main()
Note: See TracBrowser for help on using the repository browser.