From f8b0760db2159698b2867f262a21632e4fe42785 Mon Sep 17 00:00:00 2001 From: internetcoffeephone Date: Mon, 6 Jan 2025 18:31:18 +0100 Subject: [PATCH] Fix microbatch dbt list --output JSON Currently, running this command on a project containing a microbatch model results in an error, as microbatch models require a datetime value in their config which cannot be serialized by the default JSON serializer. There already exists a custom JSON serializer within the dbt-core project that converts datetime to ISO string format. This change uses the above serializer to resolve the error. --- .changes/unreleased/Fixes-20250109-123309.yaml | 6 ++++++ core/dbt/task/list.py | 4 +++- tests/functional/microbatch/test_microbatch.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Fixes-20250109-123309.yaml diff --git a/.changes/unreleased/Fixes-20250109-123309.yaml b/.changes/unreleased/Fixes-20250109-123309.yaml new file mode 100644 index 00000000000..611a1449dac --- /dev/null +++ b/.changes/unreleased/Fixes-20250109-123309.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix microbatch dbt list --output json +time: 2025-01-09T12:33:09.958795+01:00 +custom: + Author: internetcoffeephone + Issue: 10556 11098 diff --git a/core/dbt/task/list.py b/core/dbt/task/list.py index 2638920976a..13b7388d1a1 100644 --- a/core/dbt/task/list.py +++ b/core/dbt/task/list.py @@ -17,6 +17,7 @@ from dbt.node_types import NodeType from dbt.task.base import resource_types_from_args from dbt.task.runnable import GraphRunnableTask +from dbt.utils import JSONEncoder from dbt_common.events.contextvars import task_contextvars from dbt_common.events.functions import fire_event, warn_or_error from dbt_common.events.types import PrintEvent @@ -142,7 +143,8 @@ def generate_json(self): if self.args.output_keys else k in self.ALLOWED_KEYS ) - } + }, + cls=JSONEncoder, ) def generate_paths(self) -> Iterator[str]: diff --git a/tests/functional/microbatch/test_microbatch.py b/tests/functional/microbatch/test_microbatch.py index ef747138fbd..fdbd0a219a5 100644 --- a/tests/functional/microbatch/test_microbatch.py +++ b/tests/functional/microbatch/test_microbatch.py @@ -1,3 +1,4 @@ +import json from unittest import mock import pytest @@ -15,6 +16,7 @@ MicrobatchMacroOutsideOfBatchesDeprecation, MicrobatchModelNoEventTimeInputs, ) +from dbt.tests.fixtures.project import TestProjInfo from dbt.tests.util import ( get_artifact, patch_microbatch_end_time, @@ -333,6 +335,19 @@ class TestMicrobatchCLIBuild(TestMicrobatchCLI): CLI_COMMAND_NAME = "build" +class TestMicrobatchCLIRunOutputJSON(BaseMicrobatchTest): + def test_list_output_json(self, project: TestProjInfo): + """Test whether the command `dbt list --output json` works""" + model_catcher = EventCatcher(event_to_catch=LogModelResult) + batch_catcher = EventCatcher(event_to_catch=LogBatchResult) + + _, microbatch_json = run_dbt( + ["list", "--output", "json"], callbacks=[model_catcher.catch, batch_catcher.catch] + ) + microbatch_dict = json.loads(microbatch_json) + assert microbatch_dict["config"]["begin"] == "2020-01-01T00:00:00" + + class TestMicroBatchBoundsDefault(BaseMicrobatchTest): def test_run_with_event_time(self, project): # initial run -- backfills all data