Beispiel #1
0
    def test_highlighted_snippet(self):
        res = ([{'title':'Example',
                 'uri':'http://example.org/base/123/b1',
                 'text':html.P(['sollicitudin justo ',
                                html.Strong(['needle'], **{'class':'match'}),
                                ' tempor ut eu enim ... himenaeos. ',
                                html.Strong(['Needle'], **{'class':'match'}),
                                ' id tincidunt orci'])}],
               {'pagenum': 1,
                'pagecount': 1,
                'firstresult': 1,
                'lastresult': 1,
                'totalresults': 1})

        self.builder.query_string = "q=needle"
        config = {'connect.return_value': Mock(**{'query.return_value': res})}
        with patch('ferenda.wsgiapp.FulltextIndex', **config):
            status, headers, content = self.call_wsgi()
        
        self.assertResponse("200 OK",
                            {'Content-Type': 'text/html; charset=utf-8'},
                            None,
                            status, headers, None)
        
        t = etree.fromstring(content)
        docs = t.findall(".//section[@class='hit']")
        self.assertEqualXML(res[0][0]['text'].as_xhtml(),
                            docs[0][1],
                            namespace_aware=False)
Beispiel #2
0
    def test_search_multiple(self):
        self.env['QUERY_STRING'] = "q=part"
        res = (
            [
                {
                    'dcterms_title':
                    'Introduction',
                    'dcterms_identifier':
                    '123/a¶1',
                    'uri':
                    'http://example.org/base/123/a#S1',
                    'text':
                    html.P([
                        'This is ',
                        html.Strong(['part'], **{'class':
                                                 'match'}), ' of document-',
                        html.Strong(['part'], **{'class': 'match'}),
                        ' section 1</p>'
                    ])
                },
                {  #'title':'Definitions and Abbreviations',
                    'uri':
                    'http://example.org/base/123/a#S2',
                    'text':
                    html.P([
                        'second main document ',
                        html.Strong(['part'], **{'class': 'match'})
                    ])
                },
                {
                    'dcterms_title':
                    'Example',
                    'uri':
                    'http://example.org/base/123/a',
                    'text':
                    html.P([
                        'This is ',
                        html.Strong(['part'], **{'class': 'match'}),
                        ' of the main document'
                    ])
                }
            ],
            {
                'pagenum': 1,
                'pagecount': 1,
                'firstresult': 1,
                'lastresult': 3,
                'totalresults': 3
            })

        config = {'connect.return_value': Mock(**{'query.return_value': res})}
        with patch('ferenda.wsgiapp.FulltextIndex', **config):
            status, headers, content = self.call_wsgi(self.env)
        self.assertResponse("200 OK",
                            {'Content-Type': 'text/html; charset=utf-8'}, None,
                            status, headers, None)
        t = etree.parse(BytesIO(content))
        css = t.findall("head/link[@rel='stylesheet']")
        self.assertEqual(len(css),
                         3)  # bootstrap, bootstrap-theme, ferenda and sfs (?!)
        self.assertEqual(
            'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
            css[0].get('href'))
        js = t.findall("body/script")
        self.assertEqual(len(js), 4)  # jquery, bootstrap, ferenda, typeahead
        resulthead = t.find(".//article/h1").text
        self.assertEqual(resulthead, "3 matches for 'part'")
        docs = t.findall(".//section[@class='hit']")
        self.assertEqual(len(docs), 3)
        self.assertEqual(docs[0][0].tag, 'h2')
        expect = res[0]
        self.assertIn(expect[0]['dcterms_title'], docs[0][0][0].text)
        self.assertEqual(expect[0]['uri'], docs[0][0][0].get('href'))
        self.assertEqualXML(expect[0]['text'].as_xhtml(),
                            docs[0][1],
                            namespace_aware=False)

        self.assertIn(expect[1]['dcterms_title'], docs[1][0][0].text)
        self.assertEqual(expect[1]['uri'], docs[1][0][0].get('href'))
        self.assertEqualXML(expect[1]['text'].as_xhtml(),
                            docs[1][1],
                            namespace_aware=False)

        self.assertIn(expect[2]['dcterms_title'], docs[2][0][0].text)
        self.assertEqual(expect[2]['uri'], docs[2][0][0].get('href'))
        self.assertEqualXML(expect[2]['text'].as_xhtml(),
                            docs[2][1],
                            namespace_aware=False)