|
| 1 | +import pytest |
| 2 | + |
| 3 | +from .test_base import connection, engine, session, User |
| 4 | + |
| 5 | +from sqlalchemy import create_engine |
| 6 | +from sqlalchemy.dialects.postgresql import insert as pg_insert |
| 7 | + |
| 8 | +from aws_xray_sdk.core import xray_recorder, patch |
| 9 | +from aws_xray_sdk.core.context import Context |
| 10 | + |
| 11 | +import testing.postgresql |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +def postgres_db(): |
| 16 | + with testing.postgresql.Postgresql() as postgresql: |
| 17 | + yield postgresql |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture() |
| 21 | +def db_url(postgres_db): |
| 22 | + return postgres_db.url() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture() |
| 26 | +def sanitized_db_url(postgres_db): |
| 27 | + dsn = postgres_db.dsn() |
| 28 | + return 'postgresql://{user}@{host}:{port}/{db}'.format( |
| 29 | + user=dsn['user'], |
| 30 | + host=dsn['host'], |
| 31 | + port=dsn['port'], |
| 32 | + db=dsn['database'], |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def test_all(session, sanitized_db_url): |
| 37 | + """ Test calling all() on get all records. |
| 38 | + Verify we run the query and return the SQL as metdata""" |
| 39 | + session.query(User).all() |
| 40 | + assert len(xray_recorder.current_segment().subsegments) == 1 |
| 41 | + sql_meta = xray_recorder.current_segment().subsegments[0].sql |
| 42 | + assert sql_meta['url'] == sanitized_db_url |
| 43 | + assert sql_meta['sanitized_query'].startswith('SELECT') |
| 44 | + assert sql_meta['sanitized_query'].endswith('FROM users') |
| 45 | + |
| 46 | + |
| 47 | +def test_insert_on_conflict_renders(connection): |
| 48 | + statement = pg_insert(User).values(name='John', fullname="John Doe", password='123456') |
| 49 | + statement = statement.on_conflict_do_nothing() |
| 50 | + |
| 51 | + connection.execute(statement) |
| 52 | + |
| 53 | + assert len(xray_recorder.current_segment().subsegments) == 1 |
| 54 | + sql_meta = xray_recorder.current_segment().subsegments[0].sql |
| 55 | + |
| 56 | + assert sql_meta['sanitized_query'].startswith('INSERT INTO users') |
| 57 | + assert 'ON CONFLICT DO NOTHING' in sql_meta['sanitized_query'] |
0 commit comments