Esempio n. 1
0
    def testHandleRequest(self):
        response = SolrResponse()
        response.hits = ['term0', 'term&/"']
        response.total = 2
        response.qtime = 5

        def prefixSearch(**kwargs):
            raise StopIteration(response)
            yield

        self.observer.methods['prefixSearch'] = prefixSearch

        header, body = asString(
            self.auto.handleRequest(path='/path',
                                    arguments={
                                        'prefix': ['te'],
                                        'limit': ['5'],
                                        'field': ['field.one']
                                    })).split('\r\n' * 2)

        self.assertTrue(
            "Content-Type: application/x-suggestions+json" in header, header)
        self.assertEquals("""["te", ["term0", "term&/\\""]]""", body)
        self.assertEquals(['prefixSearch'],
                          [m.name for m in self.observer.calledMethods])
        self.assertEquals(
            {
                'prefix': 'te',
                'fieldname': 'field.one',
                'limit': 5
            }, self.observer.calledMethods[0].kwargs)
Esempio n. 2
0
    def testOldStyleAutocomplete(self):
        queryTemplate = '/sru?version=1.1&operation=searchRetrieve&query={searchTerms}'
        self.auto = be((
            Autocomplete(
                host='localhost',
                port=8000,
                path='/some/path',
                templateQuery=queryTemplate,
                shortname="Web Search",
                description="Use this web search to search something",
                defaultLimit=50,
                defaultField='lom',
            ),
            (self.observer, ),
        ))
        response = SolrResponse()
        response.hits = ['term0', 'term&/"']
        response.total = 2
        response.qtime = 5

        def prefixSearch(**kwargs):
            raise StopIteration(response)
            yield

        self.observer.methods['prefixSearch'] = prefixSearch

        header, body = asString(
            self.auto.handleRequest(path='/path',
                                    arguments={
                                        'prefix': ['te'],
                                        'limit': ['5'],
                                        'field': ['field.one']
                                    })).split('\r\n' * 2)

        self.assertTrue(
            "Content-Type: application/x-suggestions+json" in header, header)
        self.assertEquals("""["te", ["term0", "term&/\\""]]""", body)
        self.assertEquals(['prefixSearch'],
                          [m.name for m in self.observer.calledMethods])
        self.assertEquals(
            {
                'prefix': 'te',
                'fieldname': 'field.one',
                'limit': 5
            }, self.observer.calledMethods[0].kwargs)

        result = asString(
            self.auto.handleRequest(path='/path/opensearchdescription.xml',
                                    arguments={}))
        header, body = result.split('\r\n' * 2)

        self.assertTrue("Content-Type: text/xml" in header, header)
        self.assertEqualsWS(
            """<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>Web Search</ShortName>
    <Description>Use this web search to search something</Description>
    <Url type="text/xml" method="get" template="http://localhost:8000/sru?version=1.1&amp;operation=searchRetrieve&amp;query={searchTerms}"/>
    <Url type="application/x-suggestions+json" template="http://localhost:8000/some/path?prefix={searchTerms}"/>
</OpenSearchDescription>""", body)
    def testHandleRequest(self):
        response = SolrResponse()
        response.hits = ['term0', 'term&/"']
        response.total = 2
        response.qtime = 5
        def prefixSearch(**kwargs):
            raise StopIteration(response)
            yield
        self.observer.methods['prefixSearch'] = prefixSearch

        header, body = asString(self.auto.handleRequest(path='/path', arguments={'prefix':['te'], 'limit': ['5'], 'field': ['field.one']})).split('\r\n'*2)

        self.assertTrue("Content-Type: application/x-suggestions+json" in header, header)
        self.assertEquals("""["te", ["term0", "term&/\\""]]""", body)
        self.assertEquals(['prefixSearch'], [m.name for m in self.observer.calledMethods])
        self.assertEquals({'prefix':'te', 'fieldname':'field.one', 'limit':5}, self.observer.calledMethods[0].kwargs)
    def testOldStyleAutocomplete(self):
        queryTemplate = '/sru?version=1.1&operation=searchRetrieve&query={searchTerms}'
        self.auto = be((Autocomplete(
                host='localhost',
                port=8000,
                path='/some/path',
                templateQuery=queryTemplate,
                shortname="Web Search",
                description="Use this web search to search something",
                defaultLimit=50,
                defaultField='lom',
            ),
            (self.observer,),
        ))
        response = SolrResponse()
        response.hits = ['term0', 'term&/"']
        response.total = 2
        response.qtime = 5
        def prefixSearch(**kwargs):
            raise StopIteration(response)
            yield
        self.observer.methods['prefixSearch'] = prefixSearch

        header, body = asString(self.auto.handleRequest(path='/path', arguments={'prefix':['te'], 'limit': ['5'], 'field': ['field.one']})).split('\r\n'*2)

        self.assertTrue("Content-Type: application/x-suggestions+json" in header, header)
        self.assertEquals("""["te", ["term0", "term&/\\""]]""", body)
        self.assertEquals(['prefixSearch'], [m.name for m in self.observer.calledMethods])
        self.assertEquals({'prefix':'te', 'fieldname':'field.one', 'limit':5}, self.observer.calledMethods[0].kwargs)

        result = asString(self.auto.handleRequest(
            path='/path/opensearchdescription.xml',
            arguments={}))
        header,body = result.split('\r\n'*2)

        self.assertTrue("Content-Type: text/xml" in header, header)
        self.assertEqualsWS("""<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
    <ShortName>Web Search</ShortName>
    <Description>Use this web search to search something</Description>
    <Url type="text/xml" method="get" template="http://localhost:8000/sru?version=1.1&amp;operation=searchRetrieve&amp;query={searchTerms}"/>
    <Url type="application/x-suggestions+json" template="http://localhost:8000/some/path?prefix={searchTerms}"/>
</OpenSearchDescription>""", body)