(npartitions, parallel)
| 482 | @pytest.mark.parametrize("npartitions", (1, 2)) |
| 483 | @pytest.mark.parametrize("parallel", (False, True)) |
| 484 | def test_to_sql(npartitions, parallel): |
| 485 | df_by_age = df.set_index("age") |
| 486 | df_appended = pd.concat( |
| 487 | [ |
| 488 | df, |
| 489 | df, |
| 490 | ] |
| 491 | ) |
| 492 | |
| 493 | ddf = dd.from_pandas(df, npartitions) |
| 494 | ddf_by_age = ddf.set_index("age") |
| 495 | |
| 496 | # Simple round trip test: use existing "number" index_col |
| 497 | with tmp_db_uri() as uri: |
| 498 | ddf.to_sql("test", uri, parallel=parallel) |
| 499 | result = read_sql_table("test", uri, "number") |
| 500 | assert_eq(df, result) |
| 501 | |
| 502 | # Test writing no index, and reading back in with one of the other columns as index (`read_sql_table` requires |
| 503 | # an index_col) |
| 504 | with tmp_db_uri() as uri: |
| 505 | ddf.to_sql("test", uri, parallel=parallel, index=False) |
| 506 | |
| 507 | result = read_sql_table("test", uri, "negish") |
| 508 | assert_eq(df.set_index("negish"), result) |
| 509 | |
| 510 | result = read_sql_table("test", uri, "age") |
| 511 | assert_eq(df_by_age, result) |
| 512 | |
| 513 | # Index by "age" instead |
| 514 | with tmp_db_uri() as uri: |
| 515 | ddf_by_age.to_sql("test", uri, parallel=parallel) |
| 516 | result = read_sql_table("test", uri, "age") |
| 517 | assert_eq(df_by_age, result) |
| 518 | |
| 519 | if PANDAS_GE_300: |
| 520 | string_dtype = "str" |
| 521 | else: |
| 522 | string_dtype = "object" |
| 523 | |
| 524 | # Index column can't have "object" dtype if no partitions are provided |
| 525 | with tmp_db_uri() as uri: |
| 526 | ddf.set_index("name").to_sql("test", uri) |
| 527 | with pytest.raises( |
| 528 | TypeError, |
| 529 | match=f'Provided index column is of type "{string_dtype}". If divisions is not provided the index column type must be numeric or datetime.', |
| 530 | ): |
| 531 | read_sql_table("test", uri, "name") |
| 532 | |
| 533 | # Test various "if_exists" values |
| 534 | with tmp_db_uri() as uri: |
| 535 | ddf.to_sql("test", uri) |
| 536 | |
| 537 | # Writing a table that already exists fails |
| 538 | with pytest.raises(ValueError, match="Table 'test' already exists"): |
| 539 | ddf.to_sql("test", uri) |
| 540 | |
| 541 | ddf.to_sql("test", uri, parallel=parallel, if_exists="append") |
nothing calls this directly
no test coverage detected
searching dependent graphs…