From a8589878440c792084c52ea7b29b3a5dd2fe9a84 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 24 Sep 2012 00:33:49 +0100 Subject: [PATCH] Fixed search of types into schemas. We don't need to look for stuff implicitly into pg_catalog as all the builtin ranges are already registered. So just search into 'public' if the schema is not specified. --- lib/_range.py | 6 +++--- tests/test_types_extras.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/_range.py b/lib/_range.py index 19dfc849..a5cd3ae6 100644 --- a/lib/_range.py +++ b/lib/_range.py @@ -291,7 +291,7 @@ class RangeCaster(object): schema, tname = name.split('.', 1) else: tname = name - schema = None + schema = 'public' # get the type oid and attributes try: @@ -301,8 +301,8 @@ select rngtypid, rngsubtype, from pg_range r join pg_type t on t.oid = rngtypid join pg_namespace ns on ns.oid = typnamespace -where typname = %s and (%s is null or ns.nspname = %s); -""", (tname, schema, schema)) +where typname = %s and ns.nspname = %s; +""", (tname, schema)) except ProgrammingError: if not conn.autocommit: diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 3eefd059..dcc02e7f 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -1242,6 +1242,33 @@ class RangeCasterTestCase(unittest.TestCase): self.assertRaises(psycopg2.ProgrammingError, register_range, 'nosuchrange', 'FailRange', cur) + def test_schema_range(self): + cur = self.conn.cursor() + cur.execute("create schema rs") + cur.execute("create type r1 as range (subtype=text)") + cur.execute("create type r2 as range (subtype=text)") + cur.execute("create type rs.r2 as range (subtype=text)") + cur.execute("create type rs.r3 as range (subtype=text)") + cur.execute("savepoint x") + + from psycopg2.extras import register_range + ra1 = register_range('r1', 'r1', cur) + ra2 = register_range('r2', 'r2', cur) + rars2 = register_range('rs.r2', 'r2', cur) + rars3 = register_range('rs.r3', 'r3', cur) + + self.assertNotEqual( + ra2.typecaster.values[0], + rars2.typecaster.values[0]) + + self.assertRaises(psycopg2.ProgrammingError, + register_range, 'r3', 'FailRange', cur) + cur.execute("rollback to savepoint x;") + + self.assertRaises(psycopg2.ProgrammingError, + register_range, 'rs.r1', 'FailRange', cur) + cur.execute("rollback to savepoint x;") + decorate_all_tests(RangeCasterTestCase, skip_if_no_range)