-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathtest_postgres.py
More file actions
57 lines (40 loc) · 1.69 KB
/
Copy pathtest_postgres.py
File metadata and controls
57 lines (40 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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']