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

Allow where: functionality to work on tests #82

Open
clairetaylor352 opened this issue May 31, 2023 · 2 comments
Open

Allow where: functionality to work on tests #82

clairetaylor352 opened this issue May 31, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@clairetaylor352
Copy link

clairetaylor352 commented May 31, 2023

Describe the feature

dbt specifies that you can add where: to a test to filter the results that you are testing on. I wanted to do this with your cool mock tool so I gave it a go.

You get an error

 Compilation Error in test dbt_datamocktool_xxxxx (models/gold/gold.yml)
12:12:58    'str object' has no attribute 'name'
12:12:58    
12:12:58    > in macro get_unit_test_sql (macros/dmt_get_test_sql.sql)
12:12:58    > called by macro test_unit_test (macros/dmt_unit_test.sql)
12:12:58    > called by test dbt_datamocktool_unit_test_scorecard_results_orb

I've tracked this down to when you call model.name within get_unit_test_sql. This is because dbt replaces the "model" parameter with a string with the where clause applied, like so:
(select * from <Table> where <where_clause>) dbt_subquery

It no longer has a name attribute.

Describe alternatives you've considered

It could be possible to override this functionality to take a where_clause parameter instead and handle it differently within the code (so where: would still not work)

Would then need to modify the code to call audit_helper's compare_queries instead in this case, with the compare_model being SELECT <cols> from <mocked_table> WHERE <where_clause>.

I can't think of an alternative that wouldn't involve a PR.

Additional context

Is this feature database-specific? Which database(s) is/are relevant? Please include any other relevant context here.
I doubt it's DB specific, I am using Snowflake

Who will this benefit?

Anyone else who wants to only test certain results are present in the table we are mocking

Are you interested in contributing this feature?

Yes I could give this a go.

@clairetaylor352 clairetaylor352 added the enhancement New feature or request label May 31, 2023
@mjirv
Copy link
Owner

mjirv commented Jun 4, 2023

Good suggestion. I don't think it should be too hard, we just have to find the <Table> (model) in (select * from <Table> where <where_clause>) dbt_subquery, send that to get_unit_test_sql() and put it back in.

I'm curious, what's the use-case for adding a where clause in a unit test?

@pettersoderlund
Copy link
Collaborator

My experience with the native where-clause has sometimes been bad - especially when comparing two tables. It is impossible to access the where statement in the test macro! A common pattern is to use a parameter specifying the condition and implementing it yourself.

I am also curious of the use case for this since I myself has not felt the need for it in unit testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants