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

Enable macro unit tests #79

Merged
merged 4 commits into from
Jul 3, 2023

Conversation

LeopoldGabelmann
Copy link
Contributor

@LeopoldGabelmann LeopoldGabelmann commented May 22, 2023

This is a:

  • bug fix PR with no breaking changes
  • new functionality
  • a breaking change

Description & motivation

Within our team our yml for our dpt project got quite large. If one uses a lot of data tests, documents every column (even if it is centralized using docs) and then adds all the unit tests on top of it, it can be a lot of lines. Therefore, we were looking for a way to outsource all our unit tests inside of macros.
This has the additional benefit that it is very easy to run single a unit test while coding and controlling for possible errors. Furthermore, this makes it possible to run all data and unit tests apart from each other (e.g. in a CI/CD Pipeline) by selecting/excluding all unit tests via their folder path in tests.

Checklist

  • I have verified that these changes work locally on the following warehouses (Note: it's okay if you do not have access to all warehouses, this helps us understand what has been covered)
    • BigQuery
    • Postgres
    • Redshift
    • Snowflake
  • I have updated the README.md (if applicable)
  • I have added tests & descriptions to my macros (and models if applicable)

@LeopoldGabelmann
Copy link
Contributor Author

Hi @mjirv ,
another idea from me for your awesome package. I am very interested what you are going to think about that one, since it kind of changes your idea to have all tests at one place within the yml files.
I tested the code on our snowflake instance with dbt version 1.5.0.

@mjirv
Copy link
Owner

mjirv commented Jun 4, 2023

@LeopoldGabelmann thanks for submitting this! I think it's a great idea.

I'm taking a closer look, but would you mind adding something to the README about this option so people know it exists? Thanks for adding tests btw.

@LeopoldGabelmann LeopoldGabelmann force-pushed the enable_macro_unit_tests branch 2 times, most recently from 0405e14 to d645685 Compare June 5, 2023 08:45
Replaced the original code inside the test blocks with the new macros.
This has to be done for the values and the keys. This is necessary so that the unit tests can be executed as macros.
@LeopoldGabelmann
Copy link
Contributor Author

@mjirv - You're right, I should have added some explanation to the README on how to use this. I have done so now :)

@LeopoldGabelmann
Copy link
Contributor Author

@mjirv friendly reminder :)

@mjirv mjirv merged commit c2f0a35 into mjirv:development Jul 3, 2023
mjirv added a commit that referenced this pull request Jul 3, 2023
* added incremental model and test

* working code for incremental tests -- old unit test still works

* working code for incremental tests -- old unit test still works

* updated comments

* moved unit test incremental to its own method

* moved unit test incremental to its own method

* updates readme and version for incremental tests

* Enable macro unit tests (#79)

* Added all existing unit tests in macro form.

* Refactored the test blocks into macros blocks.
Replaced the original code inside the test blocks with the new macros.

* Adjusted the render logic for the the input mapping.
This has to be done for the values and the keys. This is necessary so that the unit tests can be executed as macros.

* Added explanations to the README on how to setup the tests as macros.

---------

Co-authored-by: Leopold Gabelmann <[email protected]>

* fixes merge issues

* minor fixes

* oops

* thought i got rid of that already

* o i see

* adds exclude_columns

* adds exclude_columns to test

* bumps versions

---------

Co-authored-by: Petter Söderlund <[email protected]>
Co-authored-by: Leopold Gabelmann <[email protected]>
Co-authored-by: Leopold Gabelmann <[email protected]>
{% macro __set_rendered_keys(ns, keys) %}
{% for k in keys %}
{% do ns.rendered_keys.update({k: render("{{ " + k + " }}")}) %}
{% macro __set_rendered_mappings(ns, input_mapping) %}

Choose a reason for hiding this comment

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

FYI: This forces the ref in the sql files and the yml files to be EXACTLY the same

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.

3 participants