|
| 1 | +import psycopg2 |
| 2 | +import psycopg2.pool |
| 3 | + |
| 4 | +import pytest |
| 5 | +import testing.postgresql |
| 6 | + |
| 7 | +from aws_xray_sdk.core import patch |
| 8 | +from aws_xray_sdk.core import xray_recorder |
| 9 | +from aws_xray_sdk.core.context import Context |
| 10 | + |
| 11 | +patch(('psycopg2',)) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(autouse=True) |
| 15 | +def construct_ctx(): |
| 16 | + """ |
| 17 | + Clean up context storage on each test run and begin a segment |
| 18 | + so that later subsegment can be attached. After each test run |
| 19 | + it cleans up context storage again. |
| 20 | + """ |
| 21 | + xray_recorder.configure(service='test', sampling=False, context=Context()) |
| 22 | + xray_recorder.clear_trace_entities() |
| 23 | + xray_recorder.begin_segment('name') |
| 24 | + yield |
| 25 | + xray_recorder.clear_trace_entities() |
| 26 | + |
| 27 | + |
| 28 | +def test_execute_dsn_kwargs(): |
| 29 | + q = 'SELECT 1' |
| 30 | + with testing.postgresql.Postgresql() as postgresql: |
| 31 | + url = postgresql.url() |
| 32 | + dsn = postgresql.dsn() |
| 33 | + conn = psycopg2.connect(dbname=dsn['database'], |
| 34 | + user=dsn['user'], |
| 35 | + password='', |
| 36 | + host=dsn['host'], |
| 37 | + port=dsn['port']) |
| 38 | + cur = conn.cursor() |
| 39 | + cur.execute(q) |
| 40 | + |
| 41 | + subsegment = xray_recorder.current_segment().subsegments[0] |
| 42 | + assert subsegment.name == 'execute' |
| 43 | + sql = subsegment.sql |
| 44 | + assert sql['database_type'] == 'PostgreSQL' |
| 45 | + assert sql['user'] == dsn['user'] |
| 46 | + assert sql['url'] == url |
| 47 | + assert sql['database_version'] |
| 48 | + |
| 49 | + |
| 50 | +def test_execute_dsn_string(): |
| 51 | + q = 'SELECT 1' |
| 52 | + with testing.postgresql.Postgresql() as postgresql: |
| 53 | + url = postgresql.url() |
| 54 | + dsn = postgresql.dsn() |
| 55 | + conn = psycopg2.connect('dbname=' + dsn['database'] + |
| 56 | + ' password=mypassword' + |
| 57 | + ' host=' + dsn['host'] + |
| 58 | + ' port=' + str(dsn['port']) + |
| 59 | + ' user=' + dsn['user']) |
| 60 | + cur = conn.cursor() |
| 61 | + cur.execute(q) |
| 62 | + |
| 63 | + subsegment = xray_recorder.current_segment().subsegments[0] |
| 64 | + assert subsegment.name == 'execute' |
| 65 | + sql = subsegment.sql |
| 66 | + assert sql['database_type'] == 'PostgreSQL' |
| 67 | + assert sql['user'] == dsn['user'] |
| 68 | + assert sql['url'] == url |
| 69 | + assert sql['database_version'] |
| 70 | + |
| 71 | + |
| 72 | +def test_execute_in_pool(): |
| 73 | + q = 'SELECT 1' |
| 74 | + with testing.postgresql.Postgresql() as postgresql: |
| 75 | + url = postgresql.url() |
| 76 | + dsn = postgresql.dsn() |
| 77 | + pool = psycopg2.pool.SimpleConnectionPool(1, 1, |
| 78 | + dbname=dsn['database'], |
| 79 | + user=dsn['user'], |
| 80 | + password='', |
| 81 | + host=dsn['host'], |
| 82 | + port=dsn['port']) |
| 83 | + cur = pool.getconn(key=dsn['user']).cursor() |
| 84 | + cur.execute(q) |
| 85 | + |
| 86 | + subsegment = xray_recorder.current_segment().subsegments[0] |
| 87 | + assert subsegment.name == 'execute' |
| 88 | + sql = subsegment.sql |
| 89 | + assert sql['database_type'] == 'PostgreSQL' |
| 90 | + assert sql['user'] == dsn['user'] |
| 91 | + assert sql['url'] == url |
| 92 | + assert sql['database_version'] |
| 93 | + |
| 94 | + |
| 95 | +def test_execute_bad_query(): |
| 96 | + q = 'SELECT blarg' |
| 97 | + with testing.postgresql.Postgresql() as postgresql: |
| 98 | + url = postgresql.url() |
| 99 | + dsn = postgresql.dsn() |
| 100 | + conn = psycopg2.connect(dbname=dsn['database'], |
| 101 | + user=dsn['user'], |
| 102 | + password='', |
| 103 | + host=dsn['host'], |
| 104 | + port=dsn['port']) |
| 105 | + cur = conn.cursor() |
| 106 | + try: |
| 107 | + cur.execute(q) |
| 108 | + except Exception: |
| 109 | + pass |
| 110 | + |
| 111 | + subsegment = xray_recorder.current_segment().subsegments[0] |
| 112 | + assert subsegment.name == 'execute' |
| 113 | + sql = subsegment.sql |
| 114 | + assert sql['database_type'] == 'PostgreSQL' |
| 115 | + assert sql['user'] == dsn['user'] |
| 116 | + assert sql['url'] == url |
| 117 | + assert sql['database_version'] |
| 118 | + |
| 119 | + exception = subsegment.cause['exceptions'][0] |
| 120 | + assert exception.type == 'ProgrammingError' |
0 commit comments