server/test/unittest_postgres.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 21 Oct 2011 13:05:45 +0200
changeset 7988 d72a2881d53c
parent 7228 9d04e12d2d39
child 8139 f9ebb6d1abc3
child 8343 6bd8db130476
permissions -rw-r--r--
[forms] update code broken by linkto refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
from __future__ import with_statement
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
6206
d4c73211b0b8 [test] skip unittest_fti if we don't have the required setup...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
     3
import socket
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
     4
from datetime import datetime
6206
d4c73211b0b8 [test] skip unittest_fti if we don't have the required setup...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
     5
6818
5fa425574548 [server test] can't use skipTest method as a class method, raise appropriate exception instead
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6780
diff changeset
     6
from logilab.common.testlib import SkipTest
5fa425574548 [server test] can't use skipTest method as a class method, raise appropriate exception instead
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6780
diff changeset
     7
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
from cubicweb.devtools import ApptestConfiguration
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
from cubicweb.devtools.testlib import CubicWebTC
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
    10
from cubicweb.selectors import is_instance
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
from cubicweb.entities.adapters import IFTIndexableAdapter
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    13
AT_LOGILAB = socket.gethostname().endswith('.logilab.fr') # XXX
6780
92e4c07f86c0 [server/test] raise SkipTest from setUpModule instead of setUp( unittest2 behaviour) + kill some deprecation warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6436
diff changeset
    14
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    15
from unittest_querier import FixedOffset
6780
92e4c07f86c0 [server/test] raise SkipTest from setUpModule instead of setUp( unittest2 behaviour) + kill some deprecation warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6436
diff changeset
    16
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
class PostgresFTITC(CubicWebTC):
6780
92e4c07f86c0 [server/test] raise SkipTest from setUpModule instead of setUp( unittest2 behaviour) + kill some deprecation warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6436
diff changeset
    18
    @classmethod
92e4c07f86c0 [server/test] raise SkipTest from setUpModule instead of setUp( unittest2 behaviour) + kill some deprecation warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6436
diff changeset
    19
    def setUpClass(cls):
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    20
        if not AT_LOGILAB: # XXX here until we can raise SkipTest in setUp to detect we can't connect to the db
6780
92e4c07f86c0 [server/test] raise SkipTest from setUpModule instead of setUp( unittest2 behaviour) + kill some deprecation warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6436
diff changeset
    21
            raise SkipTest('XXX %s: require logilab configuration' % cls.__name__)
7228
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    22
        cls.config = ApptestConfiguration('data', sourcefile='sources_postgres',
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    23
                                          apphome=cls.datadir)
6206
d4c73211b0b8 [test] skip unittest_fti if we don't have the required setup...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
    24
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
    def test_occurence_count(self):
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
        req = self.request()
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
        c1 = req.create_entity('Card', title=u'c1',
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
                               content=u'cubicweb cubicweb cubicweb')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
        c2 = req.create_entity('Card', title=u'c3',
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
                               content=u'cubicweb')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
        c3 = req.create_entity('Card', title=u'c2',
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
                               content=u'cubicweb cubicweb')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
        self.commit()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6206
diff changeset
    34
        self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
                          [[c1.eid], [c3.eid], [c2.eid]])
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
    def test_attr_weight(self):
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        class CardIFTIndexableAdapter(IFTIndexableAdapter):
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
    40
            __select__ = is_instance('Card')
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
            attr_weight = {'title': 'A'}
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
        with self.temporary_appobjects(CardIFTIndexableAdapter):
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
            req = self.request()
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
            c1 = req.create_entity('Card', title=u'c1',
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
                                   content=u'cubicweb cubicweb cubicweb')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
            c2 = req.create_entity('Card', title=u'c2',
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
                                   content=u'cubicweb cubicweb')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
            c3 = req.create_entity('Card', title=u'cubicweb',
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
                                   content=u'autre chose')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
            self.commit()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6206
diff changeset
    51
            self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
                              [[c3.eid], [c1.eid], [c2.eid]])
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
    def test_entity_weight(self):
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
        class PersonneIFTIndexableAdapter(IFTIndexableAdapter):
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
    56
            __select__ = is_instance('Personne')
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
            entity_weight = 2.0
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
        with self.temporary_appobjects(PersonneIFTIndexableAdapter):
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
            req = self.request()
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
            c1 = req.create_entity('Personne', nom=u'c1', prenom=u'cubicweb')
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
            c2 = req.create_entity('Comment', content=u'cubicweb cubicweb', comments=c1)
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
            c3 = req.create_entity('Comment', content=u'cubicweb cubicweb cubicweb', comments=c1)
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
            self.commit()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6206
diff changeset
    64
            self.assertEqual(req.execute('Any X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
                              [[c1.eid], [c3.eid], [c2.eid]])
6436
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    66
6780
92e4c07f86c0 [server/test] raise SkipTest from setUpModule instead of setUp( unittest2 behaviour) + kill some deprecation warnings
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6436
diff changeset
    67
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    68
    def test_tz_datetime(self):
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    69
        self.execute("INSERT Personne X: X nom 'bob', X tzdatenaiss %(date)s",
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    70
                     {'date': datetime(1977, 6, 7, 2, 0, tzinfo=FixedOffset(1))})
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    71
        datenaiss = self.execute("Any XD WHERE X nom 'bob', X tzdatenaiss XD")[0][0]
7211
cacff15f847d [timezone] ensure tzinfo is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7166
diff changeset
    72
        self.assertEqual(datenaiss.tzinfo, None)
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    73
        self.assertEqual(datenaiss.utctimetuple()[:5], (1977, 6, 7, 1, 0))
7228
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    74
        self.commit()
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    75
        self.execute("INSERT Personne X: X nom 'boby', X tzdatenaiss %(date)s",
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    76
                     {'date': datetime(1977, 6, 7, 2, 0)})
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    77
        datenaiss = self.execute("Any XD WHERE X nom 'boby', X tzdatenaiss XD")[0][0]
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    78
        self.assertEqual(datenaiss.tzinfo, None)
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    79
        self.assertEqual(datenaiss.utctimetuple()[:5], (1977, 6, 7, 2, 0))
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    80
6436
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    81
if __name__ == '__main__':
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    82
    from logilab.common.testlib import unittest_main
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    83
    unittest_main()