2011-01-07 04:44:19 +03:00
|
|
|
# testutils.py - utility module for psycopg2 testing.
|
2011-02-12 03:40:23 +03:00
|
|
|
|
2011-01-07 04:44:19 +03:00
|
|
|
#
|
2019-02-17 04:34:52 +03:00
|
|
|
# Copyright (C) 2010-2019 Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
2021-06-15 02:37:22 +03:00
|
|
|
# Copyright (C) 2020-2021 The Psycopg Team
|
2011-01-07 04:44:19 +03:00
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
|
2017-02-06 21:43:39 +03:00
|
|
|
import re
|
2011-02-11 01:59:31 +03:00
|
|
|
import os
|
|
|
|
import sys
|
2018-10-30 03:23:56 +03:00
|
|
|
import types
|
2019-02-16 19:30:34 +03:00
|
|
|
import ctypes
|
2015-10-15 19:01:43 +03:00
|
|
|
import select
|
2020-08-18 01:08:05 +03:00
|
|
|
import operator
|
2017-02-06 21:43:39 +03:00
|
|
|
import platform
|
2017-03-01 23:44:04 +03:00
|
|
|
import unittest
|
2013-03-20 21:17:10 +04:00
|
|
|
from functools import wraps
|
2019-02-16 19:30:34 +03:00
|
|
|
from ctypes.util import find_library
|
2020-11-17 21:39:39 +03:00
|
|
|
from io import StringIO # noqa
|
|
|
|
from io import TextIOBase # noqa
|
|
|
|
from importlib import reload # noqa
|
2019-03-16 21:56:56 +03:00
|
|
|
|
|
|
|
import psycopg2
|
2019-03-16 22:07:27 +03:00
|
|
|
import psycopg2.errors
|
2019-03-16 21:56:56 +03:00
|
|
|
import psycopg2.extensions
|
2011-02-11 01:59:31 +03:00
|
|
|
|
2019-03-16 21:56:56 +03:00
|
|
|
from .testconfig import green, dsn, repl_dsn
|
2019-03-16 18:30:15 +03:00
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
|
2015-03-16 11:50:20 +03:00
|
|
|
# Silence warnings caused by the stubbornness of the Python unittest
|
|
|
|
# maintainers
|
2018-09-23 04:54:55 +03:00
|
|
|
# https://bugs.python.org/issue9424
|
2016-10-11 02:10:53 +03:00
|
|
|
if (not hasattr(unittest.TestCase, 'assert_')
|
|
|
|
or unittest.TestCase.assert_ is not unittest.TestCase.assertTrue):
|
2011-02-11 01:59:31 +03:00
|
|
|
# mavaff...
|
|
|
|
unittest.TestCase.assert_ = unittest.TestCase.assertTrue
|
|
|
|
unittest.TestCase.failUnless = unittest.TestCase.assertTrue
|
|
|
|
unittest.TestCase.assertEquals = unittest.TestCase.assertEqual
|
|
|
|
unittest.TestCase.failUnlessEqual = unittest.TestCase.assertEqual
|
|
|
|
|
2010-11-19 06:55:37 +03:00
|
|
|
|
2017-02-06 21:56:50 +03:00
|
|
|
def assertDsnEqual(self, dsn1, dsn2, msg=None):
|
|
|
|
"""Check that two conninfo string have the same content"""
|
|
|
|
self.assertEqual(set(dsn1.split()), set(dsn2.split()), msg)
|
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2017-02-06 21:56:50 +03:00
|
|
|
unittest.TestCase.assertDsnEqual = assertDsnEqual
|
|
|
|
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
class ConnectingTestCase(unittest.TestCase):
|
|
|
|
"""A test case providing connections for tests.
|
|
|
|
|
|
|
|
A connection for the test is always available as `self.conn`. Others can be
|
|
|
|
created with `self.connect()`. All are closed on tearDown.
|
|
|
|
|
|
|
|
Subclasses needing to customize setUp and tearDown should remember to call
|
|
|
|
the base class implementations.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
self._conns = []
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# close the connections used in the test
|
|
|
|
for conn in self._conns:
|
|
|
|
if not conn.closed:
|
|
|
|
conn.close()
|
|
|
|
|
2017-02-06 21:43:39 +03:00
|
|
|
def assertQuotedEqual(self, first, second, msg=None):
|
|
|
|
"""Compare two quoted strings disregarding eventual E'' quotes"""
|
|
|
|
def f(s):
|
2020-11-17 21:39:39 +03:00
|
|
|
if isinstance(s, str):
|
2017-02-06 21:43:39 +03:00
|
|
|
return re.sub(r"\bE'", "'", s)
|
|
|
|
elif isinstance(first, bytes):
|
|
|
|
return re.sub(br"\bE'", b"'", s)
|
|
|
|
else:
|
|
|
|
return s
|
|
|
|
|
|
|
|
return self.assertEqual(f(first), f(second), msg)
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
def connect(self, **kwargs):
|
|
|
|
try:
|
|
|
|
self._conns
|
2017-11-21 07:00:35 +03:00
|
|
|
except AttributeError as e:
|
2013-04-07 03:23:30 +04:00
|
|
|
raise AttributeError(
|
2020-11-18 00:52:11 +03:00
|
|
|
f"{e} (did you forget to call ConnectingTestCase.setUp()?)")
|
2013-04-07 03:23:30 +04:00
|
|
|
|
2015-09-30 14:15:35 +03:00
|
|
|
if 'dsn' in kwargs:
|
|
|
|
conninfo = kwargs.pop('dsn')
|
|
|
|
else:
|
|
|
|
conninfo = dsn
|
|
|
|
conn = psycopg2.connect(conninfo, **kwargs)
|
2013-04-07 03:23:30 +04:00
|
|
|
self._conns.append(conn)
|
|
|
|
return conn
|
|
|
|
|
2015-09-30 14:15:35 +03:00
|
|
|
def repl_connect(self, **kwargs):
|
|
|
|
"""Return a connection set up for replication
|
|
|
|
|
|
|
|
The connection is on "PSYCOPG2_TEST_REPL_DSN" unless overridden by
|
|
|
|
a *dsn* kwarg.
|
|
|
|
|
|
|
|
Should raise a skip test if not available, but guard for None on
|
|
|
|
old Python versions.
|
|
|
|
"""
|
2016-12-24 02:18:22 +03:00
|
|
|
if repl_dsn is None:
|
|
|
|
return self.skipTest("replication tests disabled by default")
|
|
|
|
|
2015-09-30 14:15:35 +03:00
|
|
|
if 'dsn' not in kwargs:
|
|
|
|
kwargs['dsn'] = repl_dsn
|
|
|
|
try:
|
|
|
|
conn = self.connect(**kwargs)
|
2017-02-03 07:28:27 +03:00
|
|
|
if conn.async_ == 1:
|
2016-12-25 19:46:11 +03:00
|
|
|
self.wait(conn)
|
2017-11-21 07:00:35 +03:00
|
|
|
except psycopg2.OperationalError as e:
|
2016-12-25 19:46:11 +03:00
|
|
|
# If pgcode is not set it is a genuine connection error
|
|
|
|
# Otherwise we tried to run some bad operation in the connection
|
|
|
|
# (e.g. bug #482) and we'd rather know that.
|
|
|
|
if e.pgcode is None:
|
2020-11-18 00:52:11 +03:00
|
|
|
return self.skipTest(f"replication db not configured: {e}")
|
2016-12-25 19:46:11 +03:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2015-09-30 14:15:35 +03:00
|
|
|
return conn
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
def _get_conn(self):
|
|
|
|
if not hasattr(self, '_the_conn'):
|
|
|
|
self._the_conn = self.connect()
|
|
|
|
|
|
|
|
return self._the_conn
|
|
|
|
|
|
|
|
def _set_conn(self, conn):
|
|
|
|
self._the_conn = conn
|
|
|
|
|
|
|
|
conn = property(_get_conn, _set_conn)
|
|
|
|
|
2015-10-15 19:01:43 +03:00
|
|
|
# for use with async connections only
|
|
|
|
def wait(self, cur_or_conn):
|
|
|
|
pollable = cur_or_conn
|
|
|
|
if not hasattr(pollable, 'poll'):
|
|
|
|
pollable = cur_or_conn.connection
|
|
|
|
while True:
|
|
|
|
state = pollable.poll()
|
|
|
|
if state == psycopg2.extensions.POLL_OK:
|
|
|
|
break
|
|
|
|
elif state == psycopg2.extensions.POLL_READ:
|
2019-03-06 04:43:06 +03:00
|
|
|
select.select([pollable], [], [], 1)
|
2015-10-15 19:01:43 +03:00
|
|
|
elif state == psycopg2.extensions.POLL_WRITE:
|
2019-03-06 04:43:06 +03:00
|
|
|
select.select([], [pollable], [], 1)
|
2015-10-15 19:01:43 +03:00
|
|
|
else:
|
|
|
|
raise Exception("Unexpected result from poll: %r", state)
|
|
|
|
|
2019-02-16 19:30:34 +03:00
|
|
|
_libpq = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def libpq(self):
|
|
|
|
"""Return a ctypes wrapper for the libpq library"""
|
|
|
|
if ConnectingTestCase._libpq is not None:
|
|
|
|
return ConnectingTestCase._libpq
|
|
|
|
|
|
|
|
libname = find_library('pq')
|
2019-02-16 20:07:28 +03:00
|
|
|
if libname is None and platform.system() == 'Windows':
|
|
|
|
raise self.skipTest("can't import libpq on windows")
|
|
|
|
|
2021-05-24 00:26:48 +03:00
|
|
|
try:
|
|
|
|
rv = ConnectingTestCase._libpq = ctypes.pydll.LoadLibrary(libname)
|
|
|
|
except OSError as e:
|
|
|
|
raise self.skipTest("couldn't open libpq for testing: %s" % e)
|
2019-02-16 19:30:34 +03:00
|
|
|
return rv
|
|
|
|
|
2013-04-07 03:23:30 +04:00
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
def decorate_all_tests(obj, *decorators):
|
2013-04-07 03:23:30 +04:00
|
|
|
"""
|
2018-10-30 03:23:56 +03:00
|
|
|
Apply all the *decorators* to all the tests defined in the TestCase *obj*.
|
|
|
|
|
|
|
|
The decorator can also be applied to a decorator: if *obj* is a function,
|
|
|
|
return a new decorator which can be applied either to a method or to a
|
|
|
|
class, in which case it will decorate all the tests.
|
2013-04-07 03:23:30 +04:00
|
|
|
"""
|
2018-10-30 03:23:56 +03:00
|
|
|
if isinstance(obj, types.FunctionType):
|
|
|
|
def decorator(func_or_cls):
|
|
|
|
if isinstance(func_or_cls, types.FunctionType):
|
|
|
|
return obj(func_or_cls)
|
|
|
|
else:
|
|
|
|
decorate_all_tests(func_or_cls, obj)
|
|
|
|
return func_or_cls
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
for n in dir(obj):
|
2010-11-19 06:55:37 +03:00
|
|
|
if n.startswith('test'):
|
2013-04-07 03:23:30 +04:00
|
|
|
for d in decorators:
|
2018-10-30 03:23:56 +03:00
|
|
|
setattr(obj, n, d(getattr(obj, n)))
|
2010-11-28 18:03:34 +03:00
|
|
|
|
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@decorate_all_tests
|
2011-01-18 04:46:10 +03:00
|
|
|
def skip_if_no_uuid(f):
|
2019-03-16 21:56:56 +03:00
|
|
|
"""Decorator to skip a test if uuid is not supported by PG."""
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2011-01-18 04:46:10 +03:00
|
|
|
def skip_if_no_uuid_(self):
|
|
|
|
try:
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute("select typname from pg_type where typname = 'uuid'")
|
|
|
|
has = cur.fetchone()
|
|
|
|
finally:
|
|
|
|
self.conn.rollback()
|
|
|
|
|
|
|
|
if has:
|
|
|
|
return f(self)
|
|
|
|
else:
|
|
|
|
return self.skipTest("uuid type not available on the server")
|
|
|
|
|
|
|
|
return skip_if_no_uuid_
|
|
|
|
|
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@decorate_all_tests
|
2011-01-10 02:44:58 +03:00
|
|
|
def skip_if_tpc_disabled(f):
|
|
|
|
"""Skip a test if the server has tpc support disabled."""
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2011-01-10 02:44:58 +03:00
|
|
|
def skip_if_tpc_disabled_(self):
|
|
|
|
cnn = self.connect()
|
2020-07-28 00:58:43 +03:00
|
|
|
skip_if_crdb("2-phase commit", cnn)
|
2020-07-21 05:00:50 +03:00
|
|
|
|
2011-01-10 02:44:58 +03:00
|
|
|
cur = cnn.cursor()
|
|
|
|
try:
|
|
|
|
cur.execute("SHOW max_prepared_transactions;")
|
2019-03-16 21:56:56 +03:00
|
|
|
except psycopg2.ProgrammingError:
|
2011-01-10 02:44:58 +03:00
|
|
|
return self.skipTest(
|
|
|
|
"server too old: two phase transactions not supported.")
|
|
|
|
else:
|
|
|
|
mtp = int(cur.fetchone()[0])
|
|
|
|
cnn.close()
|
|
|
|
|
|
|
|
if not mtp:
|
|
|
|
return self.skipTest(
|
|
|
|
"server not configured for two phase transactions. "
|
|
|
|
"set max_prepared_transactions to > 0 to run the test")
|
|
|
|
return f(self)
|
|
|
|
|
|
|
|
return skip_if_tpc_disabled_
|
|
|
|
|
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_before_postgres(*ver):
|
|
|
|
"""Skip a test on PostgreSQL before a certain version."""
|
2018-10-30 03:23:56 +03:00
|
|
|
reason = None
|
|
|
|
if isinstance(ver[-1], str):
|
|
|
|
ver, reason = ver[:-1], ver[-1]
|
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
ver = ver + (0,) * (3 - len(ver))
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@decorate_all_tests
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_before_postgres_(f):
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_before_postgres__(self):
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version < int("%d%02d%02d" % ver):
|
2018-10-30 03:23:56 +03:00
|
|
|
return self.skipTest(
|
|
|
|
reason or "skipped because PostgreSQL %s"
|
2018-10-13 05:28:42 +03:00
|
|
|
% self.conn.info.server_version)
|
2011-02-15 20:11:07 +03:00
|
|
|
else:
|
|
|
|
return f(self)
|
|
|
|
|
|
|
|
return skip_before_postgres__
|
|
|
|
return skip_before_postgres_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_after_postgres(*ver):
|
|
|
|
"""Skip a test on PostgreSQL after (including) a certain version."""
|
|
|
|
ver = ver + (0,) * (3 - len(ver))
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@decorate_all_tests
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_after_postgres_(f):
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_after_postgres__(self):
|
2018-10-13 05:28:42 +03:00
|
|
|
if self.conn.info.server_version >= int("%d%02d%02d" % ver):
|
2011-02-15 20:11:07 +03:00
|
|
|
return self.skipTest("skipped because PostgreSQL %s"
|
2018-10-13 05:28:42 +03:00
|
|
|
% self.conn.info.server_version)
|
2011-02-15 20:11:07 +03:00
|
|
|
else:
|
|
|
|
return f(self)
|
|
|
|
|
|
|
|
return skip_after_postgres__
|
|
|
|
return skip_after_postgres_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2015-06-01 19:05:11 +03:00
|
|
|
def libpq_version():
|
|
|
|
v = psycopg2.__libpq_version__
|
|
|
|
if v >= 90100:
|
2017-03-02 20:52:29 +03:00
|
|
|
v = min(v, psycopg2.extensions.libpq_version())
|
2015-06-01 19:05:11 +03:00
|
|
|
return v
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2015-06-01 19:05:11 +03:00
|
|
|
def skip_before_libpq(*ver):
|
|
|
|
"""Skip a test if libpq we're linked to is older than a certain version."""
|
|
|
|
ver = ver + (0,) * (3 - len(ver))
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_before_libpq_(cls):
|
|
|
|
v = libpq_version()
|
|
|
|
decorator = unittest.skipIf(
|
|
|
|
v < int("%d%02d%02d" % ver),
|
2020-11-18 19:09:08 +03:00
|
|
|
f"skipped because libpq {v}",
|
2018-12-01 22:34:01 +03:00
|
|
|
)
|
|
|
|
return decorator(cls)
|
2015-06-01 19:05:11 +03:00
|
|
|
return skip_before_libpq_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2015-06-01 19:05:11 +03:00
|
|
|
def skip_after_libpq(*ver):
|
|
|
|
"""Skip a test if libpq we're linked to is newer than a certain version."""
|
|
|
|
ver = ver + (0,) * (3 - len(ver))
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_after_libpq_(cls):
|
|
|
|
v = libpq_version()
|
|
|
|
decorator = unittest.skipIf(
|
|
|
|
v >= int("%d%02d%02d" % ver),
|
2020-11-18 00:52:11 +03:00
|
|
|
f"skipped because libpq {v}",
|
2018-12-01 22:34:01 +03:00
|
|
|
)
|
|
|
|
return decorator(cls)
|
2015-06-01 19:05:11 +03:00
|
|
|
return skip_after_libpq_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_before_python(*ver):
|
|
|
|
"""Skip a test on Python before a certain version."""
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_before_python_(cls):
|
|
|
|
decorator = unittest.skipIf(
|
|
|
|
sys.version_info[:len(ver)] < ver,
|
2020-11-18 00:52:11 +03:00
|
|
|
f"skipped because Python {'.'.join(map(str, sys.version_info[:len(ver)]))}",
|
2018-12-01 22:34:01 +03:00
|
|
|
)
|
|
|
|
return decorator(cls)
|
2011-02-15 20:11:07 +03:00
|
|
|
return skip_before_python_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2011-02-15 20:11:07 +03:00
|
|
|
def skip_from_python(*ver):
|
|
|
|
"""Skip a test on Python after (including) a certain version."""
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_from_python_(cls):
|
|
|
|
decorator = unittest.skipIf(
|
|
|
|
sys.version_info[:len(ver)] >= ver,
|
2020-11-18 00:52:11 +03:00
|
|
|
f"skipped because Python {'.'.join(map(str, sys.version_info[:len(ver)]))}",
|
2018-12-01 22:34:01 +03:00
|
|
|
)
|
|
|
|
return decorator(cls)
|
2011-02-15 20:11:07 +03:00
|
|
|
return skip_from_python_
|
2011-02-11 01:59:31 +03:00
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-10-30 03:23:56 +03:00
|
|
|
@decorate_all_tests
|
2013-03-16 15:56:38 +04:00
|
|
|
def skip_if_no_superuser(f):
|
|
|
|
"""Skip a test if the database user running the test is not a superuser"""
|
2013-03-20 21:17:10 +04:00
|
|
|
@wraps(f)
|
2013-03-16 15:56:38 +04:00
|
|
|
def skip_if_no_superuser_(self):
|
|
|
|
try:
|
|
|
|
return f(self)
|
2019-03-16 22:07:27 +03:00
|
|
|
except psycopg2.errors.InsufficientPrivilege:
|
|
|
|
self.skipTest("skipped because not superuser")
|
2013-03-16 15:56:38 +04:00
|
|
|
|
|
|
|
return skip_if_no_superuser_
|
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2013-03-20 21:17:10 +04:00
|
|
|
def skip_if_green(reason):
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_if_green_(cls):
|
|
|
|
decorator = unittest.skipIf(green, reason)
|
|
|
|
return decorator(cls)
|
2013-03-20 21:17:10 +04:00
|
|
|
return skip_if_green_
|
2013-03-18 14:06:07 +04:00
|
|
|
|
2018-10-23 02:39:14 +03:00
|
|
|
|
2013-03-20 21:17:10 +04:00
|
|
|
skip_copy_if_green = skip_if_green("copy in async mode currently not supported")
|
2011-02-11 01:59:31 +03:00
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_if_no_getrefcount(cls):
|
|
|
|
decorator = unittest.skipUnless(
|
|
|
|
hasattr(sys, 'getrefcount'),
|
|
|
|
'no sys.getrefcount()',
|
|
|
|
)
|
|
|
|
return decorator(cls)
|
2013-05-06 13:39:24 +04:00
|
|
|
|
2016-10-11 02:10:53 +03:00
|
|
|
|
2018-12-01 22:34:01 +03:00
|
|
|
def skip_if_windows(cls):
|
2014-05-13 02:12:50 +04:00
|
|
|
"""Skip a test if run on windows"""
|
2018-12-01 22:34:01 +03:00
|
|
|
decorator = unittest.skipIf(
|
|
|
|
platform.system() == 'Windows',
|
|
|
|
"Not supported on Windows",
|
|
|
|
)
|
|
|
|
return decorator(cls)
|
2014-05-13 02:12:50 +04:00
|
|
|
|
|
|
|
|
2020-07-21 03:42:34 +03:00
|
|
|
def crdb_version(conn, __crdb_version=[]):
|
|
|
|
"""
|
2020-07-21 04:15:53 +03:00
|
|
|
Return the CockroachDB version if that's the db being tested, else None.
|
2020-07-21 03:42:34 +03:00
|
|
|
|
|
|
|
Return the number as an integer similar to PQserverVersion: return
|
|
|
|
v20.1.3 as 200103.
|
|
|
|
|
2020-07-21 04:15:53 +03:00
|
|
|
Assume all the connections are on the same db: return a cached result on
|
|
|
|
following calls.
|
2020-07-21 03:42:34 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
if __crdb_version:
|
|
|
|
return __crdb_version[0]
|
|
|
|
|
2020-07-21 23:48:11 +03:00
|
|
|
sver = conn.info.parameter_status("crdb_version")
|
|
|
|
if sver is None:
|
|
|
|
__crdb_version.append(None)
|
|
|
|
else:
|
|
|
|
m = re.search(r"\bv(\d+)\.(\d+)\.(\d+)", sver)
|
|
|
|
if not m:
|
|
|
|
raise ValueError(
|
2020-11-18 00:52:11 +03:00
|
|
|
f"can't parse CockroachDB version from {sver}")
|
2020-07-21 23:48:11 +03:00
|
|
|
|
|
|
|
ver = int(m.group(1)) * 10000 + int(m.group(2)) * 100 + int(m.group(3))
|
|
|
|
__crdb_version.append(ver)
|
2020-07-21 03:42:34 +03:00
|
|
|
|
|
|
|
return __crdb_version[0]
|
|
|
|
|
|
|
|
|
2020-08-18 01:08:05 +03:00
|
|
|
def skip_if_crdb(reason, conn=None, version=None):
|
2020-07-28 00:58:43 +03:00
|
|
|
"""Skip a test or test class if we are testing against CockroachDB.
|
2020-07-21 03:55:22 +03:00
|
|
|
|
2020-07-28 00:58:43 +03:00
|
|
|
Can be used as a decorator for tests function or classes:
|
|
|
|
|
|
|
|
@skip_if_crdb("my reason")
|
|
|
|
class SomeUnitTest(UnitTest):
|
|
|
|
# ...
|
|
|
|
|
|
|
|
Or as a normal function if the *conn* argument is passed.
|
2020-08-18 01:08:05 +03:00
|
|
|
|
|
|
|
If *version* is specified it should be a string such as ">= 20.1", "< 20",
|
|
|
|
"== 20.1.3": the test will be skipped only if the version matches.
|
|
|
|
|
2020-07-28 00:58:43 +03:00
|
|
|
"""
|
2020-11-17 21:39:39 +03:00
|
|
|
if not isinstance(reason, str):
|
2020-11-18 00:52:11 +03:00
|
|
|
raise TypeError(f"reason should be a string, got {reason!r} instead")
|
2020-08-17 23:27:25 +03:00
|
|
|
|
2020-07-28 00:58:43 +03:00
|
|
|
if conn is not None:
|
2020-08-18 01:08:05 +03:00
|
|
|
ver = crdb_version(conn)
|
|
|
|
if ver is not None and _crdb_match_version(ver, version):
|
2020-08-18 00:23:10 +03:00
|
|
|
if reason in crdb_reasons:
|
|
|
|
reason = (
|
|
|
|
"%s (https://github.com/cockroachdb/cockroach/issues/%s)"
|
|
|
|
% (reason, crdb_reasons[reason]))
|
2020-08-18 01:08:05 +03:00
|
|
|
raise unittest.SkipTest(
|
2020-11-17 22:37:42 +03:00
|
|
|
f"not supported on CockroachDB {ver}: {reason}")
|
2020-07-28 00:58:43 +03:00
|
|
|
|
|
|
|
@decorate_all_tests
|
|
|
|
def skip_if_crdb_(f):
|
|
|
|
@wraps(f)
|
|
|
|
def skip_if_crdb__(self, *args, **kwargs):
|
2020-08-18 01:08:05 +03:00
|
|
|
skip_if_crdb(reason, conn=self.connect(), version=version)
|
2020-07-28 00:58:43 +03:00
|
|
|
return f(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return skip_if_crdb__
|
2020-07-21 03:55:22 +03:00
|
|
|
|
|
|
|
return skip_if_crdb_
|
|
|
|
|
|
|
|
|
2020-08-18 00:23:10 +03:00
|
|
|
# mapping from reason description to ticket number
|
|
|
|
crdb_reasons = {
|
|
|
|
"2-phase commit": 22329,
|
|
|
|
"backend pid": 35897,
|
2022-03-28 19:44:50 +03:00
|
|
|
"batch statements": 44803,
|
2020-08-18 00:23:10 +03:00
|
|
|
"cancel": 41335,
|
|
|
|
"cast adds tz": 51692,
|
|
|
|
"cidr": 18846,
|
|
|
|
"composite": 27792,
|
|
|
|
"copy": 41608,
|
2022-03-28 19:44:50 +03:00
|
|
|
"cursor with hold": 77101,
|
2020-08-18 00:23:10 +03:00
|
|
|
"deferrable": 48307,
|
|
|
|
"encoding": 35882,
|
|
|
|
"hstore": 41284,
|
|
|
|
"infinity date": 41564,
|
|
|
|
"interval style": 35807,
|
|
|
|
"large objects": 243,
|
|
|
|
"named cursor": 41412,
|
|
|
|
"nested array": 32552,
|
|
|
|
"notify": 41522,
|
2021-04-20 19:27:27 +03:00
|
|
|
"password_encryption": 42519,
|
2020-08-18 00:23:10 +03:00
|
|
|
"range": 41282,
|
2022-03-28 19:44:50 +03:00
|
|
|
"scroll cursor": 77102,
|
2020-08-18 00:23:10 +03:00
|
|
|
"stored procedure": 1751,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-18 01:08:05 +03:00
|
|
|
def _crdb_match_version(version, pattern):
|
|
|
|
if pattern is None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
m = re.match(r'^(>|>=|<|<=|==|!=)\s*(\d+)(?:\.(\d+))?(?:\.(\d+))?$', pattern)
|
|
|
|
if m is None:
|
|
|
|
raise ValueError(
|
|
|
|
"bad crdb version pattern %r: should be 'OP MAJOR[.MINOR[.BUGFIX]]'"
|
|
|
|
% pattern)
|
|
|
|
|
|
|
|
ops = {'>': 'gt', '>=': 'ge', '<': 'lt', '<=': 'le', '==': 'eq', '!=': 'ne'}
|
|
|
|
op = getattr(operator, ops[m.group(1)])
|
|
|
|
ref = int(m.group(2)) * 10000 + int(m.group(3) or 0) * 100 + int(m.group(4) or 0)
|
|
|
|
return op(version, ref)
|
|
|
|
|
|
|
|
|
2020-11-17 22:37:42 +03:00
|
|
|
class raises_typeerror:
|
2014-02-19 00:55:00 +04:00
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __exit__(self, type, exc, tb):
|
2020-11-17 21:39:39 +03:00
|
|
|
assert type is TypeError
|
|
|
|
return True
|
2017-02-02 04:53:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def slow(f):
|
2017-02-02 05:09:59 +03:00
|
|
|
"""Decorator to mark slow tests we may want to skip
|
|
|
|
|
|
|
|
Note: in order to find slow tests you can run:
|
|
|
|
|
|
|
|
make check 2>&1 | ts -i "%.s" | sort -n
|
|
|
|
"""
|
2017-02-02 04:53:50 +03:00
|
|
|
@wraps(f)
|
|
|
|
def slow_(self):
|
2019-10-19 15:49:27 +03:00
|
|
|
if os.environ.get('PSYCOPG2_TEST_FAST', '0') != '0':
|
2017-02-02 04:53:50 +03:00
|
|
|
return self.skipTest("slow test")
|
|
|
|
return f(self)
|
|
|
|
return slow_
|
2019-09-04 16:58:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
def restore_types(f):
|
|
|
|
"""Decorator to restore the adaptation system after running a test"""
|
|
|
|
@wraps(f)
|
|
|
|
def restore_types_(self):
|
|
|
|
types = psycopg2.extensions.string_types.copy()
|
|
|
|
adapters = psycopg2.extensions.adapters.copy()
|
|
|
|
try:
|
|
|
|
return f(self)
|
|
|
|
finally:
|
|
|
|
psycopg2.extensions.string_types.clear()
|
|
|
|
psycopg2.extensions.string_types.update(types)
|
|
|
|
psycopg2.extensions.adapters.clear()
|
|
|
|
psycopg2.extensions.adapters.update(adapters)
|
|
|
|
|
|
|
|
return restore_types_
|