Skip to content

Commit

Permalink
[CT-1262] Convert dbt_debug (dbt-labs#6125)
Browse files Browse the repository at this point in the history
* init pr for dbt_debug test conversion

* removal of old test

* minor test format change

* add new Base class and Test classes

* reformatting test, new method for capsys and error messgae to check, todo fix badproject

* refomatting tests, ready for review

* checking yaml file, and small reformat

* modifying since update wasn't working in ci/cd
  • Loading branch information
McKnight-42 authored Nov 14, 2022
1 parent 39c5c42 commit 66ac107
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 159 deletions.
1 change: 0 additions & 1 deletion test/integration/049_dbt_debug_tests/models/model.sql

This file was deleted.

158 changes: 0 additions & 158 deletions test/integration/049_dbt_debug_tests/test_debug.py

This file was deleted.

107 changes: 107 additions & 0 deletions tests/adapter/dbt/tests/adapter/dbt_debug/test_dbt_debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import pytest
import os
import re
import yaml
from dbt.tests.util import run_dbt

MODELS__MODEL_SQL = """
seled 1 as id
"""


class BaseDebug:
@pytest.fixture(scope="class")
def models(self):
return {"model.sql": MODELS__MODEL_SQL}

@pytest.fixture(autouse=True)
def capsys(self, capsys):
self.capsys = capsys

def assertGotValue(self, linepat, result):
found = False
output = self.capsys.readouterr().out
for line in output.split('\n'):
if linepat.match(line):
found = True
assert result in line
if not found:
with pytest.raises(Exception) as exc:
msg = f"linepat {linepat} not found in stdout: {output}"
assert msg in str(exc.value)

def check_project(self, splitout, msg="ERROR invalid"):
for line in splitout:
if line.strip().startswith("dbt_project.yml file"):
assert msg in line
elif line.strip().startswith("profiles.yml file"):
assert "ERROR invalid" not in line


class BaseDebugProfileVariable(BaseDebug):
@pytest.fixture(scope="class")
def project_config_update(self):
return {
"config-version": 2,
"profile": '{{ "te" ~ "st" }}'
}


class TestDebugPostgres(BaseDebug):
def test_ok(self, project):
run_dbt(["debug"])
assert "ERROR" not in self.capsys.readouterr().out

def test_nopass(self, project):
run_dbt(["debug", "--target", "nopass"], expect_pass=False)
self.assertGotValue(re.compile(r"\s+profiles\.yml file"), "ERROR invalid")

def test_wronguser(self, project):
run_dbt(["debug", "--target", "wronguser"], expect_pass=False)
self.assertGotValue(re.compile(r"\s+Connection test"), "ERROR")

def test_empty_target(self, project):
run_dbt(["debug", "--target", "none_target"], expect_pass=False)
self.assertGotValue(re.compile(r"\s+output 'none_target'"), "misconfigured")


class TestDebugProfileVariablePostgres(BaseDebugProfileVariable):
pass


class TestDebugInvalidProjectPostgres(BaseDebug):

def test_empty_project(self, project):
with open("dbt_project.yml", "w") as f: # noqa: F841
pass

run_dbt(["debug", "--profile", "test"], expect_pass=False)
splitout = self.capsys.readouterr().out.split("\n")
self.check_project(splitout)

def test_badproject(self, project):
update_project = {"invalid-key": "not a valid key so this is bad project"}

with open("dbt_project.yml", "w") as f:
yaml.safe_dump(update_project, f)

run_dbt(["debug", "--profile", "test"], expect_pass=False)
splitout = self.capsys.readouterr().out.split("\n")
self.check_project(splitout)

def test_not_found_project(self, project):
run_dbt(["debug", "--project-dir", "nopass"], expect_pass=False)
splitout = self.capsys.readouterr().out.split("\n")
self.check_project(splitout, msg="ERROR not found")

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"
}
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)
run_dbt(["debug", "--project-dir", "custom"], expect_pass=False)
splitout = self.capsys.readouterr().out.split("\n")
self.check_project(splitout)

0 comments on commit 66ac107

Please sign in to comment.