From cf3714d2c15c64a6e51eac1d5f6a26a647aefc34 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 29 Nov 2023 14:45:40 +0000 Subject: [PATCH] [Backport 1.7.latest] Fix #9119: Get sources working again in `dbt docs generate` (#9163) Co-authored-by: Kshitij Aranke --- .../unreleased/Fixes-20231128-155225.yaml | 6 ++ core/dbt/task/generate.py | 6 ++ tests/functional/docs/test_generate.py | 69 ++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Fixes-20231128-155225.yaml diff --git a/.changes/unreleased/Fixes-20231128-155225.yaml b/.changes/unreleased/Fixes-20231128-155225.yaml new file mode 100644 index 00000000000..4b24cc37c25 --- /dev/null +++ b/.changes/unreleased/Fixes-20231128-155225.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: 'Get sources working again in dbt docs generate' +time: 2023-11-28T15:52:25.738256Z +custom: + Author: aranke + Issue: "9119" diff --git a/core/dbt/task/generate.py b/core/dbt/task/generate.py index d623fddd9af..c14eb9dc27f 100644 --- a/core/dbt/task/generate.py +++ b/core/dbt/task/generate.py @@ -247,6 +247,12 @@ def run(self) -> CatalogArtifact: if self.job_queue is not None: selected_node_ids = self.job_queue.get_selected_nodes() selected_nodes = self._get_nodes_from_ids(self.manifest, selected_node_ids) + + source_ids = self._get_nodes_from_ids( + self.manifest, self.manifest.sources.keys() + ) + selected_nodes.extend(source_ids) + relations = { adapter.Relation.create_from(adapter.config, node_id) for node_id in selected_nodes diff --git a/tests/functional/docs/test_generate.py b/tests/functional/docs/test_generate.py index ed833077120..1047600a820 100644 --- a/tests/functional/docs/test_generate.py +++ b/tests/functional/docs/test_generate.py @@ -2,12 +2,47 @@ from dbt.tests.util import run_dbt, get_manifest +sample_seed = """sample_num,sample_bool +1,true +2,false +3,true +""" -class TestGenerate: +second_seed = """sample_num,sample_bool +4,true +5,false +6,true +""" + +sample_config = """ +sources: + - name: my_seed + schema: "{{ target.schema }}" + tables: + - name: sample_seed + - name: second_seed + - name: fake_seed +""" + + +class TestBaseGenerate: @pytest.fixture(scope="class") def models(self): - return {"my_model.sql": "select 1 as fun", "alt_model.sql": "select 1 as notfun"} + return { + "my_model.sql": "select 1 as fun", + "alt_model.sql": "select 1 as notfun", + "sample_config.yml": sample_config, + } + + @pytest.fixture(scope="class") + def seeds(self): + return { + "sample_seed.csv": sample_seed, + "second_seed.csv": sample_seed, + } + +class TestGenerateManifestNotCompiled(TestBaseGenerate): def test_manifest_not_compiled(self, project): run_dbt(["docs", "generate", "--no-compile"]) # manifest.json is written out in parsing now, but it @@ -17,19 +52,49 @@ def test_manifest_not_compiled(self, project): assert model_id in manifest.nodes assert manifest.nodes[model_id].compiled is False + +class TestGenerateEmptyCatalog(TestBaseGenerate): def test_generate_empty_catalog(self, project): catalog = run_dbt(["docs", "generate", "--empty-catalog"]) assert catalog.nodes == {}, "nodes should be empty" assert catalog.sources == {}, "sources should be empty" assert catalog.errors is None, "errors should be null" + +class TestGenerateSelectLimitsCatalog(TestBaseGenerate): def test_select_limits_catalog(self, project): run_dbt(["run"]) catalog = run_dbt(["docs", "generate", "--select", "my_model"]) assert len(catalog.nodes) == 1 assert "model.test.my_model" in catalog.nodes + +class TestGenerateSelectLimitsNoMatch(TestBaseGenerate): def test_select_limits_no_match(self, project): run_dbt(["run"]) catalog = run_dbt(["docs", "generate", "--select", "my_missing_model"]) assert len(catalog.nodes) == 0 + + +class TestGenerateCatalogWithSources(TestBaseGenerate): + def test_catalog_with_sources(self, project): + run_dbt(["build"]) + catalog = run_dbt(["docs", "generate"]) + + # 2 seeds + 2 models + assert len(catalog.nodes) == 4 + # 2 sources (only ones that exist) + assert len(catalog.sources) == 2 + + +class TestGenerateSelectSource(TestBaseGenerate): + def test_select_source(self, project): + run_dbt(["build"]) + catalog = run_dbt(["docs", "generate", "--select", "source:test.my_seed.sample_seed"]) + + # 2 seeds + # TODO: Filtering doesn't work for seeds + assert len(catalog.nodes) == 2 + # 2 sources + # TODO: Filtering doesn't work for sources + assert len(catalog.sources) == 2