Skip to content

Commit

Permalink
[Snowflake] test types for unit testing (#896)
Browse files Browse the repository at this point in the history
* first pass: test unit testing on snowflake

* array and json support

* variant support

* refactor to implement cast

* geography and geometry types + cast refactor

* fix variant logic in safe_cast

* typo

* TestSnowflakeUnitTestCaseInsensitivity

* changelog entry

* TestSnowflakeUnitTestInvalidInput

* restore dev-requirements

---------

Co-authored-by: Mike Alfare <[email protected]>
  • Loading branch information
MichelleArk and mikealfare authored Feb 9, 2024
1 parent 0280342 commit d01d63c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240205-174816.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Support primative types + object, variant, array in snowflake for unit testing"
time: 2024-02-05T17:48:16.118398-05:00
custom:
Author: michelleark
Issue: "898"
9 changes: 9 additions & 0 deletions dbt/include/snowflake/macros/utils/cast.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% macro snowflake__cast(field, type) %}
{% if (type|upper == "GEOGRAPHY") -%}
to_geography({{field}})
{% elif (type|upper == "GEOMETRY") -%}
to_geometry({{field}})
{% else -%}
cast({{field}} as {{type}})
{% endif -%}
{% endmacro %}
12 changes: 11 additions & 1 deletion dbt/include/snowflake/macros/utils/safe_cast.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{% macro snowflake__safe_cast(field, type) %}
try_cast({{field}} as {{type}})
{% if type|upper == "GEOMETRY" -%}
try_to_geometry({{field}})
{% elif type|upper == "GEOGRAPHY" -%}
try_to_geography({{field}})
{% elif type|upper != "VARIANT" -%}
{#-- Snowflake try_cast does not support casting to variant, and expects the field as a string --#}
{% set field_as_string = dbt.string_literal(field) if field is number else field %}
try_cast({{field_as_string}} as {{type}})
{% else -%}
{{ adapter.dispatch('cast', 'dbt')(field, type) }}
{% endif -%}
{% endmacro %}
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# install latest changes in dbt-core
# TODO: how to automate switching from develop to version branches?
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-adapters.git
git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter

# if version 1.x or greater -> pin to major version
Expand Down
39 changes: 39 additions & 0 deletions tests/functional/adapter/unit_testing/test_unit_testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from dbt.tests.adapter.unit_testing.test_types import BaseUnitTestingTypes
from dbt.tests.adapter.unit_testing.test_case_insensitivity import BaseUnitTestCaseInsensivity
from dbt.tests.adapter.unit_testing.test_invalid_input import BaseUnitTestInvalidInput


class TestSnowflakeUnitTestingTypes(BaseUnitTestingTypes):
@pytest.fixture
def data_types(self):
# sql_value, yaml_value
return [
["1", "1"],
["2.0", "2.0"],
["'12345'", "12345"],
["'string'", "string"],
["true", "true"],
["DATE '2020-01-02'", "2020-01-02"],
["TIMESTAMP '2013-11-03 00:00:00-0'", "2013-11-03 00:00:00-0"],
["'2013-11-03 00:00:00-0'::TIMESTAMPTZ", "2013-11-03 00:00:00-0"],
["TO_NUMBER('3', 10, 9)", "3"],
["3::VARIANT", "3"],
["TO_GEOMETRY('POINT(1820.12 890.56)')", "POINT(1820.12 890.56)"],
["TO_GEOGRAPHY('POINT(-122.35 37.55)')", "POINT(-122.35 37.55)"],
[
"{'Alberta':'Edmonton','Manitoba':'Winnipeg'}",
"{'Alberta':'Edmonton','Manitoba':'Winnipeg'}",
],
["['a','b','c']", "['a','b','c']"],
["[1,2,3]", "[1, 2, 3]"],
]


class TestSnowflakeUnitTestCaseInsensitivity(BaseUnitTestCaseInsensivity):
pass


class TestSnowflakeUnitTestInvalidInput(BaseUnitTestInvalidInput):
pass

0 comments on commit d01d63c

Please sign in to comment.