Skip to content

Commit

Permalink
Reformat tests/ (dbt-labs#6622)
Browse files Browse the repository at this point in the history
* Run black + flake8 on tests dir

* Run pre-commit
  • Loading branch information
jtcohen6 authored Jan 16, 2023
1 parent 20c95a4 commit 065ab2e
Show file tree
Hide file tree
Showing 47 changed files with 420 additions and 453 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ ignore =
E203 # makes Flake8 work like black
E741
E501 # long line checking is done in black
exclude = test
exclude = test/
2 changes: 1 addition & 1 deletion core/dbt/parser/generic_test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def __init__(
column_name=column_name,
name=self.name,
key=key,
err_msg=e.msg
err_msg=e.msg,
)

if value is not None:
Expand Down
8 changes: 4 additions & 4 deletions core/dbt/task/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ def print_start_line(self):
def before_execute(self):
self.print_start_line()

def execute_test(
self, test: TestNode, manifest: Manifest
) -> TestResultData:
def execute_test(self, test: TestNode, manifest: Manifest) -> TestResultData:
context = generate_runtime_model_context(test, self.config, manifest)

materialization_macro = manifest.find_materialization_macro_by_name(
self.config.project_name, test.get_materialization(), self.adapter.type()
)

if materialization_macro is None:
raise MissingMaterializationError(materialization=test.get_materialization(), adapter_type=self.adapter.type())
raise MissingMaterializationError(
materialization=test.get_materialization(), adapter_type=self.adapter.type()
)

if "config" not in context:
raise DbtInternalError(
Expand Down
4 changes: 3 additions & 1 deletion core/dbt/tests/fixtures/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ def clean_up_logging():
# otherwise this will fail. So to test errors in those areas, you need to copy the files
# into the project in the tests instead of putting them in the fixtures.
@pytest.fixture(scope="class")
def adapter(unique_schema, project_root, profiles_root, profiles_yml, dbt_project_yml, clean_up_logging):
def adapter(
unique_schema, project_root, profiles_root, profiles_yml, dbt_project_yml, clean_up_logging
):
# The profiles.yml and dbt_project.yml should already be written out
args = Namespace(
profiles_dir=str(profiles_root), project_dir=str(project_root), target=None, profile=None
Expand Down
7 changes: 6 additions & 1 deletion core/dbt/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
from dbt.main import handle_and_check
from dbt.logger import log_manager
from dbt.contracts.graph.manifest import Manifest
from dbt.events.functions import fire_event, capture_stdout_logs, stop_capture_stdout_logs, reset_metadata_vars
from dbt.events.functions import (
fire_event,
capture_stdout_logs,
stop_capture_stdout_logs,
reset_metadata_vars,
)
from dbt.events.test_types import IntegrationTestDebug

# =============================================================================
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace_packages = true

[tool.black]
# TODO: remove global exclusion of tests when testing overhaul is complete
force-exclude = 'test'
force-exclude = 'test/'
line-length = 99
target-version = ['py38']
24 changes: 5 additions & 19 deletions tests/adapter/dbt/tests/adapter/aliases/test_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ def models(self):

@pytest.fixture(scope="class")
def macros(self):
return {
"cast.sql": MACROS__CAST_SQL,
"expect_value.sql": MACROS__EXPECT_VALUE_SQL
}
return {"cast.sql": MACROS__CAST_SQL, "expect_value.sql": MACROS__EXPECT_VALUE_SQL}

def test_alias_model_name(self, project):
results = run_dbt(["run"])
Expand All @@ -71,10 +68,7 @@ def project_config_update(self):

@pytest.fixture(scope="class")
def macros(self):
return {
"cast.sql": MACROS__CAST_SQL,
"expect_value.sql": MACROS__EXPECT_VALUE_SQL
}
return {"cast.sql": MACROS__CAST_SQL, "expect_value.sql": MACROS__EXPECT_VALUE_SQL}

@pytest.fixture(scope="class")
def models(self):
Expand All @@ -100,10 +94,7 @@ def project_config_update(self):

@pytest.fixture(scope="class")
def macros(self):
return {
"cast.sql": MACROS__CAST_SQL,
"expect_value.sql": MACROS__EXPECT_VALUE_SQL
}
return {"cast.sql": MACROS__CAST_SQL, "expect_value.sql": MACROS__EXPECT_VALUE_SQL}

@pytest.fixture(scope="class")
def models(self):
Expand All @@ -130,19 +121,14 @@ def project_config_update(self, unique_schema):
"models": {
"test": {
"alias": "duped_alias",
"model_b": {
"schema": unique_schema + "_alt"
},
"model_b": {"schema": unique_schema + "_alt"},
},
},
}

@pytest.fixture(scope="class")
def macros(self):
return {
"cast.sql": MACROS__CAST_SQL,
"expect_value.sql": MACROS__EXPECT_VALUE_SQL
}
return {"cast.sql": MACROS__CAST_SQL, "expect_value.sql": MACROS__EXPECT_VALUE_SQL}

@pytest.fixture(scope="class")
def models(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,13 @@


class BaseTableMaterialization:

@pytest.fixture(scope="class")
def seeds(self):
return {"seed.csv": seeds__seed_csv}

@pytest.fixture(scope="class")
def models(self):
return {'materialized.sql': model_sql}
return {"materialized.sql": model_sql}

def test_table_materialization_sort_dist_no_op(self, project):
# basic table materialization test, sort and dist is not supported by postgres so the result table would still be same as input
Expand Down
12 changes: 3 additions & 9 deletions tests/adapter/dbt/tests/adapter/dbt_debug/test_dbt_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def capsys(self, capsys):
def assertGotValue(self, linepat, result):
found = False
output = self.capsys.readouterr().out
for line in output.split('\n'):
for line in output.split("\n"):
if linepat.match(line):
found = True
assert result in line
Expand All @@ -41,10 +41,7 @@ def check_project(self, splitout, msg="ERROR invalid"):
class BaseDebugProfileVariable(BaseDebug):
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"config-version": 2,
"profile": '{{ "te" ~ "st" }}'
}
return {"config-version": 2, "profile": '{{ "te" ~ "st" }}'}


class TestDebugPostgres(BaseDebug):
Expand All @@ -70,7 +67,6 @@ class TestDebugProfileVariablePostgres(BaseDebugProfileVariable):


class TestDebugInvalidProjectPostgres(BaseDebug):

def test_empty_project(self, project):
with open("dbt_project.yml", "w") as f: # noqa: F841
pass
Expand All @@ -96,9 +92,7 @@ def test_not_found_project(self, project):

def test_invalid_project_outside_current_dir(self, project):
# create a dbt_project.yml
project_config = {
"invalid-key": "not a valid key in this project"
}
project_config = {"invalid-key": "not a valid key in this project"}
os.makedirs("custom", exist_ok=True)
with open("custom/dbt_project.yml", "w") as f:
yaml.safe_dump(project_config, f, default_flow_style=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ def seeds(self):
def project_config_update(self):
return {
"models": {
"+incremental_predicates": [
"id != 2"
],
"+incremental_strategy": "delete+insert"
"+incremental_predicates": ["id != 2"],
"+incremental_strategy": "delete+insert",
}
}

Expand Down Expand Up @@ -123,16 +121,21 @@ def get_expected_fields(self, relation, seed_rows, opt_model_count=None):
inc_test_model_count=1,
seed_rows=seed_rows,
opt_model_count=opt_model_count,
relation=relation
relation=relation,
)

# no unique_key test
def test__incremental_predicates(self, project):
"""seed should match model after two incremental runs"""

expected_fields = self.get_expected_fields(relation="expected_delete_insert_incremental_predicates", seed_rows=4)
expected_fields = self.get_expected_fields(
relation="expected_delete_insert_incremental_predicates", seed_rows=4
)
test_case_fields = self.get_test_fields(
project, seed="expected_delete_insert_incremental_predicates", incremental_model="delete_insert_incremental_predicates", update_sql_file=None
project,
seed="expected_delete_insert_incremental_predicates",
incremental_model="delete_insert_incremental_predicates",
update_sql_file=None,
)
self.check_scenario_correctness(expected_fields, test_case_fields, project)

Expand All @@ -144,11 +147,4 @@ class TestIncrementalPredicatesDeleteInsert(BaseIncrementalPredicates):
class TestPredicatesDeleteInsert(BaseIncrementalPredicates):
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"models": {
"+predicates": [
"id != 2"
],
"+incremental_strategy": "delete+insert"
}
}
return {"models": {"+predicates": ["id != 2"], "+incremental_strategy": "delete+insert"}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


from typing import List, Optional
import pytest

Expand All @@ -20,24 +18,22 @@
class BaseChangeRelationTypeValidator:
@pytest.fixture(scope="class")
def models(self):
return {
"model_mc_modelface.sql": _DEFAULT_CHANGE_RELATION_TYPE_MODEL
}
return {"model_mc_modelface.sql": _DEFAULT_CHANGE_RELATION_TYPE_MODEL}

def _run_and_check_materialization(self, materialization, extra_args: Optional[List] = None):
run_args = ["run", '--vars', f'materialized: {materialization}']
run_args = ["run", "--vars", f"materialized: {materialization}"]
if extra_args:
run_args.extend(extra_args)
results = run_dbt(run_args)
assert results[0].node.config.materialized == materialization
assert len(results) == 1

def test_changing_materialization_changes_relation_type(self, project):
self._run_and_check_materialization('view')
self._run_and_check_materialization('table')
self._run_and_check_materialization('view')
self._run_and_check_materialization('incremental')
self._run_and_check_materialization('table', extra_args=['--full-refresh'])
self._run_and_check_materialization("view")
self._run_and_check_materialization("table")
self._run_and_check_materialization("view")
self._run_and_check_materialization("incremental")
self._run_and_check_materialization("table", extra_args=["--full-refresh"])


class TestChangeRelationTypes(BaseChangeRelationTypeValidator):
Expand Down
8 changes: 2 additions & 6 deletions tests/functional/artifacts/expected_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,7 @@ def expected_references_manifest(project):
"unique_id": "seed.test.seed",
"checksum": checksum_file(seed_path),
"unrendered_config": get_unrendered_seed_config(),
"relation_name": '"{0}"."{1}".seed'.format(
project.database, my_schema_name
),
"relation_name": '"{0}"."{1}".seed'.format(project.database, my_schema_name),
},
"snapshot.test.snapshot_seed": {
"alias": "snapshot_seed",
Expand Down Expand Up @@ -1244,9 +1242,7 @@ def expected_references_manifest(project):
"unique_id": "doc.test.table_info",
},
"doc.test.view_summary": {
"block_contents": (
"A view of the summary of the ephemeral copy of the seed data"
),
"block_contents": ("A view of the summary of the ephemeral copy of the seed data"),
"resource_type": "doc",
"name": "view_summary",
"original_file_path": docs_path,
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/colors/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def models():

@pytest.fixture(scope="class")
def project_config_update():
return {'config-version': 2}
return {"config-version": 2}


class TestColors:
Expand Down
13 changes: 10 additions & 3 deletions tests/functional/configs/test_configs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from hologram import ValidationError
import pytest
import os
Expand Down Expand Up @@ -94,7 +93,11 @@ def test_seeds_materialization_proj_config(self, project):
class TestInvalidSeedsMaterializationSchema(object):
def test_seeds_materialization_schema_config(self, project):
seeds_dir = os.path.join(project.project_root, "seeds")
write_file("version: 2\nseeds:\n - name: myseed\n config:\n materialized: table", seeds_dir, "schema.yml")
write_file(
"version: 2\nseeds:\n - name: myseed\n config:\n materialized: table",
seeds_dir,
"schema.yml",
)
write_file("id1, id2\n1, 2", seeds_dir, "myseed.csv")

with pytest.raises(ValidationError):
Expand All @@ -116,7 +119,11 @@ def test_snapshots_materialization_proj_config(self, project):
class TestInvalidSnapshotsMaterializationSchema(object):
def test_snapshots_materialization_schema_config(self, project):
snapshots_dir = os.path.join(project.project_root, "snapshots")
write_file("version: 2\nsnapshots:\n - name: mysnapshot\n config:\n materialized: table", snapshots_dir, "schema.yml")
write_file(
"version: 2\nsnapshots:\n - name: mysnapshot\n config:\n materialized: table",
snapshots_dir,
"schema.yml",
)
write_file(simple_snapshot, snapshots_dir, "mysnapshot.sql")

with pytest.raises(ValidationError):
Expand Down
12 changes: 11 additions & 1 deletion tests/functional/context_methods/test_builtin_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,17 @@ def test_builtin_invocation_args_dict_function(self, project):
expected = "invocation_result: {'debug': True, 'log_format': 'json', 'write_json': True, 'use_colors': True, 'printer_width': 80, 'version_check': True, 'partial_parse': True, 'static_parser': True, 'profiles_dir': "
assert expected in str(result)

expected = ("'send_anonymous_usage_stats': False", "'quiet': False", "'no_print': False", "'cache_selected_only': False", "'macro': 'validate_invocation'", "'args': '{my_variable: test_variable}'", "'which': 'run-operation'", "'rpc_method': 'run-operation'", "'indirect_selection': 'eager'")
expected = (
"'send_anonymous_usage_stats': False",
"'quiet': False",
"'no_print': False",
"'cache_selected_only': False",
"'macro': 'validate_invocation'",
"'args': '{my_variable: test_variable}'",
"'which': 'run-operation'",
"'rpc_method': 'run-operation'",
"'indirect_selection': 'eager'",
)
for element in expected:
assert element in str(result)

Expand Down
6 changes: 4 additions & 2 deletions tests/functional/context_methods/test_custom_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def setup(self):
del os.environ["DBT_ENV_CUSTOM_ENV_SOME_VAR"]

def test_extra_filled(self, project):
_, log_output = run_dbt_and_capture(['--log-format=json', 'deps'],)
_, log_output = run_dbt_and_capture(
["--log-format=json", "deps"],
)
logs = parse_json_logs(log_output)
for log in logs:
assert log['info'].get('extra') == {"SOME_VAR": "value"}
assert log["info"].get("extra") == {"SOME_VAR": "value"}
Loading

0 comments on commit 065ab2e

Please sign in to comment.