From 909d8ebe8a760961d867723f7921604949fa7ac1 Mon Sep 17 00:00:00 2001 From: Chenyu Li Date: Tue, 9 Aug 2022 14:47:17 -0700 Subject: [PATCH 1/2] fix multiple args for ref and source --- .../dbt/include/global_project/macros/python_model/python.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/dbt/include/global_project/macros/python_model/python.sql b/core/dbt/include/global_project/macros/python_model/python.sql index 33b833d5c35..7f5f2d7187c 100644 --- a/core/dbt/include/global_project/macros/python_model/python.sql +++ b/core/dbt/include/global_project/macros/python_model/python.sql @@ -70,8 +70,8 @@ class this: class dbtObj: def __init__(self, load_df_function) -> None: - self.source = lambda x: source(x, dbt_load_df_function=load_df_function) - self.ref = lambda x: ref(x, dbt_load_df_function=load_df_function) + self.source = lambda *args: source(*args, dbt_load_df_function=load_df_function) + self.ref = lambda *args: ref(*args, dbt_load_df_function=load_df_function) self.config = config self.this = this() self.is_incremental = {{ is_incremental() }} From a3bfeecb6fde264b9c26e1082a5dca5fbc554806 Mon Sep 17 00:00:00 2001 From: Chenyu Li Date: Tue, 16 Aug 2022 15:35:34 -0700 Subject: [PATCH 2/2] add test for support multi part ref and source --- .../unreleased/Fixes-20220816-153401.yaml | 7 ++++ .../adapter/python_model/test_python_model.py | 42 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 .changes/unreleased/Fixes-20220816-153401.yaml diff --git a/.changes/unreleased/Fixes-20220816-153401.yaml b/.changes/unreleased/Fixes-20220816-153401.yaml new file mode 100644 index 00000000000..f734145a096 --- /dev/null +++ b/.changes/unreleased/Fixes-20220816-153401.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: multiple args for ref and source +time: 2022-08-16T15:34:01.348339-07:00 +custom: + Author: ChenyuLInx + Issue: "5634" + PR: "5635" diff --git a/tests/adapter/dbt/tests/adapter/python_model/test_python_model.py b/tests/adapter/dbt/tests/adapter/python_model/test_python_model.py index 9d07e1161a4..d7c23730648 100644 --- a/tests/adapter/dbt/tests/adapter/python_model/test_python_model.py +++ b/tests/adapter/dbt/tests/adapter/python_model/test_python_model.py @@ -1,5 +1,6 @@ import pytest - +import os +import yaml from dbt.tests.util import run_dbt basic_sql = """ @@ -16,6 +17,7 @@ def model(dbt, _): materialized='table', ) df = dbt.ref("my_sql_model") + df2 = dbt.source('test_source', 'test_table') df = df.limit(2) return df """ @@ -23,12 +25,43 @@ def model(dbt, _): second_sql = """ select * from {{ref('my_python_model')}} """ +schema_yml = """version: 2 +sources: + - name: test_source + loader: custom + schema: "{{ var(env_var('DBT_TEST_SCHEMA_NAME_VARIABLE')) }}" + quoting: + identifier: True + tags: + - my_test_source_tag + tables: + - name: test_table + identifier: source +""" + +seeds__source_csv = """favorite_color,id,first_name,email,ip_address,updated_at +blue,1,Larry,lking0@miitbeian.gov.cn,'69.135.206.194',2008-09-12 19:08:31 +blue,2,Larry,lperkins1@toplist.cz,'64.210.133.162',1978-05-09 04:15:14 +""" class BasePythonModelTests: + @pytest.fixture(scope="class", autouse=True) + def setEnvVars(self): + os.environ["DBT_TEST_SCHEMA_NAME_VARIABLE"] = "test_run_schema" + + yield + + del os.environ["DBT_TEST_SCHEMA_NAME_VARIABLE"] + + @pytest.fixture(scope="class") + def seeds(self): + return {"source.csv": seeds__source_csv} + @pytest.fixture(scope="class") def models(self): return { + "schema.yml": schema_yml, "my_sql_model.sql": basic_sql, "my_python_model.py": basic_python, "second_sql_model.sql": second_sql, @@ -36,7 +69,12 @@ def models(self): def test_singular_tests(self, project): # test command - results = run_dbt(["run"]) + vars_dict = { + "test_run_schema": project.test_schema, + } + + run_dbt(["seed", "--vars", yaml.safe_dump(vars_dict)]) + results = run_dbt(["run", "--vars", yaml.safe_dump(vars_dict)]) assert len(results) == 3