|
1 | | -from unittest.mock import patch |
| 1 | +import json |
| 2 | +from unittest.mock import Mock, patch |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 | from click.testing import CliRunner |
5 | 6 |
|
6 | | -from dbt_jobs_as_code.main import import_jobs |
| 7 | +from dbt_jobs_as_code.cloud_yaml_mapping.change_set import Change, ChangeSet |
| 8 | +from dbt_jobs_as_code.main import cli, import_jobs |
7 | 9 | from dbt_jobs_as_code.schemas.common_types import Settings, Triggers |
8 | 10 | from dbt_jobs_as_code.schemas.job import JobDefinition |
9 | 11 |
|
| 12 | +# ============= Fixtures ============= |
| 13 | + |
10 | 14 |
|
11 | 15 | @pytest.fixture |
12 | 16 | def mock_dbt_cloud(): |
@@ -55,6 +59,55 @@ def mock_dbt_cloud(): |
55 | 59 | yield instance |
56 | 60 |
|
57 | 61 |
|
| 62 | +@pytest.fixture |
| 63 | +def mock_change_set(): |
| 64 | + """Create a mock change set with both job and env var changes""" |
| 65 | + change_set = ChangeSet() |
| 66 | + |
| 67 | + # Add a job change |
| 68 | + change_set.append( |
| 69 | + Change( |
| 70 | + identifier="job1", |
| 71 | + type="job", |
| 72 | + action="update", |
| 73 | + proj_id=123, |
| 74 | + env_id=456, |
| 75 | + sync_function=Mock(), |
| 76 | + parameters={}, |
| 77 | + differences={ |
| 78 | + "values_changed": { |
| 79 | + "root['name']": {"new_value": "new_name", "old_value": "old_name"} |
| 80 | + } |
| 81 | + }, |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + # Add an env var change |
| 86 | + change_set.append( |
| 87 | + Change( |
| 88 | + identifier="job1:DBT_VAR1", |
| 89 | + type="env var overwrite", |
| 90 | + action="update", |
| 91 | + proj_id=123, |
| 92 | + env_id=456, |
| 93 | + sync_function=Mock(), |
| 94 | + parameters={}, |
| 95 | + differences={"old_value": "old_val", "new_value": "new_val"}, |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + return change_set |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture |
| 103 | +def mock_empty_change_set(): |
| 104 | + """Create an empty change set""" |
| 105 | + return ChangeSet() |
| 106 | + |
| 107 | + |
| 108 | +# ============= Import Command Tests ============= |
| 109 | + |
| 110 | + |
58 | 111 | def test_import_jobs_managed_only(mock_dbt_cloud): |
59 | 112 | """Test that --managed-only flag only imports jobs with identifiers""" |
60 | 113 | runner = CliRunner() |
@@ -98,3 +151,143 @@ def test_import_jobs_without_managed_only(mock_dbt_cloud): |
98 | 151 | assert "managed-job-1" in result.stdout |
99 | 152 | assert "managed-job-2" in result.stdout |
100 | 153 | assert "Unmanaged Job" in result.stdout |
| 154 | + |
| 155 | + |
| 156 | +# ============= Plan Command Tests ============= |
| 157 | + |
| 158 | + |
| 159 | +@patch("dbt_jobs_as_code.main.build_change_set") |
| 160 | +def test_plan_command_json_output(mock_build_change_set, mock_change_set): |
| 161 | + """Test that plan command produces valid JSON output when --json flag is used""" |
| 162 | + mock_build_change_set.return_value = mock_change_set |
| 163 | + |
| 164 | + runner = CliRunner() |
| 165 | + result = runner.invoke(cli, ["plan", "--json", "config.yml"]) |
| 166 | + |
| 167 | + assert result.exit_code == 0 |
| 168 | + |
| 169 | + # Verify the output is valid JSON |
| 170 | + json_output = json.loads(result.output) |
| 171 | + |
| 172 | + # Verify structure |
| 173 | + assert "job_changes" in json_output |
| 174 | + assert "env_var_overwrite_changes" in json_output |
| 175 | + |
| 176 | + # Verify job changes |
| 177 | + assert len(json_output["job_changes"]) == 1 |
| 178 | + job_change = json_output["job_changes"][0] |
| 179 | + assert job_change["identifier"] == "job1" |
| 180 | + assert job_change["action"] == "UPDATE" |
| 181 | + assert "differences" in job_change |
| 182 | + |
| 183 | + # Verify env var changes |
| 184 | + assert len(json_output["env_var_overwrite_changes"]) == 1 |
| 185 | + env_var_change = json_output["env_var_overwrite_changes"][0] |
| 186 | + assert env_var_change["identifier"] == "job1:DBT_VAR1" |
| 187 | + assert env_var_change["action"] == "UPDATE" |
| 188 | + assert "differences" in env_var_change |
| 189 | + |
| 190 | + |
| 191 | +@patch("dbt_jobs_as_code.main.build_change_set") |
| 192 | +def test_plan_command_json_output_no_changes(mock_build_change_set, mock_empty_change_set): |
| 193 | + """Test that plan command produces valid JSON output with no changes""" |
| 194 | + mock_build_change_set.return_value = mock_empty_change_set |
| 195 | + |
| 196 | + runner = CliRunner() |
| 197 | + result = runner.invoke(cli, ["plan", "--json", "config.yml"]) |
| 198 | + |
| 199 | + assert result.exit_code == 0 |
| 200 | + |
| 201 | + # Verify the output is valid JSON |
| 202 | + json_output = json.loads(result.output) |
| 203 | + |
| 204 | + # Verify structure |
| 205 | + assert json_output == { |
| 206 | + "job_changes": [], |
| 207 | + "env_var_overwrite_changes": [], |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | +@patch("dbt_jobs_as_code.main.build_change_set") |
| 212 | +def test_plan_command_regular_output(mock_build_change_set, mock_change_set): |
| 213 | + """Test that plan command produces regular output when --json flag is not used""" |
| 214 | + mock_build_change_set.return_value = mock_change_set |
| 215 | + |
| 216 | + runner = CliRunner() |
| 217 | + result = runner.invoke(cli, ["plan", "config.yml"]) |
| 218 | + |
| 219 | + assert result.exit_code == 0 |
| 220 | + |
| 221 | + # Verify this is not JSON |
| 222 | + with pytest.raises(json.JSONDecodeError): |
| 223 | + json.loads(result.output) |
| 224 | + |
| 225 | + |
| 226 | +# ============= Sync Command Tests ============= |
| 227 | + |
| 228 | + |
| 229 | +@patch("dbt_jobs_as_code.main.build_change_set") |
| 230 | +def test_sync_command_json_output(mock_build_change_set, mock_change_set): |
| 231 | + """Test that sync command produces valid JSON output when --json flag is used""" |
| 232 | + mock_build_change_set.return_value = mock_change_set |
| 233 | + |
| 234 | + runner = CliRunner() |
| 235 | + result = runner.invoke(cli, ["sync", "--json", "config.yml"]) |
| 236 | + |
| 237 | + assert result.exit_code == 0 |
| 238 | + |
| 239 | + # Verify the output is valid JSON |
| 240 | + json_output = json.loads(result.output) |
| 241 | + |
| 242 | + # Verify structure |
| 243 | + assert "job_changes" in json_output |
| 244 | + assert "env_var_overwrite_changes" in json_output |
| 245 | + |
| 246 | + # Verify job changes |
| 247 | + assert len(json_output["job_changes"]) == 1 |
| 248 | + job_change = json_output["job_changes"][0] |
| 249 | + assert job_change["identifier"] == "job1" |
| 250 | + assert job_change["action"] == "UPDATE" |
| 251 | + assert "differences" in job_change |
| 252 | + |
| 253 | + # Verify env var changes |
| 254 | + assert len(json_output["env_var_overwrite_changes"]) == 1 |
| 255 | + env_var_change = json_output["env_var_overwrite_changes"][0] |
| 256 | + assert env_var_change["identifier"] == "job1:DBT_VAR1" |
| 257 | + assert env_var_change["action"] == "UPDATE" |
| 258 | + assert "differences" in env_var_change |
| 259 | + |
| 260 | + |
| 261 | +@patch("dbt_jobs_as_code.main.build_change_set") |
| 262 | +def test_sync_command_json_output_no_changes(mock_build_change_set, mock_empty_change_set): |
| 263 | + """Test that sync command produces valid JSON output with no changes""" |
| 264 | + mock_build_change_set.return_value = mock_empty_change_set |
| 265 | + |
| 266 | + runner = CliRunner() |
| 267 | + result = runner.invoke(cli, ["sync", "--json", "config.yml"]) |
| 268 | + |
| 269 | + assert result.exit_code == 0 |
| 270 | + |
| 271 | + # Verify the output is valid JSON |
| 272 | + json_output = json.loads(result.output) |
| 273 | + |
| 274 | + # Verify structure |
| 275 | + assert json_output == { |
| 276 | + "job_changes": [], |
| 277 | + "env_var_overwrite_changes": [], |
| 278 | + } |
| 279 | + |
| 280 | + |
| 281 | +@patch("dbt_jobs_as_code.main.build_change_set") |
| 282 | +def test_sync_command_regular_output(mock_build_change_set, mock_change_set): |
| 283 | + """Test that sync command produces regular output when --json flag is not used""" |
| 284 | + mock_build_change_set.return_value = mock_change_set |
| 285 | + |
| 286 | + runner = CliRunner() |
| 287 | + result = runner.invoke(cli, ["sync", "config.yml"]) |
| 288 | + |
| 289 | + assert result.exit_code == 0 |
| 290 | + |
| 291 | + # Verify this is not JSON |
| 292 | + with pytest.raises(json.JSONDecodeError): |
| 293 | + json.loads(result.output) |
0 commit comments