source: ndgCommon/trunk/ndg/common/unittests/clients/xmldb/eXist/testfeedclient.py @ 4942

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

Replace old eXist connector tests with tests based on the new clients
+ extend and fix this wherever necessary.

Line 
1'''
2A test class for the FeedClient module.
3
4@author C Byrom, Tessella Jan 2009
5'''
6import unittest, time, os
7import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
8from ndg.common.src.clients.xmldb.eXist.feedclient import FeedClient as feedClient, FeedClientError
9from ndg.common.src.models.Atom import Atom
10from ndg.common.src.models.vocabtermdata import VocabTermData as VTD
11import ndg.common.unittests.testconstants as tc
12from ndg.common.unittests.testutils import testUtils as tu
13
14class testFeedClient(unittest.TestCase):
15   
16    VALID_COLLECTION_PATH = dc.ATOM_COLLECTION_PATH
17    VALID_FILE = "tst.xml"
18    VALID_FILE_PATH = VALID_COLLECTION_PATH + VALID_FILE
19    INVALID_PATH = 'blah'
20    DUMMY_COLLECTION = VALID_COLLECTION_PATH + INVALID_PATH
21   
22    def setUp(self):
23        '''
24        set up data used in the tests.
25        '''
26        self.createdAtoms = []    # array to store paths to atoms created - in order to then delete them
27        self.confFile = tc.EXIST_DBCONFIG_FILE
28        self.utils = tu(self.confFile)
29        self.feedClient = self.utils.ac.feedClient
30       
31        # NB, if running tests on a local db, feedparser, used by feedclient won't
32        # pick up any 'no_proxy' settings - so
33        self.oldProxy = None 
34        if self.utils.host == 'localhost' and os.environ.has_key(tc.PROXY_KEY):
35            self.oldProxy = os.environ[tc.PROXY_KEY]
36            del os.environ[tc.PROXY_KEY]
37
38    def testCreateAtomFeed(self):
39        try:
40            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
41                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
42        except:
43            self.fail("Should not have thrown an exception")
44
45    def testInvalidCollectionCreateAtomFeed(self):
46        try:
47            self.feedClient.createAtomFeed(self.INVALID_PATH, 
48                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
49            self.fail("Should have thrown an exception")
50        except FeedClientError, fce:
51            self.assertTrue(fce.message.find('+not+found') > -1)
52        except Exception, e:
53            self.fail("Should have thrown a FeedClientError exception")
54       
55    def testValidGetRESTfulData(self):
56        atom = self.utils.createAtomInEXist(tc.xmlString)
57        self.createdAtoms.append(atom.getFullPath())
58        doc = self.feedClient.getRESTfulData(atom.getFullPath())
59        self.assertNotEquals(None, doc)
60       
61    def testInvalidGetRESTfulData(self):
62        self.assertEquals(None, self.feedClient.getRESTfulData(self.VALID_FILE_PATH))
63
64    def testGetAtomFeed(self):
65        try:
66            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
67            self.assertTrue(feed.feed.has_key('links'))
68        except Exception, e:
69            self.fail("Should not have thrown an exception")
70
71    def testInvalidGetAtomFeed(self):
72        try:
73            feed = self.feedClient.getAtomFeed(self.INVALID_PATH)
74            self.fail("Should have thrown an exception")
75        except FeedClientError, fce:
76            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
77        except Exception, e:
78            self.fail("Should have thrown a FeedClientError exception")
79
80
81    def testCreateAtomFeedEntry(self):
82        try:
83            self.tidyUp = True
84            atom = self.utils.createAtom(tc.xmlString)
85            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
86                                                atom) 
87        except:
88            self.fail("Should not have thrown an exception")
89
90
91    def testCreateAtomFeedEntryWithIsDIFRecordKeyword(self):
92        try:
93            self.tidyUp = True
94            atom = self.utils.createAtom(tc.xmlString)
95            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
96                                                atom, isDIFRecord=True) 
97        except Exception, e:
98            self.fail("Should not have thrown an exception")
99
100
101    def testCreateAtomFeedEntryCheckFeedSize(self):
102        try:
103            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
104            self.assertTrue(feed.feed.has_key('links'))
105            entriesNo = len(feed.entries)
106            atom = self.utils.createAtom(tc.xmlString)
107            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
108                                                atom) 
109            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
110            self.assertEquals(entriesNo + 1, len(feed.entries))
111        except Exception, e:
112            self.fail("Should not have thrown an exception")
113
114
115    def testInvalidCollectionCreateAtomFeedEntry(self):
116        try:
117            atom = self.utils.createAtom(tc.xmlString)
118            self.feedClient.createAtomFeedEntry(self.INVALID_PATH,
119                                                atom) 
120            self.fail("Should have thrown an exception")
121        except FeedClientError, fce:
122            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
123        except Exception, e:
124            self.fail("Should have thrown a FeedClientError exception")
125
126
127    def testSetupBasicFeeds(self):
128        try:
129            feed = self.feedClient.setupBasicFeeds()
130        except:
131            self.fail("Should not have thrown an exception")
132       
133
134    def tearDown(self):
135        for path  in self.createdAtoms:
136            # delete the test file, in case it has been used in a test
137            self.utils.deleteDoc(path)
138           
139        if self.oldProxy:
140            os.environ[tc.PROXY_KEY] = self.oldProxy
141
142if __name__ == '__main__':
143
144    unittest.main()
Note: See TracBrowser for help on using the repository browser.