6060
6161from google .cloud .bigquery import job as bqjob
6262import google .cloud .bigquery ._job_helpers
63- from google .cloud .bigquery .dataset import DatasetReference
64- from google .cloud .bigquery .enums import DatasetView
63+ from google .cloud .bigquery .dataset import DatasetReference , Dataset
64+ from google .cloud .bigquery .enums import UpdateMode , DatasetView
6565from google .cloud .bigquery import exceptions
6666from google .cloud .bigquery import ParquetOptions
6767import google .cloud .bigquery .retry
@@ -2168,6 +2168,7 @@ def test_update_dataset(self):
21682168 },
21692169 path = "/" + PATH ,
21702170 timeout = 7.5 ,
2171+ query_params = {},
21712172 )
21722173 self .assertEqual (ds2 .description , ds .description )
21732174 self .assertEqual (ds2 .friendly_name , ds .friendly_name )
@@ -2181,6 +2182,94 @@ def test_update_dataset(self):
21812182 client .update_dataset (ds , [])
21822183 req = conn .api_request .call_args
21832184 self .assertEqual (req [1 ]["headers" ]["If-Match" ], "etag" )
2185+ self .assertEqual (req [1 ].get ("query_params" ), {})
2186+
2187+ def test_update_dataset_w_update_mode (self ):
2188+ PATH = f"projects/{ self .PROJECT } /datasets/{ self .DS_ID } "
2189+ creds = _make_credentials ()
2190+ client = self ._make_one (project = self .PROJECT , credentials = creds )
2191+
2192+ DESCRIPTION = "DESCRIPTION"
2193+ RESOURCE = {
2194+ "datasetReference" : {"projectId" : self .PROJECT , "datasetId" : self .DS_ID },
2195+ "etag" : "etag" ,
2196+ "description" : DESCRIPTION ,
2197+ }
2198+ dataset_ref = DatasetReference (self .PROJECT , self .DS_ID )
2199+ orig_dataset = Dataset (dataset_ref )
2200+ orig_dataset .description = DESCRIPTION
2201+ filter_fields = ["description" ]
2202+
2203+ test_cases = [
2204+ (None , None ),
2205+ (UpdateMode .UPDATE_MODE_UNSPECIFIED , "UPDATE_MODE_UNSPECIFIED" ),
2206+ (UpdateMode .UPDATE_METADATA , "UPDATE_METADATA" ),
2207+ (UpdateMode .UPDATE_ACL , "UPDATE_ACL" ),
2208+ (UpdateMode .UPDATE_FULL , "UPDATE_FULL" ),
2209+ ]
2210+
2211+ for update_mode_arg , expected_param_value in test_cases :
2212+ with self .subTest (
2213+ update_mode_arg = update_mode_arg ,
2214+ expected_param_value = expected_param_value ,
2215+ ):
2216+ conn = client ._connection = make_connection (RESOURCE , RESOURCE )
2217+
2218+ new_dataset = client .update_dataset (
2219+ orig_dataset ,
2220+ fields = filter_fields ,
2221+ update_mode = update_mode_arg ,
2222+ )
2223+ self .assertEqual (orig_dataset .description , new_dataset .description )
2224+
2225+ if expected_param_value :
2226+ expected_query_params = {"updateMode" : expected_param_value }
2227+ else :
2228+ expected_query_params = {}
2229+
2230+ conn .api_request .assert_called_once_with (
2231+ method = "PATCH" ,
2232+ path = "/" + PATH ,
2233+ data = {"description" : DESCRIPTION },
2234+ timeout = DEFAULT_TIMEOUT ,
2235+ query_params = expected_query_params if expected_query_params else {},
2236+ )
2237+
2238+ def test_update_dataset_w_invalid_update_mode (self ):
2239+ creds = _make_credentials ()
2240+ client = self ._make_one (project = self .PROJECT , credentials = creds )
2241+
2242+ DESCRIPTION = "DESCRIPTION"
2243+ resource = {
2244+ "datasetReference" : {"projectId" : self .PROJECT , "datasetId" : self .DS_ID },
2245+ "etag" : "etag" ,
2246+ }
2247+
2248+ dataset_ref = DatasetReference (self .PROJECT , self .DS_ID )
2249+ orig_dataset = Dataset (dataset_ref )
2250+ orig_dataset .description = DESCRIPTION
2251+ filter_fields = ["description" ] # A non-empty list of fields is required
2252+
2253+ # Mock the connection to prevent actual API calls
2254+ # and to provide a minimal valid response if the call were to proceed.
2255+ conn = client ._connection = make_connection (resource )
2256+
2257+ test_cases = [
2258+ "INVALID_STRING" ,
2259+ 123 ,
2260+ 123.45 ,
2261+ object (),
2262+ ]
2263+
2264+ for invalid_update_mode in test_cases :
2265+ with self .subTest (invalid_update_mode = invalid_update_mode ):
2266+ conn .api_request .reset_mock () # Reset mock for each sub-test
2267+ with self .assertRaises (AttributeError ):
2268+ client .update_dataset (
2269+ orig_dataset ,
2270+ fields = filter_fields ,
2271+ update_mode = invalid_update_mode ,
2272+ )
21842273
21852274 def test_update_dataset_w_custom_property (self ):
21862275 # The library should handle sending properties to the API that are not
@@ -2212,6 +2301,7 @@ def test_update_dataset_w_custom_property(self):
22122301 data = {"newAlphaProperty" : "unreleased property" },
22132302 path = path ,
22142303 timeout = DEFAULT_TIMEOUT ,
2304+ query_params = {},
22152305 )
22162306
22172307 self .assertEqual (dataset .dataset_id , self .DS_ID )
0 commit comments