|
1 | 1 | import json |
2 | 2 |
|
| 3 | +import pytest |
| 4 | +from jsonschema import validate |
| 5 | +from ruamel.yaml import YAML |
| 6 | + |
3 | 7 | from dbt_jobs_as_code.exporter.export import export_jobs_yml |
4 | 8 | from dbt_jobs_as_code.schemas.common_types import ( |
5 | 9 | Date, |
|
10 | 14 | Triggers, |
11 | 15 | ) |
12 | 16 | from dbt_jobs_as_code.schemas.job import JobDefinition |
13 | | -from jsonschema import validate |
14 | | -from ruamel.yaml import YAML |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def base_job_definition(): |
| 21 | + return JobDefinition( |
| 22 | + account_id=1, |
| 23 | + project_id=1, |
| 24 | + environment_id=1, |
| 25 | + name="Test Job", |
| 26 | + settings={}, |
| 27 | + run_generate_sources=False, |
| 28 | + execute_steps=[], |
| 29 | + generate_docs=False, |
| 30 | + schedule={"cron": "0 14 * * 0,1,2,3,4,5,6"}, |
| 31 | + triggers={}, |
| 32 | + ) |
15 | 33 |
|
16 | 34 |
|
17 | 35 | def test_export_jobs_yml(capsys): |
@@ -99,3 +117,57 @@ def test_export_jobs_yml(capsys): |
99 | 117 | yaml = YAML(typ="safe") |
100 | 118 | yaml_data = yaml.load(captured.out.strip()) |
101 | 119 | validate(instance=yaml_data, schema=json.loads(schema)) |
| 120 | + |
| 121 | + |
| 122 | +def test_export_jobs_yml_with_identifier(base_job_definition, capsys): |
| 123 | + # Create a job with identifier |
| 124 | + job_with_identifier = base_job_definition.model_copy() |
| 125 | + job_with_identifier.identifier = "existing_identifier" |
| 126 | + |
| 127 | + # Create a job without identifier |
| 128 | + job_without_identifier = base_job_definition.model_copy() |
| 129 | + |
| 130 | + jobs = [job_with_identifier, job_without_identifier] |
| 131 | + |
| 132 | + # Export jobs and capture output |
| 133 | + export_jobs_yml(jobs) |
| 134 | + captured = capsys.readouterr() |
| 135 | + |
| 136 | + # Parse the YAML output (skipping the first two lines which contain the schema) |
| 137 | + yaml = YAML() |
| 138 | + exported_jobs = yaml.load("\n".join(captured.out.split("\n")[2:])) |
| 139 | + |
| 140 | + # Verify the job keys |
| 141 | + assert "existing_identifier" in exported_jobs["jobs"] |
| 142 | + assert "import_2" in exported_jobs["jobs"] |
| 143 | + |
| 144 | + # Verify the job contents |
| 145 | + assert exported_jobs["jobs"]["existing_identifier"]["name"] == "Test Job" |
| 146 | + assert exported_jobs["jobs"]["import_2"]["name"] == "Test Job" |
| 147 | + |
| 148 | + |
| 149 | +def test_export_jobs_yml_with_linked_id(base_job_definition, capsys): |
| 150 | + # Create a job with both identifier and id |
| 151 | + job = base_job_definition.model_copy() |
| 152 | + job.identifier = "test_identifier" |
| 153 | + job.id = 123 |
| 154 | + |
| 155 | + # Export with include_linked_id=True |
| 156 | + export_jobs_yml([job], include_linked_id=True) |
| 157 | + captured = capsys.readouterr() |
| 158 | + |
| 159 | + yaml = YAML() |
| 160 | + exported_jobs = yaml.load("\n".join(captured.out.split("\n")[2:])) |
| 161 | + |
| 162 | + # Verify linked_id is included and matches the id |
| 163 | + assert exported_jobs["jobs"]["test_identifier"]["linked_id"] == 123 |
| 164 | + |
| 165 | + # Export with include_linked_id=False |
| 166 | + export_jobs_yml([job], include_linked_id=False) |
| 167 | + captured = capsys.readouterr() |
| 168 | + |
| 169 | + yaml = YAML() |
| 170 | + exported_jobs = yaml.load("\n".join(captured.out.split("\n")[2:])) |
| 171 | + |
| 172 | + # Verify linked_id is not included |
| 173 | + assert "linked_id" not in exported_jobs["jobs"]["test_identifier"] |
0 commit comments