From ca71069685b69aca6b926590d73bf738814852d3 Mon Sep 17 00:00:00 2001 From: Barry Date: Fri, 7 Jan 2022 20:38:20 +0000 Subject: [PATCH] Attempt to ignore templator tests for Windows --- .github/workflows/ci-tests.yml | 2 +- .../test/templater_db_failure_test.py | 98 +++++++++++++++++++ .../test/templater_test.py | 79 --------------- 3 files changed, 99 insertions(+), 80 deletions(-) create mode 100644 plugins/sqlfluff-templater-dbt/test/templater_db_failure_test.py diff --git a/.github/workflows/ci-tests.yml b/.github/workflows/ci-tests.yml index 19f11aeff7c..29f2b06fdfa 100644 --- a/.github/workflows/ci-tests.yml +++ b/.github/workflows/ci-tests.yml @@ -203,7 +203,7 @@ jobs: # Do not set explicitly temp dir for dbt as causes problems # None of these test need temp dir set run: | - python -m tox -e dbt018-winpy -- plugins/sqlfluff-templater-dbt + python -m tox -e dbt018-winpy -- plugins/sqlfluff-templater-dbt -k "not templater_db_failure_test" - name: Upload Coverage Report uses: codecov/codecov-action@v1 with: diff --git a/plugins/sqlfluff-templater-dbt/test/templater_db_failure_test.py b/plugins/sqlfluff-templater-dbt/test/templater_db_failure_test.py new file mode 100644 index 00000000000..82a9de5e547 --- /dev/null +++ b/plugins/sqlfluff-templater-dbt/test/templater_db_failure_test.py @@ -0,0 +1,98 @@ +"""Tests for the dbt templater.""" + +import glob +import os +import logging +from pathlib import Path +from unittest import mock + +import pytest + +from sqlfluff.core import FluffConfig, Lexer, Linter +from sqlfluff.core.errors import SQLTemplaterSkipFile +from sqlfluff_templater_dbt.templater import DBT_VERSION_TUPLE +from test.fixtures.dbt.templater import ( # noqa: F401 + DBT_FLUFF_CONFIG, + dbt_templater, + project_dir, +) +from sqlfluff_templater_dbt.templater import DbtFailedToConnectException + + +@pytest.mark.parametrize( + "fname,exception_msg", + [ + ( + "compiler_error.sql", + "dbt compilation error on file 'models/my_new_project/compiler_error.sql', " + "Unexpected end of template. Jinja was looking for the following tags: 'endfor'", + ), + ], +) +def test__templater_dbt_handle_exceptions( + project_dir, dbt_templater, fname, exception_msg # noqa: F811 +): + """Test that exceptions during compilation are returned as violation.""" + from dbt.adapters.factory import get_adapter + + src_fpath = "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/error_models/" + fname + target_fpath = os.path.abspath( + os.path.join(project_dir, "models/my_new_project/", fname) + ) + # We move the file that throws an error in and out of the project directory + # as dbt throws an error if a node fails to parse while computing the DAG + os.rename(src_fpath, target_fpath) + try: + _, violations = dbt_templater.process( + in_str="", + fname=target_fpath, + config=FluffConfig(configs=DBT_FLUFF_CONFIG), + ) + finally: + get_adapter(dbt_templater.dbt_config).connections.release() + os.rename(target_fpath, src_fpath) + assert violations + # NB: Replace slashes to deal with different plaform paths being returned. + assert violations[0].desc().replace("\\", "/").startswith(exception_msg) + + +@mock.patch("dbt.adapters.postgres.impl.PostgresAdapter.set_relations_cache") +@pytest.mark.dbt_connection_failure +def test__templater_dbt_handle_database_connection_failure( + set_relations_cache, project_dir, dbt_templater # noqa: F811 +): + """Test the result of a failed database connection.""" + from dbt.adapters.factory import get_adapter + + set_relations_cache.side_effect = DbtFailedToConnectException("dummy error") + + src_fpath = "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/error_models/exception_connect_database.sql" + target_fpath = os.path.abspath( + os.path.join( + project_dir, "models/my_new_project/exception_connect_database.sql" + ) + ) + dbt_fluff_config_fail = DBT_FLUFF_CONFIG.copy() + dbt_fluff_config_fail["templater"]["dbt"][ + "profiles_dir" + ] = "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/profiles_yml_fail" + # We move the file that throws an error in and out of the project directory + # as dbt throws an error if a node fails to parse while computing the DAG + os.rename(src_fpath, target_fpath) + try: + _, violations = dbt_templater.process( + in_str="", + fname=target_fpath, + config=FluffConfig(configs=DBT_FLUFF_CONFIG), + ) + finally: + get_adapter(dbt_templater.dbt_config).connections.release() + os.rename(target_fpath, src_fpath) + assert violations + # NB: Replace slashes to deal with different plaform paths being returned. + assert ( + violations[0] + .desc() + .replace("\\", "/") + .startswith("dbt tried to connect to the database") + ) diff --git a/plugins/sqlfluff-templater-dbt/test/templater_test.py b/plugins/sqlfluff-templater-dbt/test/templater_test.py index d9369001deb..ba1dec25b3f 100644 --- a/plugins/sqlfluff-templater-dbt/test/templater_test.py +++ b/plugins/sqlfluff-templater-dbt/test/templater_test.py @@ -282,85 +282,6 @@ def test__templater_dbt_templating_absolute_path( pytest.fail(f"Unexpected RuntimeError: {e}") -@pytest.mark.parametrize( - "fname,exception_msg", - [ - ( - "compiler_error.sql", - "dbt compilation error on file 'models/my_new_project/compiler_error.sql', " - "Unexpected end of template. Jinja was looking for the following tags: 'endfor'", - ), - ], -) -def test__templater_dbt_handle_exceptions( - project_dir, dbt_templater, fname, exception_msg # noqa: F811 -): - """Test that exceptions during compilation are returned as violation.""" - from dbt.adapters.factory import get_adapter - - src_fpath = "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/error_models/" + fname - target_fpath = os.path.abspath( - os.path.join(project_dir, "models/my_new_project/", fname) - ) - # We move the file that throws an error in and out of the project directory - # as dbt throws an error if a node fails to parse while computing the DAG - os.rename(src_fpath, target_fpath) - try: - _, violations = dbt_templater.process( - in_str="", - fname=target_fpath, - config=FluffConfig(configs=DBT_FLUFF_CONFIG), - ) - finally: - get_adapter(dbt_templater.dbt_config).connections.release() - os.rename(target_fpath, src_fpath) - assert violations - # NB: Replace slashes to deal with different plaform paths being returned. - assert violations[0].desc().replace("\\", "/").startswith(exception_msg) - - -@mock.patch("dbt.adapters.postgres.impl.PostgresAdapter.set_relations_cache") -@pytest.mark.dbt_connection_failure -def test__templater_dbt_handle_database_connection_failure( - set_relations_cache, project_dir, dbt_templater # noqa: F811 -): - """Test the result of a failed database connection.""" - from dbt.adapters.factory import get_adapter - - set_relations_cache.side_effect = DbtFailedToConnectException("dummy error") - - src_fpath = "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/error_models/exception_connect_database.sql" - target_fpath = os.path.abspath( - os.path.join( - project_dir, "models/my_new_project/exception_connect_database.sql" - ) - ) - dbt_fluff_config_fail = DBT_FLUFF_CONFIG.copy() - dbt_fluff_config_fail["templater"]["dbt"][ - "profiles_dir" - ] = "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/profiles_yml_fail" - # We move the file that throws an error in and out of the project directory - # as dbt throws an error if a node fails to parse while computing the DAG - os.rename(src_fpath, target_fpath) - try: - _, violations = dbt_templater.process( - in_str="", - fname=target_fpath, - config=FluffConfig(configs=DBT_FLUFF_CONFIG), - ) - finally: - get_adapter(dbt_templater.dbt_config).connections.release() - os.rename(target_fpath, src_fpath) - assert violations - # NB: Replace slashes to deal with different plaform paths being returned. - assert ( - violations[0] - .desc() - .replace("\\", "/") - .startswith("dbt tried to connect to the database") - ) - - def test__project_dir_does_not_exist_error(dbt_templater, caplog): # noqa: F811 """Test that an error is logged if the specified dbt project directory doesn't exist.""" dbt_templater.sqlfluff_config = FluffConfig(