From ca3d9da83b37388e251280238062dcc95ce69325 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 18 Jan 2011 01:46:10 +0000 Subject: [PATCH] Skip GC bug test if uuid is not available --- tests/bug_gc.py | 14 +++++++++++--- tests/testutils.py | 23 +++++++++++++++++++++++ tests/types_extras.py | 23 +---------------------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/tests/bug_gc.py b/tests/bug_gc.py index 71dac1c8..b3d80d1d 100755 --- a/tests/bug_gc.py +++ b/tests/bug_gc.py @@ -34,15 +34,23 @@ if sys.version_info < (3,): else: import py3tests as tests +from testutils import skip_if_no_uuid + class StolenReferenceTestCase(unittest.TestCase): + def setUp(self): + self.conn = psycopg2.connect(tests.dsn) + + def tearDown(self): + self.conn.close() + + @skip_if_no_uuid def test_stolen_reference_bug(self): def fish(val, cur): gc.collect() return 42 - conn = psycopg2.connect(tests.dsn) UUID = psycopg2.extensions.new_type((2950,), "UUID", fish) - psycopg2.extensions.register_type(UUID, conn) - curs = conn.cursor() + psycopg2.extensions.register_type(UUID, self.conn) + curs = self.conn.cursor() curs.execute("select 'b5219e01-19ab-4994-b71e-149225dc51e4'::uuid") curs.fetchone() diff --git a/tests/testutils.py b/tests/testutils.py index c1529581..cfeb0e05 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -65,6 +65,29 @@ def decorate_all_tests(cls, decorator): setattr(cls, n, decorator(getattr(cls, n))) +def skip_if_no_uuid(f): + """Decorator to skip a test if uuid is not supported by Py/PG.""" + def skip_if_no_uuid_(self): + try: + import uuid + except ImportError: + return self.skipTest("uuid not available in this Python version") + + 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_ + + def skip_if_no_pg_sleep(name): """Decorator to skip a test if pg_sleep is not supported by the server. diff --git a/tests/types_extras.py b/tests/types_extras.py index d26740e7..010449b2 100755 --- a/tests/types_extras.py +++ b/tests/types_extras.py @@ -22,34 +22,13 @@ import re import sys from datetime import date -from testutils import unittest +from testutils import unittest, skip_if_no_uuid import psycopg2 import psycopg2.extras import tests -def skip_if_no_uuid(f): - def skip_if_no_uuid_(self): - try: - import uuid - except ImportError: - return self.skipTest("uuid not available in this Python version") - - 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_ - def filter_scs(conn, s): if conn.get_parameter_status("standard_conforming_strings") == 'off': return s