Velir / dbt-ga4

dbt Package for modeling raw data exported by Google Analytics 4. BigQuery support, only.
MIT License
324 stars 135 forks source link

Refactor pytest unit tests to dbt unit tests #346

Open davidbooke4 opened 3 weeks ago

davidbooke4 commented 3 weeks ago

Description & motivation

The goal of this PR is to move the pytest unit tests to dbt unit tests.

Key Changes

Notes

Checklist

adamribaudo-velir commented 3 weeks ago

@davidbooke4 amazing! Just 3 things:

davidbooke4 commented 3 weeks ago

@adamribaudo-velir I left the unit tests folder since I wasn't able to migrate the sessions_traffic_sources_last_non_direct_daily pytest unit test to a dbt unit test because of this issue that you opened a few months back. Do you think not moving that one test justifies keeping the old unit_tests folder, or are we okay to still remove it and not have a unit test for that model for the time being until a fix is implemented for the bug?

adamribaudo-velir commented 3 weeks ago

@adamribaudo-velir I left the unit tests folder since I wasn't able to migrate the sessions_traffic_sources_last_non_direct_daily pytest unit test to a dbt unit test because of this issue that you opened a few months back. Do you think not moving that one test justifies keeping the old unit_tests folder, or are we okay to still remove it and not have a unit test for that model for the time being until a fix is implemented for the bug?

Oh good call. No harm in leaving it until that bug is resolved. We'll just want to make sure that both this Pytest + the unit tests are running during CI

davidbooke4 commented 2 weeks ago

@adamribaudo-velir I left the unit tests folder since I wasn't able to migrate the sessions_traffic_sources_last_non_direct_daily pytest unit test to a dbt unit test because of this issue that you opened a few months back. Do you think not moving that one test justifies keeping the old unit_tests folder, or are we okay to still remove it and not have a unit test for that model for the time being until a fix is implemented for the bug?

Oh good call. No harm in leaving it until that bug is resolved. We'll just want to make sure that both this Pytest + the unit tests are running during CI

Alright, this is all set for another look whenever you get the chance!

davidbooke4 commented 1 week ago

@adamribaudo-velir I also wanted to call out that these unit tests will run when users install this package and execute a dbt run or dbt build. I was thinking of adding a note in the README about including the --exclude-resource-type unit_test flag in whatever folks are using to orchestrate and execute dbt, but I wanted to get your thoughts on it as well!

dgitis commented 1 week ago

Thank you both for all of your work on this. I've been watching the conversation for awhile.

I just tried running this and it seems that you get a "Parsing Error" if you don't have the stg_ga4__derived _user_properties model enabled.

This model only gets enabled when you have derived_user_properties set.

{{ config(
  enabled = true if var('derived_user_properties', false) or env_var('GA4_DERIVED_USER_PROPERTIES', false) else false,
  materialized = "table"
) }}

I think I should maybe report this as a dbt issue. I've had a number of issues where package YML cause issues when you override at the project level. Introducing this new testing mechanism just makes them worse.

I'm thinking as a workaround, we could maybe create an empty View when the variable is not set rather than set enabled = false.

davidbooke4 commented 1 week ago

Thank you both for all of your work on this. I've been watching the conversation for awhile.

I just tried running this and it seems that you get a "Parsing Error" if you don't have the stg_ga4__derived _user_properties model enabled.

This model only gets enabled when you have derived_user_properties set.

{{ config(
  enabled = true if var('derived_user_properties', false) or env_var('GA4_DERIVED_USER_PROPERTIES', false) else false,
  materialized = "table"
) }}

I think I should maybe report this as a dbt issue. I've had a number of issues where package YML cause issues when you override at the project level. Introducing this new testing mechanism just makes them worse.

I'm thinking as a workaround, we could maybe create an empty View when the variable is not set rather than set enabled = false.

Thanks for looking this over @dgitis!

There's this open issue and PR related to disabling unit tests when their corresponding models are disabled. I was able to compile a project in which I installed this version of the dbt-ga4 package and then set the derived_session_properties, derived_user_properties, and conversion_events variables, so I don't think there's a problem with project variables overriding package variables.

I'm hopeful that the PR will be resolved soon and this problem will be fixed, but materializing the model as an empty view is an alternative I hadn't considered. I was debating keeping the pytest tests in place for these few models that are enabled based on project variables and then moving them to dbt unit tests once that issue is resolved.