forked from googleapis/python-bigquery-dataframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_clients.py
More file actions
114 lines (94 loc) · 4.15 KB
/
test_clients.py
File metadata and controls
114 lines (94 loc) · 4.15 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
# Copyright 2023 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.
from typing import Optional
import unittest.mock as mock
import google.api_core.client_info
import google.api_core.client_options
import google.api_core.exceptions
import google.api_core.gapic_v1.client_info
import google.auth.credentials
import google.cloud.bigquery
import google.cloud.bigquery_connection_v1
import google.cloud.bigquery_storage_v1
import google.cloud.functions_v2
import google.cloud.resourcemanager_v3
import bigframes.session.clients as clients
import bigframes.version
def create_clients_provider(application_name: Optional[str] = None):
credentials = mock.create_autospec(google.auth.credentials.Credentials)
return clients.ClientsProvider(
project="test-project",
location="test-region",
use_regional_endpoints=False,
credentials=credentials,
application_name=application_name,
)
def monkeypatch_client_constructors(monkeypatch):
bqclient = mock.create_autospec(google.cloud.bigquery.Client)
bqclient.return_value = bqclient
monkeypatch.setattr(google.cloud.bigquery, "Client", bqclient)
bqconnectionclient = mock.create_autospec(
google.cloud.bigquery_connection_v1.ConnectionServiceClient
)
bqconnectionclient.return_value = bqconnectionclient
monkeypatch.setattr(
google.cloud.bigquery_connection_v1,
"ConnectionServiceClient",
bqconnectionclient,
)
bqstoragereadclient = mock.create_autospec(
google.cloud.bigquery_storage_v1.BigQueryReadClient
)
bqstoragereadclient.return_value = bqstoragereadclient
monkeypatch.setattr(
google.cloud.bigquery_storage_v1, "BigQueryReadClient", bqstoragereadclient
)
cloudfunctionsclient = mock.create_autospec(
google.cloud.functions_v2.FunctionServiceClient
)
cloudfunctionsclient.return_value = cloudfunctionsclient
monkeypatch.setattr(
google.cloud.functions_v2, "FunctionServiceClient", cloudfunctionsclient
)
resourcemanagerclient = mock.create_autospec(
google.cloud.resourcemanager_v3.ProjectsClient
)
resourcemanagerclient.return_value = resourcemanagerclient
monkeypatch.setattr(
google.cloud.resourcemanager_v3, "ProjectsClient", resourcemanagerclient
)
def assert_constructed_w_user_agent(mock_client: mock.Mock, expected_user_agent: str):
assert (
expected_user_agent
in mock_client.call_args.kwargs["client_info"].to_user_agent()
)
def assert_clients_w_user_agent(
provider: clients.ClientsProvider, expected_user_agent: str
):
assert_constructed_w_user_agent(provider.bqclient, expected_user_agent)
assert_constructed_w_user_agent(provider.bqconnectionclient, expected_user_agent)
assert_constructed_w_user_agent(provider.bqstoragereadclient, expected_user_agent)
assert_constructed_w_user_agent(provider.cloudfunctionsclient, expected_user_agent)
assert_constructed_w_user_agent(provider.resourcemanagerclient, expected_user_agent)
def test_user_agent_default(monkeypatch):
monkeypatch_client_constructors(monkeypatch)
provider = create_clients_provider(application_name=None)
assert_clients_w_user_agent(provider, f"bigframes/{bigframes.version.__version__}")
def test_user_agent_custom(monkeypatch):
monkeypatch_client_constructors(monkeypatch)
provider = create_clients_provider(application_name="(gpn:testpartner;)")
assert_clients_w_user_agent(provider, "(gpn:testpartner;)")
# We still need to include attribution to bigframes, even if there's also a
# partner using the package.
assert_clients_w_user_agent(provider, f"bigframes/{bigframes.version.__version__}")