6060
6161from google .cloud .bigquery import job as bqjob
6262import google .cloud .bigquery ._job_helpers
63- from google .cloud .bigquery .dataset import DatasetReference
63+ from google .cloud .bigquery .dataset import DatasetReference , Dataset
64+ from google .cloud .bigquery .enums import UpdateMode
6465from google .cloud .bigquery import exceptions
6566from google .cloud .bigquery import ParquetOptions
6667import google .cloud .bigquery .retry
@@ -2101,6 +2102,7 @@ def test_update_dataset(self):
21012102 },
21022103 path = "/" + PATH ,
21032104 timeout = 7.5 ,
2105+ query_params = {},
21042106 )
21052107 self .assertEqual (ds2 .description , ds .description )
21062108 self .assertEqual (ds2 .friendly_name , ds .friendly_name )
@@ -2114,6 +2116,94 @@ def test_update_dataset(self):
21142116 client .update_dataset (ds , [])
21152117 req = conn .api_request .call_args
21162118 self .assertEqual (req [1 ]["headers" ]["If-Match" ], "etag" )
2119+ self .assertEqual (req [1 ].get ("query_params" ), {})
2120+
2121+ def test_update_dataset_w_update_mode (self ):
2122+ PATH = f"projects/{ self .PROJECT } /datasets/{ self .DS_ID } "
2123+ creds = _make_credentials ()
2124+ client = self ._make_one (project = self .PROJECT , credentials = creds )
2125+
2126+ DESCRIPTION = "DESCRIPTION"
2127+ RESOURCE = {
2128+ "datasetReference" : {"projectId" : self .PROJECT , "datasetId" : self .DS_ID },
2129+ "etag" : "etag" ,
2130+ "description" : DESCRIPTION ,
2131+ }
2132+ dataset_ref = DatasetReference (self .PROJECT , self .DS_ID )
2133+ orig_dataset = Dataset (dataset_ref )
2134+ orig_dataset .description = DESCRIPTION
2135+ filter_fields = ["description" ]
2136+
2137+ test_cases = [
2138+ (None , None ),
2139+ (UpdateMode .UPDATE_MODE_UNSPECIFIED , "UPDATE_MODE_UNSPECIFIED" ),
2140+ (UpdateMode .UPDATE_METADATA , "UPDATE_METADATA" ),
2141+ (UpdateMode .UPDATE_ACL , "UPDATE_ACL" ),
2142+ (UpdateMode .UPDATE_FULL , "UPDATE_FULL" ),
2143+ ]
2144+
2145+ for update_mode_arg , expected_param_value in test_cases :
2146+ with self .subTest (
2147+ update_mode_arg = update_mode_arg ,
2148+ expected_param_value = expected_param_value ,
2149+ ):
2150+ conn = client ._connection = make_connection (RESOURCE , RESOURCE )
2151+
2152+ new_dataset = client .update_dataset (
2153+ orig_dataset ,
2154+ fields = filter_fields ,
2155+ update_mode = update_mode_arg ,
2156+ )
2157+ self .assertEqual (orig_dataset .description , new_dataset .description )
2158+
2159+ if expected_param_value :
2160+ expected_query_params = {"updateMode" : expected_param_value }
2161+ else :
2162+ expected_query_params = {}
2163+
2164+ conn .api_request .assert_called_once_with (
2165+ method = "PATCH" ,
2166+ path = "/" + PATH ,
2167+ data = {"description" : DESCRIPTION },
2168+ timeout = DEFAULT_TIMEOUT ,
2169+ query_params = expected_query_params if expected_query_params else {},
2170+ )
2171+
2172+ def test_update_dataset_w_invalid_update_mode (self ):
2173+ creds = _make_credentials ()
2174+ client = self ._make_one (project = self .PROJECT , credentials = creds )
2175+
2176+ DESCRIPTION = "DESCRIPTION"
2177+ resource = {
2178+ "datasetReference" : {"projectId" : self .PROJECT , "datasetId" : self .DS_ID },
2179+ "etag" : "etag" ,
2180+ }
2181+
2182+ dataset_ref = DatasetReference (self .PROJECT , self .DS_ID )
2183+ orig_dataset = Dataset (dataset_ref )
2184+ orig_dataset .description = DESCRIPTION
2185+ filter_fields = ["description" ] # A non-empty list of fields is required
2186+
2187+ # Mock the connection to prevent actual API calls
2188+ # and to provide a minimal valid response if the call were to proceed.
2189+ conn = client ._connection = make_connection (resource )
2190+
2191+ test_cases = [
2192+ "INVALID_STRING" ,
2193+ 123 ,
2194+ 123.45 ,
2195+ object (),
2196+ ]
2197+
2198+ for invalid_update_mode in test_cases :
2199+ with self .subTest (invalid_update_mode = invalid_update_mode ):
2200+ conn .api_request .reset_mock () # Reset mock for each sub-test
2201+ with self .assertRaises (AttributeError ):
2202+ client .update_dataset (
2203+ orig_dataset ,
2204+ fields = filter_fields ,
2205+ update_mode = invalid_update_mode ,
2206+ )
21172207
21182208 def test_update_dataset_w_custom_property (self ):
21192209 # The library should handle sending properties to the API that are not
@@ -2145,6 +2235,7 @@ def test_update_dataset_w_custom_property(self):
21452235 data = {"newAlphaProperty" : "unreleased property" },
21462236 path = path ,
21472237 timeout = DEFAULT_TIMEOUT ,
2238+ query_params = {},
21482239 )
21492240
21502241 self .assertEqual (dataset .dataset_id , self .DS_ID )
0 commit comments