From a42748433d7ad963296bb1ce0251b0b1d0875e08 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 1 Nov 2022 16:26:12 -0400 Subject: [PATCH] converting 023_exit_codes_tests (#6105) * converting 023_exit_codes_tests * use packages fixture, clean up test names --- .../023_exit_codes_tests/models/bad.sql | 2 - .../023_exit_codes_tests/models/dupe.sql | 8 - .../023_exit_codes_tests/models/good.sql | 8 - .../023_exit_codes_tests/models/schema.yml | 17 -- .../023_exit_codes_tests/seeds-bad/data.csv | 2 - .../023_exit_codes_tests/seeds-good/data.csv | 2 - .../023_exit_codes_tests/snapshots-bad/b.sql | 4 - .../023_exit_codes_tests/snapshots-good/g.sql | 4 - .../023_exit_codes_tests/test_exit_codes.py | 200 ------------------ tests/CONVERTING.md | 2 +- tests/functional/exit_codes/fixtures.py | 78 +++++++ .../functional/exit_codes/test_exit_codes.py | 123 +++++++++++ 12 files changed, 202 insertions(+), 248 deletions(-) delete mode 100644 test/integration/023_exit_codes_tests/models/bad.sql delete mode 100644 test/integration/023_exit_codes_tests/models/dupe.sql delete mode 100644 test/integration/023_exit_codes_tests/models/good.sql delete mode 100644 test/integration/023_exit_codes_tests/models/schema.yml delete mode 100644 test/integration/023_exit_codes_tests/seeds-bad/data.csv delete mode 100644 test/integration/023_exit_codes_tests/seeds-good/data.csv delete mode 100644 test/integration/023_exit_codes_tests/snapshots-bad/b.sql delete mode 100644 test/integration/023_exit_codes_tests/snapshots-good/g.sql delete mode 100644 test/integration/023_exit_codes_tests/test_exit_codes.py create mode 100644 tests/functional/exit_codes/fixtures.py create mode 100644 tests/functional/exit_codes/test_exit_codes.py diff --git a/test/integration/023_exit_codes_tests/models/bad.sql b/test/integration/023_exit_codes_tests/models/bad.sql deleted file mode 100644 index dad7fe5fc10..00000000000 --- a/test/integration/023_exit_codes_tests/models/bad.sql +++ /dev/null @@ -1,2 +0,0 @@ - -select bad sql here diff --git a/test/integration/023_exit_codes_tests/models/dupe.sql b/test/integration/023_exit_codes_tests/models/dupe.sql deleted file mode 100644 index f7bb37c8b71..00000000000 --- a/test/integration/023_exit_codes_tests/models/dupe.sql +++ /dev/null @@ -1,8 +0,0 @@ - -select 1 as id, current_date as updated_at -union all -select 2 as id, current_date as updated_at -union all -select 3 as id, current_date as updated_at -union all -select 4 as id, current_date as updated_at diff --git a/test/integration/023_exit_codes_tests/models/good.sql b/test/integration/023_exit_codes_tests/models/good.sql deleted file mode 100644 index f7bb37c8b71..00000000000 --- a/test/integration/023_exit_codes_tests/models/good.sql +++ /dev/null @@ -1,8 +0,0 @@ - -select 1 as id, current_date as updated_at -union all -select 2 as id, current_date as updated_at -union all -select 3 as id, current_date as updated_at -union all -select 4 as id, current_date as updated_at diff --git a/test/integration/023_exit_codes_tests/models/schema.yml b/test/integration/023_exit_codes_tests/models/schema.yml deleted file mode 100644 index f7243286b7b..00000000000 --- a/test/integration/023_exit_codes_tests/models/schema.yml +++ /dev/null @@ -1,17 +0,0 @@ -version: 2 -models: -- name: good - columns: - - name: updated_at - tests: - - not_null -- name: bad - columns: - - name: updated_at - tests: - - not_null -- name: dupe - columns: - - name: updated_at - tests: - - unique diff --git a/test/integration/023_exit_codes_tests/seeds-bad/data.csv b/test/integration/023_exit_codes_tests/seeds-bad/data.csv deleted file mode 100644 index fcc8e001bbd..00000000000 --- a/test/integration/023_exit_codes_tests/seeds-bad/data.csv +++ /dev/null @@ -1,2 +0,0 @@ -a,b,c -1,\2,3,a,a,a diff --git a/test/integration/023_exit_codes_tests/seeds-good/data.csv b/test/integration/023_exit_codes_tests/seeds-good/data.csv deleted file mode 100644 index bfde6bfa0b8..00000000000 --- a/test/integration/023_exit_codes_tests/seeds-good/data.csv +++ /dev/null @@ -1,2 +0,0 @@ -a,b,c -1,2,3 diff --git a/test/integration/023_exit_codes_tests/snapshots-bad/b.sql b/test/integration/023_exit_codes_tests/snapshots-bad/b.sql deleted file mode 100644 index 52425b7c9bc..00000000000 --- a/test/integration/023_exit_codes_tests/snapshots-bad/b.sql +++ /dev/null @@ -1,4 +0,0 @@ -{% snapshot good_snapshot %} - {{ config(target_schema=schema, target_database=database, strategy='timestamp', unique_key='id', updated_at='updated_at_not_real')}} - select * from {{ schema }}.good -{% endsnapshot %} diff --git a/test/integration/023_exit_codes_tests/snapshots-good/g.sql b/test/integration/023_exit_codes_tests/snapshots-good/g.sql deleted file mode 100644 index 0c1205d9441..00000000000 --- a/test/integration/023_exit_codes_tests/snapshots-good/g.sql +++ /dev/null @@ -1,4 +0,0 @@ -{% snapshot good_snapshot %} - {{ config(target_schema=schema, target_database=database, strategy='timestamp', unique_key='id', updated_at='updated_at')}} - select * from {{ schema }}.good -{% endsnapshot %} diff --git a/test/integration/023_exit_codes_tests/test_exit_codes.py b/test/integration/023_exit_codes_tests/test_exit_codes.py deleted file mode 100644 index 7da8d85e321..00000000000 --- a/test/integration/023_exit_codes_tests/test_exit_codes.py +++ /dev/null @@ -1,200 +0,0 @@ -from test.integration.base import DBTIntegrationTest, use_profile - -import dbt.exceptions - - -class TestExitCodes(DBTIntegrationTest): - - @property - def schema(self): - return "exit_codes_test_023" - - @property - def models(self): - return "models" - - @property - def project_config(self): - return { - 'config-version': 2, - "snapshot-paths": ['snapshots-good'], - } - - @use_profile('postgres') - def test_postgres_exit_code_run_succeed(self): - results, success = self.run_dbt_and_check(['run', '--model', 'good']) - self.assertEqual(len(results.results), 1) - self.assertTrue(success) - self.assertTableDoesExist('good') - - @use_profile('postgres') - def test__postgres_exit_code_run_fail(self): - results, success = self.run_dbt_and_check(['run', '--model', 'bad']) - self.assertEqual(len(results.results), 1) - self.assertFalse(success) - self.assertTableDoesNotExist('bad') - - @use_profile('postgres') - def test__postgres_schema_test_pass(self): - results, success = self.run_dbt_and_check(['run', '--model', 'good']) - self.assertEqual(len(results.results), 1) - self.assertTrue(success) - results, success = self.run_dbt_and_check(['test', '--model', 'good']) - self.assertEqual(len(results.results), 1) - self.assertTrue(success) - - @use_profile('postgres') - def test__postgres_schema_test_fail(self): - results, success = self.run_dbt_and_check(['run', '--model', 'dupe']) - self.assertEqual(len(results.results), 1) - self.assertTrue(success) - results, success = self.run_dbt_and_check(['test', '--model', 'dupe']) - self.assertEqual(len(results.results), 1) - self.assertFalse(success) - - @use_profile('postgres') - def test__postgres_compile(self): - results, success = self.run_dbt_and_check(['compile']) - self.assertEqual(len(results.results), 7) - self.assertTrue(success) - - @use_profile('postgres') - def test__postgres_snapshot_pass(self): - self.run_dbt_and_check(['run', '--model', 'good']) - results, success = self.run_dbt_and_check(['snapshot']) - self.assertEqual(len(results.results), 1) - self.assertTableDoesExist('good_snapshot') - self.assertTrue(success) - - -class TestExitCodesSnapshotFail(DBTIntegrationTest): - - @property - def schema(self): - return "exit_codes_test_023" - - @property - def models(self): - return "models" - - @property - def project_config(self): - return { - 'config-version': 2, - "snapshot-paths": ['snapshots-bad'], - } - - @use_profile('postgres') - def test__postgres_snapshot_fail(self): - results, success = self.run_dbt_and_check(['run', '--model', 'good']) - self.assertTrue(success) - self.assertEqual(len(results.results), 1) - - results, success = self.run_dbt_and_check(['snapshot']) - self.assertEqual(len(results.results), 1) - self.assertTableDoesNotExist('good_snapshot') - self.assertFalse(success) - -class TestExitCodesDeps(DBTIntegrationTest): - - @property - def schema(self): - return "exit_codes_test_023" - - @property - def models(self): - return "models" - - @property - def packages_config(self): - return { - "packages": [ - { - 'git': 'https://github.com/dbt-labs/dbt-integration-project', - 'revision': 'dbt/1.0.0', - } - ] - } - - @use_profile('postgres') - def test_postgres_deps(self): - _, success = self.run_dbt_and_check(['deps']) - self.assertTrue(success) - - -class TestExitCodesDepsFail(DBTIntegrationTest): - @property - def schema(self): - return "exit_codes_test_023" - - @property - def models(self): - return "models" - - @property - def packages_config(self): - return { - "packages": [ - { - 'git': 'https://github.com/dbt-labs/dbt-integration-project', - 'revision': 'bad-branch', - }, - ] - } - - @use_profile('postgres') - def test_postgres_deps(self): - with self.assertRaises(dbt.exceptions.InternalException): - # this should fail - self.run_dbt_and_check(['deps']) - - -class TestExitCodesSeed(DBTIntegrationTest): - @property - def schema(self): - return "exit_codes_test_023" - - @property - def models(self): - return "models" - - @property - def project_config(self): - return { - 'config-version': 2, - 'seed-paths': ['seeds-good'], - 'seeds': { - 'quote_columns': False, - }, - } - - @use_profile('postgres') - def test_postgres_seed(self): - results, success = self.run_dbt_and_check(['seed']) - self.assertEqual(len(results.results), 1) - self.assertTrue(success) - - -class TestExitCodesSeedFail(DBTIntegrationTest): - @property - def schema(self): - return "exit_codes_test_023" - - @property - def models(self): - return "models" - - @property - def project_config(self): - return { - 'config-version': 2, - 'seed-paths': ['seeds-bad'], - 'seeds': { - 'quote_columns': False, - }, - } - - @use_profile('postgres') - def test_postgres_seed(self): - _, success = self.run_dbt_and_check(['seed']) - self.assertFalse(success) diff --git a/tests/CONVERTING.md b/tests/CONVERTING.md index 89801fc74b9..44057cad05b 100644 --- a/tests/CONVERTING.md +++ b/tests/CONVERTING.md @@ -30,7 +30,7 @@ * some of the legacy tests used a 'default_project' method to change (for example) the seeds directory to load a different seed. Don't do that. Copying a file is probably a better option. - +* If there are more than 50 lines of fixture strings, they should be defined in a fixtures.py and then imported. We definitely don't do this everywhere right now but should move to this model. # Integration test directories that have been converted * 001\_simple\_copy\_tests => moved to 'basic' diff --git a/tests/functional/exit_codes/fixtures.py b/tests/functional/exit_codes/fixtures.py new file mode 100644 index 00000000000..23a0bef3897 --- /dev/null +++ b/tests/functional/exit_codes/fixtures.py @@ -0,0 +1,78 @@ +import pytest + +bad_sql = """ +select bad sql here +""" + +dupe_sql = """ +select 1 as id, current_date as updated_at +union all +select 2 as id, current_date as updated_at +union all +select 3 as id, current_date as updated_at +union all +select 4 as id, current_date as updated_at +""" + +good_sql = """ +select 1 as id, current_date as updated_at +union all +select 2 as id, current_date as updated_at +union all +select 3 as id, current_date as updated_at +union all +select 4 as id, current_date as updated_at +""" + +snapshots_good_sql = """ +{% snapshot good_snapshot %} + {{ config(target_schema=schema, target_database=database, strategy='timestamp', unique_key='id', updated_at='updated_at')}} + select * from {{ schema }}.good +{% endsnapshot %} +""" + +snapshots_bad_sql = """ +{% snapshot good_snapshot %} + {{ config(target_schema=schema, target_database=database, strategy='timestamp', unique_key='id', updated_at='updated_at_not_real')}} + select * from {{ schema }}.good +{% endsnapshot %} +""" + +schema_yml = """ +version: 2 +models: +- name: good + columns: + - name: updated_at + tests: + - not_null +- name: bad + columns: + - name: updated_at + tests: + - not_null +- name: dupe + columns: + - name: updated_at + tests: + - unique +""" + +data_seed_good_csv = """a,b,c +1,2,3 +""" + +data_seed_bad_csv = """a,b,c +1,\2,3,a,a,a +""" + + +class BaseConfigProject: + @pytest.fixture(scope="class") + def models(self): + return { + "bad.sql": bad_sql, + "dupe.sql": dupe_sql, + "good.sql": good_sql, + "schema.yml": schema_yml + } diff --git a/tests/functional/exit_codes/test_exit_codes.py b/tests/functional/exit_codes/test_exit_codes.py new file mode 100644 index 00000000000..dbef6361713 --- /dev/null +++ b/tests/functional/exit_codes/test_exit_codes.py @@ -0,0 +1,123 @@ +import pytest + +import dbt.exceptions +from dbt.tests.util import ( + check_table_does_exist, + check_table_does_not_exist, + run_dbt +) +from tests.functional.exit_codes.fixtures import ( + BaseConfigProject, + snapshots_bad_sql, + snapshots_good_sql, + data_seed_bad_csv, + data_seed_good_csv +) + + +class TestExitCodes(BaseConfigProject): + @pytest.fixture(scope="class") + def snapshots(self): + return {"g.sql": snapshots_good_sql} + + def test_exit_code_run_succeed(self, project): + results = run_dbt(['run', '--model', 'good']) + assert len(results) == 1 + check_table_does_exist(project.adapter, 'good') + + def test_exit_code_run_fail(self, project): + results = run_dbt(['run', '--model', 'bad'], expect_pass=False) + assert len(results) == 1 + check_table_does_not_exist(project.adapter, 'bad') + + def test_schema_test_pass(self, project): + results = run_dbt(['run', '--model', 'good']) + assert len(results) == 1 + + results = run_dbt(['test', '--model', 'good']) + assert len(results) == 1 + + def test_schema_test_fail(self, project): + results = run_dbt(['run', '--model', 'dupe']) + assert len(results) == 1 + + results = run_dbt(['test', '--model', 'dupe'], expect_pass=False) + assert len(results) == 1 + + def test_compile(self, project): + results = run_dbt(['compile']) + assert len(results) == 7 + + def test_snapshot_pass(self, project): + results = run_dbt(['snapshot']) + assert len(results) == 1 + check_table_does_exist(project.adapter, 'good_snapshot') + + +class TestExitCodesSnapshotFail(BaseConfigProject): + @pytest.fixture(scope="class") + def snapshots(self): + return {"b.sql": snapshots_bad_sql} + + def test_snapshot_fail(self, project): + results = run_dbt(['run', '--model', 'good']) + assert len(results) == 1 + + results = run_dbt(['snapshot'], expect_pass=False) + assert len(results) == 1 + check_table_does_not_exist(project.adapter, 'good_snapshot') + + +class TestExitCodesDeps: + @pytest.fixture(scope="class") + def packages(self): + return { + "packages": [ + { + 'git': 'https://github.com/dbt-labs/dbt-integration-project', + 'revision': 'dbt/1.0.0', + } + ] + } + + def test_deps(self, project): + results = run_dbt(['deps']) + assert results is None + + +class TestExitCodesDepsFail: + @pytest.fixture(scope="class") + def packages(self): + return { + "packages": [ + { + 'git': 'https://github.com/dbt-labs/dbt-integration-project', + 'revision': 'bad-branch', + }, + ] + } + + def test_deps_fail(self, project): + with pytest.raises(dbt.exceptions.InternalException) as exc: + run_dbt(['deps']) + expected_msg = "Error checking out spec='bad-branch'" + assert expected_msg in str(exc.value) + + +class TestExitCodesSeed: + @pytest.fixture(scope="class") + def seeds(self): + return {"good.csv": data_seed_good_csv} + + def test_seed(self, project): + results = run_dbt(['seed']) + assert len(results) == 1 + + +class TestExitCodesSeedFail: + @pytest.fixture(scope="class") + def seeds(self): + return {"bad.csv": data_seed_bad_csv} + + def test_seed(self, project): + run_dbt(['seed'], expect_pass=False)