|
1 from __future__ import with_statement |
|
2 |
|
3 import socket |
|
4 from datetime import datetime |
|
5 |
|
6 from logilab.common.testlib import SkipTest |
|
7 |
|
8 from cubicweb.devtools import ApptestConfiguration |
|
9 from cubicweb.devtools.testlib import CubicWebTC |
|
10 from cubicweb.selectors import is_instance |
|
11 from cubicweb.entities.adapters import IFTIndexableAdapter |
|
12 |
|
13 AT_LOGILAB = socket.gethostname().endswith('.logilab.fr') # XXX |
|
14 |
|
15 from unittest_querier import FixedOffset |
|
16 |
|
17 class PostgresFTITC(CubicWebTC): |
|
18 config = ApptestConfiguration('data', sourcefile='sources_postgres') |
|
19 |
|
20 @classmethod |
|
21 def setUpClass(cls): |
|
22 if not AT_LOGILAB: # XXX here until we can raise SkipTest in setUp to detect we can't connect to the db |
|
23 raise SkipTest('XXX %s: require logilab configuration' % cls.__name__) |
|
24 |
|
25 def test_occurence_count(self): |
|
26 req = self.request() |
|
27 c1 = req.create_entity('Card', title=u'c1', |
|
28 content=u'cubicweb cubicweb cubicweb') |
|
29 c2 = req.create_entity('Card', title=u'c3', |
|
30 content=u'cubicweb') |
|
31 c3 = req.create_entity('Card', title=u'c2', |
|
32 content=u'cubicweb cubicweb') |
|
33 self.commit() |
|
34 self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows, |
|
35 [[c1.eid], [c3.eid], [c2.eid]]) |
|
36 |
|
37 |
|
38 def test_attr_weight(self): |
|
39 class CardIFTIndexableAdapter(IFTIndexableAdapter): |
|
40 __select__ = is_instance('Card') |
|
41 attr_weight = {'title': 'A'} |
|
42 with self.temporary_appobjects(CardIFTIndexableAdapter): |
|
43 req = self.request() |
|
44 c1 = req.create_entity('Card', title=u'c1', |
|
45 content=u'cubicweb cubicweb cubicweb') |
|
46 c2 = req.create_entity('Card', title=u'c2', |
|
47 content=u'cubicweb cubicweb') |
|
48 c3 = req.create_entity('Card', title=u'cubicweb', |
|
49 content=u'autre chose') |
|
50 self.commit() |
|
51 self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows, |
|
52 [[c3.eid], [c1.eid], [c2.eid]]) |
|
53 |
|
54 def test_entity_weight(self): |
|
55 class PersonneIFTIndexableAdapter(IFTIndexableAdapter): |
|
56 __select__ = is_instance('Personne') |
|
57 entity_weight = 2.0 |
|
58 with self.temporary_appobjects(PersonneIFTIndexableAdapter): |
|
59 req = self.request() |
|
60 c1 = req.create_entity('Personne', nom=u'c1', prenom=u'cubicweb') |
|
61 c2 = req.create_entity('Comment', content=u'cubicweb cubicweb', comments=c1) |
|
62 c3 = req.create_entity('Comment', content=u'cubicweb cubicweb cubicweb', comments=c1) |
|
63 self.commit() |
|
64 self.assertEqual(req.execute('Any X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows, |
|
65 [[c1.eid], [c3.eid], [c2.eid]]) |
|
66 |
|
67 |
|
68 def test_tz_datetime(self): |
|
69 self.execute("INSERT Personne X: X nom 'bob', X tzdatenaiss %(date)s", |
|
70 {'date': datetime(1977, 6, 7, 2, 0, tzinfo=FixedOffset(1))}) |
|
71 datenaiss = self.execute("Any XD WHERE X nom 'bob', X tzdatenaiss XD")[0][0] |
|
72 self.assertEqual(datenaiss.utctimetuple()[:5], (1977, 6, 7, 1, 0)) |
|
73 |
|
74 |
|
75 if __name__ == '__main__': |
|
76 from logilab.common.testlib import unittest_main |
|
77 unittest_main() |