server/test/unittest_postgres.py
author David Douard <david.douard@logilab.fr>
Thu, 03 Oct 2013 15:13:08 +0200
branchstable
changeset 9248 909eb8b584c4
parent 8694 d901c36bcfce
child 9499 872d16c04c79
permissions -rw-r--r--
[pkg] prepare 3.17.8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8343
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     3
#
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     4
# This file is part of CubicWeb.
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     5
#
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
     9
# any later version.
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    10
#
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    14
# details.
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    15
#
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6bd8db130476 Fix a Python 2.5 regression introduced by previous commit
David Douard <david.douard@logilab.fr>
parents: 7228
diff changeset
    18
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
    19
import socket
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    20
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
    21
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
    22
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
    23
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
    24
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
    25
from cubicweb.devtools.testlib import CubicWebTC
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8139
diff changeset
    26
from cubicweb.predicates 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
    27
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
    28
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    29
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
    30
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    31
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
    32
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
    33
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
    34
    @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
    35
    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
    36
        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
    37
            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
    38
        cls.config = ApptestConfiguration('data', sourcefile='sources_postgres',
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    39
                                          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
    40
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
    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
    42
        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
    43
        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
    44
                               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
    45
        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
    46
                               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
    47
        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
    48
                               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
    49
        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
    50
        self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
8139
f9ebb6d1abc3 [server] use lgd.process_cursor to optimize processing of large resultsets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7228
diff changeset
    51
                         [(c1.eid,), (c3.eid,), (c2.eid,)])
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
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_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
    55
        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
    56
            __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
    57
            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
    58
        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
    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('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
    61
                                   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
    62
            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
    63
                                   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
    64
            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
    65
                                   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
    66
            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
    67
            self.assertEqual(req.execute('Card X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
8139
f9ebb6d1abc3 [server] use lgd.process_cursor to optimize processing of large resultsets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7228
diff changeset
    68
                             [(c3.eid,), (c1.eid,), (c2.eid,)])
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
    69
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
    70
    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
    71
        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
    72
            __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
    73
            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
    74
        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
    75
            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
    76
            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
    77
            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
    78
            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
    79
            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
    80
            self.assertEqual(req.execute('Any X ORDERBY FTIRANK(X) DESC WHERE X has_text "cubicweb"').rows,
8139
f9ebb6d1abc3 [server] use lgd.process_cursor to optimize processing of large resultsets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7228
diff changeset
    81
                              [(c1.eid,), (c3.eid,), (c2.eid,)])
6436
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    82
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
    83
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    84
    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
    85
        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
    86
                     {'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
    87
        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
    88
        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
    89
        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
    90
        self.commit()
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    91
        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
    92
                     {'date': datetime(1977, 6, 7, 2, 0)})
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    93
        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
    94
        self.assertEqual(datenaiss.tzinfo, None)
9d04e12d2d39 [tz support] more tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7211
diff changeset
    95
        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
    96
6436
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    97
if __name__ == '__main__':
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    98
    from logilab.common.testlib import unittest_main
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
    99
    unittest_main()