source: exist/trunk/python/ndgutilstests/test_ndgRetrieve.py @ 4641

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

Improve test suite, allowing referenced artefacts to be properly set
up and removed before and after tests + set global logging + adjust
file references + add required data files + simplify and fix some tests.

Line 
1"""
2Test cases for the ndgRetrieve module
3
4NERC Data Grid Project
5"""
6__author__ = "C Byrom - Tessella"
7__date__ = "03/09/08"
8__copyright__ = "(C) 2008 STFC & NERC"
9__license__ = \
10"""This software may be distributed under the terms of the Q Public
11License, version 1.0 or later."""
12__contact__ = "b.n.lawrence@rl.ac.uk"
13__revision__ = '$Id: $'
14
15from ndgUtils.ndgObject import ndgObject as no
16from ndgUtils.ndgRetrieve import ndgRetrieve
17from ndgUtils.xmlHandler2 import xmlHandler
18import unittest, os, logging, ConfigParser
19import testconstants as tc
20import ndgUtils.lib.existdbclient as dbc
21
22logging.basicConfig(level=logging.DEBUG,
23                        format='%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s')
24
25class TestCase(unittest.TestCase):
26   
27    def setUp(self):
28       
29        f='test.config'
30        if not os.path.exists(f):
31            raise IOError("Error: config file, 'test.config' missing")
32        self.c=ConfigParser.ConfigParser()
33        self.c.read(f)
34           
35    def testRetrieveFromLocalEXIST(self):
36        ''' Test link to local exist via xml rpc.
37                users will need to ensure that the document is actually
38                in your exist database! '''
39        doc='badc.nerc.ac.uk__NDG-B0__activity_activity_cwvc'
40        #doc='neodc.nerc.ac.uk__NDG-B1__dataent_11924879127625221' #NCAVEO
41        uri=no(doc)
42        status,xml=ndgRetrieve(uri,self.c, discovery=0)
43        if not status: raise ValueError(xml)
44        x=xmlHandler(xml,string=1)
45        self.assertEqual(x.tree.tag,'{http://ndg.nerc.ac.uk/moles}dgMetadataRecord')
46       
47    def testNoB(self):
48        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
49        doc='badc.nerc.ac.uk__NDG-B0__blah'
50        uri=no(doc)
51        status,xml=ndgRetrieve(uri,self.c, discovery=0)
52        if status: raise ValueError(' NDG-B0 [%s] should not exist [%s]'%(doc,xml) )
53       
54    def testRetrieveXMLdif(self):
55        ''' Make sure we can recall at least one DIF properly '''
56        doc= tc.VALID_PROVIDER_ID + "__" + no.DIF_DOC_TYPE + \
57            "__" + tc.VALID_MOLES_DOC_ID
58        uri=no(doc)
59        status,xml=ndgRetrieve(uri,self.c, discovery=0)
60        self.assertEqual(status,1)
61        self.assertNotEqual(xml.find(tc.VALID_MOLES_DOC_ID), -1)
62
63    def OFFtestRemoteGet(self):
64        ''' Attempt a remote get '''
65        # TODO: not sure what remote repository to use for this?
66        doc='www.npm.ac.uk__NDG-B1__data_entity.363'
67        uri=no(doc)
68        status,xml=ndgRetrieve(uri,self.c,remote=1, discovery=0)
69        if not status: print xml #ie print error message if there is one!
70        self.assertEqual(status,1)
71       
72    def testRetrieveNumSim(self):
73        '''Get a NumSim document '''
74        doc= tc.VALID_PROVIDER_ID + "__" + no.NUMSIM_DOC_TYPE + \
75            "__" + tc.VALID_NUMSIM_DOC_ID
76        uri=no(doc)
77        status,xml=ndgRetrieve(uri,self.c, discovery=0)
78        self.assertEqual(status,1)
79        self.assertNotEqual(xml.find(tc.VALID_NUMSIM_DOC_ID), -1)
80       
81    def testRetrieveAtom(self):
82        '''Get an atom document '''
83        self.createdAtom = tc.createAtomInEXist(tc.xmlString)
84        self.dbc = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE, setUpDB = True)
85        self.createdAtom = tc.createAtomInEXist(tc.xmlString)
86        doc= tc.VALID_PROVIDER_ID + "__" + no.ATOM_DOC_TYPE + \
87            "__" + self.createdAtom.datasetID
88        uri=no(doc)
89        status,xml=ndgRetrieve(uri,self.c, discovery=0)
90        self.assertEqual(status,1)
91        self.assertNotEqual(xml.find(self.createdAtom.datasetID), -1)
92        atomPath = self.createdAtom.getDefaultCollectionPath() + self.createdAtom.atomName
93        self.dbc.deleteEXistFile(atomPath)
94       
95    def testRetrieveInvalidAtom(self):
96        '''Get an invalid atom document '''
97        doc= tc.VALID_PROVIDER_ID + "__" + no.ATOM_DOC_TYPE + \
98            "__" + tc.VALID_ATOM_DOC_ID + "blah"
99        uri=no(doc)
100        status,xml=ndgRetrieve(uri,self.c, discovery=0)
101        self.assertEqual(status,0)
102       
103    def testRetrieveAtomBackup(self):
104        '''Get a backed up atom document '''
105        self.createdAtom = tc.createAtomInEXist(tc.xmlString)
106        self.dbc = dbc.eXistDBClient(configFile = tc.DBCONFIG_FILE, setUpDB = True)
107        self.createdAtom = tc.createAtomInEXist(tc.xmlString)
108        doc= tc.VALID_PROVIDER_ID + "__" + no.ATOM_BACKUP_DOC_TYPE + \
109            "__" + self.createdAtom.datasetID
110        uri=no(doc)
111        status,xml=ndgRetrieve(uri,self.c, discovery=0)
112        self.assertEqual(status,1)
113        self.assertNotEqual(xml.find(self.createdAtom.datasetID), -1)
114        atomPath = self.createdAtom.getDefaultCollectionPath() + self.createdAtom.atomName
115        self.dbc.deleteEXistFile(atomPath)
116       
117if __name__=="__main__":
118
119    unittest.main()
Note: See TracBrowser for help on using the repository browser.