@@ -110,6 +110,17 @@ def _make_list_partitons_meta_info(project, dataset_id, table_id, num_rows=0):
110
110
}
111
111
112
112
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
+
113
124
class TestClient (unittest .TestCase ):
114
125
115
126
PROJECT = "PROJECT"
@@ -8665,19 +8676,31 @@ def test_load_table_from_dataframe_with_csv_source_format(self):
8665
8676
sent_config = load_table_from_file .mock_calls [0 ][2 ]["job_config" ]
8666
8677
assert sent_config .source_format == job .SourceFormat .CSV
8667
8678
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 ()
8671
8681
8672
- def json_serial ( obj ) :
8673
- """JSON serializer for objects not serializable by default json code
8682
+ class Fail :
8683
+ _fail = "This will fail"
8674
8684
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
+ )
8677
8700
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
8681
8704
8682
8705
client = self ._make_client ()
8683
8706
@@ -8702,7 +8725,9 @@ def json_serial(obj):
8702
8725
8703
8726
with load_patch as load_table_from_file :
8704
8727
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 },
8706
8731
)
8707
8732
8708
8733
load_table_from_file .assert_called_once_with (
0 commit comments