diff --git a/unittests/test_table_importer.py b/unittests/test_table_importer.py index 6d445056b240e5ede6c52cb055cdde86cfb6d3d7..7a330ddfe0b0b82cb6d8dc9d4b5883e5ed604ac1 100644 --- a/unittests/test_table_importer.py +++ b/unittests/test_table_importer.py @@ -379,6 +379,48 @@ class CSVImporterTest(TableImporterTest): assert df["int_with_gaps"].dtype == "Int64" 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): def test_full(self):