-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- Variation of dbt_utils.slugify macro using kebab-case instead of snake_case | ||
{% macro kebab_slugify(string) %} | ||
{#- Lower case the string -#} | ||
{% set string = string | lower %} | ||
|
||
{#- Replace spaces, slashes, and underscores with hyphens -#} | ||
{% set string = modules.re.sub('[ _/]+', '-', string) %} | ||
|
||
{#- Only take letters, numbers, and hyphens -#} | ||
{% set string = modules.re.sub('[^a-z0-9-]+', '', string) %} | ||
|
||
{#- Prepends "_" if string begins with a number -#} | ||
{% set string = modules.re.sub('^[0-9]', '_' + string[0], string) %} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jeancochrane
Author
Contributor
|
||
|
||
{{ return(string) }} | ||
{% endmacro %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- dbt macro unit testing inspired by this blog post: | ||
-- https://docs.getdbt.com/blog/unit-testing-dbt-packages | ||
{% macro test_all() %} | ||
{% do test_slugify() %} | ||
{% do test_generate_schema_name() %} | ||
{% endmacro %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{% macro test_generate_schema_name() %} | ||
{% do test_generate_schema_name_handles_dev_env() %} | ||
{% do test_generate_schema_name_handles_ci_env() %} | ||
{% do test_generate_schema_name_handles_prod_env() %} | ||
{% do test_generate_schema_name_raises_for_default_schema_name() %} | ||
{% endmacro %} | ||
|
||
{% macro mock_env_var(var_name) %} | ||
{% if var_name == "USER" %} | ||
{{ return("testuser") }} | ||
{% elif var_name == "GITHUB_HEAD_REF" %} | ||
{{ return("testuser/feature-branch-1") }} | ||
{% else %} | ||
{{ return("") }} | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
{% macro mock_raise_compiler_error(_error) %} | ||
{{ return("Compiler error raised") }} | ||
{% endmacro %} | ||
|
||
{% macro test_generate_schema_name_handles_dev_env() %} | ||
{% do assert_equals( | ||
"test_generate_schema_name_handles_dev_env", | ||
_generate_schema_name( | ||
"test", | ||
{"name": "test"}, | ||
{"schema": "default", "name": "dev"}, | ||
mock_env_var, | ||
exceptions.raise_compiler_error | ||
), | ||
"dev-testuser-test" | ||
) %} | ||
{% endmacro %} | ||
|
||
{% macro test_generate_schema_name_handles_ci_env() %} | ||
{% do assert_equals( | ||
"test_generate_schema_name_handles_ci_env", | ||
_generate_schema_name( | ||
"test", | ||
{"name": "test"}, | ||
{"schema": "default", "name": "ci"}, | ||
mock_env_var, | ||
exceptions.raise_compiler_error | ||
), | ||
"ci-testuser-feature-branch-1-test" | ||
) %} | ||
{% endmacro %} | ||
|
||
{% macro test_generate_schema_name_handles_prod_env() %} | ||
{% do assert_equals( | ||
"test_generate_schema_name_handles_prod_env", | ||
_generate_schema_name( | ||
"test", | ||
{"name": "test"}, | ||
{"schema": "default", "name": "prod"}, | ||
mock_env_var, | ||
exceptions.raise_compiler_error | ||
), | ||
"test" | ||
) %} | ||
{% endmacro %} | ||
|
||
{% macro test_generate_schema_name_raises_for_default_schema_name() %} | ||
{% do assert_equals( | ||
"test_generate_schema_name_raises_for_default_schema_name", | ||
_generate_schema_name( | ||
None, | ||
{"name": "test"}, | ||
{"schema": "default", "name": "prod"}, | ||
mock_env_var, | ||
mock_raise_compiler_error | ||
), | ||
"Compiler error raised" | ||
) %} | ||
{% endmacro %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{% macro test_slugify() %} | ||
{% do test_kebab_slugify() %} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify() %} | ||
{% do test_kebab_slugify_lowercases_strings() %} | ||
{% do test_kebab_slugify_replaces_spaces() %} | ||
{% do test_kebab_slugify_replaces_slashes() %} | ||
{% do test_kebab_slugify_replaces_underscores() %} | ||
{% do test_kebab_slugify_removes_special_characters() %} | ||
{% do test_kebab_slugify_handles_leading_numbers() %} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify_lowercases_strings() %} | ||
{{ assert_equals( | ||
"test_kebab_slugify_lowercases_strings", | ||
kebab_slugify("TEST"), | ||
"test" | ||
) }} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify_replaces_spaces() %} | ||
{{ assert_equals( | ||
"test_kebab_slugify_replaces_spaces", | ||
kebab_slugify("t e s t"), | ||
"t-e-s-t" | ||
) }} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify_replaces_slashes() %} | ||
{{ assert_equals( | ||
"test_kebab_slugify_replaces_slashes", | ||
kebab_slugify("t/e/s/t"), | ||
"t-e-s-t" | ||
) }} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify_replaces_underscores() %} | ||
{{ assert_equals( | ||
"test_kebab_slugify_replaces_underscores", | ||
kebab_slugify("t_e_s_t"), | ||
"t-e-s-t" | ||
) }} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify_removes_special_characters() %} | ||
{{ assert_equals( | ||
"test_kebab_slugify_removes_special_characters", | ||
kebab_slugify("t!@#e$%^s&*()t"), | ||
"test" | ||
) }} | ||
{% endmacro %} | ||
|
||
{% macro test_kebab_slugify_handles_leading_numbers() %} | ||
{{ assert_equals( | ||
"test_kebab_slugify_handles_leading_numbers", | ||
kebab_slugify("123test"), | ||
"_123test" | ||
) }} | ||
{% endmacro %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% macro assert_equals(test_name, value, expected) %} | ||
{% if value == expected %} | ||
{% do log(test_name ~ " - PASS", info=True) %} | ||
{% else %} | ||
{% do exceptions.raise_compiler_error( | ||
test_name ~ " - FAIL: " ~ value~ " != " ~ expected | ||
) %} | ||
{% endif %} | ||
{% endmacro %} |
question (blocking): Wouldn't this prepend an underscore to the beginning of most issue branches such that the final table name would be something like
dev-_123-issue-branch-location.vw_pin10_location_test
?suggestion: Maybe we should just make the inter-keyword separator an underscore and everything else a dash i.e.
dev_123-issue-branch_location.vw_pin10_location_test
ordev_jeancochrane-dev-branch_location.vw_pin10_location_test
.