@@ -17,6 +17,16 @@ def entry(value):
1717 return value * 2
1818"""
1919
20+ double_it_py_with_jinja = """
21+ def entry(value):
22+ {% if 1 == 1 %}
23+ return value * 2
24+ {% else %}
25+ {# this should never happen #}
26+ return value * 3
27+ {% endif %}
28+ """
29+
2030double_it_deterministic_sql = """
2131{{ config(volatility='deterministic') }}
2232SELECT value * 2
@@ -133,6 +143,13 @@ def functions(self) -> Dict[str, str]:
133143 }
134144
135145
146+ scalar_function_python_macro = """
147+ {% macro postgres__scalar_function_python(target_relation) %}
148+ SELECT 1;
149+ {% endmacro %}
150+ """
151+
152+
136153class TestBasicSQLUDF (BasicUDFSetup ):
137154 def test_basic_parsing (self , project ):
138155 # Simple parsing
@@ -387,3 +404,48 @@ def test_can_config_functions_from_project_config(self, project):
387404 function_node = manifest .functions ["function.test.double_it" ]
388405 # Volatility from sql should take precedence over the project config
389406 assert function_node .config .volatility == FunctionVolatility .Deterministic
407+
408+
409+ class TestPythonFunctionWithoutJinjaHasEquivalentRawCodeAndCompiledCode :
410+ @pytest .fixture (scope = "class" )
411+ def functions (self ) -> Dict [str , str ]:
412+ return {
413+ "double_it.py" : double_it_py ,
414+ "double_it.yml" : double_it_python_yml ,
415+ }
416+
417+ @pytest .fixture (scope = "class" )
418+ def macros (self ) -> Dict [str , str ]:
419+ return {
420+ "postgres__scalar_function_python.sql" : scalar_function_python_macro ,
421+ }
422+
423+ def test_udfs (self , project ):
424+ run_dbt (["build" ])
425+ result = run_dbt (["compile" ])
426+ assert len (result .results ) == 1
427+ node = result .results [0 ].node
428+ assert isinstance (node , FunctionNode )
429+ assert node .raw_code == node .compiled_code
430+
431+
432+ class TestPythonFunctionWithJinjaHasCorrectCompiledCode :
433+ @pytest .fixture (scope = "class" )
434+ def functions (self ) -> Dict [str , str ]:
435+ return {
436+ "double_it.py" : double_it_py_with_jinja ,
437+ "double_it.yml" : double_it_python_yml ,
438+ }
439+
440+ @pytest .fixture (scope = "class" )
441+ def macros (self ) -> Dict [str , str ]:
442+ return {
443+ "postgres__scalar_function_python.sql" : scalar_function_python_macro ,
444+ }
445+
446+ def test_udfs (self , project ):
447+ result = run_dbt (["compile" ])
448+ assert len (result .results ) == 1
449+ node = result .results [0 ].node
450+ assert isinstance (node , FunctionNode )
451+ assert node .compiled_code == "def entry(value):\n \n return value * 2\n "
0 commit comments