source: ndgCommon/trunk/ndg/common/unittests/dal/testndgretrieve.py @ 5012

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/unittests/dal/testndgretrieve.py@5012
Revision 5012, 4.8 KB checked in by cbyrom, 11 years ago (diff)

Various tidy ups and fixes to the test codebase - including the removal
of uneccesary files + simplify the test constants.

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 ndg.common.src.models.ndgObject import ndgObject as no
16from ndg.common.src.dal.ndgRetrieve import NDGRetrieve
17from xml.etree import ElementTree as ET
18import unittest, os, logging, ConfigParser
19import ndg.common.unittests.testconstants as tc
20from ndg.common.unittests.testutils import testUtils as tu
21import ndg.common.src.clients.xmldb.eXist.atomclient as ac
22from ndg.common.src.models.myconfig import myConfig
23
24class TestCase(unittest.TestCase):
25   
26    def setUp(self):
27        self.c = myConfig(tc.NDG_CONFIG_FILE)
28        self.createdAtoms = []    # array to store paths to atoms created - in order to then delete them
29        self.utils = tu(tc.EXIST_DBCONFIG_FILE)
30        self.ndgRetrieve = NDGRetrieve(self.c)
31       
32    def tearDown(self):
33        for path  in self.createdAtoms:
34            # delete the test file, in case it has been used in a test
35            self.utils.deleteDoc(path)
36
37    def testRetrieveOldMolesFromLocalEXIST(self):
38        ''' Test link to local exist via xml rpc.
39        '''
40        self.createdAtoms.append(self.utils.createOldMolesDoc())
41        doc= tc.VALID_NDG_URL
42        uri=no(doc)
43        status,xml = self.ndgRetrieve.retrieveDoc(uri, useDiscovery = False)
44        if not status: 
45            raise ValueError(xml)
46        x=ET.fromstring(xml)
47        self.assertEqual('dgMetadataRecord', x.tag)
48       
49    def testNoB(self):
50        ''' Test that we get a sensible error if a NDG-B doc doesn't exist '''
51        doc='badc.nerc.ac.uk__NDG-B0__blah'
52        uri=no(doc)
53        status,xml=self.ndgRetrieve.retrieveDoc(uri, useDiscovery = False)
54        self.assertEqual(0, status)
55       
56    def OFFtestRetrieveXMLdif(self):
57        # NB, this requires a valid dif doc to be available
58        ''' Make sure we can recall at least one DIF properly '''
59        doc= tc.VALID_PROVIDER_ID + "__" + no.DIF_DOC_TYPE + \
60            "__" + tc.VALID_MOLES_DOC_ID
61        uri=no(doc)
62        status,xml=self.ndgRetrieve.retrieveDoc(uri,useDiscovery = False)
63        self.assertEqual(status,1)
64        self.assertNotEqual(xml.find(tc.VALID_MOLES_DOC_ID), -1)
65
66    def testRemoteGet(self):
67        ''' Attempt a remote get '''
68        # TODO: not sure what remote repository to use for this?
69        doc='www.npm.ac.uk__NDG-B1__data_entity.363'
70        uri=no(doc)
71        status,xml=self.ndgRetrieve.retrieveDoc(uri, useRemoteEXist = True, 
72                                                useDiscovery = False)
73        if not status: print xml #ie print error message if there is one!
74        self.assertEqual(1, status)
75       
76    def FFtestRetrieveNumSim(self):
77        # NB, this requires a valid numsim doc to be available
78        '''Get a NumSim document '''
79        doc= tc.VALID_PROVIDER_ID + "__" + no.NUMSIM_DOC_TYPE + \
80            "__" + tc.VALID_NUMSIM_DOC_ID
81        uri=no(doc)
82        status,xml=self.ndgRetrieve.retrieveDoc(uri, useDiscovery = False)
83        self.assertEqual(status,1)
84        self.assertNotEqual(xml.find(tc.VALID_NUMSIM_DOC_ID), -1)
85       
86    def testRetrieveAtom(self):
87        '''Get an atom document '''
88        utils = tu(tc.EXIST_DBCONFIG_FILE)
89        createdAtom = utils.createAtomInEXist(tc.xmlString)
90        doc= tc.VALID_PROVIDER_ID + "__" + no.ATOM_DOC_TYPE + \
91            "__" + createdAtom.datasetID
92        uri=no(doc)
93        status,xml=self.ndgRetrieve.retrieveDoc(uri, useDiscovery = False)
94        self.assertEqual(status,1)
95        self.assertNotEqual(xml.find(createdAtom.datasetID), -1)
96        utils.ac.deleteAtom(createdAtom)
97       
98    def testRetrieveLocalInvalidAtom(self):
99        '''Get an invalid atom document '''
100        doc= tc.VALID_PROVIDER_ID + "__" + no.ATOM_DOC_TYPE + \
101            "__" + tc.VALID_ATOM_DOC_ID + "blah"
102        uri=no(doc)
103        status,xml=self.ndgRetrieve.retrieveDoc(uri, useDiscovery = False)
104        self.assertEqual(status,0)
105        self.assertTrue(xml.find(self.utils.ac.NO_DOCS_FOUND_ERROR) > -1)
106       
107    def testRetrieveLocalAtomBackup(self):
108        '''Get a backed up atom document '''
109        utils = tu(tc.EXIST_DBCONFIG_FILE)
110        createdAtom = utils.createAtomInEXist(tc.xmlString)
111        createdAtom = utils.createAtomInEXist(tc.xmlString)
112        doc= tc.VALID_PROVIDER_ID + "__" + no.ATOM_BACKUP_DOC_TYPE + \
113            "__" + createdAtom.datasetID
114        uri=no(doc)
115        status,xml=self.ndgRetrieve.retrieveDoc(uri, useDiscovery = False)
116        self.assertEqual(status,1)
117        self.assertNotEqual(xml.find(createdAtom.datasetID), -1)
118        utils.ac.deleteAtom(createdAtom)
119       
120if __name__=="__main__":
121
122    unittest.main()
Note: See TracBrowser for help on using the repository browser.