-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sqla-core): Add support for rendering Database Specific queries (#…
…291) * chore(sqla_core): setup an area to run postgres tests * fix: run a supported version of postgres in travis by default it runs 9.4. We need to use a feature added in 9.5 * feat(sqla-core): Add support for rendering Database Specific queries This makes the sqla-core plugin aware of how to compile a database specific query. It seems in some cases, we see strings come through (mostly pragma calls when doing 'create_all'), so do a check to see if the object looks like a SQL Expression object. Fixes: #290 * fix: remove a spurious 'self' in the fixtures * chore: make the DB URL injectable this reduces duplication of the Engine fixture.
- Loading branch information
Showing
6 changed files
with
75 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ python: | |
- "3.8" | ||
- "3.9" | ||
|
||
addons: | ||
postgresql: "9.6" | ||
|
||
install: | ||
- pip install tox tox-travis | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pytest | ||
|
||
from .test_base import connection, engine, session, User | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy.dialects.postgresql import insert as pg_insert | ||
|
||
from aws_xray_sdk.core import xray_recorder, patch | ||
from aws_xray_sdk.core.context import Context | ||
|
||
import testing.postgresql | ||
|
||
|
||
@pytest.fixture() | ||
def postgres_db(): | ||
with testing.postgresql.Postgresql() as postgresql: | ||
yield postgresql | ||
|
||
|
||
@pytest.fixture() | ||
def db_url(postgres_db): | ||
return postgres_db.url() | ||
|
||
|
||
@pytest.fixture() | ||
def sanitized_db_url(postgres_db): | ||
dsn = postgres_db.dsn() | ||
return 'postgresql://{user}@{host}:{port}/{db}'.format( | ||
user=dsn['user'], | ||
host=dsn['host'], | ||
port=dsn['port'], | ||
db=dsn['database'], | ||
) | ||
|
||
|
||
def test_all(session, sanitized_db_url): | ||
""" Test calling all() on get all records. | ||
Verify we run the query and return the SQL as metdata""" | ||
session.query(User).all() | ||
assert len(xray_recorder.current_segment().subsegments) == 1 | ||
sql_meta = xray_recorder.current_segment().subsegments[0].sql | ||
assert sql_meta['url'] == sanitized_db_url | ||
assert sql_meta['sanitized_query'].startswith('SELECT') | ||
assert sql_meta['sanitized_query'].endswith('FROM users') | ||
|
||
|
||
def test_insert_on_conflict_renders(connection): | ||
statement = pg_insert(User).values(name='John', fullname="John Doe", password='123456') | ||
statement = statement.on_conflict_do_nothing() | ||
|
||
connection.execute(statement) | ||
|
||
assert len(xray_recorder.current_segment().subsegments) == 1 | ||
sql_meta = xray_recorder.current_segment().subsegments[0].sql | ||
|
||
assert sql_meta['sanitized_query'].startswith('INSERT INTO users') | ||
assert 'ON CONFLICT DO NOTHING' in sql_meta['sanitized_query'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters