Skip to content

Commit

Permalink
Segregate dbt plugin tests (#1610)
Browse files Browse the repository at this point in the history
* Segregate dbt plugin tests

* linting

* update contributing.md

Co-authored-by: Barry Pollard <barry@tunetheweb.com>
  • Loading branch information
alanmcruickshank and tunetheweb committed Oct 14, 2021
1 parent 6e4f2fc commit 30a04a0
Show file tree
Hide file tree
Showing 42 changed files with 76 additions and 92 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
strategy:
matrix:
dbt-version: [ 'dbt018', 'dbt019', 'dbt020', 'dbt021' ]
name: DBT ${{ matrix.dbt-version }} tests
name: DBT Plugin ${{ matrix.dbt-version }} tests
steps:
- uses: actions/checkout@v2
- name: Set up Python
Expand All @@ -124,7 +124,7 @@ jobs:
pip install tox
- name: Run the tests
run: |
tox -e ${{ matrix.dbt-version }} -- -m dbt
tox -e ${{ matrix.dbt-version }} -- plugins/sqlfluff-templater-dbt
- name: Upload Coverage Report
uses: codecov/codecov-action@v1
with:
Expand Down Expand Up @@ -164,7 +164,7 @@ jobs:
files: ./coverage.xml
python-windows-dbt-tests:
runs-on: windows-latest
name: Python 3.8 Windows dbt tests
name: DBT Plugin Python 3.8 Windows tests
steps:
- name: Set git to use LF
run: |
Expand All @@ -184,7 +184,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 -- -m dbt
python -m tox -e dbt018-winpy -- plugins/sqlfluff-templater-dbt
- name: Upload Coverage Report
uses: codecov/codecov-action@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pytest -k L012 -v
To run the dbt-related tests you will have to explicitly include these tests:

```shell
tox -e cov-init,dbt018-py38,cov-report-dbt -- -m "dbt"
tox -e cov-init,dbt018-py38,cov-report-dbt -- plugins/sqlfluff-templater-dbt
```

For more information on adding and running test cases see the [Parser Test README](test/fixtures/dialects/README.md) and the [Rules Test README](test/fixtures/rules/std_rule_cases/README.md).
Expand Down
1 change: 1 addition & 0 deletions plugins/sqlfluff-templater-dbt/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init PY for tests."""
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"templater": {
"dbt": {
"profiles_dir": "test/fixtures/dbt",
"project_dir": "test/fixtures/dbt/dbt_project",
"profiles_dir": "plugins/sqlfluff-templater-dbt/test/fixtures/dbt",
"project_dir": "plugins/sqlfluff-templater-dbt/test/fixtures/dbt/dbt_project",
},
},
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions plugins/sqlfluff-templater-dbt/test/linter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""The Test file for the linter class."""

import os
import os.path

from sqlfluff.core import Linter, FluffConfig
from test.fixtures.dbt.templater import DBT_FLUFF_CONFIG, project_dir # noqa: F401


def test__linter__skip_dbt_model_disabled(project_dir): # noqa
"""Test that the linter skips disabled dbt models."""
conf = FluffConfig(configs=DBT_FLUFF_CONFIG)
lntr = Linter(config=conf)
model_file_path = os.path.join(
project_dir, "models/my_new_project/disabled_model.sql"
)
linted_path = lntr.lint_path(path=model_file_path)
# Check that the file is still there
assert len(linted_path.files) == 1
linted_file = linted_path.files[0]
# Normalise paths to control for OS variance
assert os.path.normpath(linted_file.path) == os.path.normpath(model_file_path)
assert not linted_file.templated_file
assert not linted_file.tree
29 changes: 29 additions & 0 deletions plugins/sqlfluff-templater-dbt/test/rules_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Tests for the standard set of rules."""
import pytest
import os

from sqlfluff.core.config import FluffConfig
from sqlfluff.testing.rules import assert_rule_raises_violations_in_file

from test.fixtures.dbt.templater import ( # noqa
DBT_FLUFF_CONFIG,
project_dir,
dbt_templater,
)


@pytest.mark.parametrize(
"rule,path,violations",
[
# Group By
("L021", "models/my_new_project/select_distinct_group_by.sql", [(1, 8)]),
],
)
def test__rules__std_file_dbt(rule, path, violations, project_dir): # noqa
"""Test the linter finds the given errors in (and only in) the right places (DBT)."""
assert_rule_raises_violations_in_file(
rule=rule,
fpath=os.path.join(project_dir, path),
violations=violations,
fluff_config=FluffConfig(configs=DBT_FLUFF_CONFIG, overrides=dict(rules=rule)),
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
)


@pytest.mark.dbt
def test__templater_dbt_missing(dbt_templater, project_dir): # noqa: F811
"""Check that a nice error is returned when dbt module is missing."""
try:
Expand All @@ -32,7 +31,6 @@ def test__templater_dbt_missing(dbt_templater, project_dir): # noqa: F811
)


@pytest.mark.dbt
def test__templater_dbt_profiles_dir_expanded(dbt_templater): # noqa: F811
"""Check that the profiles_dir is expanded."""
dbt_templater.sqlfluff_config = FluffConfig(
Expand All @@ -58,7 +56,6 @@ def test__templater_dbt_profiles_dir_expanded(dbt_templater): # noqa: F811
"use_var.sql",
],
)
@pytest.mark.dbt
def test__templater_dbt_templating_result(
project_dir, dbt_templater, fname # noqa: F811
):
Expand All @@ -68,7 +65,10 @@ def test__templater_dbt_templating_result(
fname=os.path.join(project_dir, "models/my_new_project/", fname),
config=FluffConfig(configs=DBT_FLUFF_CONFIG),
)
assert str(templated_file) == open("test/fixtures/dbt/" + fname).read()
assert (
str(templated_file)
== open("plugins/sqlfluff-templater-dbt/test/fixtures/dbt/" + fname).read()
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -108,7 +108,6 @@ def test__templater_dbt_templating_result(
],
],
)
@pytest.mark.dbt
def test__templater_dbt_sequence_files_ephemeral_dependency(
project_dir, dbt_templater, fnames_input, fnames_expected_sequence # noqa: F811
):
Expand All @@ -122,7 +121,6 @@ def test__templater_dbt_sequence_files_ephemeral_dependency(
assert list(result) == expected


@pytest.mark.dbt
@pytest.mark.parametrize(
"raw_file,templated_file,result",
[
Expand Down Expand Up @@ -160,7 +158,6 @@ def test__templater_dbt_slice_file_wrapped_test(
"models/my_new_project/multiple_trailing_newline.sql",
],
)
@pytest.mark.dbt
def test__templater_dbt_templating_test_lex(
project_dir, dbt_templater, fname # noqa: F811
):
Expand All @@ -186,7 +183,6 @@ def test__templater_dbt_templating_test_lex(
)


@pytest.mark.dbt
def test__templater_dbt_skips_disabled_model(dbt_templater, project_dir): # noqa: F811
"""A disabled dbt model should be skipped."""
with pytest.raises(SQLTemplaterSkipFile, match=r"model was disabled"):
Expand All @@ -207,7 +203,6 @@ def test__templater_dbt_skips_disabled_model(dbt_templater, project_dir): # noq
"L034_test.sql",
],
)
@pytest.mark.dbt
def test__dbt_templated_models_do_not_raise_lint_error(
project_dir, fname # noqa: F811
):
Expand All @@ -220,7 +215,6 @@ def test__dbt_templated_models_do_not_raise_lint_error(
assert len(violations) == 0


@pytest.mark.dbt
def test__templater_dbt_templating_absolute_path(
project_dir, dbt_templater # noqa: F811
):
Expand Down Expand Up @@ -248,14 +242,13 @@ def test__templater_dbt_templating_absolute_path(
("exception_connect_database.sql", "dbt tried to connect to the database"),
],
)
@pytest.mark.dbt
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 = "test/fixtures/dbt/error_models/" + fname
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)
)
Expand All @@ -276,7 +269,6 @@ def test__templater_dbt_handle_exceptions(
assert violations[0].desc().replace("\\", "/").startswith(exception_msg)


@pytest.mark.dbt
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
21 changes: 0 additions & 21 deletions test/core/linter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import pytest
import logging
import os
import os.path
from typing import List
from unittest.mock import patch

Expand All @@ -13,7 +11,6 @@
from sqlfluff.cli.formatters import CallbackFormatter
from sqlfluff.core.linter import LintingResult, NoQaDirective
import sqlfluff.core.linter as linter
from test.fixtures.dbt.templater import DBT_FLUFF_CONFIG, project_dir # noqa: F401
from sqlfluff.core.templaters import TemplatedFile


Expand Down Expand Up @@ -617,24 +614,6 @@ def test_linter_noqa_with_templating():
assert not result.get_violations()


@pytest.mark.dbt
def test__linter__skip_dbt_model_disabled(project_dir): # noqa
"""Test that the linter skips disabled dbt models."""
conf = FluffConfig(configs=DBT_FLUFF_CONFIG)
lntr = Linter(config=conf)
model_file_path = os.path.join(
project_dir, "models/my_new_project/disabled_model.sql"
)
linted_path = lntr.lint_path(path=model_file_path)
# Check that the file is still there
assert len(linted_path.files) == 1
linted_file = linted_path.files[0]
# Normalise paths to control for OS variance
assert os.path.normpath(linted_file.path) == os.path.normpath(model_file_path)
assert not linted_file.templated_file
assert not linted_file.tree


def test_delayed_exception():
"""Test that DelayedException stores and reraises a stored exception."""
ve = ValueError()
Expand Down
5 changes: 0 additions & 5 deletions test/core/rules/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
from sqlfluff.core.parser import NewlineSegment
from sqlfluff.testing.rules import get_rule_from_set

from test.fixtures.dbt.templater import ( # noqa
DBT_FLUFF_CONFIG,
project_dir,
dbt_templater,
)
from test.fixtures.rules.custom.L000 import Rule_L000
from test.fixtures.rules.custom.S000 import Rule_S000
from sqlfluff.core.rules.loader import get_rules_from_path
Expand Down
25 changes: 0 additions & 25 deletions test/rules/std_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
"""Tests for the standard set of rules."""
import pytest
import os

from sqlfluff.core.rules import get_ruleset
from sqlfluff.core.config import FluffConfig
from sqlfluff.testing.rules import assert_rule_raises_violations_in_file

from test.fixtures.dbt.templater import ( # noqa
DBT_FLUFF_CONFIG,
project_dir,
dbt_templater,
)


@pytest.mark.parametrize(
"rule,path,violations",
Expand Down Expand Up @@ -104,24 +97,6 @@ def test__rules__std_file(rule, path, violations):
)


@pytest.mark.dbt
@pytest.mark.parametrize(
"rule,path,violations",
[
# Group By
("L021", "models/my_new_project/select_distinct_group_by.sql", [(1, 8)]),
],
)
def test__rules__std_file_dbt(rule, path, violations, project_dir): # noqa
"""Test the linter finds the given errors in (and only in) the right places (DBT)."""
assert_rule_raises_violations_in_file(
rule=rule,
fpath=os.path.join(project_dir, path),
violations=violations,
fluff_config=FluffConfig(configs=DBT_FLUFF_CONFIG, overrides=dict(rules=rule)),
)


@pytest.mark.parametrize(
"rule_config_dict",
[
Expand Down
31 changes: 10 additions & 21 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
[tox]
envlist = generate-fixture-yml, linting, doclinting, docbuild, cov-init, py{36,37,38,39}, dbt0{17,18,19,20,21}-py{37,38}, cov-report, bench, mypy, winpy, dbt0{17,18,19,20,21}-winpy
envlist = generate-fixture-yml, linting, doclinting, docbuild, cov-init, py{36,37,38,39}, dbt0{17,18,19,20,21}-py{37,38,39}, cov-report, bench, mypy, winpy, dbt0{17,18,19,20,21}-winpy

[testenv]
passenv = CI CIRCLECI CIRCLE_* HOME
usedevelop = true
# Have option to explictly set TMPDIR for python as on GitHub Action Windows
# machines it doesn't read this from env and resets to system default, which
# is often on different drive (C) from working dir (D), which causes problems.
setenv =
COVERAGE_FILE = .coverage.{envname}
winpy: TMPDIR = temp_pytest
allowlist_externals =
make
deps =
Expand All @@ -23,35 +29,18 @@ deps =
dbt020: dbt==0.20.2
dbt021: dbt==0.21.0
# Install the plugins as required
dbt017,dbt018,dbt019,dbt020,dbt021: plugins/sqlfluff-templater-dbt
dbt0{17,18,19,20,21}: plugins/sqlfluff-templater-dbt
# Include any other steps necessary for testing below.
# {posargs} is there to allow us to specify specific tests, which
# can then be invoked from tox by calling e.g.
# tox -e py35 -- project/tests/test_file.py::TestClassName::test_method
commands =
# For the dbt test cases install dependencies.
dbt017,dbt018,dbt019,dbt020,dbt021: dbt deps --project-dir test/fixtures/dbt/dbt_project
# Clean up from previous tests
python util.py clean-tests
# Run tests
pytest -vv -rs --cov=sqlfluff --cov-report=xml {posargs:-m "not dbt"}
setenv =
COVERAGE_FILE = .coverage.{envname}

# Have option to explictly set TMPDIR for python as on GitHub Action Windows
# machines it doesn't read this from env and resets to system default, which
# is often on different drive (C) from working dir (D), which causes problems.

[testenv:winpy]
setenv =
TMPDIR = temp_pytest
commands =
# For the dbt test cases install dependencies.
dbt017,dbt018,dbt019,dbt020,dbt021: dbt deps --project-dir test/fixtures/dbt/dbt_project
dbt0{17,18,19,20,21}: dbt deps --project-dir plugins/sqlfluff-templater-dbt/test/fixtures/dbt/dbt_project
# Clean up from previous tests
python util.py clean-tests
# Run tests
pytest -vv -rs --cov=sqlfluff --cov-report=xml {posargs:-m "not dbt"}
pytest -vv -rs --cov=sqlfluff --cov-report=xml {posargs: test}

[testenv:cov-init]
setenv =
Expand Down

0 comments on commit 30a04a0

Please sign in to comment.