11import pytest
22from dbt .tests .util import run_dbt , get_connection
3- from tests .utils . util import relation_from_name
3+ from tests .fixtures . profiles import unique_schema , dbt_profile_data
44
55
6+ # Table tests - should use root_path config
67custom_schema_table_no_schema_model = """
78{{ config(
89 materialized='table'
910) }}
1011select 1 as id
1112"""
1213
13- custom_schema_table_model = """
14+ custom_schema_table_with_root_path_model = """
1415{{ config(
1516 materialized='table',
16- schema ='schema'
17+ root_path ='schema'
1718) }}
1819select 1 as id
1920"""
2021
21- custom_schema_table_nested_model = """
22+ custom_schema_table_with_schema_config_model = """
2223{{ config(
2324 materialized='table',
24- schema='nested.schema'
25+ schema='useless_config'
26+ ) }}
27+ select 1 as id
28+ """
29+
30+ custom_schema_table_nested_with_root_path_model = """
31+ {{ config(
32+ materialized='table',
33+ root_path='nested.schema'
2534) }}
2635select 1 as id
2736"""
2837
38+ # View tests - should use schema config
2939custom_schema_view_no_schema_model = """
3040{{ config(
3141 materialized='view'
3242) }}
3343select 1 as id
3444"""
3545
36- custom_schema_view_model = """
46+ custom_schema_view_with_schema_config_model = """
3747{{ config(
3848 materialized='view',
3949 schema='schema'
4050) }}
4151select 1 as id
4252"""
4353
44- custom_schema_view_nested_model = """
54+ custom_schema_view_with_root_path_config_model = """
55+ {{ config(
56+ materialized='view',
57+ root_path='useless_config'
58+ ) }}
59+ select 1 as id
60+ """
61+
62+ custom_schema_view_nested_with_schema_config_model = """
4563{{ config(
4664 materialized='view',
4765 schema='nested.schema'
@@ -54,64 +72,233 @@ class TestGetCustomSchema:
5472 @pytest .fixture (scope = "class" )
5573 def models (self ):
5674 return {
75+ # Table models
5776 "custom_schema_table_no_schema.sql" : custom_schema_table_no_schema_model ,
58- "custom_schema_table.sql" : custom_schema_table_model ,
59- "custom_schema_table_nested.sql" : custom_schema_table_nested_model ,
77+ "custom_schema_table_with_root_path.sql" : custom_schema_table_with_root_path_model ,
78+ "custom_schema_table_with_schema_config.sql" : custom_schema_table_with_schema_config_model ,
79+ "custom_schema_table_nested_with_root_path.sql" : custom_schema_table_nested_with_root_path_model ,
80+ # View models
6081 "custom_schema_view_no_schema.sql" : custom_schema_view_no_schema_model ,
61- "custom_schema_view.sql" : custom_schema_view_model ,
62- "custom_schema_view_nested.sql" : custom_schema_view_nested_model ,
82+ "custom_schema_view_with_schema_config.sql" : custom_schema_view_with_schema_config_model ,
83+ "custom_schema_view_with_root_path_config.sql" : custom_schema_view_with_root_path_config_model ,
84+ "custom_schema_view_nested_with_schema_config.sql" : custom_schema_view_nested_with_schema_config_model ,
6385 }
6486
87+ # ===== TABLE TESTS =====
88+
6589 def test_custom_schema_table_no_schema (self , project ):
90+ """Test table without custom schema - should use default root_path from profile"""
6691 run_dbt (["run" , "--select" , "custom_schema_table_no_schema" ])
67- table_relation = relation_from_name (
68- project .adapter , "custom_schema_table_no_schema" )
92+
93+ # Expected path components
94+ credentials = project .adapter .config .credentials
95+ expected_database = credentials .datalake
96+ expected_schema = credentials .root_path # e.g., "dbtdremios3.test17636798006768308215_test_nested_schema"
97+ expected_identifier = "custom_schema_table_no_schema"
98+
99+ # Get the actual relation from Dremio
100+ with get_connection (project .adapter ):
101+ actual_relation = project .adapter .get_relation (
102+ database = expected_database ,
103+ schema = expected_schema ,
104+ identifier = expected_identifier
105+ )
106+
107+ # Verify the relation was created with the expected path
108+ assert actual_relation is not None , f"Table should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
109+ assert actual_relation .type == "table"
110+
111+ columns = project .adapter .get_columns_in_relation (actual_relation )
112+ assert len (columns ) == 1
113+ assert columns [0 ].name == "id"
114+
115+ def test_custom_schema_table_with_root_path (self , project ):
116+ """Test table with root_path config - should be appended to default root_path"""
117+ run_dbt (["run" , "--select" , "custom_schema_table_with_root_path" ])
118+
119+ # Expected path components
120+ credentials = project .adapter .config .credentials
121+ expected_database = credentials .datalake
122+ expected_schema = f"{ credentials .root_path } .schema"
123+ expected_identifier = "custom_schema_table_with_root_path"
124+
125+ # Get the actual relation from Dremio
69126 with get_connection (project .adapter ):
70- columns = project .adapter .get_columns_in_relation (table_relation )
71- assert len (columns ) == 1
72- assert columns [0 ].name == "id"
73-
74- def test_custom_schema_table (self , project ):
75- run_dbt (["run" , "--select" , "custom_schema_table" ])
76- table_relation = relation_from_name (
77- project .adapter , "schema.custom_schema_table" )
127+ actual_relation = project .adapter .get_relation (
128+ database = expected_database ,
129+ schema = expected_schema ,
130+ identifier = expected_identifier
131+ )
132+
133+ # Verify the relation was created with the expected path
134+ assert actual_relation is not None , f"Table should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
135+ assert actual_relation .type == "table"
136+
137+ # Verify we can query it
138+ columns = project .adapter .get_columns_in_relation (actual_relation )
139+ assert len (columns ) == 1
140+ assert columns [0 ].name == "id"
141+
142+ def test_custom_schema_table_with_schema_config (self , project ):
143+ """Test table with schema config - should be IGNORED for tables (root_path is used instead)"""
144+ run_dbt (["run" , "--select" , "custom_schema_table_with_schema_config" ])
145+
146+ # Expected path components
147+ credentials = project .adapter .config .credentials
148+ expected_database = credentials .datalake
149+ expected_schema = credentials .root_path # schema config is ignored for tables
150+ expected_identifier = "custom_schema_table_with_schema_config"
151+
152+ # Get the actual relation from Dremio
78153 with get_connection (project .adapter ):
79- columns = project .adapter .get_columns_in_relation (table_relation )
80- assert len (columns ) == 1
81- assert columns [0 ].name == "id"
82-
83- def test_custom_schema_table_nested (self , project ):
84- run_dbt (["run" , "--select" , "custom_schema_table_nested" ])
85- table_relation = relation_from_name (
86- project .adapter , "nested.schema.custom_schema_table_nested" )
154+ actual_relation = project .adapter .get_relation (
155+ database = expected_database ,
156+ schema = expected_schema ,
157+ identifier = expected_identifier
158+ )
159+
160+ # Verify the relation was created with the expected path
161+ assert actual_relation is not None , f"Table should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
162+ assert actual_relation .type == "table"
163+
164+ # Verify we can query it
165+ columns = project .adapter .get_columns_in_relation (actual_relation )
166+ assert len (columns ) == 1
167+ assert columns [0 ].name == "id"
168+
169+ def test_custom_schema_table_nested_with_root_path (self , project ):
170+ """Test table with nested root_path config - should be appended to default root_path"""
171+ run_dbt (["run" , "--select" , "custom_schema_table_nested_with_root_path" ])
172+
173+ # Expected path components
174+ credentials = project .adapter .config .credentials
175+ expected_database = credentials .datalake
176+ expected_schema = f"{ credentials .root_path } .nested.schema"
177+ expected_identifier = "custom_schema_table_nested_with_root_path"
178+
179+ # Get the actual relation from Dremio
87180 with get_connection (project .adapter ):
88- columns = project .adapter .get_columns_in_relation (table_relation )
89- assert len (columns ) == 1
90- assert columns [0 ].name == "id"
181+ actual_relation = project .adapter .get_relation (
182+ database = expected_database ,
183+ schema = expected_schema ,
184+ identifier = expected_identifier
185+ )
186+
187+ # Verify the relation was created with the expected path
188+ assert actual_relation is not None , f"Table should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
189+ assert actual_relation .type == "table"
190+
191+ # Verify we can query it
192+ columns = project .adapter .get_columns_in_relation (actual_relation )
193+ assert len (columns ) == 1
194+ assert columns [0 ].name == "id"
195+
196+ # ===== VIEW TESTS =====
91197
92198 def test_custom_schema_view_no_schema (self , project ):
199+ """Test view without custom schema - should use default schema from profile"""
93200 run_dbt (["run" , "--select" , "custom_schema_view_no_schema" ])
94- view_relation = relation_from_name (
95- project .adapter , "custom_schema_view_no_schema" )
201+
202+ # Expected path components
203+ credentials = project .adapter .config .credentials
204+ expected_database = credentials .database
205+ expected_schema = credentials .schema # e.g., "test17636798006768308215_test_nested_schema"
206+ expected_identifier = "custom_schema_view_no_schema"
207+
208+ # Get the actual relation from Dremio
209+ with get_connection (project .adapter ):
210+ actual_relation = project .adapter .get_relation (
211+ database = expected_database ,
212+ schema = expected_schema ,
213+ identifier = expected_identifier
214+ )
215+
216+ # Verify the relation was created with the expected path
217+ assert actual_relation is not None , f"View should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
218+ assert actual_relation .type == "view"
219+
220+ # Verify we can query it
221+ columns = project .adapter .get_columns_in_relation (actual_relation )
222+ assert len (columns ) == 1
223+ assert columns [0 ].name == "id"
224+
225+ def test_custom_schema_view_with_schema_config (self , project ):
226+ """Test view with schema config - should be appended to default schema"""
227+ run_dbt (["run" , "--select" , "custom_schema_view_with_schema_config" ])
228+
229+ # Expected path components
230+ credentials = project .adapter .config .credentials
231+ expected_database = credentials .database
232+ expected_schema = f"{ credentials .schema } .schema" # Appended!
233+ expected_identifier = "custom_schema_view_with_schema_config"
234+
235+ # Get the actual relation from Dremio
96236 with get_connection (project .adapter ):
97- columns = project .adapter .get_columns_in_relation (view_relation )
98- assert len (columns ) == 1
99- assert columns [0 ].name == "id"
100-
101- def test_custom_schema_view (self , project ):
102- run_dbt (["run" , "--select" , "custom_schema_view" ])
103- view_relation = relation_from_name (
104- project .adapter , "schema.custom_schema_view" )
237+ actual_relation = project .adapter .get_relation (
238+ database = expected_database ,
239+ schema = expected_schema ,
240+ identifier = expected_identifier
241+ )
242+
243+ # Verify the relation was created with the expected path
244+ assert actual_relation is not None , f"View should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
245+ assert actual_relation .type == "view"
246+
247+ # Verify we can query it
248+ columns = project .adapter .get_columns_in_relation (actual_relation )
249+ assert len (columns ) == 1
250+ assert columns [0 ].name == "id"
251+
252+ def test_custom_schema_view_with_root_path_config (self , project ):
253+ """Test view with root_path config - should be IGNORED for views (schema is used instead)"""
254+ run_dbt (["run" , "--select" , "custom_schema_view_with_root_path_config" ])
255+
256+ # Expected path components - root_path config should be ignored for views
257+ credentials = project .adapter .config .credentials
258+ expected_database = credentials .database
259+ expected_schema = credentials .schema # root_path config is ignored for tables
260+ expected_identifier = "custom_schema_view_with_root_path_config"
261+
262+ # Get the actual relation from Dremio
105263 with get_connection (project .adapter ):
106- columns = project .adapter .get_columns_in_relation (view_relation )
107- assert len (columns ) == 1
108- assert columns [0 ].name == "id"
109-
110- def test_custom_schema_view_nested (self , project ):
111- run_dbt (["run" , "--select" , "custom_schema_view_nested" ])
112- view_relation = relation_from_name (
113- project .adapter , "nested.schema.custom_schema_view_nested" )
264+ actual_relation = project .adapter .get_relation (
265+ database = expected_database ,
266+ schema = expected_schema ,
267+ identifier = expected_identifier
268+ )
269+
270+ # Verify the relation was created with the expected path (root_path config ignored)
271+ assert actual_relation is not None , f"View should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } (root_path config should be ignored for views)"
272+ assert actual_relation .type == "view"
273+
274+ # Verify we can query it
275+ columns = project .adapter .get_columns_in_relation (actual_relation )
276+ assert len (columns ) == 1
277+ assert columns [0 ].name == "id"
278+
279+ def test_custom_schema_view_nested_with_schema_config (self , project ):
280+ """Test view with nested schema config - should be appended to default schema"""
281+ run_dbt (["run" , "--select" , "custom_schema_view_nested_with_schema_config" ])
282+
283+ # Expected path components
284+ credentials = project .adapter .config .credentials
285+ expected_database = credentials .database
286+ expected_schema = f"{ credentials .schema } .nested.schema" # Appended!
287+ expected_identifier = "custom_schema_view_nested_with_schema_config"
288+
289+ # Get the actual relation from Dremio
114290 with get_connection (project .adapter ):
115- columns = project .adapter .get_columns_in_relation (view_relation )
116- assert len (columns ) == 1
117- assert columns [0 ].name == "id"
291+ actual_relation = project .adapter .get_relation (
292+ database = expected_database ,
293+ schema = expected_schema ,
294+ identifier = expected_identifier
295+ )
296+
297+ # Verify the relation was created with the expected path
298+ assert actual_relation is not None , f"View should have been created at { expected_database } .{ expected_schema } .{ expected_identifier } "
299+ assert actual_relation .type == "view"
300+
301+ # Verify we can query it
302+ columns = project .adapter .get_columns_in_relation (actual_relation )
303+ assert len (columns ) == 1
304+ assert columns [0 ].name == "id"
0 commit comments