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

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

Add better error handling + add general keyword catcher to avoid
issues with slight differences between the search clients + extend test data and test suites - handling proxies better.

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
13import ndg.common.src.lib.utilities as utils
14from ndg.common.src.models.myconfig import myConfig
15
16
17class testFeedClient(unittest.TestCase):
18   
19    VALID_COLLECTION_PATH = dc.ATOM_COLLECTION_PATH
20    VALID_FILE = "tst.xml"
21    VALID_FILE_PATH = VALID_COLLECTION_PATH + VALID_FILE
22    INVALID_PATH = 'blah'
23    DUMMY_COLLECTION = VALID_COLLECTION_PATH + INVALID_PATH
24   
25    def setUp(self):
26        '''
27        set up data used in the tests.
28        '''
29        self.createdAtoms = []    # array to store paths to atoms created - in order to then delete them
30        self.confFile = tc.EXIST_DBCONFIG_FILE
31        self.utils = tu(self.confFile)
32        self.feedClient = self.utils.ac.feedClient
33        self.cf = myConfig(tc.NDG_CONFIG_FILE)
34        self.proxyServer = self.cf.get('DEFAULT','proxyServer')
35        if self.utils.host == 'localhost':
36            self.proxyServer = None
37
38
39    def testCreateAtomFeed(self):
40        try:
41            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
42                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
43        except:
44            self.fail("Should not have thrown an exception")
45
46    def testCreateCorrectionsFeedEntry(self):
47        try:
48            self.feedClient.createCorrectionsFeedEntry('some title info',
49                                                       'some body info')
50        except:
51            self.fail("Should not have thrown an exception")
52
53
54
55    def testInvalidCollectionCreateAtomFeed(self):
56        try:
57            self.feedClient.createAtomFeed(self.INVALID_PATH, 
58                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
59            self.fail("Should have thrown an exception")
60        except FeedClientError, fce:
61            self.assertTrue(fce.message.find('+not+found') > -1)
62        except Exception, e:
63            self.fail("Should have thrown a FeedClientError exception")
64
65    def testGetAtomFeed(self):
66        try:
67            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH,
68                                               proxyServer = self.proxyServer)
69            self.assertTrue(feed.feed.has_key('links'))
70        except Exception, e:
71            import pdb
72            pdb.set_trace()
73            self.fail("Should not have thrown an exception")
74
75    def testInvalidGetAtomFeed(self):
76        try:
77            feed = self.feedClient.getAtomFeed(self.INVALID_PATH)
78            self.fail("Should have thrown an exception")
79        except FeedClientError, fce:
80            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
81        except Exception, e:
82            self.fail("Should have thrown a FeedClientError exception")
83
84
85    def testCreateAtomFeedEntry(self):
86        try:
87            self.tidyUp = True
88            atom = self.utils.createAtom(tc.xmlString)
89            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
90                                                atom) 
91        except:
92            self.fail("Should not have thrown an exception")
93
94
95    def testCreateAtomFeedEntryWithIsDIFRecordKeyword(self):
96        try:
97            self.tidyUp = True
98            atom = self.utils.createAtom(tc.xmlString)
99            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
100                                                atom, isDIFRecord=True) 
101        except Exception, e:
102            self.fail("Should not have thrown an exception")
103
104
105    def testCreateAtomFeedEntryCheckFeedSize(self):
106        try:
107            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
108            self.assertTrue(feed.feed.has_key('links'))
109            entriesNo = len(feed.entries)
110            atom = self.utils.createAtom(tc.xmlString)
111            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
112                                                atom) 
113            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
114            self.assertEquals(entriesNo + 1, len(feed.entries))
115        except Exception, e:
116            self.fail("Should not have thrown an exception")
117
118
119    def testInvalidCollectionCreateAtomFeedEntry(self):
120        try:
121            atom = self.utils.createAtom(tc.xmlString)
122            self.feedClient.createAtomFeedEntry(self.INVALID_PATH,
123                                                atom) 
124            self.fail("Should have thrown an exception")
125        except FeedClientError, fce:
126            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
127        except Exception, e:
128            self.fail("Should have thrown a FeedClientError exception")
129
130
131    def testSetupBasicFeeds(self):
132        try:
133            feed = self.feedClient.setupBasicFeeds()
134        except:
135            self.fail("Should not have thrown an exception")
136       
137
138    def tearDown(self):
139        for path  in self.createdAtoms:
140            # delete the test file, in case it has been used in a test
141            self.utils.deleteDoc(path)
142
143if __name__ == '__main__':
144
145    unittest.main()
Note: See TracBrowser for help on using the repository browser.