Skip to content
Snippets Groups Projects
Commit aae82b60 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files
parent 0fc89beb
Branches
Tags
2 merge requests!112Release 0.12.0,!111F better csv value error
...@@ -379,6 +379,48 @@ class CSVImporterTest(TableImporterTest): ...@@ -379,6 +379,48 @@ class CSVImporterTest(TableImporterTest):
assert df["int_with_gaps"].dtype == "Int64" assert df["int_with_gaps"].dtype == "Int64"
assert df["float"].dtype == float assert df["float"].dtype == float
def test_wrong_datatype_type_errors(self):
"""Test for
https://gitlab.com/linkahead/linkahead-advanced-user-tools/-/issues/63:
Highlight rows and columns in which type errors occur.
"""
tmpfile = NamedTemporaryFile(delete=False, suffix=".csv")
with open(tmpfile.name, 'w') as tmp:
# Wrong types in row 2, columns 1 and 2, and row 4, column 2.
tmp.write(
"int,float\n"
"1,2.3\n"
"4.5,word\n"
"0,1.2\n"
"-12,12+3j\n"
)
kwargs = {
"datatypes": {
"int": int,
"float": float
},
"obligatory_columns": ["int"],
"converters": {}
}
importer = CSVImporter(**kwargs)
with pytest.raises(DataInconsistencyError) as die:
df = importer.read_file(tmpfile.name)
msg = str(die.value)
assert "Elements with wrong datatypes encountered:\n" in msg
# Errors in rows 1 and 3, no errors in 2 and 4
assert "* row 1:\n" in msg
assert "* row 2:\n" not in msg
assert "* row 3:\n" in msg
assert "* row 4:\n" not in msg
row_1_msgs, row_3_msgs = msg.split("* row 1:\n")[1].split("* row 3:\n")
# exactly 2 errors in row 1, exactly 1 in row 3
assert len(row_1_msgs.strip().split('\n')) == 2
assert len(row_3_msgs.strip().split('\n')) == 1
assert "\t* column \"int\"" in row_1_msgs
assert "\t* column \"float\"" in row_1_msgs
assert "\t* column \"float\"" in row_3_msgs
class TSVImporterTest(TableImporterTest): class TSVImporterTest(TableImporterTest):
def test_full(self): def test_full(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment