|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +import copy |
| 4 | +import datetime |
3 | 5 | from unittest import mock |
4 | 6 |
|
5 | 7 | import numpy |
@@ -477,3 +479,45 @@ def test_generate_bq_schema_deprecated(): |
477 | 479 | with pytest.warns(FutureWarning): |
478 | 480 | df = DataFrame([[1, "two"], [3, "four"]]) |
479 | 481 | gbq.generate_bq_schema(df) |
| 482 | + |
| 483 | + |
| 484 | +def test_load_does_not_modify_schema_arg(): |
| 485 | + # Test of Issue # 277 |
| 486 | + df = DataFrame( |
| 487 | + { |
| 488 | + "field1": ["a", "b"], |
| 489 | + "field2": [1, 2], |
| 490 | + "field3": [datetime.date(2019, 1, 1), datetime.date(2019, 5, 1)], |
| 491 | + } |
| 492 | + ) |
| 493 | + original_schema = [ |
| 494 | + {"name": "field1", "type": "STRING", "mode": "REQUIRED"}, |
| 495 | + {"name": "field2", "type": "INTEGER"}, |
| 496 | + {"name": "field3", "type": "DATE"}, |
| 497 | + ] |
| 498 | + original_schema_cp = copy.deepcopy(original_schema) |
| 499 | + gbq.to_gbq( |
| 500 | + df, |
| 501 | + "dataset.schematest", |
| 502 | + project_id="my-project", |
| 503 | + table_schema=original_schema, |
| 504 | + if_exists="fail", |
| 505 | + ) |
| 506 | + assert original_schema == original_schema_cp |
| 507 | + |
| 508 | + # Test again now that table exists - behavior will differ internally |
| 509 | + # branch at if table.exists(table_id) |
| 510 | + original_schema = [ |
| 511 | + {"name": "field1", "type": "STRING", "mode": "REQUIRED"}, |
| 512 | + {"name": "field2", "type": "INTEGER"}, |
| 513 | + {"name": "field3", "type": "DATE"}, |
| 514 | + ] |
| 515 | + original_schema_cp = copy.deepcopy(original_schema) |
| 516 | + gbq.to_gbq( |
| 517 | + df, |
| 518 | + "dataset.schematest", |
| 519 | + project_id="my-project", |
| 520 | + table_schema=original_schema, |
| 521 | + if_exists="append", |
| 522 | + ) |
| 523 | + assert original_schema == original_schema_cp |
0 commit comments