Skip to content

Commit ff591d3

Browse files
committed
fix: pre-process API response in update_job/create_job return value when use_desc_for_id
When use_desc_for_id=True, the API response has [[id]] in description. Now update_job and create_job call _pre_process_job_data on the response before building the returned JobDefinition, matching get_job's existing behavior.
1 parent 420b7e9 commit ff591d3

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/dbt_jobs_as_code/client/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ def update_job(self, job: JobDefinition) -> JobDefinition:
128128
else:
129129
logger.success("Job updated successfully.")
130130

131-
return JobDefinition(**(response.json()["data"]), identifier=job.identifier)
131+
raw_data = response.json()["data"]
132+
if self._use_desc_for_id:
133+
raw_data = DBTCloud._pre_process_job_data(raw_data)
134+
return JobDefinition(**raw_data)
135+
return JobDefinition(**raw_data, identifier=job.identifier)
132136

133137
def create_job(self, job: JobDefinition) -> Optional[JobDefinition]:
134138
"""Create a dbt Cloud Job using a JobDefinition"""
@@ -149,7 +153,11 @@ def create_job(self, job: JobDefinition) -> Optional[JobDefinition]:
149153
else:
150154
logger.success("Job created successfully.")
151155

152-
return JobDefinition(**(response.json()["data"]), identifier=job.identifier)
156+
raw_data = response.json()["data"]
157+
if self._use_desc_for_id:
158+
raw_data = DBTCloud._pre_process_job_data(raw_data)
159+
return JobDefinition(**raw_data)
160+
return JobDefinition(**raw_data, identifier=job.identifier)
153161

154162
def delete_job(self, job: JobDefinition) -> None:
155163
"""Delete a dbt Cloud job."""

tests/client/test_use_desc_for_id.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ def test_get_jobs_no_preprocessing_when_flag_off(self):
197197
class TestUpdateCreateDescMode:
198198
"""Tests that update_job/create_job pass use_desc_for_id to to_payload."""
199199

200-
def _make_job(self, identifier="daily_job"):
200+
def _make_job(self, identifier="daily_job", description="Runs nightly"):
201201
return JobDefinition(
202202
id=42,
203203
name=f"Daily Job [[{identifier}]]",
204+
description=description,
204205
account_id=1,
205206
project_id=100,
206207
environment_id=200,
@@ -293,3 +294,33 @@ def capture_post(**kwargs):
293294
payload = json.loads(captured["data"])
294295
assert "[[daily_job]]" in payload["name"]
295296
assert "[[daily_job]]" not in payload["description"]
297+
298+
def test_update_job_return_value_has_identifier_in_desc_mode(self):
299+
"""update_job pre-processes the API response so the returned JobDefinition has a clean identifier and description."""
300+
client = DBTCloud(account_id=1, api_key="test-key", use_desc_for_id=True)
301+
job = self._make_job(description="Runs nightly")
302+
303+
def mock_post(**kwargs):
304+
return self._make_mock_response(job, use_desc_for_id=True)
305+
306+
client._session.post = mock_post
307+
result = client.update_job(job)
308+
309+
assert result.identifier == "daily_job"
310+
assert result.name == "Daily Job"
311+
assert result.description == "Runs nightly"
312+
313+
def test_create_job_return_value_has_identifier_in_desc_mode(self):
314+
"""create_job pre-processes the API response so the returned JobDefinition has a clean identifier and description."""
315+
client = DBTCloud(account_id=1, api_key="test-key", use_desc_for_id=True)
316+
job = self._make_job(description="Runs nightly")
317+
318+
def mock_post(**kwargs):
319+
return self._make_mock_response(job, use_desc_for_id=True)
320+
321+
client._session.post = mock_post
322+
result = client.create_job(job)
323+
324+
assert result.identifier == "daily_job"
325+
assert result.name == "Daily Job"
326+
assert result.description == "Runs nightly"

0 commit comments

Comments
 (0)