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
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():

View File

@ -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'

View File

@ -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):

View File

@ -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

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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))

View File

@ -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)

View File

@ -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):

View File

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

View File

@ -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):

View File

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

View File

@ -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):

View File

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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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,))

View File

@ -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")

View File

@ -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

View File

@ -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 (

View File

@ -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):

View File

@ -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

View File

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

View File

@ -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: