- Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathtest_connection.py
73 lines (53 loc) · 2.09 KB
/
test_connection.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
""" Given a connection name, obtain all supported tests, and run these test
$ python test_connection.py <connection_name>
Example:
$ python test_connection.py thelook
Notes: Connections to Looker's internal database cannot be tested.
Last modified: Feb 27 2024
"""
fromfunctoolsimportreduce
importsys
fromtypingimportcast, MutableSequence, Sequence
importlooker_sdk
fromlooker_sdkimportmodels40asmodels
sdk=looker_sdk.init40("../../looker.ini")
defmain():
connection_name=sys.argv[1] iflen(sys.argv) >1else""
ifnotconnection_name:
raiseException("Please provide a connection name")
elifconnection_namein ["looker", "looker__internal__analytics"]:
raiseException(
f"Connection '{connection_name}' is internal and cannot be tested."
)
connection=get_connections(connection_name)
results=test_connection(connection)
output_results(cast(str, connection.name), results)
defget_connections(name: str) ->models.DBConnection:
connection=sdk.connection(name, fields="name, dialect")
returnconnection
deftest_connection(
connection: models.DBConnection,
) ->Sequence[models.DBConnectionTestResult]:
"""Run supported tests against a given connection."""
assertconnection.name
assertconnection.dialectandconnection.dialect.connection_tests
supported_tests: MutableSequence[str] =list(connection.dialect.connection_tests)
test_results=sdk.test_connection(
connection.name, models.DelimSequence(supported_tests)
)
returntest_results
defoutput_results(
connection_name: str, test_results: Sequence[models.DBConnectionTestResult]
):
"""Prints connection test results."""
errors=list(filter(lambdatest: cast(str, test.status) =="error", test_results))
iferrors:
report=reduce(
lambdafailures, error: failures+f"\n - {error.message}",
errors,
f"{connection_name}:",
)
else:
report=f"All tests for connection '{connection_name}' were successful."
print(report)
main()