MCPcopy Create free account
hub / github.com/dask/dask / test_to_sql

Function test_to_sql

dask/dataframe/io/tests/test_sql.py:479–552  ·  view source on GitHub ↗
(npartitions, parallel)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 6

read_sql_tableFunction · 0.90
assert_eqFunction · 0.90
tmp_db_uriFunction · 0.85
set_indexMethod · 0.80
to_sqlMethod · 0.80
computeMethod · 0.45

Tested by

no test coverage detected