12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from __future__ import annotations
16
+
15
17
from unittest import mock
16
18
17
19
import pytest
20
+ from snowflake .cli .api .identifiers import FQN
18
21
19
22
20
23
@pytest .fixture
@@ -26,76 +29,127 @@ def mock_connect(mock_ctx):
26
29
yield _fixture
27
30
28
31
29
- def test_dbt_list (mock_connect , runner ):
30
-
31
- result = runner .invoke (["dbt" , "list" ])
32
-
33
- assert result .exit_code == 0 , result .output
34
- assert mock_connect .mocked_ctx .get_query () == "SHOW DBT"
35
-
36
-
37
- @pytest .mark .parametrize (
38
- "args,expected_query" ,
39
- [
40
- pytest .param (
41
- [
42
- "dbt" ,
43
- "execute" ,
44
- "pipeline_name" ,
45
- "test" ,
46
- ],
47
- "EXECUTE DBT pipeline_name test" ,
48
- id = "simple-command" ,
49
- ),
50
- pytest .param (
51
- [
52
- "dbt" ,
53
- "execute" ,
54
- "pipeline_name" ,
55
- "run" ,
56
- "-f" ,
57
- "--select @source:snowplow,tag:nightly models/export" ,
58
- ],
59
- "EXECUTE DBT pipeline_name run -f --select @source:snowplow,tag:nightly models/export" ,
60
- id = "with-dbt-options" ,
61
- ),
62
- pytest .param (
63
- ["dbt" , "execute" , "pipeline_name" , "compile" , "--vars '{foo:bar}'" ],
64
- "EXECUTE DBT pipeline_name compile --vars '{foo:bar}'" ,
65
- id = "with-dbt-vars" ,
66
- ),
67
- pytest .param (
68
- [
69
- "dbt" ,
70
- "execute" ,
71
- "pipeline_name" ,
72
- "compile" ,
73
- "--format=TXT" , # collision with CLI's option; unsupported option
74
- "-v" , # collision with CLI's option
75
- "-h" ,
76
- "--debug" ,
77
- "--info" ,
78
- "--config-file=/" ,
79
- ],
80
- "EXECUTE DBT pipeline_name compile --format=TXT -v -h --debug --info --config-file=/" ,
81
- id = "with-dbt-conflicting-options" ,
82
- ),
83
- pytest .param (
84
- [
85
- "dbt" ,
86
- "execute" ,
87
- "--format=JSON" ,
88
- "pipeline_name" ,
89
- "compile" ,
90
- ],
91
- "EXECUTE DBT pipeline_name compile" ,
92
- id = "with-cli-flag" ,
93
- ),
94
- ],
95
- )
96
- def test_dbt_execute (mock_connect , runner , args , expected_query ):
97
-
98
- result = runner .invoke (args )
99
-
100
- assert result .exit_code == 0 , result .output
101
- assert mock_connect .mocked_ctx .get_query () == expected_query
32
+ class TestDBTList :
33
+ def test_dbt_list (self , mock_connect , runner ):
34
+
35
+ result = runner .invoke (["dbt" , "list" ])
36
+
37
+ assert result .exit_code == 0 , result .output
38
+ assert mock_connect .mocked_ctx .get_query () == "SHOW DBT"
39
+
40
+
41
+ class TestDBTDeploy :
42
+ @pytest .fixture
43
+ def dbt_project_path (self , tmp_path_factory ):
44
+ source_path = tmp_path_factory .mktemp ("dbt_project" )
45
+ dbt_file = source_path / "dbt_project.yml"
46
+ dbt_file .touch ()
47
+ yield source_path
48
+
49
+ @pytest .fixture
50
+ def mock_cli_console (self ):
51
+ with mock .patch ("snowflake.cli.api.console" ) as _fixture :
52
+ yield _fixture
53
+
54
+ @mock .patch ("snowflake.cli._plugins.dbt.manager.StageManager.put_recursive" )
55
+ @mock .patch ("snowflake.cli._plugins.dbt.manager.StageManager.create" )
56
+ def test_deploys_project_from_source (
57
+ self , mock_create , mock_put_recursive , mock_connect , runner , dbt_project_path
58
+ ):
59
+
60
+ result = runner .invoke (
61
+ ["dbt" , "deploy" , "TEST_PIPELINE" , f"--source={ dbt_project_path } " ]
62
+ )
63
+
64
+ assert result .exit_code == 0 , result .output
65
+ assert (
66
+ mock_connect .mocked_ctx .get_query ()
67
+ == "CREATE OR REPLACE DBT TEST_PIPELINE FROM @MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage"
68
+ )
69
+ stage_fqn = FQN .from_string (f"dbt_TEST_PIPELINE_stage" ).using_context ()
70
+ mock_create .assert_called_once_with (stage_fqn , temporary = True )
71
+ mock_put_recursive .assert_called_once_with (
72
+ dbt_project_path , "@MockDatabase.MockSchema.dbt_TEST_PIPELINE_stage"
73
+ )
74
+
75
+ def test_raises_when_dbt_project_is_not_available (
76
+ self , dbt_project_path , mock_connect , runner
77
+ ):
78
+ dbt_file = dbt_project_path / "dbt_project.yml"
79
+ dbt_file .unlink ()
80
+
81
+ result = runner .invoke (
82
+ ["dbt" , "deploy" , "TEST_PIPELINE" , f"--source={ dbt_project_path } " ]
83
+ )
84
+
85
+ assert result .exit_code == 1 , result .output
86
+ assert "dbt_project.yml does not exist in provided path." in result .output
87
+ assert mock_connect .mocked_ctx .get_query () == ""
88
+
89
+
90
+ class TestDBTExecute :
91
+ @pytest .mark .parametrize (
92
+ "args,expected_query" ,
93
+ [
94
+ pytest .param (
95
+ [
96
+ "dbt" ,
97
+ "execute" ,
98
+ "pipeline_name" ,
99
+ "test" ,
100
+ ],
101
+ "EXECUTE DBT pipeline_name test" ,
102
+ id = "simple-command" ,
103
+ ),
104
+ pytest .param (
105
+ [
106
+ "dbt" ,
107
+ "execute" ,
108
+ "pipeline_name" ,
109
+ "run" ,
110
+ "-f" ,
111
+ "--select @source:snowplow,tag:nightly models/export" ,
112
+ ],
113
+ "EXECUTE DBT pipeline_name run -f --select @source:snowplow,tag:nightly models/export" ,
114
+ id = "with-dbt-options" ,
115
+ ),
116
+ pytest .param (
117
+ ["dbt" , "execute" , "pipeline_name" , "compile" , "--vars '{foo:bar}'" ],
118
+ "EXECUTE DBT pipeline_name compile --vars '{foo:bar}'" ,
119
+ id = "with-dbt-vars" ,
120
+ ),
121
+ pytest .param (
122
+ [
123
+ "dbt" ,
124
+ "execute" ,
125
+ "pipeline_name" ,
126
+ "compile" ,
127
+ "--format=TXT" , # collision with CLI's option; unsupported option
128
+ "-v" , # collision with CLI's option
129
+ "-h" ,
130
+ "--debug" ,
131
+ "--info" ,
132
+ "--config-file=/" ,
133
+ ],
134
+ "EXECUTE DBT pipeline_name compile --format=TXT -v -h --debug --info --config-file=/" ,
135
+ id = "with-dbt-conflicting-options" ,
136
+ ),
137
+ pytest .param (
138
+ [
139
+ "dbt" ,
140
+ "execute" ,
141
+ "--format=JSON" ,
142
+ "pipeline_name" ,
143
+ "compile" ,
144
+ ],
145
+ "EXECUTE DBT pipeline_name compile" ,
146
+ id = "with-cli-flag" ,
147
+ ),
148
+ ],
149
+ )
150
+ def test_dbt_execute (self , mock_connect , runner , args , expected_query ):
151
+
152
+ result = runner .invoke (args )
153
+
154
+ assert result .exit_code == 0 , result .output
155
+ assert mock_connect .mocked_ctx .get_query () == expected_query
0 commit comments