17
17
from unittest import mock
18
18
19
19
import pytest
20
+ import yaml
20
21
from snowflake .cli ._plugins .dbt .constants import OUTPUT_COLUMN_NAME , RESULT_COLUMN_NAME
21
22
from snowflake .cli .api .identifiers import FQN
22
23
@@ -56,8 +57,20 @@ class TestDBTDeploy:
56
57
@pytest .fixture
57
58
def dbt_project_path (self , tmp_path_factory ):
58
59
source_path = tmp_path_factory .mktemp ("dbt_project" )
59
- dbt_file = source_path / "dbt_project.yml"
60
- dbt_file .touch ()
60
+ dbt_project_file = source_path / "dbt_project.yml"
61
+ dbt_project_file .write_text (yaml .dump ({"profile" : "dev" }))
62
+ dbt_profiles_file = source_path / "profiles.yml"
63
+ dbt_profiles_file .write_text (
64
+ yaml .dump (
65
+ {
66
+ "dev" : {
67
+ "outputs" : {
68
+ "local" : {"account" : "test_account" , "database" : "testdb" }
69
+ }
70
+ }
71
+ },
72
+ )
73
+ )
61
74
yield source_path
62
75
63
76
@pytest .fixture
@@ -105,68 +118,78 @@ def test_deploys_project_from_source(
105
118
dbt_project_path , "@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage"
106
119
)
107
120
121
+ @pytest .mark .parametrize ("exists" , (True , False ))
108
122
@mock .patch ("snowflake.cli._plugins.dbt.manager.StageManager.put_recursive" )
109
123
@mock .patch ("snowflake.cli._plugins.dbt.manager.StageManager.create" )
110
- def test_dbt_version_from_option_has_precedence_over_file (
124
+ def test_force_flag_uses_create_or_replace (
111
125
self ,
112
126
_mock_create ,
113
127
_mock_put_recursive ,
128
+ exists ,
114
129
mock_connect ,
115
130
runner ,
116
131
dbt_project_path ,
117
132
mock_exists ,
118
133
):
134
+ mock_exists .return_value = exists
135
+
119
136
result = runner .invoke (
120
137
[
121
138
"dbt" ,
122
139
"deploy" ,
123
140
"TEST_PIPELINE" ,
124
141
f"--source={ dbt_project_path } " ,
142
+ "--force" ,
125
143
]
126
144
)
127
145
128
146
assert result .exit_code == 0 , result .output
129
- assert (
130
- mock_connect .mocked_ctx .get_query ()
131
- == """CREATE DBT PROJECT TEST_PIPELINE
132
- FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage"""
147
+ assert mock_connect .mocked_ctx .get_query ().startswith (
148
+ "CREATE OR REPLACE DBT PROJECT"
133
149
)
134
150
135
- @pytest .mark .parametrize ("exists" , (True , False ))
136
- @mock .patch ("snowflake.cli._plugins.dbt.manager.StageManager.put_recursive" )
137
- @mock .patch ("snowflake.cli._plugins.dbt.manager.StageManager.create" )
138
- def test_force_flag_uses_create_or_replace (
139
- self ,
140
- _mock_create ,
141
- _mock_put_recursive ,
142
- exists ,
143
- mock_connect ,
144
- runner ,
145
- dbt_project_path ,
146
- mock_exists ,
151
+ def test_raises_when_dbt_project_yml_is_not_available (
152
+ self , dbt_project_path , mock_connect , runner
147
153
):
148
- mock_exists .return_value = exists
154
+ dbt_file = dbt_project_path / "dbt_project.yml"
155
+ dbt_file .unlink ()
149
156
150
157
result = runner .invoke (
151
158
[
152
159
"dbt" ,
153
160
"deploy" ,
154
161
"TEST_PIPELINE" ,
155
162
f"--source={ dbt_project_path } " ,
156
- "--force" ,
157
- ]
163
+ ],
158
164
)
159
165
160
- assert result .exit_code == 0 , result .output
161
- assert mock_connect .mocked_ctx .get_query ().startswith (
162
- "CREATE OR REPLACE DBT PROJECT"
166
+ assert result .exit_code == 1 , result .output
167
+ assert f"dbt_project.yml does not exist in directory" in result .output
168
+ assert mock_connect .mocked_ctx .get_query () == ""
169
+
170
+ def test_raises_when_dbt_project_yml_does_not_specify_profile (
171
+ self , dbt_project_path , mock_connect , runner
172
+ ):
173
+ with open ((dbt_project_path / "dbt_project.yml" ), "w" ) as f :
174
+ yaml .dump ({}, f )
175
+
176
+ result = runner .invoke (
177
+ [
178
+ "dbt" ,
179
+ "deploy" ,
180
+ "TEST_PIPELINE" ,
181
+ f"--source={ dbt_project_path } " ,
182
+ ],
163
183
)
164
184
165
- def test_raises_when_dbt_project_is_not_available (
185
+ assert result .exit_code == 1 , result .output
186
+ assert "`profile` is not defined in dbt_project.yml" in result .output
187
+ assert mock_connect .mocked_ctx .get_query () == ""
188
+
189
+ def test_raises_when_profiles_yml_is_not_available (
166
190
self , dbt_project_path , mock_connect , runner
167
191
):
168
- dbt_file = dbt_project_path / "dbt_project.yml"
169
- dbt_file .unlink ()
192
+ (dbt_project_path / "profiles.yml" ).unlink ()
170
193
171
194
result = runner .invoke (
172
195
[
@@ -178,10 +201,29 @@ def test_raises_when_dbt_project_is_not_available(
178
201
)
179
202
180
203
assert result .exit_code == 1 , result .output
181
- assert f"dbt_project.yml does not exist in directory" in result .output
204
+ assert f"profiles.yml does not exist in directory" in result .output
205
+ assert mock_connect .mocked_ctx .get_query () == ""
206
+
207
+ def test_raises_when_profiles_yml_does_not_contain_selected_profile (
208
+ self , dbt_project_path , mock_connect , runner
209
+ ):
210
+ with open ((dbt_project_path / "profiles.yml" ), "w" ) as f :
211
+ yaml .dump ({}, f )
212
+
213
+ result = runner .invoke (
214
+ [
215
+ "dbt" ,
216
+ "deploy" ,
217
+ "TEST_PIPELINE" ,
218
+ f"--source={ dbt_project_path } " ,
219
+ ],
220
+ )
221
+
222
+ assert result .exit_code == 1 , result .output
223
+ assert "profile dev is not defined in profiles.yml" in result .output
182
224
assert mock_connect .mocked_ctx .get_query () == ""
183
225
184
- def test_raises_when_dbt_project_exists_and_is_not_force (
226
+ def test_raises_when_dbt_object_exists_and_is_not_force (
185
227
self , dbt_project_path , mock_connect , runner , mock_exists
186
228
):
187
229
mock_exists .return_value = True
0 commit comments