import pytest
import omniparse
class TestIOErrors:
def test_nonexistent_file(self):
with pytest.raises(IOError) as exc_info:
omniparse.extract_from_path("nonexistent_file.txt")
error_msg = str(exc_info.value)
assert len(error_msg) > 0
def test_nonexistent_path(self):
with pytest.raises(IOError):
omniparse.extract_from_path("/invalid/path/to/file.pdf")
def test_directory_instead_of_file(self):
with pytest.raises(IOError):
omniparse.extract_from_path("test_data")
class TestValueErrors:
def test_unsupported_format_from_path(self):
with pytest.raises(ValueError) as exc_info:
omniparse.extract_from_path("test_data/fonts/DejaVuSans.ttf")
error_msg = str(exc_info.value)
assert len(error_msg) > 0
def test_unsupported_format_from_bytes(self):
invalid_data = b"\x00\x01\x02\x03\x04\x05\x06\x07"
with pytest.raises(ValueError):
omniparse.extract_from_bytes(invalid_data)
def test_corrupted_json(self):
with pytest.raises(ValueError) as exc_info:
omniparse.extract_from_path("test_data/text/invalid.json")
error_msg = str(exc_info.value)
assert len(error_msg) > 0
def test_corrupted_bytes(self):
with open("test_data/document/sample.pdf", "rb") as f:
data = f.read(100)
with pytest.raises(ValueError):
omniparse.extract_from_bytes(data, mime_hint="application/pdf")
def test_empty_bytes(self):
with pytest.raises(ValueError):
omniparse.extract_from_bytes(b"")
class TestErrorMessages:
def test_io_error_message_contains_context(self):
with pytest.raises(IOError) as exc_info:
omniparse.extract_from_path("missing_file.pdf")
error_msg = str(exc_info.value)
assert len(error_msg) > 10
assert isinstance(error_msg, str)
def test_unsupported_format_message(self):
try:
omniparse.extract_from_path("test_data/fonts/DejaVuSans.ttf")
pytest.fail("Expected ValueError for unsupported format")
except ValueError as e:
error_msg = str(e)
assert len(error_msg) > 0
assert isinstance(error_msg, str)
def test_corrupted_file_message(self):
try:
omniparse.extract_from_path("test_data/text/invalid.json")
pytest.fail("Expected ValueError for corrupted file")
except ValueError as e:
error_msg = str(e)
assert len(error_msg) > 0
assert isinstance(error_msg, str)
def test_error_message_types(self):
errors_to_test = [
(IOError, lambda: omniparse.extract_from_path("nonexistent.txt")),
(ValueError, lambda: omniparse.extract_from_bytes(b"\x00\x01\x02")),
]
for expected_error, func in errors_to_test:
with pytest.raises(expected_error) as exc_info:
func()
assert isinstance(str(exc_info.value), str)
assert len(str(exc_info.value)) > 0
class TestErrorRecovery:
def test_error_does_not_crash_subsequent_calls(self):
with pytest.raises(IOError):
omniparse.extract_from_path("nonexistent.txt")
result = omniparse.extract_from_path("test_data/text/sample.txt")
assert result.mime_type == "text/plain"
def test_multiple_errors_in_sequence(self):
with pytest.raises(IOError):
omniparse.extract_from_path("missing1.txt")
with pytest.raises(IOError):
omniparse.extract_from_path("missing2.txt")
with pytest.raises(ValueError):
omniparse.extract_from_bytes(b"\x00\x01")
result = omniparse.extract_from_path("test_data/text/sample.json")
assert result.mime_type == "application/json"
class TestExceptionTypes:
def test_io_error_type(self):
with pytest.raises(IOError):
omniparse.extract_from_path("nonexistent.pdf")
try:
omniparse.extract_from_path("nonexistent.pdf")
except IOError:
pass except ValueError:
pytest.fail("Should raise IOError, not ValueError")
except RuntimeError:
pytest.fail("Should raise IOError, not RuntimeError")
def test_value_error_type(self):
with pytest.raises(ValueError):
omniparse.extract_from_bytes(b"\x00\x01\x02")
try:
omniparse.extract_from_bytes(b"\x00\x01\x02")
except ValueError:
pass except IOError:
pytest.fail("Should raise ValueError, not IOError")
except RuntimeError:
pytest.fail("Should raise ValueError, not RuntimeError")