コード例 #1
0
ファイル: test_client.py プロジェクト: ceronman/txsolr
class QueryingDocumentsTestCase(unittest.TestCase):

    @inlineCallbacks
    def setUp(self):
        self.client = SolrClient(SOLR_URL)

        # Test documents used for querying
        self.narutoId = _randomString(20)
        self.bleachId = _randomString(20)
        self.deathnoteId = _randomString(20)

        self.documents = [
            {'id': self.narutoId,
             'title':  u'Naruto',
             'links': ['http://en.wikipedia.org/wiki/Naruto'],
             'category': 'action comedy drama fantasy',
             'popularity': 10,
             'info_t': (u'Naruto (NARUTO—ナルト—?, romanized as NARUTO) '
                        u'is an ongoing Japanese manga series written '
                        u'and illustrated by Masashi Kishimoto. The '
                        u'plot tells the story of Naruto Uzumaki, '
                        u'an adolescent ninja who constantly searches '
                        u'for recognition and aspires to become a Hokage, '
                        u'the ninja in his village that is acknowledged '
                        u'as the leader and the strongest of all.')},

            {'id': self.bleachId,
             'title':  u'Bleach',
             'category': 'action comedy drama supernatural',
             'links': ['http://en.wikipedia.org/wiki/Bleach_(manga)'],
             'popularity': 7,
             'info_t': (u'Bleach (ブリーチ Burīchi?, Romanized as BLEACH '
                        u'in Japan) is a Japanese manga series written '
                        u'and illustrated by Tite Kubo. Bleach follows '
                        u'the adventures of Ichigo Kurosaki after he '
                        u'obtains the powers of a Soul Reaper - a death '
                        u'personification similar to the Grim Reaper - '
                        u'from Rukia Kuchiki.')},

             {'id': self.deathnoteId,
             'title':  u'Death Note',
             'category': 'drama mystery psychological supernatural thriller',
             'links': ['http://en.wikipedia.org/wiki/Death_Note'],
             'popularity': 8,
             'info_t': (u'Death Note (デスノート Desu Nōto?) is a manga '
                        u'series created by writer Tsugumi Ohba and '
                        u'manga artist Takeshi Obata. The main character '
                        u'is Light Yagami, a high school student who '
                        u'discovers a supernatural notebook, the "Death '
                        u'Note", dropped on Earth by a death god '
                        u'named Ryuk.')},
        ]

        yield self.client.add(self.documents)
        yield self.client.commit()

    @inlineCallbacks
    def testSimpleQuery(self):
        """L{SolrClient.search} resolves a simple query."""
        r = yield self.client.search('title:Bleach OR title:"Death Note"')

        self.assertEqual(r.results.numFound, 2,
                         'Wrong numFound after query')

        for doc in r.results.docs:
            self.assertTrue(doc['id'] in (self.bleachId, self.deathnoteId),
                            'Document found does not match with added one')

    @inlineCallbacks
    def testUnicodeQuery(self):
        """L{SolrClient.search} resolves queries with unicode characters."""

        r = yield self.client.search(u'info_t:ブリーチ')

        self.assertEqual(r.results.numFound, 1,
                         'Wrong numFound after query')

        doc = r.results.docs[0]
        self.assertEqual(doc['id'], self.bleachId,
                        'Document found does not match with added one')

    @inlineCallbacks
    def testSearchWithUnicodeArguments(self):
        """
        L{SolrClient.search} accepts query arguments such as filter query.
        """
        r = yield self.client.search('*:*', fq=u'info_t:ブリーチ')

        self.assertEqual(r.results.numFound, 1,
                         'Wrong numFound after query')

        doc = r.results.docs[0]
        self.assertEqual(doc['id'], self.bleachId,
                        'Document found does not match with added one')

    @inlineCallbacks
    def testQueryWithFields(self):
        """
        L{SolrClient.search} shows which field to show in the result C{dict}.
        """

        # Fist test query with a single field
        r = yield self.client.search('info_t:manga', fl='links')
        for doc in r.results.docs:
            self.assertTrue('links' in doc,
                           'Results do not have specified field')

            self.assertFalse('id' in doc,
                             'Results have unrequested fields')

            self.assertFalse('info_t' in doc,
                             'Results have unrequested fields')

            self.assertFalse('popularity' in doc,
                             'Results have unrequested fields')

        # Test query with multiple fields
        r = yield self.client.search('info_t:manga', fl='links,popularity')
        for doc in r.results.docs:
            self.assertTrue('links' in doc,
                           'Results do not have specified field')

            self.assertFalse('id' in doc,
                             'Results have unrequested fields')

            self.assertFalse('info_t' in doc,
                             'Results have unrequested fields')

            self.assertTrue('popularity' in doc,
                             'Results do not have specified field')

        # Test query with all fields
        r = yield self.client.search('info_t:manga', fl='*')
        for doc in r.results.docs:
            self.assertTrue('links' in doc,
                            'Results do not have specified field')

            self.assertTrue('id' in doc,
                            'Results do not have specified field')

            self.assertTrue('info_t' in doc,
                            'Results do not have specified field')

            self.assertTrue('popularity' in doc,
                            'Results do not have specified field')

    @inlineCallbacks
    def testQueryWithScore(self):
        """L{SolrClient.search} shows the score of the results."""
        r = yield self.client.search('info_t:manga', fl='id,score')
        for doc in r.results.docs:
            self.assertTrue('id' in doc,
                           'Results do not have ID field')

            self.assertTrue('score' in doc,
                           'Results do not have score')

    # TODO: poor test. Improve it
    @inlineCallbacks
    def testQueryWithHighlight(self):
        """L{SolrClient.search} shows highlighting."""
        r = yield self.client.search('info_t:manga',
                                     hl='true',
                                     hl_fl='info_t')

        self.assertTrue(hasattr(r, 'highlighting'))

    @inlineCallbacks
    def testQueryWithSort(self):
        """L{SolrClient.search} shows sorted results."""
        r = yield self.client.search('info_t:manga', sort='popularity desc')
        docs = r.results.docs

        self.assertEqual(docs[0]['id'], self.narutoId,
                         'Wrong sorting order')

        self.assertEqual(docs[1]['id'], self.deathnoteId,
                         'Wrong sorting order')

        self.assertEqual(docs[2]['id'], self.bleachId,
                         'Wrong sorting order')

    # TODO: poor test. Improve it
    @inlineCallbacks
    def testQueryWithFacet(self):
        """L{SolrClient.search} shows facets."""
        # field facet
        r = yield self.client.search('info_t:manga', facet='true',
                                     facet_field='category')

        category_facet = r.facet_counts['facet_fields']['category']

        self.assertEqual(len(category_facet), 16, 'Unexpected facet')

        # query facet
        # FIXME: current api does not allow multiple facet queries or fields
        r = yield self.client.search('info_t:manga', facet='true',
                                     facet_query='popularity:[0 TO 8]')

        facet_queries = r.facet_counts['facet_queries']

        self.assertEqual(len(facet_queries), 1, 'Unexpected facet')

    @inlineCallbacks
    def tearDown(self):
        ids = [doc['id'] for doc in self.documents]

        yield self.client.delete(ids)
        yield self.client.commit()
コード例 #2
0
ファイル: test_client.py プロジェクト: ceronman/txsolr
class DeletingDocumentsTestCase(unittest.TestCase):

    def setUp(self):
        self.client = SolrClient(SOLR_URL)

    @inlineCallbacks
    def testDeleteOneDocumentByID(self):
        """L{SolrClient.delete} removes a document with the given id."""
        doc = {'id': _randomString(20),
               'name': _randomString(20)}

        # Fist add the document
        yield self.client.add(doc)
        yield self.client.commit()

        # Next delete the document
        yield self.client.delete(doc['id'])
        yield self.client.commit()

        r = yield self.client.search('id:%s' % doc['id'])
        self.assertEqual(r.results.numFound, 0,
                         "The document was not deleted")

        r = yield self.client.search('name:%s' % doc['name'])
        self.assertEqual(r.results.numFound, 0,
                         "The document was not deleted")

    @inlineCallbacks
    def testDeleteManyDocumentsByID(self):
        """L{SolrClient.delete} removes many document with the given ids."""
        name = _randomString(20)

        docs = []
        for _ in range(5):
            doc = {'id': _randomString(20),
                   'name': name}
            docs.append(doc)

        # Add the documents
        yield self.client.add(docs)
        yield self.client.commit()

        # Delete the documents
        ids = [doc['id'] for doc in docs]
        yield self.client.delete(ids)
        yield self.client.commit()

        r = yield self.client.search('name:%s' % name)
        self.assertEqual(r.results.numFound, 0,
                         'Document was not deleted')

        for doc in docs:
            r = yield self.client.search('id:%s' % doc['id'])
            self.assertEqual(r.results.numFound, 0,
                             'Document was not deleted')

    @inlineCallbacks
    def testDeleteOneDocumentByQuery(self):
        """
        L{SolrClient.deleteByQuery} removes one document matching the given
        query.
        """
        doc = {'id': _randomString(20),
               'name': _randomString(20)}

        # Fist add the document
        yield self.client.add(doc)
        yield self.client.commit()

        # Next delete the document
        yield self.client.deleteByQuery('id:%s' % doc['id'])
        yield self.client.commit()

        r = yield self.client.search('id:%s' % doc['id'])

        self.assertEqual(r.results.numFound, 0,
                         "The document was not deleted")

        r = yield self.client.search('name:%s' % doc['name'])

        self.assertEqual(r.results.numFound, 0,
                         "The document was not deleted")

    def testDeleteManyDocumentsByQuery(self):
        """
        L{SolrClient.deleteByQuery} removes all documents matching the given
        query.
        """
        name = _randomString(20)

        docs = []
        for _ in range(5):
            doc = {'id': _randomString(20),
                   'name': name}
            docs.append(doc)

        # Add the documents
        yield self.client.add(docs)
        yield self.client.commit()

        # Delete the documents
        yield self.client.deleteByQuery('name:%s' % name)
        yield self.client.commit()

        r = yield self.client.search('name:%s' % name)
        self.assertEqual(r.results.numFound, 0,
                         'Document was not deleted')

        for doc in docs:
            r = yield self.client.search('id:%s' % doc['id'])
            self.assertEqual(r.results.numFound, 0,
                             'Document was not deleted')