Skip to content

Commit 34a27ac

Browse files
committed
test: add sample file to read
1 parent 717b72a commit 34a27ac

5 files changed

Lines changed: 214 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ requires = ["hatchling"]
2323
build-backend = "hatchling.build"
2424

2525
[dependency-groups]
26-
dev = ["pyright>=1.1.406", "pytest>=8.4.2", "ruff>=0.14.1"]
26+
dev = [
27+
"pyright>=1.1.406",
28+
"pytest>=8.4.2",
29+
"requests>=2.32.5",
30+
"ruff>=0.14.1",
31+
]
2732

2833
[tool.pyright]
2934
typeCheckingMode = "strict"

tests/conftest.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Fixtures (sample files) for tests."""
2+
3+
import hashlib
4+
import tempfile
5+
from collections.abc import Iterator
6+
from pathlib import Path
7+
8+
import pytest
9+
import requests
10+
11+
12+
def _sha256_checksum(file_path: Path) -> str:
13+
"""Compute the SHA-256 checksum of a file.
14+
15+
:param file_path: The path to the file.
16+
:return: The SHA-256 checksum as a hexadecimal string.
17+
"""
18+
sha256 = hashlib.sha256()
19+
with file_path.open("rb") as f:
20+
for chunk in iter(lambda: f.read(8192), b""):
21+
sha256.update(chunk)
22+
return sha256.hexdigest()
23+
24+
25+
@pytest.fixture
26+
def sample_db_1() -> Iterator[Path]:
27+
"""Give a sample access database file for tests.
28+
29+
Download the Access Example .accdb file to a temporary directory,
30+
yield its path for tests, and delete it afterward.
31+
"""
32+
url = "https://github.com/Access-projects/Access-examples/raw/refs/heads/master/Access_Example_VBA.accdb"
33+
with tempfile.TemporaryDirectory() as temp_dir_str:
34+
db_path = Path(temp_dir_str) / "Access_Example_VBA.accdb"
35+
response = requests.get(url, stream=True, timeout=10)
36+
response.raise_for_status()
37+
38+
db_path.write_bytes(response.content)
39+
assert (
40+
_sha256_checksum(db_path)
41+
== "3cd1fc88d70bc93909ae6d190fa9692e709d5641b205fd2f66185c905e42cfbf"
42+
), "Downloaded file checksum does not match expected value."
43+
44+
yield db_path # Provide file path to test.
45+
# Tempdir automatically cleaned up on exit.

tests/test_list_table_names.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Test the `list_table_names()` function."""
2+
3+
from pathlib import Path
4+
5+
from polars_access_mdbtools import list_table_names
6+
7+
8+
def test_list_table_names_db_1(sample_db_1: Path) -> None:
9+
table_names = list_table_names(sample_db_1)
10+
assert isinstance(table_names, list)
11+
assert table_names == [
12+
"CMD_LINE_TB",
13+
"CUM_VAL_TB",
14+
"DATABASE_STRUCTURE_TB",
15+
"DECUM_VAL_TB",
16+
"ROULETTE_TB",
17+
"TAG_GRP_TB",
18+
"TAG_NME_TB",
19+
"tblContacts",
20+
"tblDefaults",
21+
"tblFileList",
22+
"USysRibbons",
23+
]

tests/test_read_table.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
"""Tests for reading tables from Access databases."""
22

3+
from pathlib import Path
34

4-
def test_placeholder() -> None:
5-
"""A placeholder test."""
6-
assert True
5+
from polars_access_mdbtools import read_table
6+
7+
8+
def test_can_read_all_tables_in_sample_db_1(sample_db_1: Path) -> None:
9+
"""Test reading all tables in sample_db_1. Check row counts."""
10+
for table_name, expected_rows in [
11+
("CMD_LINE_TB", 14),
12+
("CUM_VAL_TB", 59),
13+
("DATABASE_STRUCTURE_TB", 46),
14+
("DECUM_VAL_TB", 59),
15+
("ROULETTE_TB", 38),
16+
("TAG_GRP_TB", 19),
17+
("TAG_NME_TB", 933),
18+
("tblContacts", 0),
19+
("tblDefaults", 1),
20+
("tblFileList", 0),
21+
("USysRibbons", 2),
22+
]:
23+
df = read_table(sample_db_1, table_name)
24+
assert df.height == expected_rows, (
25+
f"Table {table_name} should have {expected_rows} rows, but had {df.height}."
26+
)

0 commit comments

Comments
 (0)