|
1 | 1 | import pandas as pd |
2 | | -from contextlens.analysis import profile_dataset # adjust to your actual function name |
3 | 2 |
|
4 | | -def test_profile_runs_on_simple_dataframe(): |
5 | | - df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]}) |
6 | | - result = profile_dataset(df) |
7 | | - assert result is not None |
| 3 | +from analysis import analyse_dataset, build_context_narrative, infer_task_type |
| 4 | + |
| 5 | + |
| 6 | +def test_infer_task_type_classification_on_categorical_target(): |
| 7 | + target = pd.Series(["cat", "dog", "cat", "dog", "cat"]) |
| 8 | + task, reason = infer_task_type(target) |
| 9 | + assert task == "classification" |
| 10 | + assert isinstance(reason, str) |
| 11 | + |
| 12 | + |
| 13 | +def test_infer_task_type_regression_on_continuous_target(): |
| 14 | + target = pd.Series(range(100)) + 0.5 |
| 15 | + task, reason = infer_task_type(target) |
| 16 | + assert task == "regression" |
| 17 | + |
| 18 | + |
| 19 | +def test_analyse_dataset_runs_on_simple_dataframe(): |
| 20 | + df = pd.DataFrame( |
| 21 | + { |
| 22 | + "feature_a": [1, 2, 3, 4, 5], |
| 23 | + "feature_b": ["x", "y", "x", "y", "x"], |
| 24 | + "target": [0, 1, 0, 1, 0], |
| 25 | + } |
| 26 | + ) |
| 27 | + result = analyse_dataset(df, target="target", task="classification") |
| 28 | + |
| 29 | + assert result["shape"]["rows"] == 5 |
| 30 | + assert result["target"] == "target" |
| 31 | + assert result["task"] == "classification" |
| 32 | + assert "feature_a" in result["columns"]["numeric"] |
| 33 | + assert "feature_b" in result["columns"]["categorical"] |
| 34 | + |
| 35 | + |
| 36 | +def test_analyse_dataset_raises_on_missing_target(): |
| 37 | + df = pd.DataFrame({"a": [1, 2, 3]}) |
| 38 | + try: |
| 39 | + analyse_dataset(df, target="not_a_column", task="classification") |
| 40 | + assert False, "expected ValueError" |
| 41 | + except ValueError: |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +def test_build_context_narrative_produces_summary(): |
| 46 | + df = pd.DataFrame( |
| 47 | + { |
| 48 | + "feature_a": [1, 2, 3, 4, 5], |
| 49 | + "target": [0, 1, 0, 1, 0], |
| 50 | + } |
| 51 | + ) |
| 52 | + analysis = analyse_dataset(df, target="target", task="classification") |
| 53 | + narrative = build_context_narrative(analysis) |
| 54 | + |
| 55 | + assert "summary" in narrative |
| 56 | + assert "observations" in narrative |
| 57 | + assert "evaluation_guidance" in narrative |
| 58 | + assert len(narrative["observations"]) > 0 |
0 commit comments