Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_execute_with_params(pgtestdb_test_tables, pgtestdb_conn,
test_table_data):
# Arrange
sql = "DELETE FROM src WHERE id = %s;"
params = [1]
expected = test_table_data[1:]
# Act
execute(sql, pgtestdb_conn, parameters=params)
# Assert
result = get_rows('SELECT * FROM src;', pgtestdb_conn)
assert result == expected
def test_bad_select(testdb_conn):
select_sql = "SELECT * FROM bad_table"
with pytest.raises(ETLHelperQueryError):
execute(select_sql, testdb_conn)
def test_bad_constraint(test_tables, testdb_conn):
# src already has a row with id=1
insert_sql = "INSERT INTO src (id) VALUES (1)"
with pytest.raises(ETLHelperQueryError):
execute(insert_sql, testdb_conn)
def test_execute_bad_query(pgtestdb_test_tables, pgtestdb_conn):
sql = "DELETE * FROM this_does_not_exist"
with pytest.raises(ETLHelperQueryError):
execute(sql, pgtestdb_conn)
def test_execute_happy_path(pgtestdb_test_tables, pgtestdb_conn):
# Arrange
sql = "DELETE FROM src;"
# Act
execute(sql, pgtestdb_conn)
# Assert
result = get_rows('SELECT * FROM src;', pgtestdb_conn)
assert result == []
def test_logging_execute(caplog, level, expected, pgtestdb_conn):
# Arrange
caplog.set_level(level, logger=logger.name)
select_sql = "SELECT 1 AS result;"
# Act
execute(select_sql, pgtestdb_conn)
# ID for connection object and hostname vary between tests
# and test environments
messages = [re.sub(r'object at .*;', 'object at ???;', m)
for m in caplog.messages]
messages = [re.sub(r'host=.*? ', 'host=??? ', m)
for m in messages]
# Assert
for i, message in enumerate(messages):
assert message == expected[i]
def test_bad_insert(testdb_conn):
insert_sql = "INSERT INTO bad_table (id) VALUES (1)"
with pytest.raises(ETLHelperQueryError):
execute(insert_sql, testdb_conn)