Skip to content

Commit

Permalink
Fix variable introspection for test helpers in sqlfluff.testing (#863)
Browse files Browse the repository at this point in the history
* Test introspection

* Add pytest-sugar

* Register helper test functions

* Rewrite assert testing helper functions only once

* Remove fix_str added to check variable introspection

* Add pytest as a dependency as we provide testing tooling in sqlfluff.testing
  • Loading branch information
Daniel Mateus Pires committed Mar 19, 2021
1 parent 2226549 commit ffb1194
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ coverage
hypothesis
pytest
pytest-cov
pytest-sugar
# MyPy
mypy
typing_extensions
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def read(*names, **kwargs):
"dataclasses",
# better type hints for older python versions
"typing_extensions",
# We provide a testing library for plugins in sqlfluff.testing
"pytest",
],
extras_require={
"dbt": ["dbt>=0.17"],
Expand Down
4 changes: 4 additions & 0 deletions src/sqlfluff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Sqlfluff is a SQL linter for humans."""
import sys
import pytest

# Expose the public API.
from sqlfluff.api import lint, fix, parse, rules, dialects # noqa: F401
Expand All @@ -25,3 +26,6 @@
config.read([pkg_resources.resource_filename("sqlfluff", "config.ini")])

__version__ = config.get("sqlfluff", "version")

# Register helper functions to support variable introspection on failure.
pytest.register_assert_rewrite("sqlfluff.testing")
24 changes: 24 additions & 0 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ def test_rules__test_helper_skipped_when_test_case_skipped():
with pytest.raises(Skipped) as skipped_test:
rules__test_helper(rule_test_case)
skipped_test.match("Skip this one for now")


def test_rules__test_helper_has_variable_introspection():
"""Make sure the helper gives variable introspection information on failure."""
rule_test_case = RuleTestCase(
rule="L003",
fail_str="""
select
a,
b
from table
""",
# extra comma on purpose
fix_str="""
select
a,
b,
from table
""",
)
with pytest.raises(AssertionError) as skipped_test:
rules__test_helper(rule_test_case)
# Enough to check that a query diff is displayed
skipped_test.match("select")

0 comments on commit ffb1194

Please sign in to comment.