@@ -7373,19 +7373,22 @@ def test_load_table_from_dataframe_w_nullable_int64_datatype_automatic_schema(se
73737373
73747374 @unittest .skipIf (pandas is None , "Requires `pandas`" )
73757375 @unittest .skipIf (pyarrow is None , "Requires `pyarrow`" )
7376- def test_load_table_from_dataframe_struct_fields_error (self ):
7376+ def test_load_table_from_dataframe_struct_fields (self ):
7377+ from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
73777378 from google .cloud .bigquery import job
73787379 from google .cloud .bigquery .schema import SchemaField
73797380
73807381 client = self ._make_client ()
73817382
7382- records = [{"float_column" : 3.14 , "struct_column" : [{"foo" : 1 }, {"bar" : - 1 }]}]
7383- dataframe = pandas .DataFrame (data = records )
7383+ records = [(3.14 , {"foo" : 1 , "bar" : 1 })]
7384+ dataframe = pandas .DataFrame (
7385+ data = records , columns = ["float_column" , "struct_column" ]
7386+ )
73847387
73857388 schema = [
73867389 SchemaField ("float_column" , "FLOAT" ),
73877390 SchemaField (
7388- "agg_col " ,
7391+ "struct_column " ,
73897392 "RECORD" ,
73907393 fields = [SchemaField ("foo" , "INTEGER" ), SchemaField ("bar" , "INTEGER" )],
73917394 ),
@@ -7396,14 +7399,49 @@ def test_load_table_from_dataframe_struct_fields_error(self):
73967399 "google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
73977400 )
73987401
7399- with pytest .raises (ValueError ) as exc_info , load_patch :
7400- client .load_table_from_dataframe (
7401- dataframe , self .TABLE_REF , job_config = job_config , location = self .LOCATION
7402+ if six .PY2 :
7403+ with pytest .raises (ValueError ) as exc_info , load_patch :
7404+ client .load_table_from_dataframe (
7405+ dataframe ,
7406+ self .TABLE_REF ,
7407+ job_config = job_config ,
7408+ location = self .LOCATION ,
7409+ )
7410+
7411+ err_msg = str (exc_info .value )
7412+ assert "struct" in err_msg
7413+ assert "not support" in err_msg
7414+
7415+ else :
7416+ get_table_patch = mock .patch (
7417+ "google.cloud.bigquery.client.Client.get_table" ,
7418+ autospec = True ,
7419+ side_effect = google .api_core .exceptions .NotFound ("Table not found" ),
7420+ )
7421+ with load_patch as load_table_from_file , get_table_patch :
7422+ client .load_table_from_dataframe (
7423+ dataframe ,
7424+ self .TABLE_REF ,
7425+ job_config = job_config ,
7426+ location = self .LOCATION ,
7427+ )
7428+
7429+ load_table_from_file .assert_called_once_with (
7430+ client ,
7431+ mock .ANY ,
7432+ self .TABLE_REF ,
7433+ num_retries = _DEFAULT_NUM_RETRIES ,
7434+ rewind = True ,
7435+ job_id = mock .ANY ,
7436+ job_id_prefix = None ,
7437+ location = self .LOCATION ,
7438+ project = None ,
7439+ job_config = mock .ANY ,
74027440 )
74037441
7404- err_msg = str ( exc_info . value )
7405- assert "struct" in err_msg
7406- assert "not support" in err_msg
7442+ sent_config = load_table_from_file . mock_calls [ 0 ][ 2 ][ "job_config" ]
7443+ assert sent_config . source_format == job . SourceFormat . PARQUET
7444+ assert sent_config . schema == schema
74077445
74087446 @unittest .skipIf (pandas is None , "Requires `pandas`" )
74097447 @unittest .skipIf (pyarrow is None , "Requires `pyarrow`" )
0 commit comments