Spaces:
Running on Zero
Running on Zero
File size: 4,005 Bytes
756aa06 | 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | """Offline tests for CSV validation and input constraints."""
from __future__ import annotations
import io
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
from lab.validation import (
MAX_FILE_BYTES,
MAX_HORIZON,
MAX_POINTS,
ValidationError,
parse_series_csv,
validate_run_params,
)
def _write_csv(tmp_path: Path, name: str, df: pd.DataFrame) -> Path:
path = tmp_path / name
df.to_csv(path, index=False)
return path
def test_parse_numeric_column_and_optional_date(tmp_path: Path) -> None:
df = pd.DataFrame(
{
"fecha": pd.date_range("2024-01-01", periods=40, freq="D"),
"demanda": np.arange(40, dtype=float),
"nota": ["x"] * 40,
}
)
path = _write_csv(tmp_path, "ok.csv", df)
series = parse_series_csv(path, value_column="demanda", date_column="fecha")
assert series.n_points == 40
assert series.value_column == "demanda"
assert series.timestamps is not None
assert len(series.values) == 40
assert np.isfinite(series.values).all()
def test_reject_non_csv_extension(tmp_path: Path) -> None:
path = tmp_path / "data.txt"
path.write_text("a,b\n1,2\n", encoding="utf-8")
with pytest.raises(ValidationError, match="CSV"):
parse_series_csv(path)
def test_reject_path_traversal_style_name(tmp_path: Path) -> None:
# Symlink / relative escape should be rejected when unsafe name is detected.
victim = tmp_path / "secret.csv"
victim.write_text("v\n1\n2\n", encoding="utf-8")
evil = tmp_path / ".." / victim.name
with pytest.raises(ValidationError):
parse_series_csv(evil)
def test_reject_oversized_file(tmp_path: Path) -> None:
path = tmp_path / "big.csv"
# Build a file just over the limit without allocating huge RAM for dataframes.
header = b"value\n"
row = b"1.0\n"
need = MAX_FILE_BYTES - len(header) + 1
with path.open("wb") as f:
f.write(header)
chunk = row * 1000
written = 0
while written < need:
f.write(chunk)
written += len(chunk)
assert path.stat().st_size > MAX_FILE_BYTES
with pytest.raises(ValidationError, match="1 ?MB|tamaño|size"):
parse_series_csv(path)
def test_reject_too_many_points(tmp_path: Path) -> None:
n = MAX_POINTS + 1
df = pd.DataFrame({"value": np.ones(n, dtype=float)})
path = _write_csv(tmp_path, "many.csv", df)
with pytest.raises(ValidationError, match="10000|puntos|points"):
parse_series_csv(path)
def test_reject_nan_and_nonfinite(tmp_path: Path) -> None:
df = pd.DataFrame({"value": [1.0, 2.0, np.nan, 4.0]})
path = _write_csv(tmp_path, "nan.csv", df)
with pytest.raises(ValidationError, match="NaN|finite|finit"):
parse_series_csv(path)
df2 = pd.DataFrame({"value": [1.0, np.inf, 3.0]})
path2 = _write_csv(tmp_path, "inf.csv", df2)
with pytest.raises(ValidationError, match="finite|finit|Inf"):
parse_series_csv(path2)
def test_corrupt_csv_raises(tmp_path: Path) -> None:
path = tmp_path / "bad.csv"
path.write_bytes(b"\xff\xfe not,csv\x00\x01")
with pytest.raises(ValidationError):
parse_series_csv(path)
def test_minimum_context_and_horizon_limits() -> None:
with pytest.raises(ValidationError):
validate_run_params(n_points=10, holdout=5, future_horizon=24, seasonality=7)
with pytest.raises(ValidationError):
validate_run_params(n_points=200, holdout=MAX_HORIZON + 1, future_horizon=24, seasonality=7)
params = validate_run_params(n_points=200, holdout=24, future_horizon=24, seasonality=7)
assert params.holdout == 24
assert params.future_horizon == 24
def test_auto_pick_last_numeric_column(tmp_path: Path) -> None:
df = pd.DataFrame({"a": [1, 2, 3, 4] * 20, "b": [10.0] * 80})
path = _write_csv(tmp_path, "auto.csv", df)
series = parse_series_csv(path, value_column=None)
assert series.value_column == "b"
|