Skip to content

Commit

Permalink
Parameterized testing examples utilizing happy path fixture (#10480)
Browse files Browse the repository at this point in the history
* sketch

* Bring back the happy path fixture snapshot file

The commit c783a86 removed the snapshot file from the happy path fixture.
This was done because the snapshot was breaking the tests we were adding,
`test_run_commands`. However this broke `test_ls` in `test_list.py`. In order
to move forward, we need everything to be working. Maybe the idea was to delete
the `test_list.py` file, however that is not noted anywhere, and was not done.
Thus this commit ensures that test is not broken nor or new tests.

* Create conftest for `functional` tests so that happy path fixtures are accessible

* Format `test_commands.py` and update imports to appease pre-commit hooks

* Parametrize `test_run_command` to make it easier to see which command is failing (if any)

* Update the setup for `TestRunCommands.test_run_command` to be more formulaic

* Add test to ensure resource types are selectable

* Fix docstring formatting in TestRunCommands

* Fixup documentation for test_commands.py

---------

Co-authored-by: Chenyu Li <[email protected]>
  • Loading branch information
QMalcolm and ChenyuLInx authored Aug 7, 2024
1 parent 21a4633 commit 8622360
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from tests.functional.fixtures.happy_path_fixture import ( # noqa:D
happy_path_project,
happy_path_project_files,
)
105 changes: 105 additions & 0 deletions tests/functional/list/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import shutil

import pytest

from dbt.artifacts.resources.types import NodeType
from dbt.cli.main import dbtRunner
from dbt.cli.types import Command
from dbt.events.types import NoNodesSelected
from dbt.tests.util import run_dbt
from tests.utils import EventCatcher

"""
Testing different commands against the happy path fixture
The general flow
1. Declare the commands to be tested
2. Write a paramaterized test ensure a given command reaches causes and associated desired state.
"""

# These are commands we're skipping as they don't make sense or don't work with the
# happy path fixture currently
commands_to_skip = {
"clone",
"generate",
"server",
"init",
"list",
"run-operation",
"show",
"snapshot",
"freshness",
}

# Commands to happy path test
commands = [command.value for command in Command if command.value not in commands_to_skip]


class TestRunCommands:
@pytest.fixture(scope="class", autouse=True)
def drop_snapshots(self, happy_path_project, project_root: str) -> None:
"""The snapshots are erroring out, so lets drop them.
Seems to be database related. Ideally snapshots should work in these tests. It's a bad sign that they don't. That
may have more to do with our fixture setup than the source code though.
Note: that the `happy_path_fixture_files` are a _class_ based fixture. Thus although this fixture _modifies_ the
files available to the happy path project, it doesn't affect that fixture for tests in other test classes.
"""

shutil.rmtree(f"{project_root}/snapshots")

@pytest.mark.parametrize("dbt_command", [(command,) for command in commands])
def test_run_commmand(
self,
happy_path_project,
dbt_command,
):
run_dbt([dbt_command])


"""
Testing command interactions with specific node types
The general flow
1. Declare resource (node) types to be tested
2. Write a parameterized test that ensures commands interact successfully with each resource type
"""

# TODO: Figure out which of these are just missing from the happy path fixture vs which ones aren't selectable
skipped_resource_types = {
"analysis",
"operation",
"rpc",
"sql_operation",
"doc",
"macro",
"exposure",
"group",
"unit_test",
"fixture",
}
resource_types = [
node_type.value for node_type in NodeType if node_type.value not in skipped_resource_types
]


class TestSelectResourceType:
@pytest.fixture(scope="function")
def catcher(self) -> EventCatcher:
return EventCatcher(event_to_catch=NoNodesSelected)

@pytest.fixture(scope="function")
def runner(self, catcher: EventCatcher) -> dbtRunner:
return dbtRunner(callbacks=[catcher.catch])

@pytest.mark.parametrize("resource_type", resource_types)
def test_select_by_resource_type(
self,
resource_type: str,
happy_path_project,
runner: dbtRunner,
catcher: EventCatcher,
) -> None:
runner.invoke(["list", "--select", f"resource_type:{resource_type}"])
assert len(catcher.caught_events) == 0

0 comments on commit 8622360

Please sign in to comment.