feat: add _with_context variants for generic tests#975
feat: add _with_context variants for generic tests#975
Conversation
Adds six new elementary generic tests that extend common dbt/dbt_utils/dbt_expectations tests with a `context_columns` parameter, allowing users to specify which additional columns are returned alongside failing rows. If `context_columns` is omitted, all columns are returned. New tests: - elementary.not_null_with_context - elementary.accepted_range_with_context - elementary.expect_column_values_to_not_be_null_with_context - elementary.expect_column_values_to_be_unique_with_context - elementary.expect_column_values_to_match_regex_with_context - elementary.relationships_with_context Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
👋 @joostboon |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (6)
📝 WalkthroughWalkthroughAdds six new dbt generic test macros that accept optional Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@macros/edr/tests/test_accepted_range_with_context.sql`:
- Around line 22-28: The template predicate can silently pass when both
min_value and max_value are omitted; add an explicit guard at the top of the
macro/test (before rendering the predicate that uses min_value, max_value,
column_name, inclusive) to detect when both min_value and max_value are None and
raise a compiler error (e.g., using exceptions.raise_compiler_error) with a
clear message asking for at least one bound, so the test fails early instead of
producing a misleading always-passing condition.
In `@macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql`:
- Around line 16-24: The outer select is leaking the computed n_records when
select_clause defaults to '*'; update the macro so when select_clause is '*' it
instead projects only the original model columns (e.g. use validation.* or
explicitly build a column-list excluding n_records) so the outer select does not
return the computed n_records column. Locate the select_clause handling and
change the default behavior (the select {{ select_clause }} ... from (select
..., count(*) over (...) as n_records from {{ model }})) so the outer projection
uses validation.* or an explicit list that omits n_records while still retaining
n_records internally for filtering/reporting.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 5be59681-52aa-4f32-898a-b2443d6859e1
📒 Files selected for processing (7)
macros/edr/tests/test_accepted_range_with_context.sqlmacros/edr/tests/test_expect_column_values_to_be_unique_with_context.sqlmacros/edr/tests/test_expect_column_values_to_match_regex_with_context.sqlmacros/edr/tests/test_expect_column_values_to_not_be_null_with_context.sqlmacros/edr/tests/test_not_null_with_context.sqlmacros/edr/tests/test_relationships_with_context.sqlmacros/utils/common_test_configs.sql
macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql
Outdated
Show resolved
Hide resolved
- accepted_range_with_context: raise compiler error when both min_value and max_value are omitted, preventing a silent always-pass condition - expect_column_values_to_be_unique_with_context: use explicit column list from adapter.get_columns_in_relation instead of SELECT * to prevent the computed n_records window column from leaking into the output Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary
context_columnsparametercontext_columnsis provided, only those columns (plus the tested column) are returned alongside failing rowscontext_columnsis omitted, all columns are returned (SELECT *)context_columnsemit aWARNINGlog and are skipped rather than raising an errorNew tests
elementary.not_null_with_contextdbt.not_nullelementary.accepted_range_with_contextdbt_utils.accepted_rangeelementary.expect_column_values_to_not_be_null_with_contextdbt_expectations.expect_column_values_to_not_be_nullelementary.expect_column_values_to_be_unique_with_contextdbt_expectations.expect_column_values_to_be_uniqueelementary.expect_column_values_to_match_regex_with_contextdbt_expectations.expect_column_values_to_match_regex(requiresdbt_expectationsinstalled)elementary.relationships_with_contextdbt.relationshipsUsage
Test plan
elementary.not_null_with_contexton a column with nulls — verify failing rows include the specified context columnscontext_columns— verify all columns are returnedcontext_columns— verify a WARNING is logged and the column is skipped without erroringelementary.relationships_with_contexton a column with broken FK references — verify context columns are correctly prefixed with the child aliaselementary.expect_column_values_to_be_unique_with_context— verify all individual duplicate rows are returned (not just the grouped count)🤖 Generated with Claude Code
Summary by CodeRabbit