-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_errors.py
More file actions
106 lines (80 loc) · 4 KB
/
test_errors.py
File metadata and controls
106 lines (80 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import annotations
import fastexcel
import pytest
from .utils import path_for_fixture
def test_cell_error_repr() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-type-errors.xlsx"))
_, cell_errors = excel_reader.load_sheet(0, dtypes={"Column": "int"}).to_arrow_with_errors()
assert cell_errors is not None
assert (
repr(cell_errors.errors[0])
== """CellError(position=(2, 0), offset_position=(1, 0), row_offset=1, detail="Expected int but got 'String(\\"foo\\")'")""" # noqa: E501
)
def test_read_excel_bad_type() -> None:
expected_message = "source must be a string or bytes"
with pytest.raises(fastexcel.InvalidParametersError, match=expected_message):
fastexcel.read_excel(42) # type: ignore[arg-type]
def test_does_not_exist() -> None:
expected_message = """calamine error: Cannot detect file format
Context:
0: Could not open workbook at path_does_not_exist.nope
1: could not load excel file at path_does_not_exist.nope"""
with pytest.raises(fastexcel.CalamineError, match=expected_message) as exc_info:
fastexcel.read_excel("path_does_not_exist.nope")
assert exc_info.value.__doc__ == "Generic calamine error"
# Should also work with the base error type
with pytest.raises(fastexcel.FastExcelError, match=expected_message):
fastexcel.read_excel("path_does_not_exist.nope")
def test_sheet_idx_not_found_error() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-single-sheet.xlsx"))
expected_message = """sheet at index 42 not found
Context:
0: Sheet index 42 is out of range. File has 1 sheets."""
with pytest.raises(fastexcel.SheetNotFoundError, match=expected_message) as exc_info:
excel_reader.load_sheet(42)
assert exc_info.value.__doc__ == "Sheet was not found"
# Should also work with the base error type
with pytest.raises(fastexcel.FastExcelError, match=expected_message):
excel_reader.load_sheet(42)
def test_sheet_name_not_found_error() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-single-sheet.xlsx"))
expected_message = """sheet with name "idontexist" not found
Context:
0: Sheet "idontexist" not found in file. Available sheets: "January"."""
with pytest.raises(fastexcel.SheetNotFoundError, match=expected_message) as exc_info:
excel_reader.load_sheet("idontexist")
assert exc_info.value.__doc__ == "Sheet was not found"
@pytest.mark.parametrize(
"exc_class, expected_docstring",
[
(fastexcel.FastExcelError, "The base class for all fastexcel errors"),
(
fastexcel.UnsupportedColumnTypeCombinationError,
"Column contains an unsupported type combination",
),
(fastexcel.CannotRetrieveCellDataError, "Data for a given cell cannot be retrieved"),
(
fastexcel.CalamineCellError,
"calamine returned an error regarding the content of the cell",
),
(fastexcel.CalamineError, "Generic calamine error"),
(fastexcel.ColumnNotFoundError, "Column was not found"),
(fastexcel.SheetNotFoundError, "Sheet was not found"),
(fastexcel.ArrowError, "Generic arrow error"),
(fastexcel.InvalidParametersError, "Provided parameters are invalid"),
],
)
def test_docstrings(exc_class: type[Exception], expected_docstring: str) -> None:
assert exc_class.__doc__ == expected_docstring
def test_schema_sample_rows_must_be_nonzero() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-single-sheet.xlsx"))
with pytest.raises(
fastexcel.InvalidParametersError,
match="schema_sample_rows cannot be 0, as it would prevent dtype inferring",
):
excel_reader.load_sheet(0, schema_sample_rows=0)
with pytest.raises(
fastexcel.InvalidParametersError,
match="schema_sample_rows cannot be 0, as it would prevent dtype inferring",
):
excel_reader.load_table("my-table", schema_sample_rows=0)