This repository was archived by the owner on Mar 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathtest_list_datasets.py
More file actions
125 lines (105 loc) · 4.01 KB
/
test_list_datasets.py
File metadata and controls
125 lines (105 loc) · 4.01 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
# Copyright 2021 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
#
# https://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 mock
import pytest
from google.cloud.bigquery.retry import DEFAULT_TIMEOUT
from .helpers import make_connection
@pytest.mark.parametrize(
"extra,query", [({}, {}), (dict(page_size=42), dict(maxResults=42))]
)
def test_list_datasets_defaults(client, PROJECT, extra, query):
from google.cloud.bigquery.dataset import DatasetListItem
DATASET_1 = "dataset_one"
DATASET_2 = "dataset_two"
PATH = "projects/%s/datasets" % PROJECT
TOKEN = "TOKEN"
DATA = {
"nextPageToken": TOKEN,
"datasets": [
{
"kind": "bigquery#dataset",
"id": "%s:%s" % (PROJECT, DATASET_1),
"datasetReference": {"datasetId": DATASET_1, "projectId": PROJECT},
"friendlyName": None,
},
{
"kind": "bigquery#dataset",
"id": "%s:%s" % (PROJECT, DATASET_2),
"datasetReference": {"datasetId": DATASET_2, "projectId": PROJECT},
"friendlyName": "Two",
},
],
}
conn = client._connection = make_connection(DATA)
iterator = client.list_datasets(**extra)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = next(iterator.pages)
final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
datasets = list(page)
token = iterator.next_page_token
assert len(datasets) == len(DATA["datasets"])
for found, expected in zip(datasets, DATA["datasets"]):
assert isinstance(found, DatasetListItem)
assert found.full_dataset_id == expected["id"]
assert found.friendly_name == expected["friendlyName"]
assert token == TOKEN
conn.api_request.assert_called_once_with(
method="GET", path="/%s" % PATH, query_params=query, timeout=DEFAULT_TIMEOUT
)
def test_list_datasets_w_project_and_timeout(client, PROJECT):
conn = client._connection = make_connection({})
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
list(client.list_datasets(project="other-project", timeout=7.5))
final_attributes.assert_called_once_with(
{"path": "/projects/other-project/datasets"}, client, None
)
conn.api_request.assert_called_once_with(
method="GET",
path="/projects/other-project/datasets",
query_params={},
timeout=7.5,
)
def test_list_datasets_explicit_response_missing_datasets_key(client, PROJECT):
PATH = "projects/%s/datasets" % PROJECT
TOKEN = "TOKEN"
FILTER = "FILTER"
DATA = {}
conn = client._connection = make_connection(DATA)
iterator = client.list_datasets(
include_all=True, filter=FILTER, max_results=3, page_token=TOKEN
)
with mock.patch(
"google.cloud.bigquery.opentelemetry_tracing._get_final_span_attributes"
) as final_attributes:
page = next(iterator.pages)
final_attributes.assert_called_once_with({"path": "/%s" % PATH}, client, None)
datasets = list(page)
token = iterator.next_page_token
assert len(datasets) == 0
assert token is None
conn.api_request.assert_called_once_with(
method="GET",
path="/%s" % PATH,
query_params={
"all": True,
"filter": FILTER,
"maxResults": 3,
"pageToken": TOKEN,
},
timeout=DEFAULT_TIMEOUT,
)