|
1 | 1 | """Test the CLI.""" |
2 | 2 |
|
| 3 | +import shutil |
| 4 | +from pathlib import Path |
3 | 5 | from unittest.mock import MagicMock, patch |
4 | 6 |
|
5 | 7 | from click.testing import CliRunner |
@@ -63,6 +65,92 @@ def test_lint_unparseable_manifest(tmp_path, caplog): |
63 | 65 | assert "dbt's manifest.json could not be parsed" in caplog.text |
64 | 66 |
|
65 | 67 |
|
| 68 | +def passing_evaluation() -> MagicMock: |
| 69 | + """Return a mock evaluation with passing scores.""" |
| 70 | + mock_eval = MagicMock() |
| 71 | + mock_eval.project_score = Score(10.0, "🥇") |
| 72 | + mock_eval.scores = {} |
| 73 | + return mock_eval |
| 74 | + |
| 75 | + |
| 76 | +def test_lint_project_dir(tmp_path, manifest_path): |
| 77 | + """Test lint with a project directory, which is used to find the manifest.""" |
| 78 | + runner = CliRunner() |
| 79 | + project_dir = tmp_path / "my_project" |
| 80 | + (project_dir / "target").mkdir(parents=True) |
| 81 | + shutil.copy(manifest_path, project_dir / "target" / "manifest.json") |
| 82 | + |
| 83 | + with ( |
| 84 | + patch("dbt_score.cli.Config._load_toml_file"), |
| 85 | + patch("dbt_score.cli.lint_dbt_project") as mock_lint_dbt_project, |
| 86 | + ): |
| 87 | + mock_lint_dbt_project.return_value = passing_evaluation() |
| 88 | + result = runner.invoke( |
| 89 | + lint, ["--project-dir", str(project_dir)], catch_exceptions=False |
| 90 | + ) |
| 91 | + |
| 92 | + assert result.exit_code == 0 |
| 93 | + assert mock_lint_dbt_project.call_args.kwargs["manifest_path"] == ( |
| 94 | + project_dir / "target" / "manifest.json" |
| 95 | + ) |
| 96 | + assert mock_lint_dbt_project.call_args.kwargs["project_dir"] == project_dir |
| 97 | + |
| 98 | + |
| 99 | +def test_lint_project_dir_with_manifest(tmp_path, manifest_path): |
| 100 | + """Test lint with a project directory and an explicit manifest.""" |
| 101 | + runner = CliRunner() |
| 102 | + |
| 103 | + with ( |
| 104 | + patch("dbt_score.cli.Config._load_toml_file"), |
| 105 | + patch("dbt_score.cli.lint_dbt_project") as mock_lint_dbt_project, |
| 106 | + ): |
| 107 | + mock_lint_dbt_project.return_value = passing_evaluation() |
| 108 | + result = runner.invoke( |
| 109 | + lint, |
| 110 | + ["--project-dir", str(tmp_path), "--manifest", str(manifest_path)], |
| 111 | + catch_exceptions=False, |
| 112 | + ) |
| 113 | + |
| 114 | + assert result.exit_code == 0 |
| 115 | + assert mock_lint_dbt_project.call_args.kwargs["manifest_path"] == Path( |
| 116 | + manifest_path |
| 117 | + ) |
| 118 | + assert mock_lint_dbt_project.call_args.kwargs["project_dir"] == tmp_path |
| 119 | + |
| 120 | + |
| 121 | +def test_lint_project_dir_run_dbt_parse(tmp_path): |
| 122 | + """Test lint with a project directory and dbt parse.""" |
| 123 | + runner = CliRunner() |
| 124 | + project_dir = tmp_path / "my_project" |
| 125 | + project_dir.mkdir() |
| 126 | + |
| 127 | + with ( |
| 128 | + patch("dbt_score.cli.Config._load_toml_file"), |
| 129 | + patch("dbt_score.cli.dbt_parse") as mock_dbt_parse, |
| 130 | + patch("dbt_score.cli.lint_dbt_project") as mock_lint_dbt_project, |
| 131 | + ): |
| 132 | + mock_lint_dbt_project.return_value = passing_evaluation() |
| 133 | + result = runner.invoke( |
| 134 | + lint, |
| 135 | + ["--project-dir", str(project_dir), "--run-dbt-parse"], |
| 136 | + catch_exceptions=False, |
| 137 | + ) |
| 138 | + |
| 139 | + assert result.exit_code == 0 |
| 140 | + mock_dbt_parse.assert_called_once_with(project_dir=project_dir) |
| 141 | + |
| 142 | + |
| 143 | +def test_lint_project_dir_non_existing(tmp_path): |
| 144 | + """Test lint with a non-existing project directory.""" |
| 145 | + runner = CliRunner() |
| 146 | + |
| 147 | + with patch("dbt_score.cli.Config._load_toml_file"): |
| 148 | + result = runner.invoke(lint, ["--project-dir", str(tmp_path / "nope")]) |
| 149 | + |
| 150 | + assert result.exit_code == 2 |
| 151 | + assert "does not exist" in result.output |
| 152 | + |
| 153 | + |
66 | 154 | def test_lint_dbt_parse_exception(caplog): |
67 | 155 | """Test lint with a dbt parse error.""" |
68 | 156 | runner = CliRunner() |
|
0 commit comments