Skip to content

Commit 7fbc4dd

Browse files
authored
psycopg2: Improved validation and more tests (#178)
* Fix validation and add tests for register_type * Test Env: Update default postgres env vars * Disable unicode tests; add another register_type test * One more comment...
1 parent b146473 commit 7fbc4dd

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

instana/instrumentation/psycopg2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
def register_type_with_instana(wrapped, instance, args, kwargs):
2121
args_clone = list(copy.copy(args))
2222

23-
if (len(args_clone) >= 2) and (args_clone[1], '__wrapped__'):
23+
if (len(args_clone) >= 2) and hasattr(args_clone[1], '__wrapped__'):
2424
args_clone[1] = args_clone[1].__wrapped__
2525

2626
return wrapped(*args_clone, **kwargs)

tests/helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
"""
2222
PostgreSQL Environment
2323
"""
24-
testenv['postgresql_host'] = os.environ.get('POSTGRESQL_HOST', '127.0.0.1')
25-
testenv['postgresql_port'] = int(os.environ.get('POSTGRESQL_PORT', '5432'))
26-
testenv['postgresql_db'] = os.environ.get('POSTGRESQL_DB', 'circle_test')
27-
testenv['postgresql_user'] = os.environ.get('POSTGRESQL_USER', 'root')
28-
testenv['postgresql_pw'] = os.environ.get('POSTGRESQL_PW', '')
24+
testenv['postgresql_host'] = os.environ.get('POSTGRES_HOST', '127.0.0.1')
25+
testenv['postgresql_port'] = int(os.environ.get('POSTGRES_PORT', '5432'))
26+
testenv['postgresql_db'] = os.environ.get('POSTGRES_DB', 'circle_test')
27+
testenv['postgresql_user'] = os.environ.get('POSTGRES_USER', 'root')
28+
testenv['postgresql_pw'] = os.environ.get('POSTGRES_PW', '')
2929

3030

3131
"""

tests/test_psycopg2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import psycopg2
1212
import psycopg2.extras
13+
import psycopg2.extensions as ext
1314

1415
logger = logging.getLogger(__name__)
1516

@@ -209,3 +210,46 @@ def test_error_capture(self):
209210
assert_equals(db_span.data.pg.user, testenv['postgresql_user'])
210211
assert_equals(db_span.data.pg.stmt, 'SELECT * from blah')
211212
assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host'])
213+
214+
# Added to validate unicode support and register_type.
215+
def test_unicode(self):
216+
ext.register_type(ext.UNICODE, self.cursor)
217+
#
218+
# Python 2 chokes on Unicode and CircleCI tests are hanging (but pass locally).
219+
# Disable these tests for now as we want to really just test register_type
220+
# anyways
221+
#
222+
# snowman = "\u2603"
223+
#
224+
# self.cursor.execute("delete from users where id in (1,2,3)")
225+
#
226+
# # unicode in statement
227+
# psycopg2.extras.execute_batch(self.cursor,
228+
# "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(1, 'x')])
229+
# self.cursor.execute("select id, name from users where id = 1")
230+
# assert_equals(self.cursor.fetchone(), (1, 'x'))
231+
#
232+
# # unicode in data
233+
# psycopg2.extras.execute_batch(self.cursor,
234+
# "insert into users (id, name) values (%s, %s)", [(2, snowman)])
235+
# self.cursor.execute("select id, name from users where id = 2")
236+
# assert_equals(self.cursor.fetchone(), (2, snowman))
237+
#
238+
# # unicode in both
239+
# psycopg2.extras.execute_batch(self.cursor,
240+
# "insert into users (id, name) values (%%s, %%s) -- %s" % snowman, [(3, snowman)])
241+
# self.cursor.execute("select id, name from users where id = 3")
242+
# assert_equals(self.cursor.fetchone(), (3, snowman))
243+
244+
def test_register_type(self):
245+
import uuid
246+
247+
oid1 = 2950
248+
oid2 = 2951
249+
250+
ext.UUID = ext.new_type((oid1,), "UUID", lambda data, cursor: data and uuid.UUID(data) or None)
251+
ext.UUIDARRAY = ext.new_array_type((oid2,), "UUID[]", ext.UUID)
252+
253+
ext.register_type(ext.UUID, self.cursor)
254+
ext.register_type(ext.UUIDARRAY, self.cursor)
255+

0 commit comments

Comments
 (0)