Skip to content

feat: add _with_context variants for generic tests#975

Open
joostboon wants to merge 3 commits intomasterfrom
feat/add-with-context-tests
Open

feat: add _with_context variants for generic tests#975
joostboon wants to merge 3 commits intomasterfrom
feat/add-with-context-tests

Conversation

@joostboon
Copy link
Contributor

@joostboon joostboon commented Mar 25, 2026

Summary

  • Adds 6 new elementary generic tests that extend common dbt / dbt_utils / dbt_expectations tests with a context_columns parameter
  • When context_columns is provided, only those columns (plus the tested column) are returned alongside failing rows
  • When context_columns is omitted, all columns are returned (SELECT *)
  • Non-existent columns in context_columns emit a WARNING log and are skipped rather than raising an error

New tests

Test Extends
elementary.not_null_with_context dbt.not_null
elementary.accepted_range_with_context dbt_utils.accepted_range
elementary.expect_column_values_to_not_be_null_with_context dbt_expectations.expect_column_values_to_not_be_null
elementary.expect_column_values_to_be_unique_with_context dbt_expectations.expect_column_values_to_be_unique
elementary.expect_column_values_to_match_regex_with_context dbt_expectations.expect_column_values_to_match_regex (requires dbt_expectations installed)
elementary.relationships_with_context dbt.relationships

Usage

columns:
  - name: order_id
    tests:
      - elementary.not_null_with_context:
          context_columns: [customer_id, order_date]  # omit to return all columns

      - elementary.relationships_with_context:
          to: ref('customers')
          field: id
          context_columns: [customer_id, amount]

      - elementary.accepted_range_with_context:
          min_value: 0
          max_value: 1000
          context_columns: [customer_id, order_date]

Test plan

  • Run elementary.not_null_with_context on a column with nulls — verify failing rows include the specified context columns
  • Run without context_columns — verify all columns are returned
  • Specify a non-existent column in context_columns — verify a WARNING is logged and the column is skipped without erroring
  • Run elementary.relationships_with_context on a column with broken FK references — verify context columns are correctly prefixed with the child alias
  • Run elementary.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

  • New Features
    • Added six test variants with optional context-column support: accepted_range_with_context, not_null_with_context, expect_column_values_to_not_be_null_with_context, expect_column_values_to_be_unique_with_context, expect_column_values_to_match_regex_with_context, relationships_with_context. Failing-row outputs can include selected context columns (defaults to all columns when not specified) and respect optional row filters and range/regex options.
  • Chores
    • Updated common test configuration mapping to include entries for the new tests.

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>
@github-actions
Copy link
Contributor

👋 @joostboon
Thank you for raising your pull request.
Please make sure to add tests and document all user-facing changes.
You can do this by editing the docs files in the elementary repository.

@coderabbitai
Copy link

coderabbitai bot commented Mar 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8ba76694-c5df-450d-a329-828bdf42882e

📥 Commits

Reviewing files that changed from the base of the PR and between d165a3b and 227528f.

📒 Files selected for processing (6)
  • macros/edr/tests/test_accepted_range_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_match_regex_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_not_be_null_with_context.sql
  • macros/edr/tests/test_not_null_with_context.sql
  • macros/edr/tests/test_relationships_with_context.sql
🚧 Files skipped from review as they are similar to previous changes (6)
  • macros/edr/tests/test_expect_column_values_to_match_regex_with_context.sql
  • macros/edr/tests/test_relationships_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_not_be_null_with_context.sql
  • macros/edr/tests/test_not_null_with_context.sql
  • macros/edr/tests/test_accepted_range_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql

📝 Walkthrough

Walkthrough

Adds six new dbt generic test macros that accept optional context_columns to include additional model columns in failing-row output; updates the common test configuration mapping to register these context-aware tests.

Changes

Cohort / File(s) Summary
New context-aware test macros
macros/edr/tests/test_not_null_with_context.sql, macros/edr/tests/test_accepted_range_with_context.sql, macros/edr/tests/test_expect_column_values_to_not_be_null_with_context.sql, macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql, macros/edr/tests/test_expect_column_values_to_match_regex_with_context.sql, macros/edr/tests/test_relationships_with_context.sql
Added six test macros that accept an optional context_columns parameter. Each macro validates requested context columns against the model (case-insensitive), logs warnings for missing columns, skips duplicates of the target column, and falls back to SELECT * when context_columns is not supplied or invalid. Core test predicates remain consistent with existing expectations.
Test configuration registry
macros/utils/common_test_configs.sql
Extended the common_tests_configs_mapping to register the six new context-aware tests, each with quality_dimension, failed_row_count_calc: count(*), and descriptions noting inclusion of additional context columns (defaults to all columns when not provided).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hop through tables, sniff a missing bit,
I call my context friends to help out of it,
If names don't match, I whisper a small chide,
Then show the rows where errors like to hide,
A rabbit's cheer for tests that now provide.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: add _with_context variants for generic tests' accurately describes the main change: adding new generic test variants with context column support across six dbt tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/add-with-context-tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8c7ab0 and ff9a0f1.

📒 Files selected for processing (7)
  • macros/edr/tests/test_accepted_range_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_be_unique_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_match_regex_with_context.sql
  • macros/edr/tests/test_expect_column_values_to_not_be_null_with_context.sql
  • macros/edr/tests/test_not_null_with_context.sql
  • macros/edr/tests/test_relationships_with_context.sql
  • macros/utils/common_test_configs.sql

joostboon and others added 2 commits March 25, 2026 23:15
- 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant