@@ -650,6 +650,59 @@ def test_load_does_not_modify_schema_arg(mock_bigquery_client):
650650 assert original_schema == original_schema_cp
651651
652652
653+ def test_load_modifies_schema (mock_bigquery_client ):
654+ """Test of https://github.com/googleapis/python-bigquery-pandas/issues/670"""
655+ from google .api_core .exceptions import NotFound
656+
657+ # Create table with new schema.
658+ mock_bigquery_client .get_table .side_effect = NotFound ("nope" )
659+ df = DataFrame (
660+ {
661+ "field1" : ["a" , "b" ],
662+ "field2" : [1 , 2 ],
663+ "field3" : [datetime .date (2019 , 1 , 1 ), datetime .date (2019 , 5 , 1 )],
664+ }
665+ )
666+ original_schema = [
667+ {"name" : "field1" , "type" : "STRING" , "mode" : "REQUIRED" },
668+ {"name" : "field2" , "type" : "INTEGER" },
669+ {"name" : "field3" , "type" : "DATE" },
670+ ]
671+ original_schema_cp = copy .deepcopy (original_schema )
672+ gbq .to_gbq (
673+ df ,
674+ "dataset.schematest" ,
675+ project_id = "my-project" ,
676+ table_schema = original_schema ,
677+ if_exists = "fail" ,
678+ )
679+ assert original_schema == original_schema_cp
680+
681+ # Test that when if_exists == "replace", the new table schema updates
682+ # according to the local schema.
683+ new_df = DataFrame (
684+ {
685+ "field1" : ["a" , "b" ],
686+ "field2" : ["c" , "d" ],
687+ "field3" : [datetime .date (2019 , 1 , 1 ), datetime .date (2019 , 5 , 1 )],
688+ }
689+ )
690+ new_schema = [
691+ {"name" : "field1" , "type" : "STRING" , "mode" : "REQUIRED" },
692+ {"name" : "field2" , "type" : "STRING" },
693+ {"name" : "field3" , "type" : "DATE" },
694+ ]
695+ new_schema_cp = copy .deepcopy (new_schema )
696+ gbq .to_gbq (
697+ new_df ,
698+ "dataset.schematest" ,
699+ project_id = "my-project" ,
700+ table_schema = new_schema ,
701+ if_exists = "replace" ,
702+ )
703+ assert new_schema == new_schema_cp
704+
705+
653706def test_read_gbq_passes_dtypes (mock_bigquery_client , mock_service_account_credentials ):
654707 mock_service_account_credentials .project_id = "service_account_project_id"
655708 df = gbq .read_gbq (
0 commit comments