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

Function test_to_sql

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

Source from the content-addressed store, hash-verified

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