Skip to content

Commit 9a92a55

Browse files
committed
change patching and remove unrelated tests
1 parent 4753e3f commit 9a92a55

File tree

2 files changed

+17
-102
lines changed

2 files changed

+17
-102
lines changed

aws_xray_sdk/ext/psycopg/patch.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import copy
2-
import re
31
import wrapt
42
from operator import methodcaller
53

6-
from aws_xray_sdk.ext.dbapi2 import XRayTracedConn, XRayTracedCursor
4+
from aws_xray_sdk.ext.dbapi2 import XRayTracedConn
75

86

97
def patch():
@@ -12,27 +10,17 @@ def patch():
1210
'connect',
1311
_xray_traced_connect
1412
)
15-
wrapt.wrap_function_wrapper(
16-
'psycopg.extensions',
17-
'register_type',
18-
_xray_register_type_fix
19-
)
20-
wrapt.wrap_function_wrapper(
21-
'psycopg.extensions',
22-
'quote_ident',
23-
_xray_register_type_fix
24-
)
2513

2614
wrapt.wrap_function_wrapper(
27-
'psycopg.extras',
28-
'register_default_jsonb',
29-
_xray_register_default_jsonb_fix
15+
'psycopg_pool.pool',
16+
'ConnectionPool._connect',
17+
_xray_traced_connect
3018
)
3119

3220

3321
def _xray_traced_connect(wrapped, instance, args, kwargs):
3422
conn = wrapped(*args, **kwargs)
35-
parameterized_dsn = {c[0]: c[-1] for c in map(methodcaller('split', '='), conn.dsn.split(' '))}
23+
parameterized_dsn = {c[0]: c[-1] for c in map(methodcaller('split', '='), conn.info.dsn.split(' '))}
3624
meta = {
3725
'database_type': 'PostgreSQL',
3826
'url': 'postgresql://{}@{}:{}/{}'.format(
@@ -42,28 +30,8 @@ def _xray_traced_connect(wrapped, instance, args, kwargs):
4230
parameterized_dsn.get('dbname', 'unknown'),
4331
),
4432
'user': parameterized_dsn.get('user', 'unknown'),
45-
'database_version': str(conn.server_version),
33+
'database_version': str(conn.info.server_version),
4634
'driver_version': 'Psycopg 3'
4735
}
4836

4937
return XRayTracedConn(conn, meta)
50-
51-
52-
def _xray_register_type_fix(wrapped, instance, args, kwargs):
53-
"""Send the actual connection or curser to register type."""
54-
our_args = list(copy.copy(args))
55-
if len(our_args) == 2 and isinstance(our_args[1], (XRayTracedConn, XRayTracedCursor)):
56-
our_args[1] = our_args[1].__wrapped__
57-
58-
return wrapped(*our_args, **kwargs)
59-
60-
61-
def _xray_register_default_jsonb_fix(wrapped, instance, args, kwargs):
62-
our_kwargs = dict()
63-
for key, value in kwargs.items():
64-
if key == "conn_or_curs" and isinstance(value, (XRayTracedConn, XRayTracedCursor)):
65-
# unwrap the connection or cursor to be sent to register_default_jsonb
66-
value = value.__wrapped__
67-
our_kwargs[key] = value
68-
69-
return wrapped(*args, **our_kwargs)

tests/ext/psycopg/test_psycopg.py

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import psycopg
2-
import psycopg.extras
3-
import psycopg.pool
42
import psycopg.sql
3+
import psycopg_pool
54

65
import pytest
76
import testing.postgresql
@@ -49,32 +48,6 @@ def test_execute_dsn_kwargs():
4948
assert sql['database_version']
5049

5150

52-
def test_execute_dsn_kwargs_alt_dbname():
53-
"""
54-
Psycopg supports database to be passed as `database` or `dbname`
55-
"""
56-
q = 'SELECT 1'
57-
58-
with testing.postgresql.Postgresql() as postgresql:
59-
url = postgresql.url()
60-
dsn = postgresql.dsn()
61-
conn = psycopg.connect(database=dsn['database'],
62-
user=dsn['user'],
63-
password='',
64-
host=dsn['host'],
65-
port=dsn['port'])
66-
cur = conn.cursor()
67-
cur.execute(q)
68-
69-
subsegment = xray_recorder.current_segment().subsegments[0]
70-
assert subsegment.name == 'execute'
71-
sql = subsegment.sql
72-
assert sql['database_type'] == 'PostgreSQL'
73-
assert sql['user'] == dsn['user']
74-
assert sql['url'] == url
75-
assert sql['database_version']
76-
77-
7851
def test_execute_dsn_string():
7952
q = 'SELECT 1'
8053
with testing.postgresql.Postgresql() as postgresql:
@@ -102,14 +75,16 @@ def test_execute_in_pool():
10275
with testing.postgresql.Postgresql() as postgresql:
10376
url = postgresql.url()
10477
dsn = postgresql.dsn()
105-
pool = psycopg.pool.SimpleConnectionPool(1, 1,
106-
dbname=dsn['database'],
107-
user=dsn['user'],
108-
password='',
109-
host=dsn['host'],
110-
port=dsn['port'])
111-
cur = pool.getconn(key=dsn['user']).cursor()
112-
cur.execute(q)
78+
pool = psycopg_pool.ConnectionPool('dbname=' + dsn['database'] +
79+
' password=mypassword' +
80+
' host=' + dsn['host'] +
81+
' port=' + str(dsn['port']) +
82+
' user=' + dsn['user'],
83+
min_size=1,
84+
max_size=1)
85+
with pool.connection() as conn:
86+
cur = conn.cursor()
87+
cur.execute(q)
11388

11489
subsegment = xray_recorder.current_segment().subsegments[0]
11590
assert subsegment.name == 'execute'
@@ -147,20 +122,6 @@ def test_execute_bad_query():
147122
exception = subsegment.cause['exceptions'][0]
148123
assert exception.type == 'UndefinedColumn'
149124

150-
151-
def test_register_extensions():
152-
with testing.postgresql.Postgresql() as postgresql:
153-
url = postgresql.url()
154-
dsn = postgresql.dsn()
155-
conn = psycopg.connect('dbname=' + dsn['database'] +
156-
' password=mypassword' +
157-
' host=' + dsn['host'] +
158-
' port=' + str(dsn['port']) +
159-
' user=' + dsn['user'])
160-
assert psycopg.extras.register_uuid(None, conn)
161-
assert psycopg.extras.register_uuid(None, conn.cursor())
162-
163-
164125
def test_query_as_string():
165126
with testing.postgresql.Postgresql() as postgresql:
166127
url = postgresql.url()
@@ -173,17 +134,3 @@ def test_query_as_string():
173134
test_sql = psycopg.sql.Identifier('test')
174135
assert test_sql.as_string(conn)
175136
assert test_sql.as_string(conn.cursor())
176-
177-
178-
def test_register_default_jsonb():
179-
with testing.postgresql.Postgresql() as postgresql:
180-
url = postgresql.url()
181-
dsn = postgresql.dsn()
182-
conn = psycopg.connect('dbname=' + dsn['database'] +
183-
' password=mypassword' +
184-
' host=' + dsn['host'] +
185-
' port=' + str(dsn['port']) +
186-
' user=' + dsn['user'])
187-
188-
assert psycopg.extras.register_default_jsonb(conn_or_curs=conn, loads=lambda x: x)
189-
assert psycopg.extras.register_default_jsonb(conn_or_curs=conn.cursor(), loads=lambda x: x)

0 commit comments

Comments
 (0)