@@ -179,16 +179,34 @@ def test_dataframe_to_bigquery_fields_fallback_needed_w_pyarrow(module_under_tes
179179
180180
181181def test_dataframe_to_bigquery_fields_w_extra_fields (module_under_test ):
182- with pytest .raises (ValueError ) as exc_context :
183- module_under_test .dataframe_to_bigquery_fields (
184- pandas .DataFrame (),
185- override_bigquery_fields = (schema .SchemaField ("not_in_df" , "STRING" ),),
182+ dataframe = pandas .DataFrame ({"in_df" : [1 , 2 , 3 ]})
183+ bq_schema = (
184+ schema .SchemaField ("in_df" , "INTEGER" ),
185+ schema .SchemaField ("not_in_df" , "STRING" ),
186+ schema .SchemaField ("also_not_in_df" , "INTEGER" ),
187+ )
188+
189+ with pytest .warns (UserWarning ) as record :
190+ returned_schema = module_under_test .dataframe_to_bigquery_fields (
191+ dataframe , override_bigquery_fields = bq_schema
186192 )
187- message = str (exc_context .value )
193+
194+ assert len (record ) == 1
195+ message = str (record [0 ].message )
188196 assert (
189- "Provided BigQuery fields contain field(s) not present in DataFrame: " in message
197+ "Provided BigQuery fields contain field(s) not present in DataFrame" in message
190198 )
191- assert "not_in_df" in message
199+ # Note: The field names are sorted in the warning message.
200+ assert "['also_not_in_df', 'not_in_df']" in message
201+
202+ expected_schema = (
203+ schema .SchemaField ("in_df" , "INTEGER" ),
204+ # Note: The fields are sorted by name as they are added from the set of
205+ # unused fields.
206+ schema .SchemaField ("also_not_in_df" , "INTEGER" ),
207+ schema .SchemaField ("not_in_df" , "STRING" ),
208+ )
209+ assert returned_schema == expected_schema
192210
193211
194212def test_dataframe_to_bigquery_fields_geography (module_under_test ):
0 commit comments