source: TI07-MOLES/trunk/PythonCode/wsgi/DiscoveryState.py @ 2340

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/DiscoveryState.py@2340
Revision 2340, 1.8 KB checked in by lawrence, 13 years ago (diff)

Bug fixes and test cases for ticket:672

Line 
1from paste.request import parse_querystring,construct_url
2class DiscoveryState:
3    ''' This class holds the state associated with presenting multiple slices
4    of a large result set '''
5    def __init__(self,sessionID,environ,hits,offset=1,stride=10):
6        ''' On instantiation, provide
7                the backend sessionID
8                the application URL that produced this query
9                the stride through the result set '''
10        self.environ=environ # the wsgi environment
11        self.sessID=sessionID
12        self.hits=hits
13        self.offset=offset
14        self.stride=stride
15    def geturl(self,**kw):
16        ''' Get a url from the wsgi environment, modified by the keyword arguments offset and stride
17        which are to be part of the querystring '''
18        args=dict(parse_querystring(self.environ))
19        offset,stride=kw.get('offset'),kw.get('stride')
20        if offset is not None:args['start']=offset
21        if stride is not None:args['howmany']=stride
22        q=''
23        for i in args: q+='%s=%s&'%(i,args[i])
24        q=q[0:-1]
25        url=construct_url(self.environ, with_query_string=True, with_path_info=True, querystring=q)
26        return url
27       
28import unittest
29
30class TestCase(unittest.TestCase):
31   
32    def testDiscoveryState(self):
33        ''' Test creation of a discovery state variable '''
34        DummyEnviron={'QUERY_STRING':'start=10&howmany=10','HTTP_HOST':'example.ndg',
35                      'PATH_INFO':'/discovery','wsgi.url_scheme':'http','SERVER_PORT':'80'}
36        d=DiscoveryState('123',DummyEnviron,12)
37        self.assertEqual(d.geturl(offset='11',stride='20'),
38                         'http://example.ndg/discovery?start=11&howmany=20')
39
40 
41       
42if __name__=="__main__":
43    unittest.main()
44               
45
46                     
47       
Note: See TracBrowser for help on using the repository browser.