Skip to content

Commit

Permalink
added tests for get_url_parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Tworkiewicz committed Feb 4, 2022
1 parent 2263ec5 commit 4782b6f
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 15 deletions.
56 changes: 56 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## teradata_utils 0.0.2

### Features
* Added tests and/or implementations for the following `dbt_utils` macros:
* pivot
* unpivot
* get_url_parameter
* Added support for multi-character delimiters in `split_part` macro.

### Fixes

### Docs

### Under the hood


## teradata_utils 0.0.1

### Features
* initial release that covers the following `dbt_utils` macros:
* equal_rowcount
* fewer_rows_than
* equality
* expression_is_true
* recency
* at_least_one
* not_constant
* cardinality_equality
* not_null_proportion
* not_accepted_values
* relationships_where
* mutually_exclusive_ranges
* sequential_values
* unique_combination_of_columns
* accepted_range
* get_column_values
* get_relations_by_pattern
* get_relations_by_prefix
* get_query_results_as_dict
* date_spine
* haversine_distance
* group_by
* star
* union_relations
* generate_series
* surrogate_key
* safe_add


### Fixes

### Docs
* Initial documentation

### Under the hood
* Added tests
18 changes: 12 additions & 6 deletions macros/split_part.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{# The default implementation does not quote function parameters #}
{# Also, the function used in the original macro ('split_part') is called 'strtok' in Teradata #}
{# The function used in the original macro ('split_part') is called 'strtok' in Teradata #}

{% macro default__split_part(string_text, delimiter_text, part_number) %}

{% if delimiter_text|length == 1 %}
strtok(
'{{ string_text }}',
'{{ delimiter_text }}',
{{ string_text }},
{{ delimiter_text }},
{{ part_number }}
)

{% else %}
-- This is a hack. We are replacing the delimiter text with chr(1). We then let strtok() tokenize.
-- chr(1) Is a unique character. If there is chr(1) in the search string this query will produce hard-to-debug errors.
strtok(oreplace({{ string_text }}, {{ delimiter_text }}, chr(1)),
chr(1),
{{ part_number }}
)
{% endif %}
{% endmacro %}
69 changes: 69 additions & 0 deletions test/integration/get_url_parameter.dbtspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
target:
type: teradata
server: "{{ env_var('DBT_TERADATA_SERVER_NAME', 'localhost') }}"
username: "{{ env_var('DBT_TERADATA_USERNAME', 'dbc') }}"
password: "{{ env_var('DBT_TERADATA_PASSWORD', 'dbc') }}"
schema: "dbt_test_{{ var('_dbt_random_suffix') }}"
tmode: ANSI
log: "0"

projects:
- name: project_for_test
paths:
dbt_project.yml: |
name: 'project_for_test'
version: '1.0.0'
config-version: 2
dispatch:
- macro_namespace: dbt_utils
search_order:
- teradata_utils
- dbt_utils
seeds:
project_for_test: # you must include the project name
test_table1:
+column_types:
date: date
size: varchar(1)
color: varchar(5)
status: varchar(15)

packages.yml: |
packages:
- package: dbt-labs/dbt_utils
version: 0.8.0
- local: "{{ env_var('DBT_TERADATA_UTILS_ROOT') }}"

seeds/urls.csv: |
id,url
1,https://github.com/dbt-labs/dbt-utils#pivot-source?utm_source=123
2,https://search.google.com/u/1/search-console/mobile-usability?resource_id=https://quickstarts.teradata.com/&utm_source=123&utm_medium=gamma&utm_campaign=wnc_10030322&utm_content=msg_100058679&hl=en

models/report.sql: |
{{
config(
materialized="table"
)
}}
SELECT {{ dbt_utils.get_url_parameter(field='url', url_parameter='utm_source') }} AS utm_source_value
FROM {{ ref('urls') }}
WHERE utm_source_value = '123'


sequences:
test:
project: project_for_test
sequence:
- type: dbt
cmd: deps
- type: dbt
cmd: seed
- type: dbt
cmd: run
- type: run_results
exists: True
names:
- report
- type: relation_rows
name: report
length: 2
23 changes: 14 additions & 9 deletions test/integration/split_part.dbtspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ projects:
search_order:
- teradata_utils
- dbt_utils
seeds:
project_for_test: # you must include the project name
test_table1:
+column_types:
attrA: varchar(5)
attrB: varchar(5)
attrC: varchar(5)
id: int

packages.yml: |
packages:
Expand All @@ -40,7 +32,16 @@ projects:
materialized="table"
)
}}
SELECT {{ dbt_utils.split_part(string_text='1,2,3', delimiter_text=',', part_number=2) }} AS split_result
SELECT {{ dbt_utils.split_part(string_text='\'1,2,3\'', delimiter_text='\',\'', part_number=2) }} AS split_result
WHERE split_result = '2'

models/report2.sql: |
{{
config(
materialized="table"
)
}}
SELECT {{ dbt_utils.split_part(string_text='\'1param2\'', delimiter_text='\'param\'', part_number=2) }} AS split_result
WHERE split_result = '2'

sequences:
Expand All @@ -55,6 +56,10 @@ sequences:
exists: True
names:
- report
- report2
- type: relation_rows
name: report
length: 1
- type: relation_rows
name: report2
length: 1

0 comments on commit 4782b6f

Please sign in to comment.