fix: read source-level meta.elementary config in tests#1002
fix: read source-level meta.elementary config in tests#1002
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a helper to merge ChangesElementary config merge, adapter propagation, and tests
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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 |
|
👋 @joostboon |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
integration_tests/dbt_project/macros/get_anomaly_config.sql (1)
9-73: ⚡ Quick winSignificant duplication across the three adapter macros.
default__get_anomaly_config,spark__get_anomaly_config, andclickhouse__get_anomaly_configare nearly identical — the only difference is the first argument toapi.Relation.create(...). The shared body (mock-model construction,source_meta_configinjection, context update) could be extracted into a private helper to avoid this triplication.♻️ Sketch of a DRY refactor
+{% macro _build_anomaly_config(model_config, config, source_meta_config, relation) %} + {% set mock_model = { + "alias": "mock_model", + "config": {"elementary": model_config}, + } %} + {% if source_meta_config is not none %} + {% do mock_model.update({"source_meta": source_meta_config}) %} + {% endif %} + {% do context.update( + { + "model": {"depends_on": {"nodes": ["id"]}}, + "graph": {"nodes": {"id": mock_model}}, + } + ) %} + {% do return( + elementary.get_anomalies_test_configuration(relation, **config)[0] + ) %} +{% endmacro %} + {% macro default__get_anomaly_config(model_config, config, source_meta_config=none) %} - {% set mock_model = { - "alias": "mock_model", - "config": {"elementary": model_config}, - } %} - {% if source_meta_config is not none %} - {% do mock_model.update({"source_meta": source_meta_config}) %} - {% endif %} - {% do context.update(...) %} - {% do return( - elementary.get_anomalies_test_configuration( - api.Relation.create("db", "schema", "mock_model"), **config - )[0] - ) %} + {% do return( + elementary_tests._build_anomaly_config( + model_config, config, source_meta_config, + api.Relation.create("db", "schema", "mock_model") + ) + ) %} {% endmacro %}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@integration_tests/dbt_project/macros/get_anomaly_config.sql` around lines 9 - 73, Duplication: the three macros default__get_anomaly_config, spark__get_anomaly_config, and clickhouse__get_anomaly_config repeat the same mock_model build, optional source_meta injection, context.update, and call to elementary.get_anomalies_test_configuration — only the api.Relation.create(...) argument differs; extract the shared body into a new helper macro (e.g., _get_anomaly_config_base(model_config, config, source_meta_config, relation)) that builds mock_model, applies source_meta_config, updates context, and returns elementary.get_anomalies_test_configuration(relation, **config)[0], then rewrite default__get_anomaly_config, spark__get_anomaly_config, and clickhouse__get_anomaly_config to call that helper passing the appropriate api.Relation.create(...) value for each adapter.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@integration_tests/dbt_project/macros/get_anomaly_config.sql`:
- Around line 1-9: The file failed sqlfmt formatting; run the pre-commit hook to
apply sqlfmt and commit the changes for the macros get_anomaly_config and
default__get_anomaly_config: run `pre-commit run --files
integration_tests/dbt_project/macros/get_anomaly_config.sql` (or run your repo's
pre-commit for all files), review the diffs produced for those macros, stage and
commit the resulting formatting changes so the pipeline no longer reports
formatting failures.
---
Nitpick comments:
In `@integration_tests/dbt_project/macros/get_anomaly_config.sql`:
- Around line 9-73: Duplication: the three macros default__get_anomaly_config,
spark__get_anomaly_config, and clickhouse__get_anomaly_config repeat the same
mock_model build, optional source_meta injection, context.update, and call to
elementary.get_anomalies_test_configuration — only the api.Relation.create(...)
argument differs; extract the shared body into a new helper macro (e.g.,
_get_anomaly_config_base(model_config, config, source_meta_config, relation))
that builds mock_model, applies source_meta_config, updates context, and returns
elementary.get_anomalies_test_configuration(relation, **config)[0], then rewrite
default__get_anomaly_config, spark__get_anomaly_config, and
clickhouse__get_anomaly_config to call that helper passing the appropriate
api.Relation.create(...) value for each adapter.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1b308415-687e-492b-8554-c53d62f74ec6
📒 Files selected for processing (3)
integration_tests/dbt_project/macros/get_anomaly_config.sqlintegration_tests/tests/test_anomaly_test_configuration.pymacros/utils/graph/get_elementary_config_from_node.sql
dbt stores source-level meta separately in node.source_meta (not merged into node.meta for table nodes). get_elementary_config_from_node only read node.meta, so meta.elementary set at the source level (above tables:) was silently ignored for test configuration like timestamp_column, days_back, and seasonality. Source_meta is already read and merged in upload_dbt_sources.sql for artifact upload — this aligns test config resolution to match. Table-level meta still takes precedence over source-level meta. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Extract repeated meta→elementary merge pattern into a _merge_elementary_from_meta helper, reducing duplication from three identical blocks to one. - Extend get_anomaly_config test macro to accept source_meta_config so the source-meta code path can be exercised in integration tests. - Add two integration tests: source-level meta.elementary is picked up when no model/test config overrides it, and model-level meta wins when both are set. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
In real dbt, node.source_meta is the full meta dict with the elementary config nested under an "elementary" key (same structure as node.meta). The mock was setting source_meta directly to the elementary config dict, so _merge_elementary_from_meta couldn't find the "elementary" sub-key. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Rebase onto master picked up a new commit that added microbatch files without running pre-commit. Apply black/isort/sqlfmt/prettier so code-quality CI passes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
eb4d044 to
a8aa951
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
integration_tests/tests/test_dbt_artifacts/test_microbatch_compiled_code.py (1)
99-117: 💤 Low valueHardcoded macro file path may conflict under parallel test execution.
macro_pathis alwaysmacros/microbatch.sqlregardless ofmacro_name. The two parametrize cases ("with_override"/"without_override") use different names but share the same file. Under sequential pytest this is fine, but if the suite is ever run withpytest-xdist(-n auto), both workers would race to create/delete the same path.A simple guard is to include
macro_namein the filename:♻️ Proposed fix
- macro_path = dbt_project.project_dir_path / "macros" / "microbatch.sql" + macro_path = dbt_project.project_dir_path / "macros" / f"microbatch_{macro_name}.sql"🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@integration_tests/tests/test_dbt_artifacts/test_microbatch_compiled_code.py` around lines 99 - 117, The helper _with_microbatch_macro_file creates a hardcoded macro file at macros/microbatch.sql which can cause race conditions when tests run in parallel; change the macro_path construction to include the macro_name (e.g., macros/microbatch_{macro_name}.sql) so each parametrized case writes a unique file, keep the same write_text/yield/finally cleanup logic, and ensure the exists check and unlink refer to the updated macro_path variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@integration_tests/tests/test_dbt_artifacts/test_microbatch_compiled_code.py`:
- Around line 99-117: The helper _with_microbatch_macro_file creates a hardcoded
macro file at macros/microbatch.sql which can cause race conditions when tests
run in parallel; change the macro_path construction to include the macro_name
(e.g., macros/microbatch_{macro_name}.sql) so each parametrized case writes a
unique file, keep the same write_text/yield/finally cleanup logic, and ensure
the exists check and unlink refer to the updated macro_path variable.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: da347541-436d-4ced-9296-814bc0e5baf1
📒 Files selected for processing (7)
integration_tests/dbt_project/dbt_project.ymlintegration_tests/dbt_project/macros/get_anomaly_config.sqlintegration_tests/tests/test_anomaly_test_configuration.pyintegration_tests/tests/test_dbt_artifacts/test_microbatch_compiled_code.pymacros/edr/dbt_artifacts/microbatch/capture_microbatch_compiled_code.sqlmacros/utils/graph/get_compiled_code.sqlmacros/utils/graph/get_elementary_config_from_node.sql
✅ Files skipped from review due to trivial changes (2)
- integration_tests/dbt_project/dbt_project.yml
- macros/edr/dbt_artifacts/microbatch/capture_microbatch_compiled_code.sql
🚧 Files skipped from review as they are similar to previous changes (2)
- integration_tests/tests/test_anomaly_test_configuration.py
- integration_tests/dbt_project/macros/get_anomaly_config.sql
Problem
When
meta.elementary(e.g.timestamp_column,days_back,seasonality) is configured at the source level in a dbt sources YAML, it is silently ignored by Elementary tests. Tests on tables under that source don't pick up the config, leading to unexpected behaviour (tests running without a timestamp column, or SQL errors when the config is required).Root cause: dbt stores source-level meta in
node.source_metaon each table node — it is not merged intonode.meta.get_elementary_config_from_nodeonly readnode.meta, so source-level config was never found.Fix
Read
node.source_metainget_elementary_config_from_node, inserted beforenode.metaso that table-level meta still wins (consistent with this macro's existing precedence convention —node.metaalready won overnode.config.metabefore this change).Also extracts the repeated meta→elementary merge pattern into a
_merge_elementary_from_metahelper to reduce duplication.Example — now works
Table-level
meta.elementarystill overrides source-level config.CI failures
test-cloud (fusion, bigquery)andtest-cloud (latest_official, fabric)both failed at "Check DWH connection" before any tests ran — pre-existing infra issue, unrelated to this change.Test plan
test_source_meta_config_is_picked_up— asserts source-leveltimestamp_columnandwhere_expressionare resolved when no model/test config is settest_model_meta_overrides_source_meta_config— asserts table-level meta wins when both are setsource_metaabsent on model nodes → new block is a no-op)🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests