@@ -5228,15 +5228,23 @@ def test_load_table_from_file_bad_mode(self):
5228
5228
def test_load_table_from_dataframe (self ):
5229
5229
from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
5230
5230
from google .cloud .bigquery import job
5231
+ from google .cloud .bigquery .schema import SchemaField
5231
5232
5232
5233
client = self ._make_client ()
5233
5234
records = [{"id" : 1 , "age" : 100 }, {"id" : 2 , "age" : 60 }]
5234
5235
dataframe = pandas .DataFrame (records )
5235
5236
5237
+ get_table_patch = mock .patch (
5238
+ "google.cloud.bigquery.client.Client.get_table" ,
5239
+ autospec = True ,
5240
+ return_value = mock .Mock (
5241
+ schema = [SchemaField ("id" , "INTEGER" ), SchemaField ("age" , "INTEGER" )]
5242
+ ),
5243
+ )
5236
5244
load_patch = mock .patch (
5237
5245
"google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5238
5246
)
5239
- with load_patch as load_table_from_file :
5247
+ with load_patch as load_table_from_file , get_table_patch :
5240
5248
client .load_table_from_dataframe (dataframe , self .TABLE_REF )
5241
5249
5242
5250
load_table_from_file .assert_called_once_with (
@@ -5263,15 +5271,23 @@ def test_load_table_from_dataframe(self):
5263
5271
def test_load_table_from_dataframe_w_client_location (self ):
5264
5272
from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
5265
5273
from google .cloud .bigquery import job
5274
+ from google .cloud .bigquery .schema import SchemaField
5266
5275
5267
5276
client = self ._make_client (location = self .LOCATION )
5268
5277
records = [{"id" : 1 , "age" : 100 }, {"id" : 2 , "age" : 60 }]
5269
5278
dataframe = pandas .DataFrame (records )
5270
5279
5280
+ get_table_patch = mock .patch (
5281
+ "google.cloud.bigquery.client.Client.get_table" ,
5282
+ autospec = True ,
5283
+ return_value = mock .Mock (
5284
+ schema = [SchemaField ("id" , "INTEGER" ), SchemaField ("age" , "INTEGER" )]
5285
+ ),
5286
+ )
5271
5287
load_patch = mock .patch (
5272
5288
"google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5273
5289
)
5274
- with load_patch as load_table_from_file :
5290
+ with load_patch as load_table_from_file , get_table_patch :
5275
5291
client .load_table_from_dataframe (dataframe , self .TABLE_REF )
5276
5292
5277
5293
load_table_from_file .assert_called_once_with (
@@ -5298,16 +5314,24 @@ def test_load_table_from_dataframe_w_client_location(self):
5298
5314
def test_load_table_from_dataframe_w_custom_job_config (self ):
5299
5315
from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
5300
5316
from google .cloud .bigquery import job
5317
+ from google .cloud .bigquery .schema import SchemaField
5301
5318
5302
5319
client = self ._make_client ()
5303
5320
records = [{"id" : 1 , "age" : 100 }, {"id" : 2 , "age" : 60 }]
5304
5321
dataframe = pandas .DataFrame (records )
5305
5322
job_config = job .LoadJobConfig ()
5306
5323
5324
+ get_table_patch = mock .patch (
5325
+ "google.cloud.bigquery.client.Client.get_table" ,
5326
+ autospec = True ,
5327
+ return_value = mock .Mock (
5328
+ schema = [SchemaField ("id" , "INTEGER" ), SchemaField ("age" , "INTEGER" )]
5329
+ ),
5330
+ )
5307
5331
load_patch = mock .patch (
5308
5332
"google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5309
5333
)
5310
- with load_patch as load_table_from_file :
5334
+ with load_patch as load_table_from_file , get_table_patch :
5311
5335
client .load_table_from_dataframe (
5312
5336
dataframe , self .TABLE_REF , job_config = job_config , location = self .LOCATION
5313
5337
)
@@ -5370,7 +5394,20 @@ def test_load_table_from_dataframe_w_automatic_schema(self):
5370
5394
"google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5371
5395
)
5372
5396
5373
- with load_patch as load_table_from_file :
5397
+ get_table_patch = mock .patch (
5398
+ "google.cloud.bigquery.client.Client.get_table" ,
5399
+ autospec = True ,
5400
+ return_value = mock .Mock (
5401
+ schema = [
5402
+ SchemaField ("int_col" , "INTEGER" ),
5403
+ SchemaField ("float_col" , "FLOAT" ),
5404
+ SchemaField ("bool_col" , "BOOLEAN" ),
5405
+ SchemaField ("dt_col" , "DATETIME" ),
5406
+ SchemaField ("ts_col" , "TIMESTAMP" ),
5407
+ ]
5408
+ ),
5409
+ )
5410
+ with load_patch as load_table_from_file , get_table_patch :
5374
5411
client .load_table_from_dataframe (
5375
5412
dataframe , self .TABLE_REF , location = self .LOCATION
5376
5413
)
@@ -5398,6 +5435,71 @@ def test_load_table_from_dataframe_w_automatic_schema(self):
5398
5435
SchemaField ("ts_col" , "TIMESTAMP" ),
5399
5436
)
5400
5437
5438
+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
5439
+ @unittest .skipIf (pyarrow is None , "Requires `pyarrow`" )
5440
+ def test_load_table_from_dataframe_unknown_df_columns (self ):
5441
+ from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
5442
+ from google .cloud .bigquery import job
5443
+ from google .cloud .bigquery .schema import SchemaField
5444
+
5445
+ client = self ._make_client ()
5446
+ records = [{"id" : 1 , "typo_age" : 100 }, {"id" : 2 , "typo_age" : 60 }]
5447
+ dataframe = pandas .DataFrame (records )
5448
+
5449
+ get_table_patch = mock .patch (
5450
+ "google.cloud.bigquery.client.Client.get_table" ,
5451
+ autospec = True ,
5452
+ return_value = mock .Mock (
5453
+ schema = [SchemaField ("id" , "INTEGER" ), SchemaField ("age" , "INTEGER" )]
5454
+ ),
5455
+ )
5456
+ load_patch = mock .patch (
5457
+ "google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5458
+ )
5459
+ with pytest .raises (ValueError ) as exc_info , load_patch , get_table_patch :
5460
+ client .load_table_from_dataframe (dataframe , self .TABLE_REF )
5461
+
5462
+ err_msg = str (exc_info .value )
5463
+ assert "Dataframe contains columns that are not present in table" in err_msg
5464
+ assert "typo_age" in err_msg
5465
+ assert "id" not in err_msg
5466
+
5467
+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
5468
+ @unittest .skipIf (pyarrow is None , "Requires `pyarrow`" )
5469
+ def test_load_table_from_dataframe_unknown_table (self ):
5470
+ from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
5471
+ from google .cloud .bigquery import job
5472
+ from google .cloud .bigquery .schema import SchemaField
5473
+
5474
+ client = self ._make_client ()
5475
+ records = [{"id" : 1 , "age" : 100 }, {"id" : 2 , "age" : 60 }]
5476
+ dataframe = pandas .DataFrame (records )
5477
+
5478
+ get_table_patch = mock .patch (
5479
+ "google.cloud.bigquery.client.Client.get_table" ,
5480
+ autospec = True ,
5481
+ side_effect = google .api_core .exceptions .NotFound ("Table not found" ),
5482
+ )
5483
+ load_patch = mock .patch (
5484
+ "google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5485
+ )
5486
+ with load_patch as load_table_from_file , get_table_patch :
5487
+ # there should be no error
5488
+ client .load_table_from_dataframe (dataframe , self .TABLE_REF )
5489
+
5490
+ load_table_from_file .assert_called_once_with (
5491
+ client ,
5492
+ mock .ANY ,
5493
+ self .TABLE_REF ,
5494
+ num_retries = _DEFAULT_NUM_RETRIES ,
5495
+ rewind = True ,
5496
+ job_id = mock .ANY ,
5497
+ job_id_prefix = None ,
5498
+ location = None ,
5499
+ project = None ,
5500
+ job_config = mock .ANY ,
5501
+ )
5502
+
5401
5503
@unittest .skipIf (pandas is None , "Requires `pandas`" )
5402
5504
@unittest .skipIf (pyarrow is None , "Requires `pyarrow`" )
5403
5505
def test_load_table_from_dataframe_struct_fields_error (self ):
@@ -5686,10 +5788,19 @@ def test_load_table_from_dataframe_w_schema_arrow_custom_compression(self):
5686
5788
@unittest .skipIf (pandas is None , "Requires `pandas`" )
5687
5789
@unittest .skipIf (pyarrow is None , "Requires `pyarrow`" )
5688
5790
def test_load_table_from_dataframe_wo_pyarrow_custom_compression (self ):
5791
+ from google .cloud .bigquery .schema import SchemaField
5792
+
5689
5793
client = self ._make_client ()
5690
5794
records = [{"id" : 1 , "age" : 100 }, {"id" : 2 , "age" : 60 }]
5691
5795
dataframe = pandas .DataFrame (records )
5692
5796
5797
+ get_table_patch = mock .patch (
5798
+ "google.cloud.bigquery.client.Client.get_table" ,
5799
+ autospec = True ,
5800
+ return_value = mock .Mock (
5801
+ schema = [SchemaField ("id" , "INTEGER" ), SchemaField ("age" , "INTEGER" )]
5802
+ ),
5803
+ )
5693
5804
load_patch = mock .patch (
5694
5805
"google.cloud.bigquery.client.Client.load_table_from_file" , autospec = True
5695
5806
)
@@ -5698,7 +5809,7 @@ def test_load_table_from_dataframe_wo_pyarrow_custom_compression(self):
5698
5809
dataframe , "to_parquet" , wraps = dataframe .to_parquet
5699
5810
)
5700
5811
5701
- with load_patch , pyarrow_patch , to_parquet_patch as to_parquet_spy :
5812
+ with load_patch , get_table_patch , pyarrow_patch , to_parquet_patch as to_parquet_spy :
5702
5813
client .load_table_from_dataframe (
5703
5814
dataframe ,
5704
5815
self .TABLE_REF ,
0 commit comments