server/test/unittest_fti.py
changeset 7166 dde161937d3e
parent 7165 b817d44cb606
child 7167 fd502219eb76
equal deleted inserted replaced
7165:b817d44cb606 7166:dde161937d3e
     1 from __future__ import with_statement
       
     2 
       
     3 import socket
       
     4 
       
     5 from logilab.common.testlib import SkipTest
       
     6 
       
     7 from cubicweb.devtools import ApptestConfiguration
       
     8 from cubicweb.devtools.testlib import CubicWebTC
       
     9 from cubicweb.selectors import is_instance
       
    10 from cubicweb.entities.adapters import IFTIndexableAdapter
       
    11 
       
    12 AT_LOGILAB = socket.gethostname().endswith('.logilab.fr')
       
    13 
       
    14 
       
    15 class PostgresFTITC(CubicWebTC):
       
    16     config = ApptestConfiguration('data', sourcefile='sources_fti')
       
    17 
       
    18     @classmethod
       
    19     def setUpClass(cls):
       
    20         if not AT_LOGILAB:
       
    21             raise SkipTest('XXX %s: require logilab configuration' % cls.__name__)
       
    22 
       
    23     def test_occurence_count(self):
       
    24         req = self.request()
       
    25         c1 = req.create_entity('Card', title=u'c1',
       
    26                                content=u'cubicweb cubicweb cubicweb')
       
    27         c2 = req.create_entity('Card', title=u'c3',
       
    28                                content=u'cubicweb')
       
    29         c3 = req.create_entity('Card', title=u'c2',
       
    30                                content=u'cubicweb cubicweb')
       
    31         self.commit()
       
    32         self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
       
    33                           [[c1.eid], [c3.eid], [c2.eid]])
       
    34 
       
    35 
       
    36     def test_attr_weight(self):
       
    37         class CardIFTIndexableAdapter(IFTIndexableAdapter):
       
    38             __select__ = is_instance('Card')
       
    39             attr_weight = {'title': 'A'}
       
    40         with self.temporary_appobjects(CardIFTIndexableAdapter):
       
    41             req = self.request()
       
    42             c1 = req.create_entity('Card', title=u'c1',
       
    43                                    content=u'cubicweb cubicweb cubicweb')
       
    44             c2 = req.create_entity('Card', title=u'c2',
       
    45                                    content=u'cubicweb cubicweb')
       
    46             c3 = req.create_entity('Card', title=u'cubicweb',
       
    47                                    content=u'autre chose')
       
    48             self.commit()
       
    49             self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
       
    50                               [[c3.eid], [c1.eid], [c2.eid]])
       
    51 
       
    52     def test_entity_weight(self):
       
    53         class PersonneIFTIndexableAdapter(IFTIndexableAdapter):
       
    54             __select__ = is_instance('Personne')
       
    55             entity_weight = 2.0
       
    56         with self.temporary_appobjects(PersonneIFTIndexableAdapter):
       
    57             req = self.request()
       
    58             c1 = req.create_entity('Personne', nom=u'c1', prenom=u'cubicweb')
       
    59             c2 = req.create_entity('Comment', content=u'cubicweb cubicweb', comments=c1)
       
    60             c3 = req.create_entity('Comment', content=u'cubicweb cubicweb cubicweb', comments=c1)
       
    61             self.commit()
       
    62             self.assertEqual(req.execute('Any X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
       
    63                               [[c1.eid], [c3.eid], [c2.eid]])
       
    64 
       
    65 
       
    66 if __name__ == '__main__':
       
    67     from logilab.common.testlib import unittest_main
       
    68     unittest_main()