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

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

Extend feed client code to allow setting of author data on generic
feed entries + add setting up of discovery feed as standard feed
setup + extend tests.

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