Skip to content

Commit

Permalink
adapt writer tests to explicitely ask for validation when it was impl…
Browse files Browse the repository at this point in the history
…icit before making it OFF by default
  • Loading branch information
fsoubelet committed Dec 18, 2024
1 parent 8be5b73 commit be6f123
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_write_int_float_columns(self, tmp_path):

def test_tfs_write_read_with_validation(self, _tfs_dataframe, tmp_path):
write_location = tmp_path / "test.tfs"
write_tfs(write_location, _tfs_dataframe)
write_tfs(write_location, _tfs_dataframe, validate="madx") # strictest
assert write_location.is_file()

new = read_tfs(write_location, validate="madx")
Expand Down Expand Up @@ -261,70 +261,68 @@ def test_tfs_write_madng_compatible_is_read_by_madng(self, _tfs_dataframe_madng,


class TestFailures:
def test_raising_on_non_unique_columns(self, caplog):
def test_raising_on_non_unique_columns_when_validating(self, caplog):
df = TfsDataFrame(columns=["A", "B", "A"])
with pytest.raises(DuplicateColumnsError, match="The dataframe contains non-unique columns."):
write_tfs("", df, non_unique_behavior="raise")
write_tfs("", df, non_unique_behavior="raise", validate="madx") # strictest

for record in caplog.records:
assert record.levelname == "WARNING"
assert "Non-unique column names found" in caplog.text

def test_raising_on_non_unique_index(self, caplog):
def test_raising_on_non_unique_index_when_validating(self, caplog):
df = TfsDataFrame(index=["A", "B", "A"])
with pytest.raises(DuplicateIndicesError, match="The dataframe contains non-unique indices."):
write_tfs("", df, non_unique_behavior="raise")
write_tfs("", df, non_unique_behavior="raise", validate="madx") # strictest

for record in caplog.records:
assert record.levelname == "WARNING"
assert "Non-unique indices found" in caplog.text

def test_raising_on_non_unique_both(self, caplog):
def test_raising_on_non_unique_both_when_validating(self, caplog):
df = TfsDataFrame(index=["A", "B", "A"], columns=["A", "B", "A"])
with pytest.raises(DuplicateIndicesError, match="The dataframe contains non-unique indices."):
write_tfs("", df, non_unique_behavior="raise")
write_tfs("", df, non_unique_behavior="raise", validate="madx") # strictest

for record in caplog.records:
assert record.levelname == "WARNING"
assert "Non-unique indices found" in caplog.text # first checked and raised

def test_fail_on_spaces_columns(self, caplog):
def test_fail_on_spaces_columns_when_validating(self, caplog):
caplog.set_level(logging.DEBUG)
df = TfsDataFrame(columns=["allowed", "not allowed"])
with pytest.raises(SpaceinColumnNameError, match="TFS-Columns can not contain spaces."):
write_tfs("", df)
write_tfs("", df, validate="madx") # strictest

for record in caplog.records:
assert record.levelname == "DEBUG"
assert "Space(s) found in TFS columns" in caplog.text

def test_messed_up_dataframe_fails_writes(self, _messed_up_dataframe: TfsDataFrame):
def test_messed_up_dataframe_fails_writes_when_validating(self, _messed_up_dataframe: TfsDataFrame):
messed_tfs = _messed_up_dataframe
# This df raises in validate because of list elements
with pytest.raises(
IterableInDataFrameError, match="Lists or tuple elements are not accepted in a TfsDataFrame"
):
write_tfs("", messed_tfs)
with pytest.raises(IterableInDataFrameError, match="Lists or tuple elements are not accepted in a TfsDataFrame"):
write_tfs("", messed_tfs, validate="madx") # strictest

def test_dict_column_dataframe_fails_writes(self, _dict_column_in_dataframe: TfsDataFrame, tmp_path):
def test_dict_column_dataframe_fails_writes_when_validating(self, _dict_column_in_dataframe: TfsDataFrame, tmp_path):
dict_col_tfs = _dict_column_in_dataframe
with pytest.raises(TypeError): # tries to format dict.__dict__, can't get a % formatter
write_tfs("", dict_col_tfs)
write_tfs("", dict_col_tfs, validate="madx") # strictest

del dict_col_tfs["d"] # should work without the column of dicts
write_location = tmp_path / "test.tfs"
write_tfs(write_location, dict_col_tfs)
assert write_location.is_file()

def test_list_column_dataframe_fails_writes(self, _list_column_in_dataframe: TfsDataFrame, tmp_path, caplog):
def test_list_column_dataframe_fails_writes_when_validating(self, _list_column_in_dataframe: TfsDataFrame, tmp_path, caplog):
list_col_tfs = _list_column_in_dataframe
write_location = tmp_path / "test.tfs"
# This df raises in validate because of list colnames
with pytest.raises(
IterableInDataFrameError,
match="Lists or tuple elements are not accepted in a TfsDataFrame",
):
write_tfs(write_location, list_col_tfs)
write_tfs(write_location, list_col_tfs, validate="madx") # strictest

for record in caplog.records:
assert record.levelname == "ERROR"
Expand Down

0 comments on commit be6f123

Please sign in to comment.