@@ -110,6 +110,17 @@ def _make_list_partitons_meta_info(project, dataset_id, table_id, num_rows=0):
110110 }
111111
112112
113+ def _json_serial_date_only (obj ):
114+ """JSON serializer for objects not serializable by default json code
115+
116+ Ref: https://stackoverflow.com/a/22238613
117+ """
118+
119+ if isinstance (obj , (datetime .datetime , datetime .date )):
120+ return obj .isoformat ()
121+ raise TypeError ("Type %s not serializable" % type (obj ))
122+
123+
113124class TestClient (unittest .TestCase ):
114125
115126 PROJECT = "PROJECT"
@@ -8665,19 +8676,31 @@ def test_load_table_from_dataframe_with_csv_source_format(self):
86658676 sent_config = load_table_from_file .mock_calls [0 ][2 ]["job_config" ]
86668677 assert sent_config .source_format == job .SourceFormat .CSV
86678678
8668- def test_load_table_from_json_basic_use_with_json_dumps_kwargs (self ):
8669- from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
8670- from google .cloud .bigquery import job
8679+ def test_load_table_from_json_basic_use_with_json_dumps_kwargs_fail (self ):
8680+ client = self ._make_client ()
86718681
8672- def json_serial ( obj ) :
8673- """JSON serializer for objects not serializable by default json code
8682+ class Fail :
8683+ _fail = "This will fail"
86748684
8675- Ref: https://stackoverflow.com/a/22238613
8676- """
8685+ json_rows = [
8686+ {
8687+ "name" : "One" ,
8688+ "age" : 11 ,
8689+ "birthday" : datetime .date (2008 , 9 , 10 ),
8690+ "adult" : Fail (),
8691+ }
8692+ ]
8693+
8694+ with pytest .raises (TypeError ):
8695+ client .load_table_from_json (
8696+ json_rows ,
8697+ self .TABLE_REF ,
8698+ json_dumps_kwargs = {"default" : _json_serial_date_only },
8699+ )
86778700
8678- if isinstance ( obj , ( datetime . datetime , datetime . date ) ):
8679- return obj . isoformat ()
8680- raise TypeError ( "Type %s not serializable" % type ( obj ))
8701+ def test_load_table_from_json_basic_use_with_json_dumps_kwargs ( self ):
8702+ from google . cloud . bigquery . client import _DEFAULT_NUM_RETRIES
8703+ from google . cloud . bigquery import job
86818704
86828705 client = self ._make_client ()
86838706
@@ -8702,7 +8725,9 @@ def json_serial(obj):
87028725
87038726 with load_patch as load_table_from_file :
87048727 client .load_table_from_json (
8705- json_rows , self .TABLE_REF , json_dumps_kwargs = {"default" : json_serial }
8728+ json_rows ,
8729+ self .TABLE_REF ,
8730+ json_dumps_kwargs = {"default" : _json_serial_date_only },
87068731 )
87078732
87088733 load_table_from_file .assert_called_once_with (
0 commit comments