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

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

Update feed client test to properly clean up artifacts after tests +
remove some unecessary code.

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.SCHEMAS_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.createdFeeds = []    # 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            self.createdFeeds.append(self.VALID_COLLECTION_PATH)
44        except:
45            self.fail("Should not have thrown an exception")
46
47
48    def testCreateGenericFeedEntry(self):
49        try:
50            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
51                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
52            self.createdFeeds.append(self.VALID_COLLECTION_PATH)
53            feed, xml = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
54            self.assertTrue(feed.feed.has_key('links'))
55            entriesNo = len(feed.entries)
56            self.feedClient.createGenericFeedEntry('some title info',
57                                                   'some body info',
58                                                   self.VALID_COLLECTION_PATH)
59            feed, xml = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
60            self.assertEquals(entriesNo + 1, len(feed.entries))
61
62        except Exception, e:
63            self.fail("Should not have thrown an exception")
64
65
66
67    def testInvalidCollectionCreateAtomFeed(self):
68        try:
69            self.feedClient.createAtomFeed(self.INVALID_PATH, 
70                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
71            self.fail("Should have thrown an exception")
72        except FeedClientError, fce:
73            self.assertTrue(fce.message.find('+not+found') > -1)
74        except Exception, e:
75            self.fail("Should have thrown a FeedClientError exception")
76
77    def testGetAtomFeed(self):
78        try:
79            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
80                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
81            self.createdFeeds.append(self.VALID_COLLECTION_PATH)
82            feed, xml = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH,
83                                                    proxyServer = self.proxyServer)
84            self.assertTrue(feed.feed.has_key('links'))
85        except Exception, e:
86            self.fail("Should not have thrown an exception (%s)" %e)
87
88    def testInvalidGetAtomFeed(self):
89        try:
90            feed, xml = self.feedClient.getAtomFeed(self.INVALID_PATH)
91            self.fail("Should have thrown an exception")
92        except FeedClientError, fce:
93            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
94        except Exception, e:
95            self.fail("Should have thrown a FeedClientError exception")
96
97
98    def testCreateAtomFeedEntry(self):
99        try:
100            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
101                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
102            self.createdFeeds.append(self.VALID_COLLECTION_PATH)
103            atom = self.utils.createAtom(tc.xmlString)
104            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
105                                                atom) 
106        except:
107            self.fail("Should not have thrown an exception")
108
109
110    def testCreateAtomFeedEntryWithIsDIFRecordKeyword(self):
111        try:
112            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
113                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
114            self.createdFeeds.append(self.VALID_COLLECTION_PATH)
115            atom = self.utils.createAtom(tc.xmlString)
116            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
117                                                atom, isDIFRecord=True) 
118        except Exception, e:
119            self.fail("Should not have thrown an exception")
120
121
122    def testCreateAtomFeedEntryCheckFeedSize(self):
123        try:
124            self.feedClient.createAtomFeed(self.VALID_COLLECTION_PATH, 
125                                          self.feedClient.TOPLEVEL_ATOM_FEED_TITLE)
126            self.createdFeeds.append(self.VALID_COLLECTION_PATH)
127            feed, xml = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
128            self.assertTrue(feed.feed.has_key('links'))
129            entriesNo = len(feed.entries)
130            atom = self.utils.createAtom(tc.xmlString)
131            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
132                                                atom) 
133            feed, xml = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
134            self.assertEquals(entriesNo + 1, len(feed.entries))
135        except Exception, e:
136            self.fail("Should not have thrown an exception")
137
138
139    def testInvalidCollectionCreateAtomFeedEntry(self):
140        try:
141            atom = self.utils.createAtom(tc.xmlString)
142            self.feedClient.createAtomFeedEntry(self.INVALID_PATH,
143                                                atom) 
144            self.fail("Should have thrown an exception")
145        except FeedClientError, fce:
146            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
147        except Exception, e:
148            self.fail("Should have thrown a FeedClientError exception")
149
150
151    def testSetupBasicFeeds(self):
152        try:
153            feed = self.feedClient.setupBasicFeeds()
154        except:
155            self.fail("Should not have thrown an exception")
156       
157
158    def tearDown(self):
159        for path in self.createdFeeds:
160            # delete the test file, in case it has been used in a test
161            self.utils.deleteDoc(path + '/.feed.atom')
162            self.utils.ac.client.removeCollection(path + '/.feed.entry')
163
164if __name__ == '__main__':
165
166    unittest.main()
Note: See TracBrowser for help on using the repository browser.