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

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

Add a new feed for collecting corrections info submitted by users of
the atom editor + create new method on client to do this + add new
tests and tidy up some 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
14
15class testFeedClient(unittest.TestCase):
16   
17    VALID_COLLECTION_PATH = dc.ATOM_COLLECTION_PATH
18    VALID_FILE = "tst.xml"
19    VALID_FILE_PATH = VALID_COLLECTION_PATH + VALID_FILE
20    INVALID_PATH = 'blah'
21    DUMMY_COLLECTION = VALID_COLLECTION_PATH + INVALID_PATH
22   
23    def setUp(self):
24        '''
25        set up data used in the tests.
26        '''
27        self.createdAtoms = []    # array to store paths to atoms created - in order to then delete them
28        self.confFile = tc.EXIST_DBCONFIG_FILE
29        self.utils = tu(self.confFile)
30        self.feedClient = self.utils.ac.feedClient
31
32        # NB, if running tests on a local db, feedparser, used by feedclient won't
33        # pick up any 'no_proxy' settings - so
34        self.oldProxy = None 
35        if self.utils.host == 'localhost' and os.environ.has_key(utils.PROXY_KEY):
36            self.oldProxy = os.environ[utils.PROXY_KEY]
37            del os.environ[utils.PROXY_KEY]
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            self.assertTrue(feed.feed.has_key('links'))
69        except Exception, e:
70            self.fail("Should not have thrown an exception")
71
72    def testInvalidGetAtomFeed(self):
73        try:
74            feed = self.feedClient.getAtomFeed(self.INVALID_PATH)
75            self.fail("Should have thrown an exception")
76        except FeedClientError, fce:
77            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
78        except Exception, e:
79            self.fail("Should have thrown a FeedClientError exception")
80
81
82    def testCreateAtomFeedEntry(self):
83        try:
84            self.tidyUp = True
85            atom = self.utils.createAtom(tc.xmlString)
86            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
87                                                atom) 
88        except:
89            self.fail("Should not have thrown an exception")
90
91
92    def testCreateAtomFeedEntryWithIsDIFRecordKeyword(self):
93        try:
94            self.tidyUp = True
95            atom = self.utils.createAtom(tc.xmlString)
96            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
97                                                atom, isDIFRecord=True) 
98        except Exception, e:
99            self.fail("Should not have thrown an exception")
100
101
102    def testCreateAtomFeedEntryCheckFeedSize(self):
103        try:
104            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
105            self.assertTrue(feed.feed.has_key('links'))
106            entriesNo = len(feed.entries)
107            atom = self.utils.createAtom(tc.xmlString)
108            self.feedClient.createAtomFeedEntry(self.VALID_COLLECTION_PATH,
109                                                atom) 
110            feed = self.feedClient.getAtomFeed(self.VALID_COLLECTION_PATH)
111            self.assertEquals(entriesNo + 1, len(feed.entries))
112        except Exception, e:
113            self.fail("Should not have thrown an exception")
114
115
116    def testInvalidCollectionCreateAtomFeedEntry(self):
117        try:
118            atom = self.utils.createAtom(tc.xmlString)
119            self.feedClient.createAtomFeedEntry(self.INVALID_PATH,
120                                                atom) 
121            self.fail("Should have thrown an exception")
122        except FeedClientError, fce:
123            self.assertTrue(fce.message.startswith(self.feedClient.FEED_ERROR_MSG) > -1)
124        except Exception, e:
125            self.fail("Should have thrown a FeedClientError exception")
126
127
128    def testSetupBasicFeeds(self):
129        try:
130            feed = self.feedClient.setupBasicFeeds()
131        except:
132            self.fail("Should not have thrown an exception")
133       
134
135    def tearDown(self):
136        for path  in self.createdAtoms:
137            # delete the test file, in case it has been used in a test
138            self.utils.deleteDoc(path)
139
140        if self.oldProxy:
141            os.environ[utils.PROXY_KEY] = self.oldProxy
142
143if __name__ == '__main__':
144
145    unittest.main()
Note: See TracBrowser for help on using the repository browser.