From a6ebcb59bb480780cb1736545f3db5772cd11f4f Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Mon, 4 Jul 2022 17:19:36 +0100 Subject: [PATCH] feat: default value for if_exist in to_sql changed to fail, test for this behavior added. --- pyerrors/input/pandas.py | 6 +++--- tests/pandas_test.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pyerrors/input/pandas.py b/pyerrors/input/pandas.py index 1488dac0..4b0e1d99 100644 --- a/pyerrors/input/pandas.py +++ b/pyerrors/input/pandas.py @@ -7,7 +7,7 @@ from ..correlators import Corr from .json import create_json_string, import_json_string -def to_sql(df, table_name, db, if_exists="replace", gz=True): +def to_sql(df, table_name, db, if_exists='fail', gz=True): """Write DataFrame including Obs or Corr valued columns to sqlite database. Parameters @@ -30,7 +30,7 @@ def to_sql(df, table_name, db, if_exists="replace", gz=True): def read_sql_query(sql, db, auto_gamma=False): - """Execute SQL query on sqlite database and obatin DataFrame including Obs or Corr valued columns. + """Execute SQL query on sqlite database and obtain DataFrame including Obs or Corr valued columns. Parameters ---------- @@ -113,7 +113,7 @@ def _serialize_df(df, gz=False): df : pandas.DataFrame DataFrame to be serilized. gz: bool - gzip the json string represenation. Default False. + gzip the json string representation. Default False. """ out = df.copy() for column in out: diff --git a/tests/pandas_test.py b/tests/pandas_test.py index 29d8df86..22dc74a2 100644 --- a/tests/pandas_test.py +++ b/tests/pandas_test.py @@ -1,6 +1,7 @@ import numpy as np import pandas as pd import pyerrors as pe +import pytest def test_df_export_import(tmp_path): my_dict = {"int": 1, @@ -61,3 +62,13 @@ def test_sql(tmp_path): for auto_gamma in [False, True]: re_df = pe.input.pandas.read_sql_query("SELECT * from My_table", my_db, auto_gamma=auto_gamma) assert np.all(re_df == pe_df) + + +def test_sql_if_exists_fail(tmp_path): + pe_df = pd.DataFrame([{"Label": 1, "Obs": pe.pseudo_Obs(5 * np.exp(-0.2), 0.01, "test_ensemble", 20)}]) + my_db = (tmp_path / "test_db.sqlite").as_posix() + pe.input.pandas.to_sql(pe_df, "My_table", my_db) + with pytest.raises(ValueError): + pe.input.pandas.to_sql(pe_df, "My_table", my_db) + pe.input.pandas.to_sql(pe_df, "My_table", my_db, if_exists='append') + pe.input.pandas.to_sql(pe_df, "My_table", my_db, if_exists='replace')