-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathtest__main.py
561 lines (513 loc) · 17.8 KB
/
test__main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import logging
import os
from unittest import mock
import pytest
from data_validation import cli_tools, exceptions, config_manager, consts
from data_validation import __main__ as main
TEST_CONN = '{"source_type":"Example"}'
CLI_ARGS = {
"command": "validate",
"validate_cmd": "column",
"source_conn": TEST_CONN,
"target_conn": TEST_CONN,
"tables_list": "my_schema.my_table",
"sum": "col_a,col_b",
"count": "col_a,col_b",
"config_file": "example_test.yaml",
"verbose": True,
}
CONFIG_RUNNER_ARGS_1 = {
"verbose": False,
"log_level": "INFO",
"command": "configs",
"validation_config_cmd": "run",
"dry_run": False,
"config_file": "gs://pso-project/resources/test/unit/test__main/3validations/first.yaml",
"config_dir": None,
"kube_completions": True,
}
CONFIG_RUNNER_ARGS_2 = {
"verbose": False,
"log_level": "INFO",
"dry_run": False,
"command": "configs",
"validation_config_cmd": "run",
"kube_completions": True,
"config_dir": "gs://pso-kokoro-resources/resources/test/unit/test__main/3validations",
}
CONFIG_RUNNER_ARGS_3 = {
"verbose": False,
"log_level": "INFO",
"dry_run": False,
"command": "configs",
"kube_completions": True,
"validation_config_cmd": "run",
"config_dir": "gs://pso-project/resources/test/unit/test__main/4partitions",
}
CONFIG_RUNNER_ARGS_4 = {
"verbose": False,
"log_level": "INFO",
"dry_run": False,
"command": "configs",
"kube_completions": False,
"validation_config_cmd": "run",
"config_dir": "gs://pso-kokoro-resources/resources/test/unit/test__main/4partitions",
}
CONFIG_RUNNER_EXCEPTION_TEXT = (
"Error '{}' occurred while running config file {}. Skipping it for now."
)
VALIDATE_COLUMN_CONFIG = {
"verbose": False,
"log_level": "INFO",
"command": "validate",
"validate_cmd": "column",
"dry_run": False,
consts.CONFIG_TYPE: consts.COLUMN_VALIDATION,
consts.CONFIG_SOURCE_CONN: TEST_CONN,
consts.CONFIG_TARGET_CONN: TEST_CONN,
consts.CONFIG_FILE: None,
consts.CONFIG_FILE_JSON: None,
}
BROKEN_VALIDATE_COLUMN_CONFIG_MISSING_COMMAND = {
"verbose": False,
"log_level": "INFO",
"validate_cmd": "column",
"dry_run": False,
consts.CONFIG_TYPE: consts.COLUMN_VALIDATION,
consts.CONFIG_SOURCE_CONN: TEST_CONN,
consts.CONFIG_TARGET_CONN: TEST_CONN,
consts.CONFIG_FILE: None,
consts.CONFIG_FILE_JSON: None,
} # same as VALIDATE_COLUMN_CONFIG but without the command item
BROKEN_VALIDATE_COLUMN_CONFIG_INCORRECT_COMMAND = {
"verbose": False,
"log_level": "INFO",
"command": "incorrectcommand",
"validate_cmd": "column",
"dry_run": False,
consts.CONFIG_TYPE: consts.COLUMN_VALIDATION,
consts.CONFIG_SOURCE_CONN: TEST_CONN,
consts.CONFIG_TARGET_CONN: TEST_CONN,
consts.CONFIG_FILE: None,
consts.CONFIG_FILE_JSON: None,
} # same as VALIDATE_COLUMN_CONFIG but with the command item replaced
VALIDATE_ROW_CONFIG = {
"verbose": False,
"log_level": "INFO",
"command": "validate",
"validate_cmd": "row",
"dry_run": False,
consts.CONFIG_TYPE: consts.ROW_VALIDATION,
consts.CONFIG_SOURCE_CONN: TEST_CONN,
consts.CONFIG_TARGET_CONN: TEST_CONN,
consts.CONFIG_FILE: None,
consts.CONFIG_FILE_JSON: None,
} # same as VALIDATE_COLUMN_CONFIG but with 2 items replaced
VALIDATE_CONFIG = {
"verbose": False,
"log_level": "INFO",
"command": "configs",
"validation_config_cmd": "run",
"dry_run": False,
consts.CONFIG_TYPE: consts.ROW_VALIDATION,
consts.CONFIG_FILE: "test.yaml",
"config_dir": None,
"kube_completions": None,
}
CONNECTION_LIST_ARGS = {
"verbose": False,
"log_level": "INFO",
"command": "connections",
"connect_cmd": "list",
}
CONNECTION_ADD_ARGS = {
"verbose": False,
"log_level": "INFO",
"command": "connections",
"connect_cmd": "add",
"connect_type": consts.SOURCE_TYPE_BIGQUERY,
consts.SECRET_MANAGER_TYPE: "gcp",
consts.SECRET_MANAGER_PROJECT_ID: "dummy-gcp-project",
consts.PROJECT_ID: "dummy-gcp-project",
consts.GOOGLE_SERVICE_ACCOUNT_KEY_PATH: None,
"connection_name": "dummy-bq-connection",
"api_endpoint": None,
}
CONNECTION_DESCRIBE_ARGS = {
"verbose": False,
"log_level": "INFO",
"command": "connections",
"connect_cmd": "describe",
"connection_name": "dummy-bq-connection",
"output_format": "yaml",
}
CONNECTION_DELETE_ARGS = {
"verbose": False,
"log_level": "INFO",
"command": "connections",
"connect_cmd": "delete",
"connection_name": "dummy-bq-connection",
}
BROKEN_CONNECTION_CONFIG_INCORRECT_COMMAND = {
"verbose": False,
"log_level": "INFO",
"command": "connections",
"connect_cmd": "incorrectconnectioncommand",
"connect_type": consts.SOURCE_TYPE_BIGQUERY,
consts.SECRET_MANAGER_TYPE: "gcp",
consts.SECRET_MANAGER_PROJECT_ID: "dummy-gcp-project",
consts.PROJECT_ID: "dummy-gcp-project",
consts.GOOGLE_SERVICE_ACCOUNT_KEY_PATH: None,
"connection_name": "dummy-bq-connection",
"api_endpoint": None,
} # same as CONNECTION_ADD_ARGS but with the command item replaced
FIND_TABLES_ARGS = {
"verbose": False,
"log_level": "INFO",
"command": "find-tables",
}
DEPLOY_ARGS = {
"verbose": False,
"log_level": "INFO",
"command": "deploy",
}
GENERATE_PARTITIONS_CONFIG = {
"verbose": False,
"log_level": "INFO",
"command": "generate-table-partitions",
"partition_num": 9,
"parts_per_file": 5,
"tables_list": "my_schema.my_table",
consts.CONFIG_TYPE: consts.COLUMN_VALIDATION,
}
QUERY_CONFIG = {
"verbose": False,
"log_level": "INFO",
"command": "query",
"conn": "dummy-bq-connection",
"query": "SELECT 1 AS TEST",
"output_format": consts.FORMAT_TYPE_PYTHON,
}
class MockIbisClient(object):
_source_type = consts.SOURCE_TYPE_BIGQUERY
name = "bigquery"
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CLI_ARGS),
)
def test_configure_arg_parser(mock_args):
"""Test arg parser values."""
args = cli_tools.get_parsed_args()
file_path = main._get_arg_config_file(args)
assert file_path == "example_test.yaml"
@mock.patch("data_validation.__main__.run_validations")
@mock.patch(
"data_validation.__main__.build_config_managers_from_yaml",
return_value=["config dict from one file"],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONFIG_RUNNER_ARGS_1),
)
def test_config_runner_1(mock_args, mock_build, mock_run, caplog):
"""config_runner, runs the validations, so we have to mock run_validations and examine the arguments
passed to it. Build Config Managers reads the yaml files and builds the validation configs,
which also includes creating a connection to the database. That is beyond a unit test, so mock
build_config_managers_from_yaml.
First test - run validation on a single file - and provide the -kc argument
Expected result
1. One config manager created
2. Warning about inappropriate use of -kc
Other test cases can be developed.
"""
caplog.set_level(logging.WARNING)
args = cli_tools.get_parsed_args()
caplog.clear()
main.config_runner(args)
# assert warning is seen
assert caplog.messages == [
"--kube-completions or -kc specified, which requires a config directory, however a specific config file is provided."
]
# assert that only one config manager object is present
assert len(mock_run.call_args.args[1]) == 1
@mock.patch("data_validation.__main__.run_validations")
@mock.patch(
"data_validation.__main__.build_config_managers_from_yaml",
return_value=["config dict from one file"],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONFIG_RUNNER_ARGS_2),
)
def test_config_runner_2(mock_args, mock_build, mock_run, caplog):
"""Second test - run validation on a directory - and provide the -kc argument,
but not running in a Kubernetes Completion Configuration. Expected result
1. Multiple (3) config manager created for validation
2. Warning about inappropriate use of -kc"""
caplog.set_level(logging.WARNING)
args = cli_tools.get_parsed_args()
caplog.clear()
main.config_runner(args)
# assert warning is seen
assert caplog.messages == [
"--kube-completions or -kc specified, however not running in Kubernetes Job completion, check your command line."
]
# assert that validation is called thrice, once for each file
assert mock_run.call_count == 3
@mock.patch("data_validation.__main__.run_validations")
@mock.patch(
"data_validation.__main__.build_config_managers_from_yaml",
return_value=["config dict from one file"],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONFIG_RUNNER_ARGS_3),
)
def test_config_runner_3(mock_args, mock_build, mock_run, caplog):
"""Second test - run validation on a directory - and provide the -kc argument,
have system believe it is running in a Kubernetes Completion Environment. Expected result
1. No warnings
2. run validation called as though config file is provided (config_dir is None)
3. run validation config file name corresponds to value of JOB_COMPLETION_INDEX
4. One config manager created for validation
"""
caplog.set_level(logging.WARNING)
os.environ["JOB_COMPLETION_INDEX"] = "2"
args = cli_tools.get_parsed_args()
caplog.clear()
main.config_runner(args)
# assert no warnings
assert caplog.messages == []
# assert that only one config manager and one validation corresponding to JOB_COMPLETION_INDEX is set.
assert mock_run.call_args.args[0].config_dir is None
assert os.path.basename(mock_run.call_args.args[0].config_file) == "0002.yaml"
assert len(mock_run.call_args.args[1]) == 1
@mock.patch("data_validation.__main__.run_validations")
@mock.patch(
"data_validation.__main__.build_config_managers_from_yaml",
return_value=["config dict from one file"],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONFIG_RUNNER_ARGS_4),
)
def test_config_runner_4(mock_args, mock_build, mock_run, caplog):
"""Third test - run validation on a directory with failures in one validation,
Running in a non Kube completions environment. Expected Result:
1. All 4 files are validated, even though one of them raises an exception.
2. Exception from one validation is trapped, file skipped and raised at the end.
"""
mock_run.side_effect = [10, ValueError("Boom!"), 12, 10]
caplog.set_level(logging.WARNING)
args = cli_tools.get_parsed_args()
caplog.clear()
with pytest.raises(exceptions.ValidationException) as e_info:
main.config_runner(args)
# assert that exception message was output for the failed validation
# validation is called four times, once for each file
# After all four files were validated, an exception was raised back to main to return status
assert caplog.messages[0] == CONFIG_RUNNER_EXCEPTION_TEXT.format(
"Boom!", "0001.yaml"
)
assert mock_run.call_count == 4
assert e_info.value.args[0] == "Some of the validations raised an exception"
@mock.patch("data_validation.__main__.run_validation")
@mock.patch(
"data_validation.__main__.build_config_managers_from_args",
return_value=[
config_manager.ConfigManager(
VALIDATE_COLUMN_CONFIG, MockIbisClient(), MockIbisClient(), verbose=False
)
],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**VALIDATE_COLUMN_CONFIG),
)
def test_successful_column_validation_with_mocked_run_validation(
mock_args, mock_build, mock_run
):
main.main()
@mock.patch("data_validation.__main__.run_validation")
@mock.patch(
"data_validation.__main__.build_config_managers_from_args",
return_value=[
config_manager.ConfigManager(
BROKEN_VALIDATE_COLUMN_CONFIG_MISSING_COMMAND,
MockIbisClient(),
MockIbisClient(),
verbose=False,
)
],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**BROKEN_VALIDATE_COLUMN_CONFIG_MISSING_COMMAND),
)
def test_throws_for_malformed_input_config_missing_command(
mock_args, mock_build, mock_run
):
with pytest.raises(Exception) as e_info:
main.main()
assert e_info.value.args[0] == "'Namespace' object has no attribute 'command'"
@mock.patch("data_validation.__main__.run_validation")
@mock.patch(
"data_validation.__main__.build_config_managers_from_args",
return_value=[
config_manager.ConfigManager(
BROKEN_VALIDATE_COLUMN_CONFIG_INCORRECT_COMMAND,
MockIbisClient(),
MockIbisClient(),
verbose=False,
)
],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**BROKEN_VALIDATE_COLUMN_CONFIG_INCORRECT_COMMAND),
)
def test_throws_for_malformed_input_config_incorrect_command(
mock_args, mock_build, mock_run
):
with pytest.raises(ValueError) as e_info:
main.main()
assert (
e_info.value.args[0]
== "Positional Argument 'incorrectcommand' is not supported"
)
@mock.patch("data_validation.__main__.run_validation")
@mock.patch(
"data_validation.__main__.build_config_managers_from_args",
return_value=[
config_manager.ConfigManager(
VALIDATE_ROW_CONFIG, MockIbisClient(), MockIbisClient(), verbose=False
)
],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**VALIDATE_ROW_CONFIG),
)
def test_successful_row_validation_with_mocked_run_validation(
mock_args, mock_build, mock_run
):
main.main()
@mock.patch("data_validation.__main__.run_validation")
@mock.patch(
"data_validation.__main__.build_config_managers_from_yaml",
return_value=[
config_manager.ConfigManager(
VALIDATE_CONFIG, MockIbisClient(), MockIbisClient(), verbose=False
)
],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**VALIDATE_CONFIG),
)
def test_successful_validation_config_with_mocked_run_validation(
mock_args, mock_build, mock_run
):
main.main()
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONNECTION_LIST_ARGS),
)
@mock.patch("data_validation.cli_tools.list_connections")
def test_successful_connection_list_with_mocked_list_connections(mock_args, mock_run):
main.main()
@mock.patch("data_validation.clients.get_data_client")
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONNECTION_ADD_ARGS),
)
def test_successful_connection_add_with_mocked_list_connections(mock_args, mock_run):
main.main()
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONNECTION_DESCRIBE_ARGS),
)
@mock.patch("data_validation.cli_tools.describe_connection")
def test_successful_connection_describe_with_mocked_describe_connection(
mock_args, mock_describe
):
main.main()
@mock.patch("data_validation.clients.get_data_client")
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**CONNECTION_DELETE_ARGS),
)
@mock.patch("data_validation.cli_tools.delete_connection")
def test_successful_connection_delete_with_mocked_delete_connection(
mock_args, mock_run, mock_client
):
main.main()
@mock.patch("data_validation.clients.get_data_client")
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**BROKEN_CONNECTION_CONFIG_INCORRECT_COMMAND),
)
def test_throws_for_malformed_input_connection_config_incorrect_command(
mock_args, mock_run
):
with pytest.raises(ValueError) as e_info:
main.main()
assert (
e_info.value.args[0]
== "Connections Argument 'incorrectconnectioncommand' is not supported"
)
@mock.patch("data_validation.__main__.find_tables_using_string_matching")
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**FIND_TABLES_ARGS),
)
def test_successful_find_tables_with_mock(mock_args, mock_run):
main.main()
@mock.patch("data_validation.app.app.run")
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**DEPLOY_ARGS),
)
def test_successful_deploy_with_mocked_app_run(mock_args, mock_run):
main.main()
@mock.patch("data_validation.__main__.PartitionBuilder")
@mock.patch(
"data_validation.__main__.build_config_managers_from_args",
return_value=[
config_manager.ConfigManager(
GENERATE_PARTITIONS_CONFIG,
MockIbisClient(),
MockIbisClient(),
verbose=False,
)
],
)
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**GENERATE_PARTITIONS_CONFIG),
)
def test_successful_generate_partitions_with_mocked_partition_builder(
mock_args, mock_build, mock_run
):
main.main()
@mock.patch("data_validation.clients.get_data_client")
@mock.patch(
"argparse.ArgumentParser.parse_args",
return_value=argparse.Namespace(**QUERY_CONFIG),
)
def test_successful_query_with_mocked_get_data_client(mock_args, mock_run):
main.main()