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 , DatasetView
6465from google .cloud .bigquery import exceptions
6566from google .cloud .bigquery import ParquetOptions
6667import google .cloud .bigquery .retry
@@ -752,7 +753,7 @@ def test_get_dataset(self):
752753 final_attributes .assert_called_once_with ({"path" : "/%s" % path }, client , None )
753754
754755 conn .api_request .assert_called_once_with (
755- method = "GET" , path = "/%s" % path , timeout = 7.5
756+ method = "GET" , path = "/%s" % path , timeout = 7.5 , query_params = {}
756757 )
757758 self .assertEqual (dataset .dataset_id , self .DS_ID )
758759
@@ -818,6 +819,72 @@ def test_get_dataset(self):
818819
819820 self .assertEqual (dataset .dataset_id , self .DS_ID )
820821
822+ def test_get_dataset_with_dataset_view (self ):
823+ path = "projects/%s/datasets/%s" % (self .PROJECT , self .DS_ID )
824+ creds = _make_credentials ()
825+ http = object ()
826+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
827+ resource = {
828+ "id" : "%s:%s" % (self .PROJECT , self .DS_ID ),
829+ "datasetReference" : {"projectId" : self .PROJECT , "datasetId" : self .DS_ID },
830+ }
831+ dataset_ref = DatasetReference (self .PROJECT , self .DS_ID )
832+
833+ test_cases = [
834+ (None , None ),
835+ (DatasetView .DATASET_VIEW_UNSPECIFIED , "DATASET_VIEW_UNSPECIFIED" ),
836+ (DatasetView .METADATA , "METADATA" ),
837+ (DatasetView .ACL , "ACL" ),
838+ (DatasetView .FULL , "FULL" ),
839+ ]
840+
841+ for dataset_view_arg , expected_param_value in test_cases :
842+ with self .subTest (
843+ dataset_view_arg = dataset_view_arg ,
844+ expected_param_value = expected_param_value ,
845+ ):
846+ # Re-initialize the connection mock for each sub-test to reset side_effect
847+ conn = client ._connection = make_connection (resource )
848+
849+ dataset = client .get_dataset (dataset_ref , dataset_view = dataset_view_arg )
850+
851+ self .assertEqual (dataset .dataset_id , self .DS_ID )
852+
853+ if expected_param_value :
854+ expected_query_params = {"datasetView" : expected_param_value }
855+ else :
856+ expected_query_params = {}
857+
858+ conn .api_request .assert_called_once_with (
859+ method = "GET" ,
860+ path = "/%s" % path ,
861+ timeout = DEFAULT_TIMEOUT ,
862+ query_params = expected_query_params if expected_query_params else {},
863+ )
864+
865+ def test_get_dataset_with_invalid_dataset_view (self ):
866+ invalid_view_values = [
867+ "INVALID_STRING" ,
868+ 123 ,
869+ 123.45 ,
870+ object (),
871+ ]
872+ creds = _make_credentials ()
873+ http = object ()
874+ client = self ._make_one (project = self .PROJECT , credentials = creds , _http = http )
875+ resource = {
876+ "id" : "%s:%s" % (self .PROJECT , self .DS_ID ),
877+ "datasetReference" : {"projectId" : self .PROJECT , "datasetId" : self .DS_ID },
878+ }
879+ conn = client ._connection = make_connection (resource )
880+ dataset_ref = DatasetReference (self .PROJECT , self .DS_ID )
881+
882+ for invalid_view_value in invalid_view_values :
883+ with self .subTest (invalid_view_value = invalid_view_value ):
884+ conn .api_request .reset_mock () # Reset mock for each sub-test
885+ with self .assertRaises (AttributeError ):
886+ client .get_dataset (dataset_ref , dataset_view = invalid_view_value )
887+
821888 def test_ensure_bqstorage_client_creating_new_instance (self ):
822889 bigquery_storage = pytest .importorskip ("google.cloud.bigquery_storage" )
823890
@@ -2101,6 +2168,7 @@ def test_update_dataset(self):
21012168 },
21022169 path = "/" + PATH ,
21032170 timeout = 7.5 ,
2171+ query_params = {},
21042172 )
21052173 self .assertEqual (ds2 .description , ds .description )
21062174 self .assertEqual (ds2 .friendly_name , ds .friendly_name )
@@ -2114,6 +2182,94 @@ def test_update_dataset(self):
21142182 client .update_dataset (ds , [])
21152183 req = conn .api_request .call_args
21162184 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+ )
21172273
21182274 def test_update_dataset_w_custom_property (self ):
21192275 # The library should handle sending properties to the API that are not
@@ -2145,6 +2301,7 @@ def test_update_dataset_w_custom_property(self):
21452301 data = {"newAlphaProperty" : "unreleased property" },
21462302 path = path ,
21472303 timeout = DEFAULT_TIMEOUT ,
2304+ query_params = {},
21482305 )
21492306
21502307 self .assertEqual (dataset .dataset_id , self .DS_ID )
0 commit comments