@@ -8951,6 +8951,8 @@ def test_load_table_from_dataframe_w_higher_scale_decimal128_datatype(self):
89518951SchemaField ("x" ,"BIGNUMERIC" ,"NULLABLE" ,None ),
89528952 )
89538953
8954+ # With autodetect specified, we pass the value as is. For more info, see
8955+ # https://github.com/googleapis/python-bigquery/issues/1228#issuecomment-1910946297
89548956def test_load_table_from_json_basic_use (self ):
89558957from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
89568958from google .cloud .bigquery import job
@@ -8962,12 +8964,28 @@ def test_load_table_from_json_basic_use(self):
89628964 {"name" :"Two" ,"age" :22 ,"birthday" :"1997-08-09" ,"adult" :True },
89638965 ]
89648966
8967+ job_config = job .LoadJobConfig (autodetect = True )
8968+
89658969load_patch = mock .patch (
89668970"google.cloud.bigquery.client.Client.load_table_from_file" ,autospec = True
89678971 )
89688972
8969- with load_patch as load_table_from_file :
8970- client .load_table_from_json (json_rows ,self .TABLE_REF )
8973+ # mock: remote table already exists
8974+ get_table_reference = {
8975+ "projectId" :"project_id" ,
8976+ "datasetId" :"test_dataset" ,
8977+ "tableId" :"test_table" ,
8978+ }
8979+ get_table_patch = mock .patch (
8980+ "google.cloud.bigquery.client.Client.get_table" ,
8981+ autospec = True ,
8982+ return_value = mock .Mock (table_reference = get_table_reference ),
8983+ )
8984+
8985+ with load_patch as load_table_from_file ,get_table_patch :
8986+ client .load_table_from_json (
8987+ json_rows ,self .TABLE_REF ,job_config = job_config
8988+ )
89718989
89728990load_table_from_file .assert_called_once_with (
89738991client ,
@@ -9066,6 +9084,174 @@ def test_load_table_from_json_w_invalid_job_config(self):
90669084err_msg = str (exc .value )
90679085assert "Expected an instance of LoadJobConfig" in err_msg
90689086
9087+ # When all following are true:
9088+ # (1) no schema provided;
9089+ # (2) no autodetect value provided;
9090+ # (3) writeDisposition == WRITE_APPEND or None;
9091+ # (4) table already exists,
9092+ # client sets autodetect == False
9093+ # For more details, see https://github.com/googleapis/python-bigquery/issues/1228#issuecomment-1910946297
9094+ def test_load_table_from_json_wo_schema_wo_autodetect_write_append_w_table (self ):
9095+ from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
9096+ from google .cloud .bigquery import job
9097+ from google .cloud .bigquery .job import WriteDisposition
9098+
9099+ client = self ._make_client ()
9100+
9101+ json_rows = [
9102+ {"name" :"One" ,"age" :11 ,"birthday" :"2008-09-10" ,"adult" :False },
9103+ {"name" :"Two" ,"age" :22 ,"birthday" :"1997-08-09" ,"adult" :True },
9104+ ]
9105+
9106+ job_config = job .LoadJobConfig (write_disposition = WriteDisposition .WRITE_APPEND )
9107+
9108+ load_patch = mock .patch (
9109+ "google.cloud.bigquery.client.Client.load_table_from_file" ,autospec = True
9110+ )
9111+
9112+ # mock: remote table already exists
9113+ get_table_reference = {
9114+ "projectId" :"project_id" ,
9115+ "datasetId" :"test_dataset" ,
9116+ "tableId" :"test_table" ,
9117+ }
9118+ get_table_patch = mock .patch (
9119+ "google.cloud.bigquery.client.Client.get_table" ,
9120+ autospec = True ,
9121+ return_value = mock .Mock (table_reference = get_table_reference ),
9122+ )
9123+
9124+ with load_patch as load_table_from_file ,get_table_patch :
9125+ client .load_table_from_json (
9126+ json_rows ,self .TABLE_REF ,job_config = job_config
9127+ )
9128+
9129+ load_table_from_file .assert_called_once_with (
9130+ client ,
9131+ mock .ANY ,
9132+ self .TABLE_REF ,
9133+ size = mock .ANY ,
9134+ num_retries = _DEFAULT_NUM_RETRIES ,
9135+ job_id = mock .ANY ,
9136+ job_id_prefix = None ,
9137+ location = client .location ,
9138+ project = client .project ,
9139+ job_config = mock .ANY ,
9140+ timeout = DEFAULT_TIMEOUT ,
9141+ )
9142+
9143+ sent_config = load_table_from_file .mock_calls [0 ][2 ]["job_config" ]
9144+ assert sent_config .source_format == job .SourceFormat .NEWLINE_DELIMITED_JSON
9145+ assert sent_config .schema is None
9146+ assert not sent_config .autodetect
9147+
9148+ # When all following are true:
9149+ # (1) no schema provided;
9150+ # (2) no autodetect value provided;
9151+ # (3) writeDisposition == WRITE_APPEND or None;
9152+ # (4) table does NOT exist,
9153+ # client sets autodetect == True
9154+ # For more details, see https://github.com/googleapis/python-bigquery/issues/1228#issuecomment-1910946297
9155+ def test_load_table_from_json_wo_schema_wo_autodetect_write_append_wo_table (self ):
9156+ import google .api_core .exceptions as core_exceptions
9157+ from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
9158+ from google .cloud .bigquery import job
9159+ from google .cloud .bigquery .job import WriteDisposition
9160+
9161+ client = self ._make_client ()
9162+
9163+ json_rows = [
9164+ {"name" :"One" ,"age" :11 ,"birthday" :"2008-09-10" ,"adult" :False },
9165+ {"name" :"Two" ,"age" :22 ,"birthday" :"1997-08-09" ,"adult" :True },
9166+ ]
9167+
9168+ job_config = job .LoadJobConfig (write_disposition = WriteDisposition .WRITE_APPEND )
9169+
9170+ load_patch = mock .patch (
9171+ "google.cloud.bigquery.client.Client.load_table_from_file" ,autospec = True
9172+ )
9173+
9174+ # mock: remote table doesn't exist
9175+ get_table_patch = mock .patch (
9176+ "google.cloud.bigquery.client.Client.get_table" ,
9177+ autospec = True ,
9178+ side_effect = core_exceptions .NotFound ("" ),
9179+ )
9180+
9181+ with load_patch as load_table_from_file ,get_table_patch :
9182+ client .load_table_from_json (
9183+ json_rows ,self .TABLE_REF ,job_config = job_config
9184+ )
9185+
9186+ load_table_from_file .assert_called_once_with (
9187+ client ,
9188+ mock .ANY ,
9189+ self .TABLE_REF ,
9190+ size = mock .ANY ,
9191+ num_retries = _DEFAULT_NUM_RETRIES ,
9192+ job_id = mock .ANY ,
9193+ job_id_prefix = None ,
9194+ location = client .location ,
9195+ project = client .project ,
9196+ job_config = mock .ANY ,
9197+ timeout = DEFAULT_TIMEOUT ,
9198+ )
9199+
9200+ sent_config = load_table_from_file .mock_calls [0 ][2 ]["job_config" ]
9201+ assert sent_config .source_format == job .SourceFormat .NEWLINE_DELIMITED_JSON
9202+ assert sent_config .schema is None
9203+ assert sent_config .autodetect
9204+
9205+ # When all following are true:
9206+ # (1) no schema provided;
9207+ # (2) no autodetect value provided;
9208+ # (3) writeDisposition == WRITE_TRUNCATE or WRITE_EMPTY;
9209+ # client sets autodetect == True
9210+ # For more details, see https://github.com/googleapis/python-bigquery/issues/1228#issuecomment-1910946297
9211+ def test_load_table_from_json_wo_schema_wo_autodetect_others (self ):
9212+ from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
9213+ from google .cloud .bigquery import job
9214+ from google .cloud .bigquery .job import WriteDisposition
9215+
9216+ client = self ._make_client ()
9217+
9218+ json_rows = [
9219+ {"name" :"One" ,"age" :11 ,"birthday" :"2008-09-10" ,"adult" :False },
9220+ {"name" :"Two" ,"age" :22 ,"birthday" :"1997-08-09" ,"adult" :True },
9221+ ]
9222+
9223+ job_config = job .LoadJobConfig (
9224+ write_disposition = WriteDisposition .WRITE_TRUNCATE
9225+ )
9226+
9227+ load_patch = mock .patch (
9228+ "google.cloud.bigquery.client.Client.load_table_from_file" ,autospec = True
9229+ )
9230+
9231+ with load_patch as load_table_from_file :
9232+ client .load_table_from_json (
9233+ json_rows ,self .TABLE_REF ,job_config = job_config
9234+ )
9235+
9236+ load_table_from_file .assert_called_once_with (
9237+ client ,
9238+ mock .ANY ,
9239+ self .TABLE_REF ,
9240+ size = mock .ANY ,
9241+ num_retries = _DEFAULT_NUM_RETRIES ,
9242+ job_id = mock .ANY ,
9243+ job_id_prefix = None ,
9244+ location = client .location ,
9245+ project = client .project ,
9246+ job_config = mock .ANY ,
9247+ timeout = DEFAULT_TIMEOUT ,
9248+ )
9249+
9250+ sent_config = load_table_from_file .mock_calls [0 ][2 ]["job_config" ]
9251+ assert sent_config .source_format == job .SourceFormat .NEWLINE_DELIMITED_JSON
9252+ assert sent_config .schema is None
9253+ assert sent_config .autodetect
9254+
90699255def test_load_table_from_json_w_explicit_job_config_override (self ):
90709256from google .cloud .bigquery import job
90719257from google .cloud .bigquery .client import _DEFAULT_NUM_RETRIES
@@ -9190,8 +9376,19 @@ def test_load_table_from_json_unicode_emoji_data_case(self):
91909376load_patch = mock .patch (
91919377"google.cloud.bigquery.client.Client.load_table_from_file" ,autospec = True
91929378 )
9379+ # mock: remote table already exists
9380+ get_table_reference = {
9381+ "projectId" :"project_id" ,
9382+ "datasetId" :"test_dataset" ,
9383+ "tableId" :"test_table" ,
9384+ }
9385+ get_table_patch = mock .patch (
9386+ "google.cloud.bigquery.client.Client.get_table" ,
9387+ autospec = True ,
9388+ return_value = mock .Mock (table_reference = get_table_reference ),
9389+ )
91939390
9194- with load_patch as load_table_from_file :
9391+ with load_patch as load_table_from_file , get_table_patch :
91959392client .load_table_from_json (json_rows ,self .TABLE_REF )
91969393
91979394load_table_from_file .assert_called_once_with (