File size: 2,355 Bytes
c0608db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3624c7d
 
 
 
 
 
 
c0608db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
import pandas as pd
import pytest

from tabicl_mcp import data as D


def test_parse_csv_basic():
    df = D.parse_csv("a,b\n1,x\n2,y\n")
    assert list(df.columns) == ["a", "b"]
    assert len(df) == 2


def test_parse_csv_bom_and_whitespace():
    df = D.parse_csv("a,b\n1,2\n")
    assert list(df.columns) == ["a", "b"]


def test_parse_csv_empty():
    with pytest.raises(D.DataError):
        D.parse_csv("   ")


def test_parse_csv_single_column_rejected():
    with pytest.raises(D.DataError, match="single column"):
        D.parse_csv("a\n1\n2\n")


def test_parse_csv_row_limit():
    rows = "\n".join(f"{i},{i}" for i in range(30))
    with pytest.raises(D.DataError, match="row limit"):
        D.parse_csv("a,b\n" + rows, max_rows=10)


def test_normalize_google_sheets_url():
    url = "https://docs.google.com/spreadsheets/d/abc123XYZ/edit?usp=sharing#gid=42"
    assert (
        D.normalize_url(url)
        == "https://docs.google.com/spreadsheets/d/abc123XYZ/export?format=csv&gid=42"
    )


def test_normalize_github_blob_url():
    url = "https://github.com/user/repo/blob/main/data/x.csv"
    assert D.normalize_url(url) == "https://raw.githubusercontent.com/user/repo/main/data/x.csv"


def test_normalize_plain_url_untouched():
    assert D.normalize_url("https://example.com/x.csv") == "https://example.com/x.csv"


def test_load_from_path_chat_upload_guidance():
    # A path that only exists in an assistant's sandbox must produce an error
    # that teaches the model to retry with csv_content or url.
    with pytest.raises(D.DataError, match="csv_content"):
        D.load_from_path("/mnt/data/customer_churn.csv")


def test_cache_roundtrip_and_miss():
    df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
    ds_id = D.CACHE.put(df, name="t")
    assert D.CACHE.get(ds_id).df.equals(df)
    with pytest.raises(D.DataError, match="not found"):
        D.CACHE.get("ds_nope")


def test_resolve_requires_exactly_one_source():
    with pytest.raises(D.DataError):
        D.resolve("", "")
    with pytest.raises(D.DataError):
        D.resolve("some_id", "a,b\n1,2")


def test_summarize_with_target():
    df = pd.DataFrame({"x": [1.0, 2.0, 3.0], "y": ["a", "b", "a"]})
    s = D.summarize(df, "y")
    assert s["n_rows"] == 3
    assert s["target"]["suggested_task"] == "classification"
    assert len(s["preview"]) == 3