From 1a8e992fcc87b2d15911b60cc9220ec50a70a4ff Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2017 18:47:19 -0800 Subject: [PATCH] Use relative imports throughout tests The tests relied on Python2 relative import semantics. Python3 changed import semantics to always search sys.path by default. To import using a relative path it must have a leading dot. Forward compatible with newer Pythons. Works towards the goal of moving tests outside of the installed package. For more information, see PEP-328: https://www.python.org/dev/peps/pep-0328/ --- tests/__init__.py | 52 ++++++++++++++++----------------- tests/dbapi20.py | 6 ++-- tests/test_async.py | 4 +-- tests/test_async_keyword.py | 6 ++-- tests/test_bug_gc.py | 2 +- tests/test_cancel.py | 4 +-- tests/test_connection.py | 4 +-- tests/test_copy.py | 18 ++++++------ tests/test_cursor.py | 2 +- tests/test_dates.py | 2 +- tests/test_errcodes.py | 2 +- tests/test_extras_dictcursor.py | 2 +- tests/test_fast_executemany.py | 2 +- tests/test_green.py | 2 +- tests/test_ipaddress.py | 2 +- tests/test_lobject.py | 2 +- tests/test_module.py | 2 +- tests/test_notify.py | 4 +-- tests/test_psycopg2_dbapi20.py | 10 +++---- tests/test_quote.py | 10 +++---- tests/test_replication.py | 6 ++-- tests/test_sql.py | 2 +- tests/test_transaction.py | 2 +- tests/test_types_basic.py | 6 ++-- tests/test_types_extras.py | 2 +- tests/test_with.py | 2 +- tests/testutils.py | 4 +-- 27 files changed, 81 insertions(+), 81 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 2cb219b0..e58b6fa7 100755 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -28,36 +28,36 @@ import warnings warnings.simplefilter('error') # noqa import sys -from testconfig import dsn +from .testconfig import dsn import unittest -import test_async -import test_bugX000 -import test_bug_gc -import test_cancel -import test_connection -import test_copy -import test_cursor -import test_dates -import test_errcodes -import test_extras_dictcursor -import test_fast_executemany -import test_green -import test_ipaddress -import test_lobject -import test_module -import test_notify -import test_psycopg2_dbapi20 -import test_quote -import test_replication -import test_sql -import test_transaction -import test_types_basic -import test_types_extras -import test_with +from . import test_async +from . import test_bugX000 +from . import test_bug_gc +from . import test_cancel +from . import test_connection +from . import test_copy +from . import test_cursor +from . import test_dates +from . import test_errcodes +from . import test_extras_dictcursor +from . import test_fast_executemany +from . import test_green +from . import test_ipaddress +from . import test_lobject +from . import test_module +from . import test_notify +from . import test_psycopg2_dbapi20 +from . import test_quote +from . import test_replication +from . import test_sql +from . import test_transaction +from . import test_types_basic +from . import test_types_extras +from . import test_with if sys.version_info[:2] < (3, 6): - import test_async_keyword + from . import test_async_keyword def test_suite(): diff --git a/tests/dbapi20.py b/tests/dbapi20.py index 4facf8e8..212d0544 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -90,12 +90,12 @@ class DatabaseAPI20Test(unittest.TestCase): self.driver, connect_args and connect_kw_args. Class specification should be as follows: - import dbapi20 + from . import dbapi20 class mytest(dbapi20.DatabaseAPI20Test): [...] - Don't 'import DatabaseAPI20Test from dbapi20', or you will - confuse the unit tester - just 'import dbapi20'. + Don't 'from .dbapi20 import DatabaseAPI20Test', or you will + confuse the unit tester - just 'from . import dbapi20'. ''' # The self.driver module. This should be the module where the 'connect' diff --git a/tests/test_async.py b/tests/test_async.py index 2c4bfa3b..6469a252 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -24,7 +24,7 @@ # License for more details. import unittest -from testutils import skip_before_postgres, slow +from .testutils import skip_before_postgres, slow import psycopg2 from psycopg2 import extensions as ext @@ -32,7 +32,7 @@ from psycopg2 import extensions as ext import time import StringIO -from testutils import ConnectingTestCase +from .testutils import ConnectingTestCase class PollableStub(object): diff --git a/tests/test_async_keyword.py b/tests/test_async_keyword.py index e08c2750..fa37500c 100755 --- a/tests/test_async_keyword.py +++ b/tests/test_async_keyword.py @@ -28,11 +28,11 @@ import time import psycopg2 from psycopg2 import extras -from testconfig import dsn +from .testconfig import dsn import unittest -from testutils import ConnectingTestCase, skip_before_postgres, slow +from .testutils import ConnectingTestCase, skip_before_postgres, slow -from test_replication import ReplicationTestCase, skip_repl_if_green +from .test_replication import ReplicationTestCase, skip_repl_if_green from psycopg2.extras import LogicalReplicationConnection, StopReplication diff --git a/tests/test_bug_gc.py b/tests/test_bug_gc.py index 084236ef..c04756be 100755 --- a/tests/test_bug_gc.py +++ b/tests/test_bug_gc.py @@ -27,7 +27,7 @@ import psycopg2.extensions import unittest import gc -from testutils import ConnectingTestCase, skip_if_no_uuid +from .testutils import ConnectingTestCase, skip_if_no_uuid class StolenReferenceTestCase(ConnectingTestCase): diff --git a/tests/test_cancel.py b/tests/test_cancel.py index 7888bbff..5e550aa6 100755 --- a/tests/test_cancel.py +++ b/tests/test_cancel.py @@ -30,9 +30,9 @@ import psycopg2 import psycopg2.extensions from psycopg2 import extras -from testconfig import dsn +from .testconfig import dsn import unittest -from testutils import ConnectingTestCase, skip_before_postgres, slow +from .testutils import ConnectingTestCase, skip_before_postgres, slow class CancelTests(ConnectingTestCase): diff --git a/tests/test_connection.py b/tests/test_connection.py index 369c204c..02db2645 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -34,12 +34,12 @@ import psycopg2 import psycopg2.errorcodes from psycopg2 import extensions as ext -from testutils import ( +from .testutils import ( unittest, decorate_all_tests, skip_if_no_superuser, skip_before_postgres, skip_after_postgres, skip_before_libpq, ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow) -from testconfig import dsn, dbname +from .testconfig import dsn, dbname class ConnectionTests(ConnectingTestCase): diff --git a/tests/test_copy.py b/tests/test_copy.py index 3201fb27..d8eba082 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -25,7 +25,7 @@ import sys import string import unittest -from testutils import (ConnectingTestCase, decorate_all_tests, +from .testutils import (ConnectingTestCase, decorate_all_tests, skip_before_postgres, slow) from cStringIO import StringIO from itertools import cycle, izip @@ -33,8 +33,8 @@ from subprocess import Popen, PIPE import psycopg2 import psycopg2.extensions -from testutils import skip_copy_if_green -from testconfig import dsn +from .testutils import skip_copy_if_green +from .testconfig import dsn if sys.version_info[0] < 3: @@ -99,7 +99,7 @@ class CopyTests(ConnectingTestCase): def test_copy_from_cols(self): curs = self.conn.cursor() f = StringIO() - for i in xrange(10): + for i in range(10): f.write("%s\n" % (i,)) f.seek(0) @@ -111,7 +111,7 @@ class CopyTests(ConnectingTestCase): def test_copy_from_cols_err(self): curs = self.conn.cursor() f = StringIO() - for i in xrange(10): + for i in range(10): f.write("%s\n" % (i,)) f.seek(0) @@ -141,7 +141,7 @@ class CopyTests(ConnectingTestCase): about = abin.decode('latin1').replace('\\', '\\\\') else: - abin = bytes(range(32, 127) + range(160, 256)).decode('latin1') + abin = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') about = abin.replace('\\', '\\\\') curs = self.conn.cursor() @@ -162,7 +162,7 @@ class CopyTests(ConnectingTestCase): abin = ''.join(map(chr, range(32, 127) + range(160, 255))) about = abin.replace('\\', '\\\\') else: - abin = bytes(range(32, 127) + range(160, 255)).decode('latin1') + abin = bytes(list(range(32, 127)) + list(range(160, 255))).decode('latin1') about = abin.replace('\\', '\\\\').encode('latin1') curs = self.conn.cursor() @@ -185,7 +185,7 @@ class CopyTests(ConnectingTestCase): about = abin.replace('\\', '\\\\') else: - abin = bytes(range(32, 127) + range(160, 256)).decode('latin1') + abin = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') about = abin.replace('\\', '\\\\') import io @@ -225,7 +225,7 @@ class CopyTests(ConnectingTestCase): def _copy_from(self, curs, nrecs, srec, copykw): f = StringIO() - for i, c in izip(xrange(nrecs), cycle(string.ascii_letters)): + for i, c in zip(range(nrecs), cycle(string.ascii_letters)): l = c * srec f.write("%s\t%s\n" % (i, l)) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 5c94f422..840819ac 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -27,7 +27,7 @@ import pickle import psycopg2 import psycopg2.extensions import unittest -from testutils import (ConnectingTestCase, skip_before_postgres, +from .testutils import (ConnectingTestCase, skip_before_postgres, skip_if_no_getrefcount, slow, skip_if_no_superuser, skip_if_windows) diff --git a/tests/test_dates.py b/tests/test_dates.py index ccfdc20a..47ef41cb 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -26,7 +26,7 @@ import math import psycopg2 from psycopg2.tz import FixedOffsetTimezone, ZERO import unittest -from testutils import ConnectingTestCase, skip_before_postgres +from .testutils import ConnectingTestCase, skip_before_postgres def total_seconds(d): diff --git a/tests/test_errcodes.py b/tests/test_errcodes.py index 0fdf8e59..6775d83e 100755 --- a/tests/test_errcodes.py +++ b/tests/test_errcodes.py @@ -23,7 +23,7 @@ # License for more details. import unittest -from testutils import ConnectingTestCase, slow +from .testutils import ConnectingTestCase, slow try: reload diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index b014a81b..5a726ec6 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -19,7 +19,7 @@ from datetime import timedelta import psycopg2 import psycopg2.extras import unittest -from testutils import ConnectingTestCase, skip_before_postgres +from .testutils import ConnectingTestCase, skip_before_postgres class ExtrasDictCursorTests(ConnectingTestCase): diff --git a/tests/test_fast_executemany.py b/tests/test_fast_executemany.py index 32b34545..ab0a129a 100755 --- a/tests/test_fast_executemany.py +++ b/tests/test_fast_executemany.py @@ -16,7 +16,7 @@ from datetime import date -import testutils +from . import testutils import unittest import psycopg2 diff --git a/tests/test_green.py b/tests/test_green.py index 8c1c20ce..65f483ca 100755 --- a/tests/test_green.py +++ b/tests/test_green.py @@ -27,7 +27,7 @@ import psycopg2 import psycopg2.extensions import psycopg2.extras -from testutils import ConnectingTestCase, slow +from .testutils import ConnectingTestCase, slow class ConnectionStub(object): diff --git a/tests/test_ipaddress.py b/tests/test_ipaddress.py index ea193bf8..2b4ff6af 100755 --- a/tests/test_ipaddress.py +++ b/tests/test_ipaddress.py @@ -19,7 +19,7 @@ from __future__ import unicode_literals import sys from functools import wraps -import testutils +from . import testutils import unittest import psycopg2 diff --git a/tests/test_lobject.py b/tests/test_lobject.py index 7e91fe7a..a9966313 100755 --- a/tests/test_lobject.py +++ b/tests/test_lobject.py @@ -30,7 +30,7 @@ from functools import wraps import psycopg2 import psycopg2.extensions import unittest -from testutils import (decorate_all_tests, skip_if_tpc_disabled, +from .testutils import (decorate_all_tests, skip_if_tpc_disabled, ConnectingTestCase, skip_if_green, slow) diff --git a/tests/test_module.py b/tests/test_module.py index e0533ac8..e1c065d5 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -27,7 +27,7 @@ import sys from subprocess import Popen import unittest -from testutils import (skip_before_postgres, +from .testutils import (skip_before_postgres, ConnectingTestCase, skip_copy_if_green, slow) import psycopg2 diff --git a/tests/test_notify.py b/tests/test_notify.py index 64835efb..bce061e7 100755 --- a/tests/test_notify.py +++ b/tests/test_notify.py @@ -26,8 +26,8 @@ import unittest import psycopg2 from psycopg2 import extensions -from testutils import ConnectingTestCase, slow -from testconfig import dsn +from .testutils import ConnectingTestCase, slow +from .testconfig import dsn import sys import time diff --git a/tests/test_psycopg2_dbapi20.py b/tests/test_psycopg2_dbapi20.py index 3bcedc41..d69c6073 100755 --- a/tests/test_psycopg2_dbapi20.py +++ b/tests/test_psycopg2_dbapi20.py @@ -22,14 +22,14 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. -import dbapi20 -import dbapi20_tpc -from testutils import skip_if_tpc_disabled +from . import dbapi20 +from . import dbapi20_tpc +from .testutils import skip_if_tpc_disabled import unittest -from testutils import decorate_all_tests +from .testutils import decorate_all_tests import psycopg2 -from testconfig import dsn +from .testconfig import dsn class Psycopg2Tests(dbapi20.DatabaseAPI20Test): diff --git a/tests/test_quote.py b/tests/test_quote.py index 0f60dd9f..a79bb56e 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -23,9 +23,9 @@ # License for more details. import sys -import testutils +from . import testutils import unittest -from testutils import ConnectingTestCase +from .testutils import ConnectingTestCase import psycopg2 import psycopg2.extensions @@ -81,7 +81,7 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data += "".join(map(chr, range(256))) else: - data += bytes(range(256)) + data += bytes(list(range(256))) curs = self.conn.cursor() curs.execute("SELECT %s::bytea;", (psycopg2.Binary(data),)) @@ -126,7 +126,7 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data = ''.join(map(chr, range(32, 127) + range(160, 256))) else: - data = bytes(range(32, 127) + range(160, 256)).decode('latin1') + data = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') # as string curs.execute("SELECT %s::text;", (data,)) @@ -150,7 +150,7 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data = ''.join(map(chr, range(32, 127) + range(128, 256))) else: - data = bytes(range(32, 127) + range(128, 256)).decode('koi8_r') + data = bytes(list(range(32, 127)) + list(range(128, 256))).decode('koi8_r') # as string curs.execute("SELECT %s::text;", (data,)) diff --git a/tests/test_replication.py b/tests/test_replication.py index 444dd111..f686d2bc 100755 --- a/tests/test_replication.py +++ b/tests/test_replication.py @@ -26,10 +26,10 @@ import psycopg2 from psycopg2.extras import ( PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication) -import testconfig +from . import testconfig import unittest -from testutils import ConnectingTestCase -from testutils import skip_before_postgres, skip_if_green +from .testutils import ConnectingTestCase +from .testutils import skip_before_postgres, skip_if_green skip_repl_if_green = skip_if_green("replication not supported in green mode") diff --git a/tests/test_sql.py b/tests/test_sql.py index 566cc281..6cb978ff 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -25,7 +25,7 @@ import datetime as dt from cStringIO import StringIO import unittest -from testutils import (ConnectingTestCase, +from .testutils import (ConnectingTestCase, skip_before_postgres, skip_before_python, skip_copy_if_green) import psycopg2 diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 26704d8f..060c8d41 100755 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -24,7 +24,7 @@ import threading import unittest -from testutils import ConnectingTestCase, skip_before_postgres, slow +from .testutils import ConnectingTestCase, skip_before_postgres, slow import psycopg2 from psycopg2.extensions import ( diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index cd1a4f6b..6e34a477 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -26,9 +26,9 @@ import decimal import sys from functools import wraps -import testutils +from . import testutils import unittest -from testutils import ConnectingTestCase, decorate_all_tests +from .testutils import ConnectingTestCase, decorate_all_tests import psycopg2 @@ -326,7 +326,7 @@ class TypesBasicTests(ConnectingTestCase): self.assertEqual(1, f1) i1 = self.execute("select -%s;", (-1,)) self.assertEqual(1, i1) - l1 = self.execute("select -%s;", (-1L,)) + l1 = self.execute("select -%s;", (long(-1),)) self.assertEqual(1, l1) def testGenericArray(self): diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 576e13c8..8901bab1 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -23,7 +23,7 @@ from functools import wraps from pickle import dumps, loads import unittest -from testutils import (skip_if_no_uuid, skip_before_postgres, +from .testutils import (skip_if_no_uuid, skip_before_postgres, ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow) import psycopg2 diff --git a/tests/test_with.py b/tests/test_with.py index 83612dec..1392d85f 100755 --- a/tests/test_with.py +++ b/tests/test_with.py @@ -26,7 +26,7 @@ import psycopg2 import psycopg2.extensions as ext import unittest -from testutils import ConnectingTestCase +from .testutils import ConnectingTestCase class WithTestCase(ConnectingTestCase): diff --git a/tests/testutils.py b/tests/testutils.py index 2ee07af2..e1744c84 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -29,7 +29,7 @@ import select import platform import unittest from functools import wraps -from testconfig import dsn, repl_dsn +from .testconfig import dsn, repl_dsn # Silence warnings caused by the stubbornness of the Python unittest @@ -338,7 +338,7 @@ def skip_if_green(reason): def skip_if_green_(f): @wraps(f) def skip_if_green__(self): - from testconfig import green + from .testconfig import green if green: return self.skipTest(reason) else: