Skip to content

Commit

Permalink
Attempt to ignore templator tests for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Jan 7, 2022
1 parent 8c37557 commit ca71069
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
98 changes: 98 additions & 0 deletions plugins/sqlfluff-templater-dbt/test/templater_db_failure_test.py
Original file line number Diff line number Diff line change
@@ -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")
)
79 changes: 0 additions & 79 deletions plugins/sqlfluff-templater-dbt/test/templater_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ca71069

Please sign in to comment.