Skip to content

Commit 059fd9a

Browse files
feat: Add UpdateMode to update_dataset
This commit introduces the `UpdateMode` enum and integrates it into the `update_dataset` method in the BigQuery client. The `UpdateMode` enum allows you to specify which parts of a dataset should be updated (metadata, ACL, or full update). The following changes were made: - Defined the `UpdateMode` enum in `google/cloud/bigquery/enums.py` with values: `UPDATE_MODE_UNSPECIFIED`, `UPDATE_METADATA`, `UPDATE_ACL`, and `UPDATE_FULL`. - Modified the `update_dataset` method in `google/cloud/bigquery/client.py` to accept an optional `update_mode` parameter. This parameter is added to the query parameters if provided. - Added unit tests in `tests/unit/test_client.py` to verify the correct handling of the `update_mode` parameter, including testing all enum values and the default case where it's not provided.
1 parent d92b487 commit 059fd9a

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

google/cloud/bigquery/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,7 @@ def update_dataset(
11981198
fields: Sequence[str],
11991199
retry: retries.Retry = DEFAULT_RETRY,
12001200
timeout: TimeoutType = DEFAULT_TIMEOUT,
1201+
update_mode: Optional[enums.UpdateMode] = None,
12011202
) -> Dataset:
12021203
"""Change some fields of a dataset.
12031204
@@ -1249,6 +1250,9 @@ def update_dataset(
12491250
headers = None
12501251
path = dataset.path
12511252
span_attributes = {"path": path, "fields": fields}
1253+
query_params: Dict[str, Any] = {}
1254+
if update_mode is not None:
1255+
query_params["updateMode"] = str(update_mode.value)
12521256

12531257
api_response = self._call_api(
12541258
retry,
@@ -1259,6 +1263,7 @@ def update_dataset(
12591263
data=partial,
12601264
headers=headers,
12611265
timeout=timeout,
1266+
query_params=query_params if query_params else None,
12621267
)
12631268
return Dataset.from_api_repr(api_response)
12641269

google/cloud/bigquery/enums.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,24 @@ class BigLakeTableFormat(object):
409409
"""Apache Iceberg format."""
410410

411411

412+
class UpdateMode(str, enum.Enum):
413+
"""Specifies the kind of information to update in a dataset."""
414+
415+
UPDATE_MODE_UNSPECIFIED = "UPDATE_MODE_UNSPECIFIED"
416+
"""The default value. Default to the UPDATE_FULL."""
417+
418+
UPDATE_METADATA = "UPDATE_METADATA"
419+
"""Includes metadata information for the dataset, such as friendlyName,
420+
description, labels, etc."""
421+
422+
UPDATE_ACL = "UPDATE_ACL"
423+
"""Includes ACL information for the dataset, which defines dataset access
424+
for one or more entities."""
425+
426+
UPDATE_FULL = "UPDATE_FULL"
427+
"""Includes both dataset metadata and ACL information."""
428+
429+
412430
class JobCreationMode(object):
413431
"""Documented values for Job Creation Mode."""
414432

tests/unit/test_client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,56 @@ def test_update_dataset(self):
21142114
client.update_dataset(ds, [])
21152115
req = conn.api_request.call_args
21162116
self.assertEqual(req[1]["headers"]["If-Match"], "etag")
2117+
self.assertIsNone(req[1].get("query_params"))
2118+
2119+
def test_update_dataset_w_update_mode(self):
2120+
from google.cloud.bigquery.dataset import Dataset
2121+
from google.cloud.bigquery import enums
2122+
2123+
PATH = "projects/%s/datasets/%s" % (self.PROJECT, self.DS_ID)
2124+
DESCRIPTION = "DESCRIPTION"
2125+
RESOURCE = {
2126+
"datasetReference": {"projectId": self.PROJECT, "datasetId": self.DS_ID},
2127+
"etag": "etag",
2128+
"description": DESCRIPTION,
2129+
}
2130+
creds = _make_credentials()
2131+
client = self._make_one(project=self.PROJECT, credentials=creds)
2132+
ds = Dataset(DatasetReference(self.PROJECT, self.DS_ID))
2133+
ds.description = DESCRIPTION
2134+
filter_fields = ["description"]
2135+
2136+
# Test each UpdateMode enum value
2137+
for update_mode_enum in enums.UpdateMode:
2138+
conn = client._connection = make_connection(RESOURCE)
2139+
ds2 = client.update_dataset(
2140+
ds,
2141+
fields=filter_fields,
2142+
update_mode=update_mode_enum,
2143+
)
2144+
self.assertEqual(ds2.description, ds.description)
2145+
conn.api_request.assert_called_once_with(
2146+
method="PATCH",
2147+
data={"description": DESCRIPTION},
2148+
path="/" + PATH,
2149+
timeout=DEFAULT_TIMEOUT,
2150+
query_params={"updateMode": str(update_mode_enum.value)},
2151+
)
2152+
2153+
# Test when update_mode is not provided
2154+
conn = client._connection = make_connection(RESOURCE)
2155+
ds2 = client.update_dataset(
2156+
ds,
2157+
fields=filter_fields,
2158+
)
2159+
self.assertEqual(ds2.description, ds.description)
2160+
conn.api_request.assert_called_once_with(
2161+
method="PATCH",
2162+
data={"description": DESCRIPTION},
2163+
path="/" + PATH,
2164+
timeout=DEFAULT_TIMEOUT,
2165+
query_params=None, # Expect None or empty dict when not provided
2166+
)
21172167

21182168
def test_update_dataset_w_custom_property(self):
21192169
# The library should handle sending properties to the API that are not

0 commit comments

Comments
 (0)