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