-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtest_cloud_workspace_error_handling.py
More file actions
154 lines (135 loc) · 4.53 KB
/
test_cloud_workspace_error_handling.py
File metadata and controls
154 lines (135 loc) · 4.53 KB
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
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
"""Unit tests for CloudWorkspace error handling in list operations."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from airbyte_api.errors import SDKError
from airbyte.cloud.workspaces import _is_corrupted_resource_error
@pytest.mark.parametrize(
"error_body,expected",
[
pytest.param(
'{"message":"Secret reference 0c646a6d-8cfa-4c97-9615-86d8ad3bbaf8 '
'does not exist but is referenced in the config"}',
True,
id="secret_reference_error",
),
pytest.param(
'{"message":"Internal server error"}',
False,
id="unrelated_500_error",
),
pytest.param(
'{"message":"Secret reference abc123"}',
False,
id="partial_match_secret_only",
),
pytest.param(
'{"message":"does not exist but is referenced in the config"}',
False,
id="partial_match_config_only",
),
pytest.param(
"",
False,
id="empty_body",
),
pytest.param(
None,
False,
id="none_body",
),
],
)
def test_is_corrupted_resource_error(error_body: str | None, expected: bool) -> None:
"""Test that _is_corrupted_resource_error correctly identifies corrupted resource errors."""
error = MagicMock(spec=SDKError)
error.body = error_body
assert _is_corrupted_resource_error(error) is expected
@pytest.mark.parametrize(
"list_method,api_mock_path",
[
pytest.param(
"list_destinations",
"airbyte.cloud.workspaces.api_util.list_destinations",
id="list_destinations",
),
pytest.param(
"list_sources",
"airbyte.cloud.workspaces.api_util.list_sources",
id="list_sources",
),
pytest.param(
"list_connections",
"airbyte.cloud.workspaces.api_util.list_connections",
id="list_connections",
),
],
)
def test_list_operations_return_empty_on_corrupted_resource(
list_method: str, api_mock_path: str
) -> None:
"""List operations should return empty list when corrupted resource error occurs."""
corrupted_error = SDKError(
message="API error occurred",
status_code=500,
body=(
'{"message":"Secret reference 0c646a6d-8cfa-4c97-9615-86d8ad3bbaf8 '
'does not exist but is referenced in the config"}'
),
raw_response=MagicMock(),
)
with patch(api_mock_path) as mock_list:
mock_list.side_effect = corrupted_error
from airbyte.cloud.workspaces import CloudWorkspace
with patch.object(CloudWorkspace, "__post_init__"):
workspace = CloudWorkspace(
workspace_id="test-workspace-id",
client_id="test-client-id",
client_secret="test-client-secret",
)
method = getattr(workspace, list_method)
result = method()
assert result == []
@pytest.mark.parametrize(
"list_method,api_mock_path",
[
pytest.param(
"list_destinations",
"airbyte.cloud.workspaces.api_util.list_destinations",
id="list_destinations",
),
pytest.param(
"list_sources",
"airbyte.cloud.workspaces.api_util.list_sources",
id="list_sources",
),
pytest.param(
"list_connections",
"airbyte.cloud.workspaces.api_util.list_connections",
id="list_connections",
),
],
)
def test_list_operations_raise_on_other_errors(
list_method: str, api_mock_path: str
) -> None:
"""List operations should raise for non-corrupted-resource errors."""
other_error = SDKError(
message="API error occurred",
status_code=500,
body='{"message":"Internal server error"}',
raw_response=MagicMock(),
)
with patch(api_mock_path) as mock_list:
mock_list.side_effect = other_error
from airbyte.cloud.workspaces import CloudWorkspace
with patch.object(CloudWorkspace, "__post_init__"):
workspace = CloudWorkspace(
workspace_id="test-workspace-id",
client_id="test-client-id",
client_secret="test-client-secret",
)
method = getattr(workspace, list_method)
with pytest.raises(SDKError):
method()