From 4c09c21b3f1b2477cfa072377547306dbb35d402 Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Fri, 3 Mar 2023 10:10:12 -0600 Subject: [PATCH 1/7] first pass with contract check --- dbt/include/snowflake/macros/adapters.sql | 3 ++ tests/functional/adapter/test_constraints.py | 30 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/dbt/include/snowflake/macros/adapters.sql b/dbt/include/snowflake/macros/adapters.sql index a8dd34dc2..daff74714 100644 --- a/dbt/include/snowflake/macros/adapters.sql +++ b/dbt/include/snowflake/macros/adapters.sql @@ -94,6 +94,9 @@ {{ get_persist_docs_column_list(model_columns, query_columns) }} {%- endif %} + {% if config.get('contract', False) -%} + {{ get_assert_columns_equivalent(sql) }} + {%- endif %} {% if copy_grants -%} copy grants {%- endif %} as ( {{ sql }} ); diff --git a/tests/functional/adapter/test_constraints.py b/tests/functional/adapter/test_constraints.py index 8e2607f76..459d5fdfc 100644 --- a/tests/functional/adapter/test_constraints.py +++ b/tests/functional/adapter/test_constraints.py @@ -2,7 +2,8 @@ from dbt.tests.util import relation_from_name from dbt.tests.adapter.constraints.test_constraints import ( - BaseConstraintsColumnsEqual, + BaseTableConstraintsColumnsEqual, + BaseViewConstraintsColumnsEqual, BaseConstraintsRuntimeEnforcement ) @@ -21,8 +22,33 @@ """ -class TestSnowflakeConstraintsColumnsEqual(BaseConstraintsColumnsEqual): +class TestSnowflakeTableConstraintsColumnsEqual(BaseTableConstraintsColumnsEqual): + @pytest.fixture + def int_type(self): + return "FIXED" + + @pytest.fixture + def schema_int_type(self): + return "INT" + + @pytest.fixture + def data_types(self, int_type, schema_int_type, string_type): + # sql_column_value, schema_data_type, error_data_type + return [ + ['1', schema_int_type, int_type], + ["'1'", string_type, string_type], + ["cast('2019-01-01' as date)", 'date', 'DATE'], + ["true", 'boolean', 'BOOLEAN'], + ["'2013-11-03 00:00:00-07'::timestamptz", 'timestamp_tz', 'TIMESTAMP_TZ'], + ["'2013-11-03 00:00:00-07'::timestamp", 'timestamp', 'TIMESTAMP_NTZ'], + ["ARRAY_CONSTRUCT('a','b','c')", 'array', 'ARRAY'], + ["ARRAY_CONSTRUCT(1,2,3)", 'array', 'ARRAY'], + ["""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""", 'variant', 'VARIANT'], + ] + +class TestSnowflakeViewConstraintsColumnsEqual(BaseViewConstraintsColumnsEqual): + # TODO: need to verify this @pytest.fixture def int_type(self): return "FIXED" From 44626c78668d006dcd3ef08aaa08ac550d289e8f Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Fri, 3 Mar 2023 10:19:59 -0600 Subject: [PATCH 2/7] rename test class --- tests/functional/adapter/test_constraints.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/functional/adapter/test_constraints.py b/tests/functional/adapter/test_constraints.py index 459d5fdfc..2526e0afe 100644 --- a/tests/functional/adapter/test_constraints.py +++ b/tests/functional/adapter/test_constraints.py @@ -21,6 +21,7 @@ ); """ +<<<<<<< HEAD class TestSnowflakeTableConstraintsColumnsEqual(BaseTableConstraintsColumnsEqual): @pytest.fixture @@ -71,6 +72,14 @@ def data_types(self, int_type, schema_int_type, string_type): ["ARRAY_CONSTRUCT(1,2,3)", 'array', 'ARRAY'], ["""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""", 'variant', 'VARIANT'], ] +======= +class TestSnowflakeTableConstraintsColumnsEqual(BaseTableConstraintsColumnsEqual): + pass + + +class TestSnowflakeViewConstraintsColumnsEqual(BaseViewConstraintsColumnsEqual): + pass +>>>>>>> ace43a6 (rename test class) class TestSnowflakeConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement): From bb0e4620af6422b654f4fef830fb59b0893355db Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Mon, 6 Mar 2023 14:12:43 -0600 Subject: [PATCH 3/7] file merge cleanup --- tests/functional/adapter/test_constraints.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/functional/adapter/test_constraints.py b/tests/functional/adapter/test_constraints.py index 2526e0afe..346288aa7 100644 --- a/tests/functional/adapter/test_constraints.py +++ b/tests/functional/adapter/test_constraints.py @@ -21,7 +21,6 @@ ); """ -<<<<<<< HEAD class TestSnowflakeTableConstraintsColumnsEqual(BaseTableConstraintsColumnsEqual): @pytest.fixture @@ -49,7 +48,6 @@ def data_types(self, int_type, schema_int_type, string_type): class TestSnowflakeViewConstraintsColumnsEqual(BaseViewConstraintsColumnsEqual): - # TODO: need to verify this @pytest.fixture def int_type(self): return "FIXED" @@ -72,14 +70,6 @@ def data_types(self, int_type, schema_int_type, string_type): ["ARRAY_CONSTRUCT(1,2,3)", 'array', 'ARRAY'], ["""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""", 'variant', 'VARIANT'], ] -======= -class TestSnowflakeTableConstraintsColumnsEqual(BaseTableConstraintsColumnsEqual): - pass - - -class TestSnowflakeViewConstraintsColumnsEqual(BaseViewConstraintsColumnsEqual): - pass ->>>>>>> ace43a6 (rename test class) class TestSnowflakeConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement): From 6ab93752c7e7967cd361e609ad725880c224de83 Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Mon, 6 Mar 2023 14:57:56 -0600 Subject: [PATCH 4/7] clean up tests --- dev-requirements.txt | 4 +-- tests/functional/adapter/test_constraints.py | 28 ++++---------------- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index e7f7bd2fa..f5acff1b6 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,7 +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-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter +git+https://github.com/dbt-labs/dbt-core.git@er/ct-2167-model-contracts-mats#egg=dbt-core&subdirectory=core +git+https://github.com/dbt-labs/dbt-core.git@er/ct-2167-model-contracts-mats#egg=dbt-tests-adapter&subdirectory=tests/adapter # if version 1.x or greater -> pin to major version # if version 0.x -> pin to minor diff --git a/tests/functional/adapter/test_constraints.py b/tests/functional/adapter/test_constraints.py index 346288aa7..bf8e3c9a2 100644 --- a/tests/functional/adapter/test_constraints.py +++ b/tests/functional/adapter/test_constraints.py @@ -22,7 +22,7 @@ """ -class TestSnowflakeTableConstraintsColumnsEqual(BaseTableConstraintsColumnsEqual): +class SnowflakeSetup: @pytest.fixture def int_type(self): return "FIXED" @@ -46,30 +46,12 @@ def data_types(self, int_type, schema_int_type, string_type): ["""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""", 'variant', 'VARIANT'], ] +class TestSnowflakeTableConstraintsColumnsEqual(SnowflakeSetup, BaseTableConstraintsColumnsEqual): + pass -class TestSnowflakeViewConstraintsColumnsEqual(BaseViewConstraintsColumnsEqual): - @pytest.fixture - def int_type(self): - return "FIXED" - @pytest.fixture - def schema_int_type(self): - return "INT" - - @pytest.fixture - def data_types(self, int_type, schema_int_type, string_type): - # sql_column_value, schema_data_type, error_data_type - return [ - ['1', schema_int_type, int_type], - ["'1'", string_type, string_type], - ["cast('2019-01-01' as date)", 'date', 'DATE'], - ["true", 'boolean', 'BOOLEAN'], - ["'2013-11-03 00:00:00-07'::timestamptz", 'timestamp_tz', 'TIMESTAMP_TZ'], - ["'2013-11-03 00:00:00-07'::timestamp", 'timestamp', 'TIMESTAMP_NTZ'], - ["ARRAY_CONSTRUCT('a','b','c')", 'array', 'ARRAY'], - ["ARRAY_CONSTRUCT(1,2,3)", 'array', 'ARRAY'], - ["""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""", 'variant', 'VARIANT'], - ] +class TestSnowflakeViewConstraintsColumnsEqual(SnowflakeSetup, BaseViewConstraintsColumnsEqual): + pass class TestSnowflakeConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement): From c36af21c705c924ea4c75e87f46bb0318032084b Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Mon, 6 Mar 2023 15:07:44 -0600 Subject: [PATCH 5/7] changelog --- .changes/unreleased/Features-20230223-182450.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.changes/unreleased/Features-20230223-182450.yaml b/.changes/unreleased/Features-20230223-182450.yaml index 45c0efe71..9d4446888 100644 --- a/.changes/unreleased/Features-20230223-182450.yaml +++ b/.changes/unreleased/Features-20230223-182450.yaml @@ -1,6 +1,6 @@ kind: Features -body: implement data_type_code_to_name on SnowflakeConnectionManager +body: Enforce contracts on models materialized as tables and views time: 2023-02-23T18:24:50.272955-05:00 custom: - Author: michelleark - Issue: "464" + Author: michelleark emmyoop + Issue: 464 490 From 636909bbfc75d03e17267afd1018904250c86385 Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Tue, 7 Mar 2023 09:39:25 -0600 Subject: [PATCH 6/7] fix class name --- tests/functional/adapter/test_constraints.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/functional/adapter/test_constraints.py b/tests/functional/adapter/test_constraints.py index bf8e3c9a2..fd1f41847 100644 --- a/tests/functional/adapter/test_constraints.py +++ b/tests/functional/adapter/test_constraints.py @@ -22,7 +22,7 @@ """ -class SnowflakeSetup: +class SnowflakeColumnEqualSetup: @pytest.fixture def int_type(self): return "FIXED" @@ -46,11 +46,11 @@ def data_types(self, int_type, schema_int_type, string_type): ["""TO_VARIANT(PARSE_JSON('{"key3": "value3", "key4": "value4"}'))""", 'variant', 'VARIANT'], ] -class TestSnowflakeTableConstraintsColumnsEqual(SnowflakeSetup, BaseTableConstraintsColumnsEqual): +class TestSnowflakeTableConstraintsColumnsEqual(SnowflakeColumnEqualSetup, BaseTableConstraintsColumnsEqual): pass -class TestSnowflakeViewConstraintsColumnsEqual(SnowflakeSetup, BaseViewConstraintsColumnsEqual): +class TestSnowflakeViewConstraintsColumnsEqual(SnowflakeColumnEqualSetup, BaseViewConstraintsColumnsEqual): pass From c2472691d29b80b0c2fc6f86b38bcb5565610fd7 Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Tue, 7 Mar 2023 16:31:18 -0600 Subject: [PATCH 7/7] remove dbt-core pin --- dev-requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index f5acff1b6..e7f7bd2fa 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,7 +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@er/ct-2167-model-contracts-mats#egg=dbt-core&subdirectory=core -git+https://github.com/dbt-labs/dbt-core.git@er/ct-2167-model-contracts-mats#egg=dbt-tests-adapter&subdirectory=tests/adapter +git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core +git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter # if version 1.x or greater -> pin to major version # if version 0.x -> pin to minor