File size: 2,198 Bytes
3468bd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for generic dataset preparation."""

from __future__ import annotations

import csv
from pathlib import Path

from environment.data_prep import prepare_dataset


def _write_sample_csv(path: Path) -> None:
    rows = [
        {"id": 1, "target": "yes", "age": 22, "city": "Paris", "signup_date": "2024-01-15", "notes": "first customer"},
        {"id": 2, "target": "no", "age": "", "city": "Berlin", "signup_date": "2024-01-16", "notes": "repeat buyer"},
        {"id": 3, "target": "yes", "age": 41, "city": "", "signup_date": "2024-01-17", "notes": "vip account"},
        {"id": 4, "target": "no", "age": 35, "city": "Paris", "signup_date": "2024-01-18", "notes": "new lead"},
    ]
    with path.open("w", newline="", encoding="utf-8") as handle:
        writer = csv.DictWriter(handle, fieldnames=rows[0].keys())
        writer.writeheader()
        writer.writerows(rows)


def test_prepare_dataset_writes_artifacts(tmp_path: Path) -> None:
    csv_path = tmp_path / "sample.csv"
    output_dir = tmp_path / "prepared"
    _write_sample_csv(csv_path)

    artifacts = prepare_dataset(
        csv_path=str(csv_path),
        target_column="target",
        output_dir=str(output_dir),
        validation_fraction=0.25,
        random_seed=7,
    )

    payload = artifacts.as_dict()
    assert Path(payload["prepared_full_path"]).exists()
    assert Path(payload["prepared_train_path"]).exists()
    assert Path(payload["prepared_valid_path"]).exists()
    assert Path(payload["manifest_path"]).exists()
    assert payload["target_column"] == "target"
    assert payload["ready_for_training"] is True
    assert payload["feature_count"] > 0


def test_prepare_dataset_endpoint(test_client, tmp_path: Path) -> None:
    csv_path = tmp_path / "sample.csv"
    _write_sample_csv(csv_path)

    response = test_client.post(
        "/prepare-dataset",
        json={
            "csv_path": str(csv_path),
            "target_column": "target",
            "output_dir": str(tmp_path / "out"),
        },
    )

    assert response.status_code == 200
    payload = response.json()
    assert payload["ready_for_training"] is True
    assert Path(payload["prepared_full_path"]).exists()