table-comparator / tests /test_loader.py
Jakub
Initial commit: table-comparator
531d507
Raw History Blame Contribute Delete
2.12 kB
"""Tests for loader.py – file-format detection and error paths."""
import pandas as pd
import pytest
from table_comparator.loader import load_table
def test_load_csv(tmp_path):
p = tmp_path / "t.csv"
p.write_text("x,y\n1,2\n3,4\n")
df = load_table(str(p))
assert list(df.columns) == ["x", "y"]
assert len(df) == 2
assert df["x"].tolist() == [1, 3]
def test_load_tsv(tmp_path):
p = tmp_path / "t.tsv"
p.write_text("x\ty\n1\t2\n3\t4\n")
df = load_table(str(p))
assert list(df.columns) == ["x", "y"]
assert len(df) == 2
def test_load_excel(tmp_path):
p = tmp_path / "t.xlsx"
pd.DataFrame({"a": [10, 20], "b": [30, 40]}).to_excel(p, index=False)
df = load_table(str(p))
assert list(df.columns) == ["a", "b"]
assert len(df) == 2
assert df["a"].tolist() == [10, 20]
def test_load_xls_extension(tmp_path):
"""Extension is .xls but we write xlsx content – pandas/openpyxl reads it
fine; the point is that the extension dispatching accepts .xls."""
p = tmp_path / "t.xls"
pd.DataFrame({"z": [1]}).to_excel(p, index=False)
df = load_table(str(p))
assert list(df.columns) == ["z"]
def test_load_missing_file():
with pytest.raises(FileNotFoundError):
load_table("/this/path/does/not/exist.csv")
def test_load_unsupported_extension(tmp_path):
p = tmp_path / "data.json"
p.write_text("{}")
with pytest.raises(ValueError, match="Unsupported format"):
load_table(str(p))
def test_load_sample_table_a():
"""Smoke-check that the shipped sample_data/table_a.csv loads correctly."""
from pathlib import Path
path = Path(__file__).resolve().parent.parent / "sample_data" / "table_a.csv"
df = load_table(str(path))
assert len(df) == 10
assert "Name" in df.columns
assert "Transaction Date" in df.columns
def test_load_sample_table_b():
from pathlib import Path
path = Path(__file__).resolve().parent.parent / "sample_data" / "table_b.csv"
df = load_table(str(path))
assert len(df) == 9
assert "Customer" in df.columns
assert "Status" in df.columns