1
1
import json
2
2
import os
3
3
4
+ import yaml
4
5
from data_diff .diff_tables import Algorithm
5
6
from .test_cli import run_datadiff_cli
6
7
@@ -49,112 +50,102 @@ def test_get_datadiff_variables_empty(self):
49
50
DbtParser .get_datadiff_variables (mock_self )
50
51
51
52
@patch ("builtins.open" , new_callable = mock_open , read_data = "{}" )
52
- @patch ("data_diff.dbt.parse_run_results" )
53
- @patch ("data_diff.dbt.parse_manifest" )
54
- def test_get_models (self , mock_manifest_parser , mock_run_parser , mock_open ):
53
+ def test_get_models (self , mock_open ):
55
54
expected_value = "expected_value"
56
55
mock_self = Mock ()
57
56
mock_self .project_dir = ""
58
57
mock_run_results = Mock ()
59
58
mock_success_result = Mock ()
60
59
mock_failed_result = Mock ()
61
60
mock_manifest = Mock ()
62
- mock_run_parser .return_value = mock_run_results
61
+ mock_self . parse_run_results .return_value = mock_run_results
63
62
mock_run_results .metadata .dbt_version = "1.0.0"
64
63
mock_success_result .unique_id = "success_unique_id"
65
64
mock_failed_result .unique_id = "failed_unique_id"
66
65
mock_success_result .status .name = "success"
67
66
mock_failed_result .status .name = "failed"
68
67
mock_run_results .results = [mock_success_result , mock_failed_result ]
69
- mock_manifest_parser .return_value = mock_manifest
68
+ mock_self . parse_manifest .return_value = mock_manifest
70
69
mock_manifest .nodes = {"success_unique_id" : expected_value }
71
70
72
71
models = DbtParser .get_models (mock_self )
73
72
74
73
self .assertEqual (expected_value , models [0 ])
75
74
mock_open .assert_any_call (RUN_RESULTS_PATH )
76
75
mock_open .assert_any_call (MANIFEST_PATH )
77
- mock_run_parser .assert_called_once_with (run_results = {})
78
- mock_manifest_parser .assert_called_once_with (manifest = {})
76
+ mock_self . parse_run_results .assert_called_once_with (run_results = {})
77
+ mock_self . parse_manifest .assert_called_once_with (manifest = {})
79
78
80
79
@patch ("builtins.open" , new_callable = mock_open , read_data = "{}" )
81
- @patch ("data_diff.dbt.parse_run_results" )
82
- @patch ("data_diff.dbt.parse_manifest" )
83
- def test_get_models_bad_lower_dbt_version (self , mock_manifest_parser , mock_run_parser , mock_open ):
80
+ def test_get_models_bad_lower_dbt_version (self , mock_open ):
84
81
mock_self = Mock ()
85
82
mock_self .project_dir = ""
86
83
mock_run_results = Mock ()
87
- mock_run_parser .return_value = mock_run_results
84
+ mock_self . parse_run_results .return_value = mock_run_results
88
85
mock_run_results .metadata .dbt_version = "0.19.0"
89
86
90
87
with self .assertRaises (Exception ) as ex :
91
88
DbtParser .get_models (mock_self )
92
89
93
90
mock_open .assert_called_once_with (RUN_RESULTS_PATH )
94
- mock_run_parser .assert_called_once_with (run_results = {})
95
- mock_manifest_parser .assert_not_called ()
91
+ mock_self . parse_run_results .assert_called_once_with (run_results = {})
92
+ mock_self . parse_manifest .assert_not_called ()
96
93
self .assertIn ("version to be" , ex .exception .args [0 ])
97
94
98
95
@patch ("builtins.open" , new_callable = mock_open , read_data = "{}" )
99
- @patch ("data_diff.dbt.parse_run_results" )
100
- @patch ("data_diff.dbt.parse_manifest" )
101
- def test_get_models_bad_upper_dbt_version (self , mock_manifest_parser , mock_run_parser , mock_open ):
96
+ def test_get_models_bad_upper_dbt_version (self , mock_open ):
102
97
mock_self = Mock ()
103
98
mock_self .project_dir = ""
104
99
mock_run_results = Mock ()
105
- mock_run_parser .return_value = mock_run_results
100
+ mock_self . parse_run_results .return_value = mock_run_results
106
101
mock_run_results .metadata .dbt_version = "1.5.1"
107
102
108
103
with self .assertRaises (Exception ) as ex :
109
104
DbtParser .get_models (mock_self )
110
105
111
106
mock_open .assert_called_once_with (RUN_RESULTS_PATH )
112
- mock_run_parser .assert_called_once_with (run_results = {})
113
- mock_manifest_parser .assert_not_called ()
107
+ mock_self . parse_run_results .assert_called_once_with (run_results = {})
108
+ mock_self . parse_manifest .assert_not_called ()
114
109
self .assertIn ("version to be" , ex .exception .args [0 ])
115
110
116
111
@patch ("builtins.open" , new_callable = mock_open , read_data = "{}" )
117
- @patch ("data_diff.dbt.parse_run_results" )
118
- @patch ("data_diff.dbt.parse_manifest" )
119
- def test_get_models_no_success (self , mock_manifest_parser , mock_run_parser , mock_open ):
112
+ def test_get_models_no_success (self , mock_open ):
120
113
mock_self = Mock ()
121
114
mock_self .project_dir = ""
122
115
mock_run_results = Mock ()
123
116
mock_success_result = Mock ()
124
117
mock_failed_result = Mock ()
125
118
mock_manifest = Mock ()
126
- mock_run_parser .return_value = mock_run_results
119
+ mock_self . parse_run_results .return_value = mock_run_results
127
120
mock_run_results .metadata .dbt_version = "1.0.0"
128
121
mock_failed_result .unique_id = "failed_unique_id"
129
122
mock_success_result .status .name = "success"
130
123
mock_failed_result .status .name = "failed"
131
124
mock_run_results .results = [mock_failed_result ]
132
- mock_manifest_parser .return_value = mock_manifest
125
+ mock_self . parse_manifest .return_value = mock_manifest
133
126
mock_manifest .nodes = {"success_unique_id" : "a_unique_id" }
134
127
135
128
with self .assertRaises (Exception ):
136
129
DbtParser .get_models (mock_self )
137
130
138
131
mock_open .assert_any_call (RUN_RESULTS_PATH )
139
132
mock_open .assert_any_call (MANIFEST_PATH )
140
- mock_run_parser .assert_called_once_with (run_results = {})
141
- mock_manifest_parser .assert_called_once_with (manifest = {})
133
+ mock_self . parse_run_results .assert_called_once_with (run_results = {})
134
+ mock_self . parse_manifest .assert_called_once_with (manifest = {})
142
135
143
- @patch ("data_diff.dbt.yaml.safe_load" )
144
136
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
145
- def test_set_project_dict (self , mock_open , mock_yaml_parse ):
137
+ def test_set_project_dict (self , mock_open ):
146
138
expected_dict = {"key1" : "value1" }
147
139
mock_self = Mock ()
148
140
mock_self .project_dir = ""
149
- mock_yaml_parse .return_value = expected_dict
141
+ mock_self . yaml . safe_load .return_value = expected_dict
150
142
DbtParser .set_project_dict (mock_self )
151
143
152
144
self .assertEqual (mock_self .project_dict , expected_dict )
153
145
mock_open .assert_called_once_with (PROJECT_FILE )
154
146
155
- @patch ("data_diff.dbt.yaml.safe_load" )
156
147
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
157
- def test_set_connection_snowflake (self , mock_open_file , mock_yaml_parse ):
148
+ def test_set_connection_snowflake (self , mock_open_file ):
158
149
expected_driver = "snowflake"
159
150
expected_password = "password_value"
160
151
profiles_dict = {
@@ -172,19 +163,19 @@ def test_set_connection_snowflake(self, mock_open_file, mock_yaml_parse):
172
163
mock_self = Mock ()
173
164
mock_self .profiles_dir = ""
174
165
mock_self .project_dict = {"profile" : "profile_name" }
175
- mock_yaml_parse .return_value = profiles_dict
166
+ mock_self .yaml .safe_load .return_value = profiles_dict
167
+ mock_self .ProfileRenderer ().render_data .return_value = profiles_dict ["profile_name" ]["outputs" ]["connection1" ]
176
168
DbtParser .set_connection (mock_self )
177
169
178
170
self .assertIsInstance (mock_self .connection , dict )
179
171
self .assertEqual (mock_self .connection .get ("driver" ), expected_driver )
180
172
self .assertEqual (mock_self .connection .get ("password" ), expected_password )
181
173
self .assertEqual (mock_self .requires_upper , True )
182
174
mock_open_file .assert_called_once_with (PROFILES_FILE )
183
- mock_yaml_parse .assert_called_once_with (mock_open_file ())
175
+ mock_self . yaml . safe_load .assert_called_once_with (mock_open_file ())
184
176
185
- @patch ("data_diff.dbt.yaml.safe_load" )
186
177
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
187
- def test_set_connection_snowflake_no_password (self , mock_open_file , mock_yaml_parse ):
178
+ def test_set_connection_snowflake_no_password (self , mock_open_file ):
188
179
expected_driver = "snowflake"
189
180
profiles_dict = {
190
181
"profile_name" : {
@@ -196,18 +187,18 @@ def test_set_connection_snowflake_no_password(self, mock_open_file, mock_yaml_pa
196
187
mock_self = Mock ()
197
188
mock_self .profiles_dir = ""
198
189
mock_self .project_dict = {"profile" : "profile_name" }
199
- mock_yaml_parse .return_value = profiles_dict
190
+ mock_self .yaml .safe_load .return_value = profiles_dict
191
+ mock_self .ProfileRenderer ().render_data .return_value = profiles_dict ["profile_name" ]["outputs" ]["connection1" ]
200
192
201
193
with self .assertRaises (Exception ):
202
194
DbtParser .set_connection (mock_self )
203
195
204
196
mock_open_file .assert_called_once_with (PROFILES_FILE )
205
- mock_yaml_parse .assert_called_once_with (mock_open_file ())
197
+ mock_self . yaml . safe_load .assert_called_once_with (mock_open_file ())
206
198
self .assertNotIsInstance (mock_self .connection , dict )
207
199
208
- @patch ("data_diff.dbt.yaml.safe_load" )
209
200
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
210
- def test_set_connection_bigquery (self , mock_open_file , mock_yaml_parse ):
201
+ def test_set_connection_bigquery (self , mock_open_file ):
211
202
expected_driver = "bigquery"
212
203
expected_method = "oauth"
213
204
expected_project = "a_project"
@@ -229,19 +220,19 @@ def test_set_connection_bigquery(self, mock_open_file, mock_yaml_parse):
229
220
mock_self = Mock ()
230
221
mock_self .profiles_dir = ""
231
222
mock_self .project_dict = {"profile" : "profile_name" }
232
- mock_yaml_parse .return_value = profiles_dict
223
+ mock_self .yaml .safe_load .return_value = profiles_dict
224
+ mock_self .ProfileRenderer ().render_data .return_value = profiles_dict ["profile_name" ]["outputs" ]["connection1" ]
233
225
DbtParser .set_connection (mock_self )
234
226
235
227
self .assertIsInstance (mock_self .connection , dict )
236
228
self .assertEqual (mock_self .connection .get ("driver" ), expected_driver )
237
229
self .assertEqual (mock_self .connection .get ("project" ), expected_project )
238
230
self .assertEqual (mock_self .connection .get ("dataset" ), expected_dataset )
239
231
mock_open_file .assert_called_once_with (PROFILES_FILE )
240
- mock_yaml_parse .assert_called_once_with (mock_open_file ())
232
+ mock_self . yaml . safe_load .assert_called_once_with (mock_open_file ())
241
233
242
- @patch ("data_diff.dbt.yaml.safe_load" )
243
234
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
244
- def test_set_connection_bigquery_not_oauth (self , mock_open_file , mock_yaml_parse ):
235
+ def test_set_connection_bigquery_not_oauth (self , mock_open_file ):
245
236
expected_driver = "bigquery"
246
237
expected_method = "not_oauth"
247
238
expected_project = "a_project"
@@ -263,17 +254,17 @@ def test_set_connection_bigquery_not_oauth(self, mock_open_file, mock_yaml_parse
263
254
mock_self = Mock ()
264
255
mock_self .profiles_dir = ""
265
256
mock_self .project_dict = {"profile" : "profile_name" }
266
- mock_yaml_parse .return_value = profiles_dict
257
+ mock_self .yaml .safe_load .return_value = profiles_dict
258
+ mock_self .ProfileRenderer ().render_data .return_value = profiles_dict ["profile_name" ]["outputs" ]["connection1" ]
267
259
with self .assertRaises (Exception ):
268
260
DbtParser .set_connection (mock_self )
269
261
270
262
mock_open_file .assert_called_once_with (PROFILES_FILE )
271
- mock_yaml_parse .assert_called_once_with (mock_open_file ())
263
+ mock_self . yaml . safe_load .assert_called_once_with (mock_open_file ())
272
264
self .assertNotIsInstance (mock_self .connection , dict )
273
265
274
- @patch ("data_diff.dbt.yaml.safe_load" )
275
266
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
276
- def test_set_connection_key_error (self , mock_open_file , mock_yaml_parse ):
267
+ def test_set_connection_key_error (self , mock_open_file ):
277
268
profiles_dict = {
278
269
"profile_name" : {
279
270
"outputs" : {
@@ -290,17 +281,17 @@ def test_set_connection_key_error(self, mock_open_file, mock_yaml_parse):
290
281
mock_self .profiles_dir = ""
291
282
mock_self .project_dir = ""
292
283
mock_self .project_dict = {"profile" : "bad_key" }
293
- mock_yaml_parse .return_value = profiles_dict
284
+ mock_self .yaml .safe_load .return_value = profiles_dict
285
+ mock_self .ProfileRenderer ().render_data .return_value = profiles_dict ["profile_name" ]["outputs" ]["connection1" ]
294
286
with self .assertRaises (Exception ):
295
287
DbtParser .set_connection (mock_self )
296
288
297
289
mock_open_file .assert_called_once_with (PROFILES_FILE )
298
- mock_yaml_parse .assert_called_once_with (mock_open_file ())
290
+ mock_self . yaml . safe_load .assert_called_once_with (mock_open_file ())
299
291
self .assertNotIsInstance (mock_self .connection , dict )
300
292
301
- @patch ("data_diff.dbt.yaml.safe_load" )
302
293
@patch ("builtins.open" , new_callable = mock_open , read_data = "key:\n value" )
303
- def test_set_connection_not_implemented (self , mock_open_file , mock_yaml_parse ):
294
+ def test_set_connection_not_implemented (self , mock_open_file ):
304
295
expected_driver = "not_implemented"
305
296
profiles_dict = {
306
297
"profile_name" : {
@@ -317,12 +308,13 @@ def test_set_connection_not_implemented(self, mock_open_file, mock_yaml_parse):
317
308
mock_self .profiles_dir = ""
318
309
mock_self .project_dir = ""
319
310
mock_self .project_dict = {"profile" : "profile_name" }
320
- mock_yaml_parse .return_value = profiles_dict
311
+ mock_self .yaml .safe_load .return_value = profiles_dict
312
+ mock_self .ProfileRenderer ().render_data .return_value = profiles_dict ["profile_name" ]["outputs" ]["connection1" ]
321
313
with self .assertRaises (NotImplementedError ):
322
314
DbtParser .set_connection (mock_self )
323
315
324
316
mock_open_file .assert_called_once_with (PROFILES_FILE )
325
- mock_yaml_parse .assert_called_once_with (mock_open_file ())
317
+ mock_self . yaml . safe_load .assert_called_once_with (mock_open_file ())
326
318
self .assertNotIsInstance (mock_self .connection , dict )
327
319
328
320
0 commit comments