File size: 2,124 Bytes
531d507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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