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/
This commit is contained in:
Jon Dufresne 2017-12-03 18:47:19 -08:00
parent 9de46e416e
commit 1a8e992fcc
27 changed files with 81 additions and 81 deletions

View File

@ -28,36 +28,36 @@ import warnings
warnings.simplefilter('error') # noqa warnings.simplefilter('error') # noqa
import sys import sys
from testconfig import dsn from .testconfig import dsn
import unittest import unittest
import test_async from . import test_async
import test_bugX000 from . import test_bugX000
import test_bug_gc from . import test_bug_gc
import test_cancel from . import test_cancel
import test_connection from . import test_connection
import test_copy from . import test_copy
import test_cursor from . import test_cursor
import test_dates from . import test_dates
import test_errcodes from . import test_errcodes
import test_extras_dictcursor from . import test_extras_dictcursor
import test_fast_executemany from . import test_fast_executemany
import test_green from . import test_green
import test_ipaddress from . import test_ipaddress
import test_lobject from . import test_lobject
import test_module from . import test_module
import test_notify from . import test_notify
import test_psycopg2_dbapi20 from . import test_psycopg2_dbapi20
import test_quote from . import test_quote
import test_replication from . import test_replication
import test_sql from . import test_sql
import test_transaction from . import test_transaction
import test_types_basic from . import test_types_basic
import test_types_extras from . import test_types_extras
import test_with from . import test_with
if sys.version_info[:2] < (3, 6): if sys.version_info[:2] < (3, 6):
import test_async_keyword from . import test_async_keyword
def test_suite(): def test_suite():

View File

@ -90,12 +90,12 @@ class DatabaseAPI20Test(unittest.TestCase):
self.driver, connect_args and connect_kw_args. Class specification self.driver, connect_args and connect_kw_args. Class specification
should be as follows: should be as follows:
import dbapi20 from . import dbapi20
class mytest(dbapi20.DatabaseAPI20Test): class mytest(dbapi20.DatabaseAPI20Test):
[...] [...]
Don't 'import DatabaseAPI20Test from dbapi20', or you will Don't 'from .dbapi20 import DatabaseAPI20Test', or you will
confuse the unit tester - just 'import dbapi20'. confuse the unit tester - just 'from . import dbapi20'.
''' '''
# The self.driver module. This should be the module where the 'connect' # The self.driver module. This should be the module where the 'connect'

View File

@ -24,7 +24,7 @@
# License for more details. # License for more details.
import unittest import unittest
from testutils import skip_before_postgres, slow from .testutils import skip_before_postgres, slow
import psycopg2 import psycopg2
from psycopg2 import extensions as ext from psycopg2 import extensions as ext
@ -32,7 +32,7 @@ from psycopg2 import extensions as ext
import time import time
import StringIO import StringIO
from testutils import ConnectingTestCase from .testutils import ConnectingTestCase
class PollableStub(object): class PollableStub(object):

View File

@ -28,11 +28,11 @@ import time
import psycopg2 import psycopg2
from psycopg2 import extras from psycopg2 import extras
from testconfig import dsn from .testconfig import dsn
import unittest 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 from psycopg2.extras import LogicalReplicationConnection, StopReplication

View File

@ -27,7 +27,7 @@ import psycopg2.extensions
import unittest import unittest
import gc import gc
from testutils import ConnectingTestCase, skip_if_no_uuid from .testutils import ConnectingTestCase, skip_if_no_uuid
class StolenReferenceTestCase(ConnectingTestCase): class StolenReferenceTestCase(ConnectingTestCase):

View File

@ -30,9 +30,9 @@ import psycopg2
import psycopg2.extensions import psycopg2.extensions
from psycopg2 import extras from psycopg2 import extras
from testconfig import dsn from .testconfig import dsn
import unittest import unittest
from testutils import ConnectingTestCase, skip_before_postgres, slow from .testutils import ConnectingTestCase, skip_before_postgres, slow
class CancelTests(ConnectingTestCase): class CancelTests(ConnectingTestCase):

View File

@ -34,12 +34,12 @@ import psycopg2
import psycopg2.errorcodes import psycopg2.errorcodes
from psycopg2 import extensions as ext from psycopg2 import extensions as ext
from testutils import ( from .testutils import (
unittest, decorate_all_tests, skip_if_no_superuser, unittest, decorate_all_tests, skip_if_no_superuser,
skip_before_postgres, skip_after_postgres, skip_before_libpq, skip_before_postgres, skip_after_postgres, skip_before_libpq,
ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow) ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow)
from testconfig import dsn, dbname from .testconfig import dsn, dbname
class ConnectionTests(ConnectingTestCase): class ConnectionTests(ConnectingTestCase):

View File

@ -25,7 +25,7 @@
import sys import sys
import string import string
import unittest import unittest
from testutils import (ConnectingTestCase, decorate_all_tests, from .testutils import (ConnectingTestCase, decorate_all_tests,
skip_before_postgres, slow) skip_before_postgres, slow)
from cStringIO import StringIO from cStringIO import StringIO
from itertools import cycle, izip from itertools import cycle, izip
@ -33,8 +33,8 @@ from subprocess import Popen, PIPE
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
from testutils import skip_copy_if_green from .testutils import skip_copy_if_green
from testconfig import dsn from .testconfig import dsn
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
@ -99,7 +99,7 @@ class CopyTests(ConnectingTestCase):
def test_copy_from_cols(self): def test_copy_from_cols(self):
curs = self.conn.cursor() curs = self.conn.cursor()
f = StringIO() f = StringIO()
for i in xrange(10): for i in range(10):
f.write("%s\n" % (i,)) f.write("%s\n" % (i,))
f.seek(0) f.seek(0)
@ -111,7 +111,7 @@ class CopyTests(ConnectingTestCase):
def test_copy_from_cols_err(self): def test_copy_from_cols_err(self):
curs = self.conn.cursor() curs = self.conn.cursor()
f = StringIO() f = StringIO()
for i in xrange(10): for i in range(10):
f.write("%s\n" % (i,)) f.write("%s\n" % (i,))
f.seek(0) f.seek(0)
@ -141,7 +141,7 @@ class CopyTests(ConnectingTestCase):
about = abin.decode('latin1').replace('\\', '\\\\') about = abin.decode('latin1').replace('\\', '\\\\')
else: 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('\\', '\\\\') about = abin.replace('\\', '\\\\')
curs = self.conn.cursor() curs = self.conn.cursor()
@ -162,7 +162,7 @@ class CopyTests(ConnectingTestCase):
abin = ''.join(map(chr, range(32, 127) + range(160, 255))) abin = ''.join(map(chr, range(32, 127) + range(160, 255)))
about = abin.replace('\\', '\\\\') about = abin.replace('\\', '\\\\')
else: 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') about = abin.replace('\\', '\\\\').encode('latin1')
curs = self.conn.cursor() curs = self.conn.cursor()
@ -185,7 +185,7 @@ class CopyTests(ConnectingTestCase):
about = abin.replace('\\', '\\\\') about = abin.replace('\\', '\\\\')
else: 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('\\', '\\\\') about = abin.replace('\\', '\\\\')
import io import io
@ -225,7 +225,7 @@ class CopyTests(ConnectingTestCase):
def _copy_from(self, curs, nrecs, srec, copykw): def _copy_from(self, curs, nrecs, srec, copykw):
f = StringIO() 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 l = c * srec
f.write("%s\t%s\n" % (i, l)) f.write("%s\t%s\n" % (i, l))

View File

@ -27,7 +27,7 @@ import pickle
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
import unittest 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_no_getrefcount, slow, skip_if_no_superuser,
skip_if_windows) skip_if_windows)

View File

@ -26,7 +26,7 @@ import math
import psycopg2 import psycopg2
from psycopg2.tz import FixedOffsetTimezone, ZERO from psycopg2.tz import FixedOffsetTimezone, ZERO
import unittest import unittest
from testutils import ConnectingTestCase, skip_before_postgres from .testutils import ConnectingTestCase, skip_before_postgres
def total_seconds(d): def total_seconds(d):

View File

@ -23,7 +23,7 @@
# License for more details. # License for more details.
import unittest import unittest
from testutils import ConnectingTestCase, slow from .testutils import ConnectingTestCase, slow
try: try:
reload reload

View File

@ -19,7 +19,7 @@ from datetime import timedelta
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
import unittest import unittest
from testutils import ConnectingTestCase, skip_before_postgres from .testutils import ConnectingTestCase, skip_before_postgres
class ExtrasDictCursorTests(ConnectingTestCase): class ExtrasDictCursorTests(ConnectingTestCase):

View File

@ -16,7 +16,7 @@
from datetime import date from datetime import date
import testutils from . import testutils
import unittest import unittest
import psycopg2 import psycopg2

View File

@ -27,7 +27,7 @@ import psycopg2
import psycopg2.extensions import psycopg2.extensions
import psycopg2.extras import psycopg2.extras
from testutils import ConnectingTestCase, slow from .testutils import ConnectingTestCase, slow
class ConnectionStub(object): class ConnectionStub(object):

View File

@ -19,7 +19,7 @@ from __future__ import unicode_literals
import sys import sys
from functools import wraps from functools import wraps
import testutils from . import testutils
import unittest import unittest
import psycopg2 import psycopg2

View File

@ -30,7 +30,7 @@ from functools import wraps
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
import unittest 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) ConnectingTestCase, skip_if_green, slow)

View File

@ -27,7 +27,7 @@ import sys
from subprocess import Popen from subprocess import Popen
import unittest import unittest
from testutils import (skip_before_postgres, from .testutils import (skip_before_postgres,
ConnectingTestCase, skip_copy_if_green, slow) ConnectingTestCase, skip_copy_if_green, slow)
import psycopg2 import psycopg2

View File

@ -26,8 +26,8 @@ import unittest
import psycopg2 import psycopg2
from psycopg2 import extensions from psycopg2 import extensions
from testutils import ConnectingTestCase, slow from .testutils import ConnectingTestCase, slow
from testconfig import dsn from .testconfig import dsn
import sys import sys
import time import time

View File

@ -22,14 +22,14 @@
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details. # License for more details.
import dbapi20 from . import dbapi20
import dbapi20_tpc from . import dbapi20_tpc
from testutils import skip_if_tpc_disabled from .testutils import skip_if_tpc_disabled
import unittest import unittest
from testutils import decorate_all_tests from .testutils import decorate_all_tests
import psycopg2 import psycopg2
from testconfig import dsn from .testconfig import dsn
class Psycopg2Tests(dbapi20.DatabaseAPI20Test): class Psycopg2Tests(dbapi20.DatabaseAPI20Test):

View File

@ -23,9 +23,9 @@
# License for more details. # License for more details.
import sys import sys
import testutils from . import testutils
import unittest import unittest
from testutils import ConnectingTestCase from .testutils import ConnectingTestCase
import psycopg2 import psycopg2
import psycopg2.extensions import psycopg2.extensions
@ -81,7 +81,7 @@ class QuotingTestCase(ConnectingTestCase):
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
data += "".join(map(chr, range(256))) data += "".join(map(chr, range(256)))
else: else:
data += bytes(range(256)) data += bytes(list(range(256)))
curs = self.conn.cursor() curs = self.conn.cursor()
curs.execute("SELECT %s::bytea;", (psycopg2.Binary(data),)) curs.execute("SELECT %s::bytea;", (psycopg2.Binary(data),))
@ -126,7 +126,7 @@ class QuotingTestCase(ConnectingTestCase):
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
data = ''.join(map(chr, range(32, 127) + range(160, 256))) data = ''.join(map(chr, range(32, 127) + range(160, 256)))
else: 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 # as string
curs.execute("SELECT %s::text;", (data,)) curs.execute("SELECT %s::text;", (data,))
@ -150,7 +150,7 @@ class QuotingTestCase(ConnectingTestCase):
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
data = ''.join(map(chr, range(32, 127) + range(128, 256))) data = ''.join(map(chr, range(32, 127) + range(128, 256)))
else: 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 # as string
curs.execute("SELECT %s::text;", (data,)) curs.execute("SELECT %s::text;", (data,))

View File

@ -26,10 +26,10 @@ import psycopg2
from psycopg2.extras import ( from psycopg2.extras import (
PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication) PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication)
import testconfig from . import testconfig
import unittest import unittest
from testutils import ConnectingTestCase from .testutils import ConnectingTestCase
from testutils import skip_before_postgres, skip_if_green from .testutils import skip_before_postgres, skip_if_green
skip_repl_if_green = skip_if_green("replication not supported in green mode") skip_repl_if_green = skip_if_green("replication not supported in green mode")

View File

@ -25,7 +25,7 @@
import datetime as dt import datetime as dt
from cStringIO import StringIO from cStringIO import StringIO
import unittest import unittest
from testutils import (ConnectingTestCase, from .testutils import (ConnectingTestCase,
skip_before_postgres, skip_before_python, skip_copy_if_green) skip_before_postgres, skip_before_python, skip_copy_if_green)
import psycopg2 import psycopg2

View File

@ -24,7 +24,7 @@
import threading import threading
import unittest import unittest
from testutils import ConnectingTestCase, skip_before_postgres, slow from .testutils import ConnectingTestCase, skip_before_postgres, slow
import psycopg2 import psycopg2
from psycopg2.extensions import ( from psycopg2.extensions import (

View File

@ -26,9 +26,9 @@ import decimal
import sys import sys
from functools import wraps from functools import wraps
import testutils from . import testutils
import unittest import unittest
from testutils import ConnectingTestCase, decorate_all_tests from .testutils import ConnectingTestCase, decorate_all_tests
import psycopg2 import psycopg2
@ -326,7 +326,7 @@ class TypesBasicTests(ConnectingTestCase):
self.assertEqual(1, f1) self.assertEqual(1, f1)
i1 = self.execute("select -%s;", (-1,)) i1 = self.execute("select -%s;", (-1,))
self.assertEqual(1, i1) self.assertEqual(1, i1)
l1 = self.execute("select -%s;", (-1L,)) l1 = self.execute("select -%s;", (long(-1),))
self.assertEqual(1, l1) self.assertEqual(1, l1)
def testGenericArray(self): def testGenericArray(self):

View File

@ -23,7 +23,7 @@ from functools import wraps
from pickle import dumps, loads from pickle import dumps, loads
import unittest 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) ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow)
import psycopg2 import psycopg2

View File

@ -26,7 +26,7 @@ import psycopg2
import psycopg2.extensions as ext import psycopg2.extensions as ext
import unittest import unittest
from testutils import ConnectingTestCase from .testutils import ConnectingTestCase
class WithTestCase(ConnectingTestCase): class WithTestCase(ConnectingTestCase):

View File

@ -29,7 +29,7 @@ import select
import platform import platform
import unittest import unittest
from functools import wraps 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 # Silence warnings caused by the stubbornness of the Python unittest
@ -338,7 +338,7 @@ def skip_if_green(reason):
def skip_if_green_(f): def skip_if_green_(f):
@wraps(f) @wraps(f)
def skip_if_green__(self): def skip_if_green__(self):
from testconfig import green from .testconfig import green
if green: if green:
return self.skipTest(reason) return self.skipTest(reason)
else: else: