Skip to content

Commit

Permalink
Run test examples from docs (#17)
Browse files Browse the repository at this point in the history
Run the test examples as docs by creating a dbt_project in the docs to run the test for and directly referencing example macros in the docs.
  • Loading branch information
JCZuurmond authored Jun 17, 2022
1 parent f784c08 commit e82122d
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 26 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
shell: bash
run: pre-commit run --all-file

tox:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -56,11 +56,14 @@ jobs:
- name: Test with tox
run: tox

- name: Test docs with tox
run: tox -e docs

test-release:
if: contains(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
needs:
- tox
- tests
- "pre-commit"
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ dmypy.json

# Version
src/pytest_dbt_core/_version.py

# dbt
.user.yml
dbt_packages/
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ repos:
rev: v1.0.4
hooks:
- id: python-bandit-vulnerability-check
args: [-l, --recursive, -x, tests]
args: [-l, --recursive, -x, "tests,docs/source/_static/dbt_project/tests"]
name: Check for vulnerabilities in code with bandit
files: .py$
- repo: /~https://github.com/Lucas-C/pre-commit-hooks-safety
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Run test examples from docs ([issue](/~https://github.com/godatadriven/pytest-dbt-core/issues/14), [PR](/~https://github.com/godatadriven/pytest-dbt-core/pull/17))
- Add target flag ([issue](/~https://github.com/godatadriven/pytest-dbt-core/issues/11), [PR](/~https://github.com/godatadriven/pytest-dbt-core/pull/13))
- Delete session module [is included in dbt-spark](/~https://github.com/dbt-labs/dbt-spark/issues/272)
- Add Github templates
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<p align="center">
<a href="/~https://github.com/godatadriven/pytest-dbt-core/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-Apache%202-blue.svg" alt="License: Apache 2.0"></a>
<a href="https://pypi.org/project/pytest-dbt-core/"><img alt="Pypi pytest-dbt-core" src="https://img.shields.io/badge/pypi-pytest%20dbt%20core-green.svg"></a>
<a href="/~https://github.com/godatadriven/pytest-dbt-core/actions/workflows/workflow.yml"><img alt="build pytest-dbt-core" src="/~https://github.com/godatadriven/pytest-dbt-core/actions/workflows/workflow.yml/badge.svg"></a>
<a href="/~https://github.com/godatadriven/pytest-dbt-core/blob/main/LICENSE"><img alt="License: Apache 2.0" src="https://img.shields.io/badge/license-Apache%202-blue.svg"></a>
<a href="https://pypi.org/project/pytest-dbt-core/"><img alt="pypi: pytest-dbt-core" src="https://img.shields.io/badge/pypi-pytest%20dbt%20core-green.svg"></a>
<a href="/~https://github.com/godatadriven/pytest-dbt-core/actions/workflows/workflow.yml"><img alt="build: pytest-dbt-core" src="/~https://github.com/godatadriven/pytest-dbt-core/actions/workflows/workflow.yml/badge.svg"></a>
<a href="/~https://github.com/psf/black"><img alt="code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
<a href="https://pepy.tech/badge/pytest-dbt-core/"><img alt="downloads" src="https://pepy.tech/badge/pytest-dbt-core/month"></a>
</p>

Pytest dbt core is a [pytest](https://docs.pytest.org) plugin for testing your
Expand Down
6 changes: 6 additions & 0 deletions docs/source/_static/dbt_project/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: dbt_project
profile: dbt_project
version: '0.3.0'
config-version: 2
require-dbt-version: [">=1.0.0", "<2.0.0"]
macro-paths: ["macros"]
3 changes: 3 additions & 0 deletions docs/source/_static/dbt_project/packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- package: dbt-labs/spark_utils
version: 0.3.0
8 changes: 8 additions & 0 deletions docs/source/_static/dbt_project/profiles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dbt_project:
target: test
outputs:
test:
type: spark
method: session
schema: test
host: NA # not used, but required by `dbt-core`
16 changes: 16 additions & 0 deletions docs/source/_static/dbt_project/tests/test_spark_get_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from dbt.clients.jinja import MacroGenerator
from pyspark.sql import SparkSession


@pytest.mark.parametrize(
"macro_generator", ["macro.spark_utils.get_tables"], indirect=True
)
def test_get_tables(
spark_session: SparkSession, macro_generator: MacroGenerator
) -> None:
"""The get tables macro should return the created table."""
expected_table = "default.example"
spark_session.sql(f"CREATE TABLE {expected_table} (id int) USING parquet")
tables = macro_generator()
assert tables == [expected_table]
7 changes: 7 additions & 0 deletions docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ point the `---dbt-project-dir`
`option <https://docs.pytest.org/en/6.2.x/usage.html#getting-help-on-version-option-names-environment-variables>`_
to the root of your project.

Target
************************
If you want to use another
`target <https://docs.getdbt.com/reference/dbt-jinja-functions/target>`_
than the default, you set the `--dbt-target`
`option <https://docs.pytest.org/en/6.2.x/usage.html#getting-help-on-version-option-names-environment-variables>`_.

Profiles directory
**********************
If you want to change dbt's profiles directory, use the `DBT_PROFILES_DIR` environment `variable <https://docs.getdbt.com/dbt-cli/configure-your-profile/#advanced-customizing-a-profile-directory>`_.
22 changes: 2 additions & 20 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,8 @@ Usage

Unit test a macro:

.. code-block:: python
from __future__ import annotations
import pytest
from dbt.clients.jinja import MacroGenerator
from pyspark.sql import SparkSession
@pytest.mark.parametrize(
"macro_generator", ["macro.spark_utils.get_tables"], indirect=True
)
def test_create_table(
spark_session: SparkSession, macro_generator: MacroGenerator
) -> None:
expected_table = "default.example"
spark_session.sql(f"CREATE TABLE {expected_table} (id int) USING parquet")
tables = macro_generator()
assert tables == [expected_table]
.. literalinclude :: _static/dbt_project/tests/test_spark_get_tables.py
:language: python
.. toctree::
:maxdepth: 2
Expand Down
15 changes: 15 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@ deps =
pip >= 19.3.1
extras = test
commands = pytest {posargs:tests}

[testenv:docs]
description = run the tests in docs
setenv =
PIP_DISABLE_PIP_VERSION_CHECK = 1
{py27,pypy}: PYTHONWARNINGS=ignore:DEPRECATION::pip._internal.cli.base_command
DBT_PROFILES_DIR = {env:DBT_PROFILES_DIR:{toxinidir}/docs/source/_static/dbt_project}
passenv =
PYTEST_*
PIP_CACHE_DIR
deps =
pip >= 19.3.1
extras = test
commands_pre = dbt deps --project-dir {toxinidir}/docs/source/_static/dbt_project
commands = pytest {posargs:docs/source/_static/dbt_project/tests} --dbt-project-dir={toxinidir}/docs/source/_static/dbt_project

0 comments on commit e82122d

Please sign in to comment.