@@ -5434,7 +5434,7 @@ def test_insert_rows_from_dataframe_w_explicit_none_insert_ids(self):
54345434 method = "POST" , path = API_PATH , data = EXPECTED_SENT_DATA , timeout = None
54355435 )
54365436
5437- def test_insert_rows_json (self ):
5437+ def test_insert_rows_json_default_behavior (self ):
54385438 from google .cloud .bigquery .dataset import DatasetReference
54395439 from google .cloud .bigquery .schema import SchemaField
54405440 from google .cloud .bigquery .table import Table
@@ -5481,29 +5481,127 @@ def test_insert_rows_json(self):
54815481 method = "POST" , path = "/%s" % PATH , data = SENT , timeout = 7.5 ,
54825482 )
54835483
5484- def test_insert_rows_json_with_string_id (self ):
5485- rows = [{"col1" : "val1" }]
5484+ def test_insert_rows_json_w_explicitly_requested_autogenerated_insert_ids (self ):
5485+ from google .cloud .bigquery import AutoRowIDs
5486+
5487+ rows = [{"col1" : "val1" }, {"col2" : "val2" }]
54865488 creds = _make_credentials ()
54875489 http = object ()
54885490 client = self ._make_one (
54895491 project = "default-project" , credentials = creds , _http = http
54905492 )
54915493 conn = client ._connection = make_connection ({})
54925494
5493- with mock .patch ("uuid.uuid4" , side_effect = map (str , range (len (rows )))):
5494- errors = client .insert_rows_json ("proj.dset.tbl" , rows )
5495+ uuid_patcher = mock .patch ("uuid.uuid4" , side_effect = map (str , range (len (rows ))))
5496+ with uuid_patcher :
5497+ errors = client .insert_rows_json (
5498+ "proj.dset.tbl" , rows , row_ids = AutoRowIDs .GENERATE_UUID
5499+ )
54955500
54965501 self .assertEqual (len (errors ), 0 )
5497- expected = {
5498- "rows" : [{"json" : row , "insertId" : str (i )} for i , row in enumerate (rows )]
5502+
5503+ # Check row data sent to the backend.
5504+ expected_row_data = {
5505+ "rows" : [
5506+ {"json" : {"col1" : "val1" }, "insertId" : "0" },
5507+ {"json" : {"col2" : "val2" }, "insertId" : "1" },
5508+ ]
54995509 }
55005510 conn .api_request .assert_called_once_with (
55015511 method = "POST" ,
55025512 path = "/projects/proj/datasets/dset/tables/tbl/insertAll" ,
5503- data = expected ,
5513+ data = expected_row_data ,
5514+ timeout = None ,
5515+ )
5516+
5517+ def test_insert_rows_json_w_explicitly_disabled_insert_ids (self ):
5518+ from google .cloud .bigquery import AutoRowIDs
5519+
5520+ rows = [{"col1" : "val1" }, {"col2" : "val2" }]
5521+ creds = _make_credentials ()
5522+ http = object ()
5523+ client = self ._make_one (
5524+ project = "default-project" , credentials = creds , _http = http
5525+ )
5526+ conn = client ._connection = make_connection ({})
5527+
5528+ errors = client .insert_rows_json (
5529+ "proj.dset.tbl" , rows , row_ids = AutoRowIDs .DISABLED ,
5530+ )
5531+
5532+ self .assertEqual (len (errors ), 0 )
5533+
5534+ expected_row_data = {
5535+ "rows" : [
5536+ {"json" : {"col1" : "val1" }, "insertId" : None },
5537+ {"json" : {"col2" : "val2" }, "insertId" : None },
5538+ ]
5539+ }
5540+ conn .api_request .assert_called_once_with (
5541+ method = "POST" ,
5542+ path = "/projects/proj/datasets/dset/tables/tbl/insertAll" ,
5543+ data = expected_row_data ,
5544+ timeout = None ,
5545+ )
5546+
5547+ def test_insert_rows_json_with_iterator_row_ids (self ):
5548+ rows = [{"col1" : "val1" }, {"col2" : "val2" }, {"col3" : "val3" }]
5549+ creds = _make_credentials ()
5550+ http = object ()
5551+ client = self ._make_one (
5552+ project = "default-project" , credentials = creds , _http = http
5553+ )
5554+ conn = client ._connection = make_connection ({})
5555+
5556+ row_ids_iter = map (str , itertools .count (42 ))
5557+ errors = client .insert_rows_json ("proj.dset.tbl" , rows , row_ids = row_ids_iter )
5558+
5559+ self .assertEqual (len (errors ), 0 )
5560+ expected_row_data = {
5561+ "rows" : [
5562+ {"json" : {"col1" : "val1" }, "insertId" : "42" },
5563+ {"json" : {"col2" : "val2" }, "insertId" : "43" },
5564+ {"json" : {"col3" : "val3" }, "insertId" : "44" },
5565+ ]
5566+ }
5567+ conn .api_request .assert_called_once_with (
5568+ method = "POST" ,
5569+ path = "/projects/proj/datasets/dset/tables/tbl/insertAll" ,
5570+ data = expected_row_data ,
55045571 timeout = None ,
55055572 )
55065573
5574+ def test_insert_rows_json_with_non_iterable_row_ids (self ):
5575+ rows = [{"col1" : "val1" }]
5576+ creds = _make_credentials ()
5577+ http = object ()
5578+ client = self ._make_one (
5579+ project = "default-project" , credentials = creds , _http = http
5580+ )
5581+ client ._connection = make_connection ({})
5582+
5583+ with self .assertRaises (TypeError ) as exc :
5584+ client .insert_rows_json ("proj.dset.tbl" , rows , row_ids = object ())
5585+
5586+ err_msg = str (exc .exception )
5587+ self .assertIn ("row_ids" , err_msg )
5588+ self .assertIn ("iterable" , err_msg )
5589+
5590+ def test_insert_rows_json_with_too_few_row_ids (self ):
5591+ rows = [{"col1" : "val1" }, {"col2" : "val2" }, {"col3" : "val3" }]
5592+ creds = _make_credentials ()
5593+ http = object ()
5594+ client = self ._make_one (
5595+ project = "default-project" , credentials = creds , _http = http
5596+ )
5597+ client ._connection = make_connection ({})
5598+
5599+ insert_ids = ["10" , "20" ]
5600+
5601+ error_msg_pattern = "row_ids did not generate enough IDs.*index 2"
5602+ with self .assertRaisesRegex (ValueError , error_msg_pattern ):
5603+ client .insert_rows_json ("proj.dset.tbl" , rows , row_ids = insert_ids )
5604+
55075605 def test_insert_rows_json_w_explicit_none_insert_ids (self ):
55085606 rows = [{"col1" : "val1" }, {"col2" : "val2" }]
55095607 creds = _make_credentials ()
@@ -5526,6 +5624,45 @@ def test_insert_rows_json_w_explicit_none_insert_ids(self):
55265624 timeout = None ,
55275625 )
55285626
5627+ def test_insert_rows_json_w_none_insert_ids_sequence (self ):
5628+ rows = [{"col1" : "val1" }, {"col2" : "val2" }]
5629+ creds = _make_credentials ()
5630+ http = object ()
5631+ client = self ._make_one (
5632+ project = "default-project" , credentials = creds , _http = http
5633+ )
5634+ conn = client ._connection = make_connection ({})
5635+
5636+ uuid_patcher = mock .patch ("uuid.uuid4" , side_effect = map (str , range (len (rows ))))
5637+ with warnings .catch_warnings (record = True ) as warned , uuid_patcher :
5638+ errors = client .insert_rows_json ("proj.dset.tbl" , rows , row_ids = None )
5639+
5640+ self .assertEqual (len (errors ), 0 )
5641+
5642+ # Passing row_ids=None should have resulted in a deprecation warning.
5643+ matches = [
5644+ warning
5645+ for warning in warned
5646+ if issubclass (warning .category , DeprecationWarning )
5647+ and "row_ids" in str (warning )
5648+ and "AutoRowIDs.GENERATE_UUID" in str (warning )
5649+ ]
5650+ assert matches , "The expected deprecation warning was not raised."
5651+
5652+ # Check row data sent to the backend.
5653+ expected_row_data = {
5654+ "rows" : [
5655+ {"json" : {"col1" : "val1" }, "insertId" : "0" },
5656+ {"json" : {"col2" : "val2" }, "insertId" : "1" },
5657+ ]
5658+ }
5659+ conn .api_request .assert_called_once_with (
5660+ method = "POST" ,
5661+ path = "/projects/proj/datasets/dset/tables/tbl/insertAll" ,
5662+ data = expected_row_data ,
5663+ timeout = None ,
5664+ )
5665+
55295666 def test_insert_rows_w_wrong_arg (self ):
55305667 from google .cloud .bigquery .dataset import DatasetReference
55315668 from google .cloud .bigquery .schema import SchemaField
0 commit comments