forked from dremio/dbt-dremio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_connection.py
More file actions
70 lines (59 loc) · 2.8 KB
/
test_connection.py
File metadata and controls
70 lines (59 loc) · 2.8 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
# Copyright (C) 2022 Dremio Corporation
# 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 pytest
from unittest.mock import MagicMock, patch
from dbt_common.exceptions import DbtRuntimeError
from dbt.adapters.dremio.api.rest.error import (
DremioAlreadyExistsException,
DremioRequestTimeoutException,
)
from dbt.adapters.dremio.connections import DremioConnectionManager
class TestRetryConnection:
@patch("dbt.adapters.dremio.api.rest.client._post")
@patch("dbt.adapters.contracts.connection.Connection")
# When you nest patch decorators the mocks are passed in to the decorated function in bottom up order.
def test_connection_retry(
self,
mocked_connection_obj,
mocked_post_func,
):
# Arrange
TOTAL_CONNECTION_ATTEMPTS = (
DremioConnectionManager.DEFAULT_CONNECTION_RETRIES + 1
)
mocked_connection_obj.credentials.software_host = ""
mocked_connection_obj.credentials.cloud_host = None
mocked_connection_obj.credentials.port = 9047
mocked_connection_obj.credentials.use_ssl = False
mocked_post_func.side_effect = DremioRequestTimeoutException(
msg="Request timeout: Test",
original_exception="408 original exception",
)
# Act
with pytest.raises(DbtRuntimeError):
DremioConnectionManager.open(connection=mocked_connection_obj)
# Assert
assert mocked_post_func.call_count == TOTAL_CONNECTION_ATTEMPTS
class TestCreateFolders:
def test_create_folders_swallows_already_exists(self):
# Guards the integration path: _check_error normalizes Cloud's
# 400 "already exists" to DremioAlreadyExistsException, which the
# existing handler in _create_folders is responsible for swallowing.
mgr = DremioConnectionManager.__new__(DremioConnectionManager)
mgr._make_new_folder_json = MagicMock(return_value="{}")
rest_client = MagicMock()
rest_client.create_catalog_api.side_effect = DremioAlreadyExistsException(
msg="Already exists:",
original_exception="400 Client Error: Bad Request",
)
mgr._create_folders("mySource", "staging.subfolder", rest_client)
# Both folders in the path attempted; both swallowed
assert rest_client.create_catalog_api.call_count == 2