Skip to content

Commit 4e6c7c6

Browse files
Add tests and documentation for Athena quoting logic
- Add comprehensive tests for athena__get_clean_elementary_test_tables_queries macro - Test database.schema.table and schema.table identifier formats - Test special characters in table/schema names - Add regression tests to ensure normal names still work - Add explanatory comment about Athena backtick quoting requirement Addresses automated GitHub comments requesting tests and documentation for the Athena-specific quoting behavior in DROP TABLE statements. Co-Authored-By: Yosef Arbiv <[email protected]>
1 parent 9e88ce0 commit 4e6c7c6

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{% macro test_athena_quoting_with_database() %}
2+
{% set mock_relations = [
3+
api.Relation.create(database='test_db', schema='test_schema', identifier='test_table')
4+
] %}
5+
{% set queries = elementary.athena__get_clean_elementary_test_tables_queries(mock_relations) %}
6+
{% for query in queries %}
7+
{{ log(query, info=True) }}
8+
{% endfor %}
9+
{{ return('') }}
10+
{% endmacro %}
11+
12+
{% macro test_athena_quoting_without_database() %}
13+
{% set mock_relations = [
14+
api.Relation.create(schema='test_schema', identifier='test_table')
15+
] %}
16+
{% set queries = elementary.athena__get_clean_elementary_test_tables_queries(mock_relations) %}
17+
{% for query in queries %}
18+
{{ log(query, info=True) }}
19+
{% endfor %}
20+
{{ return('') }}
21+
{% endmacro %}
22+
23+
{% macro test_athena_quoting_special_chars() %}
24+
{% set mock_relations = [
25+
api.Relation.create(database='test-db', schema='test_schema', identifier='test-table')
26+
] %}
27+
{% set queries = elementary.athena__get_clean_elementary_test_tables_queries(mock_relations) %}
28+
{% for query in queries %}
29+
{{ log(query, info=True) }}
30+
{% endfor %}
31+
{{ return('') }}
32+
{% endmacro %}
33+
34+
{% macro test_athena_quoting_normal_names() %}
35+
{% set mock_relations = [
36+
api.Relation.create(database='normal_db', schema='normal_schema', identifier='normal_table')
37+
] %}
38+
{% set queries = elementary.athena__get_clean_elementary_test_tables_queries(mock_relations) %}
39+
{% for query in queries %}
40+
{{ log(query, info=True) }}
41+
{% endfor %}
42+
{{ return('') }}
43+
{% endmacro %}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pytest
2+
from dbt_project import DbtProject
3+
4+
5+
@pytest.mark.only_on_targets(["athena"])
6+
def test_athena_clean_elementary_test_tables_quoting(
7+
test_id: str, dbt_project: DbtProject
8+
):
9+
"""Test that Athena DROP TABLE statements use proper backtick quoting."""
10+
11+
test_result = dbt_project.dbt_runner.run_operation(
12+
"elementary_tests.test_athena_quoting_with_database", return_raw_edr_logs=True
13+
)
14+
15+
assert any(
16+
"DROP TABLE IF EXISTS `" in result for result in test_result
17+
), "Expected backtick quoting in DROP TABLE statement"
18+
assert any(
19+
"test_db.test_schema.test_table" in result for result in test_result
20+
), "Expected database.schema.table format"
21+
22+
test_result = dbt_project.dbt_runner.run_operation(
23+
"elementary_tests.test_athena_quoting_without_database",
24+
return_raw_edr_logs=True,
25+
)
26+
27+
assert any(
28+
"DROP TABLE IF EXISTS `" in result for result in test_result
29+
), "Expected backtick quoting in DROP TABLE statement"
30+
assert any(
31+
"test_schema.test_table" in result for result in test_result
32+
), "Expected schema.table format"
33+
34+
test_result = dbt_project.dbt_runner.run_operation(
35+
"elementary_tests.test_athena_quoting_special_chars", return_raw_edr_logs=True
36+
)
37+
38+
assert any(
39+
"DROP TABLE IF EXISTS `" in result for result in test_result
40+
), "Expected backtick quoting for special characters"
41+
assert any(
42+
"test-db.test_schema.test-table" in result for result in test_result
43+
), "Expected special characters to be handled"
44+
45+
46+
@pytest.mark.only_on_targets(["athena"])
47+
def test_athena_quoting_regression(test_id: str, dbt_project: DbtProject):
48+
"""Regression test to ensure Athena quoting doesn't break existing functionality."""
49+
50+
test_result = dbt_project.dbt_runner.run_operation(
51+
"elementary_tests.test_athena_quoting_normal_names", return_raw_edr_logs=True
52+
)
53+
54+
assert any(
55+
"DROP TABLE IF EXISTS `" in result for result in test_result
56+
), "Expected backtick quoting for normal names"
57+
assert any(
58+
"normal_db.normal_schema.normal_table" in result for result in test_result
59+
), "Expected normal names to work"

macros/edr/tests/test_utils/clean_elementary_test_tables.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
{% endmacro %}
4141

4242
{% macro athena__get_clean_elementary_test_tables_queries(test_table_relations) %}
43+
{# Athena requires explicit backtick quoting for DROP TABLE statements to avoid parsing errors (important-comment) #}
4344
{% set queries = [] %}
4445
{% for test_relation in test_table_relations %}
4546
{% if test_relation.database %}

0 commit comments

Comments
 (0)