source: mauRepo/HPFos/trunk/hpfos/tests/testos.py @ 8523

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/HPFos/trunk/hpfos/tests/testos.py@8523
Revision 8523, 6.7 KB checked in by mnagni, 9 years ago (diff)

Requests, parse and display a fatcat atom feed

Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 8 Aug 2012
30
31@author: mnagni
32'''
33from hpfos.HPFos.view.view import getDescription, doSearchL0
34from hpfos.tests.testsetup import HPFosTest
35from hpfos.HPFos.osImpl.myimpl import MyOSAtomResponse
36from ceda_markup.opensearch.template.osresponse import Subresult, Result
37from ceda_markup.opensearch.query import create_query
38
39
40class TestOS(HPFosTest):
41
42    def description_test(self):
43        get_request = self.reqFactory.get('/description/')
44        res = getDescription(request = get_request)
45        print res       
46
47    def search_test(self):
48        iformat = 'atom'
49        get_request = self.reqFactory.get('/atom/search', {'count': '3'})
50        get_request.moles3EPB = self.epbRepo
51        res = doSearchL0(iformat = iformat, request = get_request)
52        print res
53
54    def create_query_test(self):
55        context = self._generate_context() 
56        query = create_query('atom', self.os_engine.os_query.params_model, context)
57        print query
58        return query
59   
60    def generate_reponse_test(self):
61        ar = MyOSAtomResponse()
62        context = self._generate_context()       
63        queries = self.create_query_test()
64       
65        result = self._generate_results(count=7, start_index=3, start_page=1, tot_results=40)
66        response = ar.generate_response(result, queries, '', context)
67        self.assertTrue('<link href="/search/atom/?startIndex=3" rel="self" type="application/atom+xml"/>' in response, "Error")
68        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="prev" type="application/atom+xml"/>' in response, "Error")       
69        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error")
70        self.assertTrue('<link href="/search/atom/?startIndex=10" rel="next" type="application/atom+xml"/>' in response, "Error")
71        self.assertTrue('<link href="/search/atom/?startIndex=38" rel="last" type="application/atom+xml"/>' in response, "Error")
72       
73        result = self._generate_results(count=10, start_index=5, start_page=1, tot_results=20)
74        response = ar.generate_response(result, queries, '', context)
75        self.assertTrue('<link href="/search/atom/?startIndex=5" rel="self" type="application/atom+xml"/>' in response, "Error")
76        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="prev" type="application/atom+xml"/>' in response, "Error")       
77        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error")
78        self.assertTrue('<link href="/search/atom/?startIndex=15" rel="next" type="application/atom+xml"/>' in response, "Error")
79        self.assertTrue('<link href="/search/atom/?startIndex=15" rel="last" type="application/atom+xml"/>' in response, "Error")
80
81        result = self._generate_results(count=2, start_index=2, start_page=1, tot_results=5)               
82        response = ar.generate_response(result, queries, '', context)
83        self.assertTrue('<link href="/search/atom/?startIndex=2" rel="self" type="application/atom+xml"/>' in response, "Error")
84        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="prev" type="application/atom+xml"/>' in response, "Error")       
85        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error")
86        self.assertTrue('<link href="/search/atom/?startIndex=4" rel="next" type="application/atom+xml"/>' in response, "Error")
87        self.assertTrue('<link href="/search/atom/?startIndex=4" rel="last" type="application/atom+xml"/>' in response, "Error")
88
89        result = self._generate_results(count=1, start_index=18, start_page=1, tot_results=20)               
90        response = ar.generate_response(result, queries, '', context)
91        self.assertTrue('<link href="/search/atom/?startIndex=18" rel="self" type="application/atom+xml"/>' in response, "Error")
92        self.assertTrue('<link href="/search/atom/?startIndex=17" rel="prev" type="application/atom+xml"/>' in response, "Error")       
93        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error")
94        self.assertTrue('<link href="/search/atom/?startIndex=19" rel="next" type="application/atom+xml"/>' in response, "Error")
95        self.assertTrue('<link href="/search/atom/?startIndex=20" rel="last" type="application/atom+xml"/>' in response, "Error")       
96
97
98    def _generate_results(self, count=1, start_index=0, start_page=1, tot_results=20):           
99        subresults = []   
100        for index in range(tot_results):
101            subresults.append(Subresult('id_%s' % (str(index)), \
102                      'title_%s' % (str(index)) , \
103                      description = 'description_%s' % (str(index))))         
104        return Result(count, start_index, start_page, tot_results, subresult = subresults)
105   
106   
107    def _generate_context(self, count='1', start_index='0', start_page='1'):
108        context = {}
109        context['count'] = count
110        context['startIndex'] = start_index
111        context['startPage'] = start_page
112        return context
Note: See TracBrowser for help on using the repository browser.