diff options
author | Jason Erickson <jerickso@stickpeople.com> | 2011-02-10 15:59:31 -0700 |
---|---|---|
committer | Jason Erickson <jerickso@stickpeople.com> | 2011-02-10 15:59:31 -0700 |
commit | b075017ad92b7724b12ccfd03eca8df96b7607ce (patch) | |
tree | fb22025556529fef096d862de1e896aceb0c6b08 /tests/testutils.py | |
parent | 8d28509f495c2b23150f046fa3c15e4f7fe1ad04 (diff) | |
download | psycopg2-b075017ad92b7724b12ccfd03eca8df96b7607ce.tar.gz |
Pulled down changes from dvarrazzo branch on gh
Pulled the master branch from of Daniele's psycopg branch on github and
merged the changes.
Diffstat (limited to 'tests/testutils.py')
-rw-r--r-- | tests/testutils.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/testutils.py b/tests/testutils.py index cfeb0e0..98297fc 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -23,6 +23,9 @@ # Use unittest2 if available. Otherwise mock a skip facility with warnings. +import os +import sys + try: import unittest2 unittest = unittest2 @@ -57,6 +60,16 @@ else: unittest.TestCase.skipTest = skipTest +# Silence warnings caused by the stubborness of the Python unittest maintainers +# http://bugs.python.org/issue9424 +if not hasattr(unittest.TestCase, 'assert_') \ +or unittest.TestCase.assert_ is not unittest.TestCase.assertTrue: + # 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 + def decorate_all_tests(cls, decorator): """Apply *decorator* to all the tests defined in the TestCase *cls*.""" @@ -138,3 +151,60 @@ def skip_if_tpc_disabled(f): return skip_if_tpc_disabled_ +def skip_if_no_iobase(f): + """Skip a test if io.TextIOBase is not available.""" + def skip_if_no_iobase_(self): + try: + from io import TextIOBase + except ImportError: + return self.skipTest("io.TextIOBase not found.") + else: + return f(self) + + return skip_if_no_iobase_ + + +def skip_on_python2(f): + """Skip a test on Python 3 and following.""" + def skip_on_python2_(self): + if sys.version_info[0] < 3: + return self.skipTest("skipped because Python 2") + else: + return f(self) + + return skip_on_python2_ + +def skip_on_python3(f): + """Skip a test on Python 3 and following.""" + def skip_on_python3_(self): + if sys.version_info[0] >= 3: + return self.skipTest("skipped because Python 3") + else: + return f(self) + + return skip_on_python3_ + +def script_to_py3(script): + """Convert a script to Python3 syntax if required.""" + if sys.version_info[0] < 3: + return script + + import tempfile + f = tempfile.NamedTemporaryFile(suffix=".py") + f.write(script.encode()) + f.flush() + + # 2to3 is way too chatty + import logging + logging.basicConfig(filename=os.devnull) + + from lib2to3.main import main + if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', f.name]): + raise Exception('py3 conversion failed') + + f2 = open(f.name) + try: + return f2.read() + finally: + f2.close() + |