diff --git a/.changes/unreleased/Under the Hood-20230106-112855.yaml b/.changes/unreleased/Under the Hood-20230106-112855.yaml new file mode 100644 index 00000000000..1344b3397c0 --- /dev/null +++ b/.changes/unreleased/Under the Hood-20230106-112855.yaml @@ -0,0 +1,6 @@ +kind: Under the Hood +body: '[CT-1693] Port severity test to Pytest' +time: 2023-01-06T11:28:55.800547-08:00 +custom: + Author: aranke + Issue: "6466" diff --git a/test/integration/045_test_severity_tests/models/model.sql b/test/integration/045_test_severity_tests/models/model.sql deleted file mode 100644 index 3e29210ab0a..00000000000 --- a/test/integration/045_test_severity_tests/models/model.sql +++ /dev/null @@ -1 +0,0 @@ -select * from {{ source('source', 'nulls') }} diff --git a/test/integration/045_test_severity_tests/models/schema.yml b/test/integration/045_test_severity_tests/models/schema.yml deleted file mode 100644 index 207c16c16c7..00000000000 --- a/test/integration/045_test_severity_tests/models/schema.yml +++ /dev/null @@ -1,19 +0,0 @@ -version: 2 -models: - - name: model - columns: - - name: email - tests: - - not_null: - severity: "{{ 'error' if var('strict', false) else 'warn' }}" -sources: - - name: source - schema: "{{ var('test_run_schema') }}" - tables: - - name: nulls - identifier: null_seed - columns: - - name: email - tests: - - not_null: - severity: "{{ 'error' if var('strict', false) else 'warn' }}" diff --git a/test/integration/045_test_severity_tests/seeds/null_seed.csv b/test/integration/045_test_severity_tests/seeds/null_seed.csv deleted file mode 100644 index b26a87430ac..00000000000 --- a/test/integration/045_test_severity_tests/seeds/null_seed.csv +++ /dev/null @@ -1,21 +0,0 @@ -id,first_name,last_name,email,gender,ip_address,updated_at -1,Judith,Kennedy,jkennedy0@phpbb.com,Female,54.60.24.128,2015-12-24 12:19:28 -2,Arthur,Kelly,akelly1@eepurl.com,Male,62.56.24.215,2015-10-28 16:22:15 -3,Rachel,Moreno,rmoreno2@msu.edu,Female,31.222.249.23,2016-04-05 02:05:30 -4,Ralph,Turner,rturner3@hp.com,Male,157.83.76.114,2016-08-08 00:06:51 -5,Laura,Gonzales,lgonzales4@howstuffworks.com,Female,30.54.105.168,2016-09-01 08:25:38 -6,Katherine,Lopez,null,Female,169.138.46.89,2016-08-30 18:52:11 -7,Jeremy,Hamilton,jhamilton6@mozilla.org,Male,231.189.13.133,2016-07-17 02:09:46 -8,Heather,Rose,hrose7@goodreads.com,Female,87.165.201.65,2015-12-29 22:03:56 -9,Gregory,Kelly,gkelly8@trellian.com,Male,154.209.99.7,2016-03-24 21:18:16 -10,Rachel,Lopez,rlopez9@themeforest.net,Female,237.165.82.71,2016-08-20 15:44:49 -11,Donna,Welch,dwelcha@shutterfly.com,Female,103.33.110.138,2016-02-27 01:41:48 -12,Russell,Lawrence,rlawrenceb@qq.com,Male,189.115.73.4,2016-06-11 03:07:09 -13,Michelle,Montgomery,mmontgomeryc@scientificamerican.com,Female,243.220.95.82,2016-06-18 16:27:19 -14,Walter,Castillo,null,Male,71.159.238.196,2016-10-06 01:55:44 -15,Robin,Mills,rmillse@vkontakte.ru,Female,172.190.5.50,2016-10-31 11:41:21 -16,Raymond,Holmes,rholmesf@usgs.gov,Male,148.153.166.95,2016-10-03 08:16:38 -17,Gary,Bishop,gbishopg@plala.or.jp,Male,161.108.182.13,2016-08-29 19:35:20 -18,Anna,Riley,arileyh@nasa.gov,Female,253.31.108.22,2015-12-11 04:34:27 -19,Sarah,Knight,sknighti@foxnews.com,Female,222.220.3.177,2016-09-26 00:49:06 -20,Phyllis,Fox,pfoxj@creativecommons.org,Female,163.191.232.95,2016-08-21 10:35:19 diff --git a/test/integration/045_test_severity_tests/test_severity.py b/test/integration/045_test_severity_tests/test_severity.py deleted file mode 100644 index 965862a2e7a..00000000000 --- a/test/integration/045_test_severity_tests/test_severity.py +++ /dev/null @@ -1,93 +0,0 @@ -from test.integration.base import DBTIntegrationTest, use_profile - - -class TestSeverity(DBTIntegrationTest): - @property - def schema(self): - return "severity_045" - - @property - def models(self): - return "models" - - @property - def project_config(self): - return { - 'config-version': 2, - 'seed-paths': ['seeds'], - 'test-paths': ['tests'], - 'seeds': { - 'quote_columns': False, - }, - } - - def run_dbt_with_vars(self, cmd, strict_var, *args, **kwargs): - cmd.extend(['--vars', - '{{test_run_schema: {}, strict: {}}}'.format(self.unique_schema(), strict_var)]) - return self.run_dbt(cmd, *args, **kwargs) - - @use_profile('postgres') - def test_postgres_severity_warnings(self): - self.run_dbt_with_vars(['seed'], 'false') - self.run_dbt_with_vars(['run'], 'false') - results = self.run_dbt_with_vars( - ['test', '--select', 'test_type:generic'], 'false') - self.assertEqual(len(results), 2) - self.assertEqual(results[0].status, 'warn') - self.assertEqual(results[0].failures, 2) - self.assertEqual(results[1].status, 'warn') - self.assertEqual(results[1].failures, 2) - - @use_profile('postgres') - def test_postgres_severity_rendered_errors(self): - self.run_dbt_with_vars(['seed'], 'false') - self.run_dbt_with_vars(['run'], 'false') - results = self.run_dbt_with_vars( - ['test', '--select', 'test_type:generic'], 'true', expect_pass=False) - self.assertEqual(len(results), 2) - self.assertEqual(results[0].status, 'fail') - self.assertEqual(results[0].failures, 2) - self.assertEqual(results[1].status, 'fail') - self.assertEqual(results[1].failures, 2) - - @use_profile('postgres') - def test_postgres_severity_warnings_strict(self): - self.run_dbt_with_vars(['seed'], 'false') - self.run_dbt_with_vars(['run'], 'false') - results = self.run_dbt_with_vars( - ['test', '--select', 'test_type:generic'], 'false', expect_pass=True) - self.assertEqual(len(results), 2) - self.assertEqual(results[0].status, 'warn') - self.assertEqual(results[0].failures, 2) - self.assertEqual(results[1].status, 'warn') - self.assertEqual(results[1].failures, 2) - - @use_profile('postgres') - def test_postgres_data_severity_warnings(self): - self.run_dbt_with_vars(['seed'], 'false') - self.run_dbt_with_vars(['run'], 'false') - results = self.run_dbt_with_vars( - ['test', '--select', 'test_type:singular'], 'false') - self.assertEqual(len(results), 1) - self.assertEqual(results[0].status, 'warn') - self.assertEqual(results[0].failures, 2) - - @use_profile('postgres') - def test_postgres_data_severity_rendered_errors(self): - self.run_dbt_with_vars(['seed'], 'false') - self.run_dbt_with_vars(['run'], 'false') - results = self.run_dbt_with_vars( - ['test', '--select', 'test_type:singular'], 'true', expect_pass=False) - self.assertEqual(len(results), 1) - self.assertEqual(results[0].status, 'fail') - self.assertEqual(results[0].failures, 2) - - @use_profile('postgres') - def test_postgres_data_severity_warnings_strict(self): - self.run_dbt_with_vars(['seed'], 'false') - self.run_dbt_with_vars(['run'], 'false') - results = self.run_dbt_with_vars( - ['test', '--select', 'test_type:singular'], 'false', expect_pass=True) - self.assertEqual(len(results), 1) - self.assertTrue(results[0].status, 'fail') - self.assertEqual(results[0].failures, 2) diff --git a/test/integration/045_test_severity_tests/tests/data.sql b/test/integration/045_test_severity_tests/tests/data.sql deleted file mode 100644 index 65c5863ff03..00000000000 --- a/test/integration/045_test_severity_tests/tests/data.sql +++ /dev/null @@ -1,2 +0,0 @@ -{{ config(severity='error' if var('strict', false) else 'warn') }} -select * from {{ ref('model') }} where email is null diff --git a/tests/functional/severity/test_severity.py b/tests/functional/severity/test_severity.py new file mode 100644 index 00000000000..050ccd22325 --- /dev/null +++ b/tests/functional/severity/test_severity.py @@ -0,0 +1,119 @@ +import pytest + +from dbt.tests.util import run_dbt + +models__sample_model_sql = """ +select * from {{ source("raw", "sample_seed") }} +""" + +models__schema_yml = """ +version: 2 +sources: + - name: raw + database: "{{ target.database }}" + schema: "{{ target.schema }}" + tables: + - name: sample_seed + columns: + - name: email + tests: + - not_null: + severity: "{{ 'error' if var('strict', false) else 'warn' }}" +models: + - name: sample_model + columns: + - name: email + tests: + - not_null: + severity: "{{ 'error' if var('strict', false) else 'warn' }}" +""" + +seeds__sample_seed_csv = """id,first_name,last_name,email,gender,ip_address,updated_at +1,Judith,Kennedy,jkennedy0@phpbb.com,Female,54.60.24.128,2015-12-24 12:19:28 +2,Arthur,Kelly,akelly1@eepurl.com,Male,62.56.24.215,2015-10-28 16:22:15 +3,Rachel,Moreno,rmoreno2@msu.edu,Female,31.222.249.23,2016-04-05 02:05:30 +4,Ralph,Turner,rturner3@hp.com,Male,157.83.76.114,2016-08-08 00:06:51 +5,Laura,Gonzales,lgonzales4@howstuffworks.com,Female,30.54.105.168,2016-09-01 08:25:38 +6,Katherine,Lopez,null,Female,169.138.46.89,2016-08-30 18:52:11 +7,Jeremy,Hamilton,jhamilton6@mozilla.org,Male,231.189.13.133,2016-07-17 02:09:46 +8,Heather,Rose,hrose7@goodreads.com,Female,87.165.201.65,2015-12-29 22:03:56 +9,Gregory,Kelly,gkelly8@trellian.com,Male,154.209.99.7,2016-03-24 21:18:16 +10,Rachel,Lopez,rlopez9@themeforest.net,Female,237.165.82.71,2016-08-20 15:44:49 +11,Donna,Welch,dwelcha@shutterfly.com,Female,103.33.110.138,2016-02-27 01:41:48 +12,Russell,Lawrence,rlawrenceb@qq.com,Male,189.115.73.4,2016-06-11 03:07:09 +13,Michelle,Montgomery,mmontgomeryc@scientificamerican.com,Female,243.220.95.82,2016-06-18 16:27:19 +14,Walter,Castillo,null,Male,71.159.238.196,2016-10-06 01:55:44 +15,Robin,Mills,rmillse@vkontakte.ru,Female,172.190.5.50,2016-10-31 11:41:21 +16,Raymond,Holmes,rholmesf@usgs.gov,Male,148.153.166.95,2016-10-03 08:16:38 +17,Gary,Bishop,gbishopg@plala.or.jp,Male,161.108.182.13,2016-08-29 19:35:20 +18,Anna,Riley,arileyh@nasa.gov,Female,253.31.108.22,2015-12-11 04:34:27 +19,Sarah,Knight,sknighti@foxnews.com,Female,222.220.3.177,2016-09-26 00:49:06 +20,Phyllis,Fox,pfoxj@creativecommons.org,Female,163.191.232.95,2016-08-21 10:35:19 +""" + + +tests__sample_test_sql = """ +{{ config(severity='error' if var('strict', false) else 'warn') }} +select * from {{ ref("sample_model") }} where email is null +""" + + +@pytest.fixture(scope="class") +def models(): + return { + "sample_model.sql": models__sample_model_sql, + "schema.yml": models__schema_yml + } + + +@pytest.fixture(scope="class") +def seeds(): + return {"sample_seed.csv": seeds__sample_seed_csv} + + +@pytest.fixture(scope="class") +def tests(): + return {"null_email.sql": tests__sample_test_sql} + + +@pytest.fixture(scope="class") +def project_config_update(): + return { + 'config-version': 2, + 'seed-paths': ['seeds'], + 'test-paths': ['tests'], + "seeds": { + "quote_columns": False, + }, + } + + +class TestSeverity: + @pytest.fixture(scope="class", autouse=True) + def seed_and_run(self, project): + run_dbt(["seed"]) + run_dbt(["run"]) + + def test_generic_default(self, project): + results = run_dbt(['test', '--select', 'test_type:generic']) + assert len(results) == 2 + assert all([r.status == 'warn' for r in results]) + assert all([r.failures == 2 for r in results]) + + def test_generic_strict(self, project): + results = run_dbt(['test', '--select', 'test_type:generic', "--vars", '{"strict": True}'], expect_pass=False) + assert len(results) == 2 + assert all([r.status == 'fail' for r in results]) + assert all([r.failures == 2 for r in results]) + + def test_singular_default(self, project): + results = run_dbt(['test', '--select', 'test_type:singular']) + assert len(results) == 1 + assert all([r.status == 'warn' for r in results]) + assert all([r.failures == 2 for r in results]) + + def test_singular_strict(self, project): + results = run_dbt(['test', '--select', 'test_type:singular', "--vars", '{"strict": True}'], expect_pass=False) + assert len(results) == 1 + assert all([r.status == 'fail' for r in results]) + assert all([r.failures == 2 for r in results])