From 0c0b9482a7708cdb818e51d0e7b3cb36e053f1ea Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Sun, 30 Jun 2019 17:51:32 +0200 Subject: [PATCH 1/4] Fix validation and add tests for register_type --- instana/instrumentation/psycopg2.py | 2 +- tests/test_psycopg2.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/instana/instrumentation/psycopg2.py b/instana/instrumentation/psycopg2.py index 555d82b0..fab11e43 100644 --- a/instana/instrumentation/psycopg2.py +++ b/instana/instrumentation/psycopg2.py @@ -20,7 +20,7 @@ def register_type_with_instana(wrapped, instance, args, kwargs): args_clone = list(copy.copy(args)) - if (len(args_clone) >= 2) and (args_clone[1], '__wrapped__'): + if (len(args_clone) >= 2) and hasattr(args_clone[1], '__wrapped__'): args_clone[1] = args_clone[1].__wrapped__ return wrapped(*args_clone, **kwargs) diff --git a/tests/test_psycopg2.py b/tests/test_psycopg2.py index c6a43e43..05e2e59b 100644 --- a/tests/test_psycopg2.py +++ b/tests/test_psycopg2.py @@ -10,6 +10,7 @@ import psycopg2 import psycopg2.extras +import psycopg2.extensions as ext logger = logging.getLogger(__name__) @@ -209,3 +210,29 @@ def test_error_capture(self): assert_equals(db_span.data.pg.user, testenv['postgresql_user']) assert_equals(db_span.data.pg.stmt, 'SELECT * from blah') assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host']) + + # Added to validate unicode support and register_type. + def test_unicode(self): + ext.register_type(ext.UNICODE, self.cursor) + snowman = "\u2603" + + self.cursor.execute("delete from users where id in (1,2,3)") + + # unicode in statement + psycopg2.extras.execute_batch(self.cursor, + "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(1, 'x')]) + self.cursor.execute("select id, name from users where id = 1") + assert_equals(self.cursor.fetchone(), (1, 'x')) + + # unicode in data + psycopg2.extras.execute_batch(self.cursor, + "insert into users (id, name) values (%s, %s)", [(2, snowman)]) + self.cursor.execute("select id, name from users where id = 2") + assert_equals(self.cursor.fetchone(), (2, snowman)) + + # unicode in both + psycopg2.extras.execute_batch(self.cursor, + "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(3, snowman)]) + self.cursor.execute("select id, name from users where id = 3") + assert_equals(self.cursor.fetchone(), (3, snowman)) + From 918ceb99d7db50c47491f6cf0aad4a1638f70a0c Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Sun, 30 Jun 2019 17:54:46 +0200 Subject: [PATCH 2/4] Test Env: Update default postgres env vars --- tests/helpers.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/helpers.py b/tests/helpers.py index 5d64a58e..0bfd0efc 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -21,11 +21,11 @@ """ PostgreSQL Environment """ -testenv['postgresql_host'] = os.environ.get('POSTGRESQL_HOST', '127.0.0.1') -testenv['postgresql_port'] = int(os.environ.get('POSTGRESQL_PORT', '5432')) -testenv['postgresql_db'] = os.environ.get('POSTGRESQL_DB', 'circle_test') -testenv['postgresql_user'] = os.environ.get('POSTGRESQL_USER', 'root') -testenv['postgresql_pw'] = os.environ.get('POSTGRESQL_PW', '') +testenv['postgresql_host'] = os.environ.get('POSTGRES_HOST', '127.0.0.1') +testenv['postgresql_port'] = int(os.environ.get('POSTGRES_PORT', '5432')) +testenv['postgresql_db'] = os.environ.get('POSTGRES_DB', 'circle_test') +testenv['postgresql_user'] = os.environ.get('POSTGRES_USER', 'root') +testenv['postgresql_pw'] = os.environ.get('POSTGRES_PW', '') """ From d7c5b08c25fcc03e8f0a02c7a30db00d1d88c6ab Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Wed, 3 Jul 2019 11:16:47 +0200 Subject: [PATCH 3/4] Disable unicode tests; add another register_type test --- tests/test_psycopg2.py | 54 ++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/tests/test_psycopg2.py b/tests/test_psycopg2.py index 05e2e59b..a8a842b7 100644 --- a/tests/test_psycopg2.py +++ b/tests/test_psycopg2.py @@ -214,25 +214,37 @@ def test_error_capture(self): # Added to validate unicode support and register_type. def test_unicode(self): ext.register_type(ext.UNICODE, self.cursor) - snowman = "\u2603" - - self.cursor.execute("delete from users where id in (1,2,3)") - - # unicode in statement - psycopg2.extras.execute_batch(self.cursor, - "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(1, 'x')]) - self.cursor.execute("select id, name from users where id = 1") - assert_equals(self.cursor.fetchone(), (1, 'x')) - - # unicode in data - psycopg2.extras.execute_batch(self.cursor, - "insert into users (id, name) values (%s, %s)", [(2, snowman)]) - self.cursor.execute("select id, name from users where id = 2") - assert_equals(self.cursor.fetchone(), (2, snowman)) - - # unicode in both - psycopg2.extras.execute_batch(self.cursor, - "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(3, snowman)]) - self.cursor.execute("select id, name from users where id = 3") - assert_equals(self.cursor.fetchone(), (3, snowman)) + # snowman = "\u2603" + # + # self.cursor.execute("delete from users where id in (1,2,3)") + # + # # unicode in statement + # psycopg2.extras.execute_batch(self.cursor, + # "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(1, 'x')]) + # self.cursor.execute("select id, name from users where id = 1") + # assert_equals(self.cursor.fetchone(), (1, 'x')) + # + # # unicode in data + # psycopg2.extras.execute_batch(self.cursor, + # "insert into users (id, name) values (%s, %s)", [(2, snowman)]) + # self.cursor.execute("select id, name from users where id = 2") + # assert_equals(self.cursor.fetchone(), (2, snowman)) + # + # # unicode in both + # psycopg2.extras.execute_batch(self.cursor, + # "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(3, snowman)]) + # self.cursor.execute("select id, name from users where id = 3") + # assert_equals(self.cursor.fetchone(), (3, snowman)) + + def test_register_type(self): + import uuid + + oid1 = 2950 + oid2 = 2951 + + ext.UUID = ext.new_type((oid1,), "UUID", lambda data, cursor: data and uuid.UUID(data) or None) + ext.UUIDARRAY = ext.new_array_type((oid2,), "UUID[]", ext.UUID) + + ext.register_type(ext.UUID, self.cursor) + ext.register_type(ext.UUIDARRAY, self.cursor) From 04f85fd6ad53d315972ae9ce2d14ecd2971e29d4 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Wed, 3 Jul 2019 11:24:24 +0200 Subject: [PATCH 4/4] One more comment... --- tests/test_psycopg2.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_psycopg2.py b/tests/test_psycopg2.py index a8a842b7..d988effb 100644 --- a/tests/test_psycopg2.py +++ b/tests/test_psycopg2.py @@ -214,6 +214,11 @@ def test_error_capture(self): # Added to validate unicode support and register_type. def test_unicode(self): ext.register_type(ext.UNICODE, self.cursor) + # + # Python 2 chokes on Unicode and CircleCI tests are hanging (but pass locally). + # Disable these tests for now as we want to really just test register_type + # anyways + # # snowman = "\u2603" # # self.cursor.execute("delete from users where id in (1,2,3)")