Skip to content

Commit d88f28a

Browse files
committed
test: ensure "table not found" raises ValueError properly
1 parent 3d6fb2a commit d88f28a

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/polars_access_mdbtools/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,31 @@ def _read_table_mdb_schema(
125125
table_name,
126126
str(db_path),
127127
]
128-
cmd_output = subprocess.check_output(cmd) # noqa: S603
128+
try:
129+
cmd_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) # noqa: S603
130+
except subprocess.CalledProcessError as e:
131+
# Remap "table not found" problem.
132+
if b"No table named" in e.output:
133+
msg = f'Table "{table_name}" not found in database.'
134+
raise ValueError(msg) from e
135+
raise
136+
129137
cmd_output = cmd_output.decode(encoding)
130138
lines = cmd_output.splitlines()
131139
schema_ddl = "\n".join(line for line in lines if line and not line.startswith("-"))
132140

133141
create_table_matches = CREATE_TABLE_RE.findall(schema_ddl)
142+
143+
# These failures are likely more related to implementation issues, and less
144+
# about the table being missing.
134145
if len(create_table_matches) == 0:
135146
msg = f'Table schema "{table_name}" not found in "mdb-schema" output.'
136147
raise ValueError(msg)
137148
if len(create_table_matches) > 1:
138-
msg = f'Multiple table schemas found for "{table_name}" in "mdb-schema" output.'
149+
msg = (
150+
f'Multiple table schemas found for "{table_name}" in "mdb-schema" output. '
151+
"Logical error."
152+
)
139153
raise ValueError(msg)
140154

141155
table_name_mdb, defs = create_table_matches[0]

tests/test_read_table.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from pathlib import Path
44

5+
import pytest
6+
57
from polars_access_mdbtools import read_table
68

79

@@ -24,3 +26,13 @@ def test_can_read_all_tables_in_sample_db_1(sample_db_1: Path) -> None:
2426
assert df.height == expected_rows, (
2527
f"Table {table_name} should have {expected_rows} rows, but had {df.height}."
2628
)
29+
30+
31+
def test_read_nonexistent_table_raises_value_error(sample_db_1: Path) -> None:
32+
"""Test that reading a nonexistent table raises ValueError."""
33+
nonexistent_table_name = "NONEXISTENT_TABLE"
34+
with pytest.raises(
35+
ValueError,
36+
match=f'Table "{nonexistent_table_name}" not found in database',
37+
):
38+
read_table(sample_db_1, nonexistent_table_name)

0 commit comments

Comments
 (0)