Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] can't override namespaced macros for unit testing #9624

Closed
2 tasks done
Tracked by #8283
graciegoheen opened this issue Feb 21, 2024 · 0 comments · Fixed by #9626 or dbt-labs/docs.getdbt.com#5124
Closed
2 tasks done
Tracked by #8283

[Bug] can't override namespaced macros for unit testing #9624

graciegoheen opened this issue Feb 21, 2024 · 0 comments · Fixed by #9626 or dbt-labs/docs.getdbt.com#5124
Assignees
Labels
bug Something isn't working High Severity bug with significant impact that should be resolved in a reasonable timeframe pre-release Bug not yet in a stable release unit tests Issues related to built-in dbt unit testing functionality
Milestone

Comments

@graciegoheen
Copy link
Contributor

graciegoheen commented Feb 21, 2024

Is this a new bug in dbt-core?

  • I believe this is a new bug in dbt-core
  • I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

I have a model called stg_payments that uses {{ dbt.current_timestamp() }}:

with

source as (

    select * from {{ source('jaffle_shop',  'orders') }}

),

staged as (

    select 
        id as order_id,
        user_id as customer_id,
        order_date,
        datediff('day', order_date, {{ dbt.current_timestamp() }} ) as days_since_ordered
    from source

)

select * from staged

I try to unit test this model with an override for current_timestamp:

unit_tests:
  - name: test_days_since_ordered
    description: Test my datediff caluclation for days_since_ordered
    model: stg_orders
    overrides:
      macros:
        current_timestamp: "date('2023-01-15')"
    given:
      - input: source('jaffle_shop',  'orders')
        rows:
          - {order_date: 2023-01-01}
    expect:
      rows:
        - {order_date: 2023-01-01, days_since_ordered: 14}

My unit test fails because the current_timestamp macro is not overridden.

Screenshot 2024-02-21 at 4 59 25 PM

I get the same failure when I try:

    overrides:
      macros:
        dbt.current_timestamp: "date('2023-01-15')"

Current workaround

If I instead don't use dbt. in front of my current_timestamp() macro, the unit test succeeds as expected:

datediff('day', order_date, {{ current_timestamp() }} ) as days_since_ordered

# OR

{{ dbt.datediff('order_date', current_timestamp(), 'day') }} as days_since_ordered

Screenshot 2024-02-21 at 5 01 37 PM

But this SQL goes against our documentation which tells you to use the current_timestamp() macro within your dbt SQL files like this:

{{ dbt.current_timestamp() }}

This also prevents you from being able to override macros from other namespaces (like imported packages). :(

Expected Behavior

I should be able to override namespaced macros (dbt., dbt_utils., etc.) in my unit test definition.

Which database adapter are you using with dbt?

snowflake

@graciegoheen graciegoheen added bug Something isn't working triage pre-release Bug not yet in a stable release and removed triage labels Feb 21, 2024
@graciegoheen graciegoheen added the High Severity bug with significant impact that should be resolved in a reasonable timeframe label Feb 22, 2024
@graciegoheen graciegoheen added this to the v1.8 milestone Feb 22, 2024
@MichelleArk MichelleArk self-assigned this Feb 28, 2024
mirnawong1 added a commit to dbt-labs/docs.getdbt.com that referenced this issue Mar 21, 2024
## What are you changing in this pull request and why?
<!---
Describe your changes and why you're making them. If related to an open 
issue or a pull request on dbt Core, then link to them here! 

To learn more about the writing conventions used in the dbt Labs docs,
see the [Content style
guide](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/content-style-guide.md).
-->

Update docs now that core bug is fixed
dbt-labs/dbt-core#9624

## Checklist
<!--
Uncomment when publishing docs for a prerelease version of dbt:
- [ ] Add versioning components, as described in [Versioning
Docs](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/single-sourcing-content.md#versioning-entire-pages)
- [ ] Add a note to the prerelease version [Migration
Guide](https://github.com/dbt-labs/docs.getdbt.com/tree/current/website/docs/docs/dbt-versions/core-upgrade)
-->
- [ ] Review the [Content style
guide](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/content-style-guide.md)
so my content adheres to these guidelines.
- [ ] For [docs
versioning](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/single-sourcing-content.md#about-versioning),
review how to [version a whole
page](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/single-sourcing-content.md#adding-a-new-version)
and [version a block of
content](https://github.com/dbt-labs/docs.getdbt.com/blob/current/contributing/single-sourcing-content.md#versioning-blocks-of-content).
- [ ] Add a checklist item for anything that needs to happen before this
PR is merged, such as "needs technical review" or "change base branch."

Adding or removing pages (delete if not applicable):
- [ ] Add/remove page in `website/sidebars.js`
- [ ] Provide a unique filename for new pages
- [ ] Add an entry for deleted pages in `website/vercel.json`
- [ ] Run link testing locally with `npm run build` to update the links
that point to deleted pages
@dbeatty10 dbeatty10 added the unit tests Issues related to built-in dbt unit testing functionality label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working High Severity bug with significant impact that should be resolved in a reasonable timeframe pre-release Bug not yet in a stable release unit tests Issues related to built-in dbt unit testing functionality
Projects
None yet
3 participants