cubicweb/server/test/unittest_postgres.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 16 Mar 2016 11:56:32 +0100
changeset 11215 4e79acdc36a6
parent 11077 09be48c01fa4
child 11257 dede32213426
permissions -rw-r--r--
[schema] use json to serialize constraints Require yams 0.43: constraints are serialized to json, which means we need to recreate the actual checks in the database on upgrade. Temporary deps in tox.ini to pull respective changes in yams.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10439
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
     1
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8343
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
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    19
from datetime import datetime
9585
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    20
from threading import Thread
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
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10522
diff changeset
    22
from six.moves import range
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10522
diff changeset
    23
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
    24
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
    25
10812
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    26
import logilab.database as lgdb
10446
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
    27
from cubicweb import ValidationError
10439
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    28
from cubicweb.devtools import PostgresApptestConfiguration, startpgcluster, stoppgcluster
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
    29
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
    30
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
    31
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
    32
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
    33
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
    34
10439
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    35
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    36
def setUpModule():
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    37
    startpgcluster(__file__)
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    38
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    39
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    40
def tearDownModule():
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    41
    stoppgcluster(__file__)
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    42
45e18b4a7466 [devtools] change the way we start/stop postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    43
10455
ad0615d4500d [server/test] Add a test for db-statement-timeout option
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    44
class PostgresTimeoutConfiguration(PostgresApptestConfiguration):
10522
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
    45
    def __init__(self, *args, **kwargs):
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
    46
        self.default_sources = PostgresApptestConfiguration.default_sources.copy()
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
    47
        self.default_sources['system'] = PostgresApptestConfiguration.default_sources['system'].copy()
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
    48
        self.default_sources['system']['db-statement-timeout'] = 200
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
    49
        super(PostgresTimeoutConfiguration, self).__init__(*args, **kwargs)
10455
ad0615d4500d [server/test] Add a test for db-statement-timeout option
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    50
ad0615d4500d [server/test] Add a test for db-statement-timeout option
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    51
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
class PostgresFTITC(CubicWebTC):
10455
ad0615d4500d [server/test] Add a test for db-statement-timeout option
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
    53
    configcls = PostgresTimeoutConfiguration
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
    54
10812
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    55
    @classmethod
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    56
    def setUpClass(cls):
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    57
        cls.orig_connect_hooks = lgdb.SQL_CONNECT_HOOKS['postgres'][:]
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    58
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    59
    @classmethod
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    60
    def tearDownClass(cls):
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    61
        lgdb.SQL_CONNECT_HOOKS['postgres'] = cls.orig_connect_hooks
a747e211266b [server/test] don't leak timeout from one test class to the next
Julien Cristau <julien.cristau@logilab.fr>
parents: 10609
diff changeset
    62
9585
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    63
    def test_eid_range(self):
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    64
        # concurrent allocation of eid ranges
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    65
        source = self.session.repo.sources_by_uri['system']
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    66
        range1 = []
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    67
        range2 = []
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    68
        def allocate_eid_ranges(session, target):
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10522
diff changeset
    69
            for x in range(1, 10):
9585
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    70
                eid = source.create_eid(session, count=x)
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    71
                target.extend(range(eid-x, eid))
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    72
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    73
        t1 = Thread(target=lambda: allocate_eid_ranges(self.session, range1))
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    74
        t2 = Thread(target=lambda: allocate_eid_ranges(self.session, range2))
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    75
        t1.start()
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    76
        t2.start()
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    77
        t1.join()
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    78
        t2.join()
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    79
        self.assertEqual(range1, sorted(range1))
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    80
        self.assertEqual(range2, sorted(range2))
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    81
        self.assertEqual(set(), set(range1) & set(range2))
3f5b59527d31 [source/native] allow many eid creation per .create_eid call (closes #3526594)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9499
diff changeset
    82
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
    83
    def test_occurence_count(self):
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    84
        with self.admin_access.repo_cnx() as cnx:
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    85
            c1 = cnx.create_entity('Card', title=u'c1',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    86
                                   content=u'cubicweb cubicweb cubicweb')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    87
            c2 = cnx.create_entity('Card', title=u'c3',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    88
                                   content=u'cubicweb')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    89
            c3 = cnx.create_entity('Card', title=u'c2',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    90
                                   content=u'cubicweb cubicweb')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    91
            cnx.commit()
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    92
            self.assertEqual(cnx.execute('Card X ORDERBY FTIRANK(X) DESC '
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    93
                                         'WHERE X has_text "cubicweb"').rows,
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
    94
                             [[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
    95
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
    96
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
    97
    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
    98
        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
    99
            __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
   100
            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
   101
        with self.temporary_appobjects(CardIFTIndexableAdapter):
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   102
            with self.admin_access.repo_cnx() as cnx:
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   103
                c1 = cnx.create_entity('Card', title=u'c1',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   104
                                       content=u'cubicweb cubicweb cubicweb')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   105
                c2 = cnx.create_entity('Card', title=u'c2',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   106
                                       content=u'cubicweb cubicweb')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   107
                c3 = cnx.create_entity('Card', title=u'cubicweb',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   108
                                       content=u'autre chose')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   109
                cnx.commit()
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   110
                self.assertEqual(cnx.execute('Card X ORDERBY FTIRANK(X) DESC '
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   111
                                             'WHERE X has_text "cubicweb"').rows,
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   112
                                 [[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
   113
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
   114
    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
   115
        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
   116
            __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
   117
            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
   118
        with self.temporary_appobjects(PersonneIFTIndexableAdapter):
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   119
            with self.admin_access.repo_cnx() as cnx:
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   120
                c1 = cnx.create_entity('Personne', nom=u'c1', prenom=u'cubicweb')
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   121
                c2 = cnx.create_entity('Comment', content=u'cubicweb cubicweb',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   122
                                       comments=c1)
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   123
                c3 = cnx.create_entity('Comment', content=u'cubicweb cubicweb cubicweb',
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   124
                                       comments=c1)
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   125
                cnx.commit()
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   126
                self.assertEqual(cnx.execute('Any X ORDERBY FTIRANK(X) DESC '
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   127
                                             'WHERE X has_text "cubicweb"').rows,
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   128
                                  [[c1.eid,], [c3.eid,], [c2.eid,]])
6436
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   129
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
   130
    def test_tz_datetime(self):
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   131
        with self.admin_access.repo_cnx() as cnx:
11034
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   132
            bob = cnx.create_entity('Personne', nom=u'bob',
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   133
                                   tzdatenaiss=datetime(1977, 6, 7, 2, 0, tzinfo=FixedOffset(1)))
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   134
            datenaiss = cnx.execute("Any XD WHERE X nom 'bob', X tzdatenaiss XD")[0][0]
11034
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   135
            self.assertIsNotNone(datenaiss.tzinfo)
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   136
            self.assertEqual(datenaiss.utctimetuple()[:5], (1977, 6, 7, 1, 0))
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   137
            cnx.commit()
11034
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   138
            cnx.create_entity('Personne', nom=u'boby',
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   139
                              tzdatenaiss=datetime(1977, 6, 7, 2, 0))
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   140
            datenaiss = cnx.execute("Any XD WHERE X nom 'boby', X tzdatenaiss XD")[0][0]
11034
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   141
            self.assertIsNotNone(datenaiss.tzinfo)
9804
fe9ae959da08 [tests/postgres] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9585
diff changeset
   142
            self.assertEqual(datenaiss.utctimetuple()[:5], (1977, 6, 7, 2, 0))
11034
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   143
            rset = cnx.execute("Any X WHERE X tzdatenaiss %(d)s",
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   144
                               {'d': datetime(1977, 6, 7, 2, 0, tzinfo=FixedOffset(1))})
75d752e6daf7 [server] improve TZDatetime support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10812
diff changeset
   145
            self.assertEqual(rset.rows, [[bob.eid]])
7166
dde161937d3e [time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6818
diff changeset
   146
10446
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   147
    def test_constraint_validationerror(self):
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   148
        with self.admin_access.repo_cnx() as cnx:
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   149
            with cnx.allow_all_hooks_but('integrity'):
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   150
                with self.assertRaises(ValidationError) as cm:
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   151
                    cnx.execute("INSERT Note N: N type 'nogood'")
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   152
                self.assertEqual(cm.exception.errors,
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   153
                        {'type-subject': u'invalid value %(KEY-value)s, it must be one of %(KEY-choices)s'})
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   154
                self.assertEqual(cm.exception.msgargs,
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   155
                        {'type-subject-value': u'"nogood"',
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   156
                         'type-subject-choices': u'"todo", "a", "b", "T", "lalala"'})
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   157
10522
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
   158
    def test_statement_timeout(self):
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
   159
        with self.admin_access.repo_cnx() as cnx:
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
   160
            cnx.system_sql('select pg_sleep(0.1)')
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
   161
            with self.assertRaises(Exception):
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
   162
                cnx.system_sql('select pg_sleep(0.3)')
1660a0fa4f43 merge 3.20.9 into 3.21
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10470 10455
diff changeset
   163
10446
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10439
diff changeset
   164
10328
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   165
class PostgresLimitSizeTC(CubicWebTC):
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   166
    configcls = PostgresApptestConfiguration
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   167
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   168
    def test(self):
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   169
        with self.admin_access.repo_cnx() as cnx:
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   170
            def sql(string):
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   171
                return cnx.system_sql(string).fetchone()[0]
11077
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   172
            for html, size, expected in [
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   173
                ('<p>hello</p>', 20, '<p>hello</p>'),
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   174
                ('<p>hello</p>', 2, 'he...'),
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   175
                ('<br/>hello', 2, 'he...'),
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   176
                ('<span class=\"1\">he</span>llo', 2, 'he...'),
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   177
                ('<span>a>b</span>', 2, 'a>...'),
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   178
            ]:
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   179
                with self.subTest(html=html, size=size):
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   180
                    actual = sql("SELECT limit_size('%s', 'text/html', %d)" % (html, size))
09be48c01fa4 [test] Re-implement generative tests with subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   181
                    self.assertEqual(actual, expected)
10328
d7ff46d958f4 [postgres] fix limit_size regexp to properly remove one char tags like <p>.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9804
diff changeset
   182
10455
ad0615d4500d [server/test] Add a test for db-statement-timeout option
Julien Cristau <julien.cristau@logilab.fr>
parents: 10328
diff changeset
   183
6436
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   184
if __name__ == '__main__':
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   185
    from logilab.common.testlib import unittest_main
275e9f402ccc [test] missing main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   186
    unittest_main()