Skip to content

Commit

Permalink
fix: Resolve error when cloning Python models (dbt-labs#645) (dbt-lab…
Browse files Browse the repository at this point in the history
…s#651)

Co-authored-by: nicor88 <[email protected]>
  • Loading branch information
jeancochrane and nicor88 authored May 23, 2024
1 parent 74e0c5b commit 20f039d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

-- build model
{% call statement('main') -%}
{{ create_view_as(target_relation, sql) }}
{{ create_view_as(target_relation, compiled_code) }}
{%- endcall %}

{% if lf_tags_config is not none %}
Expand Down
88 changes: 88 additions & 0 deletions tests/functional/adapter/test_python_submissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# import os
# import shutil
# from collections import Counter
# from copy import deepcopy

# import pytest
# import yaml

Expand Down Expand Up @@ -87,3 +92,86 @@

# results = run_dbt(["run", "--vars", yaml.safe_dump(vars_dict)])
# assert len(results) == 2


# class TestPythonClonePossible:
# """Test that basic clone operations are possible on Python models. This
# class has been adapted from the BaseClone and BaseClonePossible classes in
# dbt-core and modified to test Python models in addition to SQL models."""

# @pytest.fixture(scope="class")
# def models(self):
# return {
# "schema.yml": schema_yml,
# "model.sql": basic_sql,
# "my_python_model.py": basic_python,
# }

# @pytest.fixture(scope="class")
# def other_schema(self, unique_schema):
# return unique_schema + "_other"

# @pytest.fixture(scope="class")
# def profiles_config_update(self, dbt_profile_target, unique_schema, other_schema):
# """Update the profiles config to duplicate the default schema to a
# separate schema called `otherschema`."""
# outputs = {"default": dbt_profile_target, "otherschema": deepcopy(dbt_profile_target)}
# outputs["default"]["schema"] = unique_schema
# outputs["otherschema"]["schema"] = other_schema
# return {"test": {"outputs": outputs, "target": "default"}}

# def copy_state(self, project_root):
# """Copy the manifest.json project for a run into a separate `state/`
# directory inside the project root, so that we can reference it
# for cloning."""
# state_path = os.path.join(project_root, "state")
# if not os.path.exists(state_path):
# os.makedirs(state_path)
# shutil.copyfile(f"{project_root}/target/manifest.json", f"{state_path}/manifest.json")

# def run_and_save_state(self, project_root):
# """Run models and save the state to a separate directory to prepare
# for testing clone operations."""
# results = run_dbt(["run"])
# assert len(results) == 2
# self.copy_state(project_root)

# def assert_relation_types_match_counter(self, project, schema, counter):
# """Check that relation types in a given database and schema match the
# counts specified by a Counter object."""
# schema_relations = project.adapter.list_relations(database=project.database, schema=schema)
# schema_types = [str(r.type) for r in schema_relations]
# assert Counter(schema_types) == counter

# def test_can_clone_true(self, project, unique_schema, other_schema):
# """Test that Python models can be cloned using `dbt clone`. Adapted from
# the BaseClonePossible.test_can_clone_true test in dbt-core."""
# project.create_test_schema(other_schema)
# self.run_and_save_state(project.project_root)

# # Base models should be materialized as tables
# self.assert_relation_types_match_counter(project, unique_schema, Counter({"table": 2}))

# clone_args = [
# "clone",
# "--state",
# "state",
# "--target",
# "otherschema",
# ]

# results = run_dbt(clone_args)
# assert len(results) == 2

# # Cloned models should be materialized as views
# self.assert_relation_types_match_counter(project, other_schema, Counter({"view": 2}))

# # Objects already exist, so this is a no-op
# results = run_dbt(clone_args)
# assert len(results) == 2
# assert all("no-op" in r.message.lower() for r in results)

# # Recreate all objects
# results = run_dbt([*clone_args, "--full-refresh"])
# assert len(results) == 2
# assert not any("no-op" in r.message.lower() for r in results)

0 comments on commit 20f039d

Please sign in to comment.