Spaces:
Runtime error
Runtime error
File size: 9,396 Bytes
5fc6e5d |
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
import os
from pathlib import Path
import sys
import numpy as np
import pandas as pd
import pytest
import turing.config as config
from turing.dataset import DatasetManager
from turing.reporting import TestReportGenerator
# --- Path Setup ---
script_dir = os.path.dirname(os.path.abspath(__file__))
proj_root = os.path.dirname(os.path.dirname(script_dir))
sys.path.append(proj_root)
train_dir = os.path.join(proj_root, "turing", "modeling")
sys.path.insert(1, train_dir)
try:
# Import train.py
import turing.modeling.train as train
except ImportError as e:
pytest.skip(
f"Could not import 'train.py'. Check sys.path. Error: {e}", allow_module_level=True
)
# --- Reporting Setup ---
execution_results = []
active_categories = set()
def clean_test_name(nodeid):
"""Pulisce il nome del test rimuovendo parametri lunghi."""
parts = nodeid.split("::")
test_name = parts[-1]
if len(test_name) > 50:
test_name = test_name[:47] + "..."
return test_name
def format_error_message(long_repr):
"""Estrae solo l'errore principale."""
if not long_repr:
return ""
lines = str(long_repr).split("\n")
last_line = lines[-1]
clean_msg = last_line.replace("|", "-").strip()
if len(clean_msg) > 60:
clean_msg = clean_msg[:57] + "..."
return clean_msg
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call":
path_str = str(item.fspath)
category = "GENERAL"
if "unit" in path_str:
category = "UNIT"
elif "behavioral" in path_str:
category = "BEHAVIORAL"
elif "modeling" in path_str:
category = "MODELING"
active_categories.add(category)
# Simplified status mapping
status_map = {"passed": "PASS", "failed": "FAIL", "skipped": "SKIP"}
status_str = status_map.get(report.outcome, report.outcome.upper())
execution_results.append(
{
"Category": category,
"Module": item.fspath.basename,
"Test Case": clean_test_name(item.nodeid),
"Result": status_str,
"Time": f"{report.duration:.2f}s",
"Message": format_error_message(report.longrepr) if report.failed else "",
}
)
def pytest_sessionfinish(session, exitstatus):
"""Generate enhanced test report at session end."""
if not execution_results:
return
report_type = (
f"{list(active_categories)[0].lower()}_tests"
if len(active_categories) == 1
else "unit_and_behavioral_tests"
)
try:
manager = TestReportGenerator(context_name="turing", report_category=report_type)
# Main title
manager.add_header("Turing Test Execution Report")
manager.add_divider("section")
# Environment info
manager.add_environment_metadata()
manager.add_divider("thin")
df = pd.DataFrame(execution_results)
# Sommario
total = len(df)
passed = len(df[df["Result"] == "[ PASS ]"])
failed = len(df[df["Result"] == "[ FAILED ]"])
summary = pd.DataFrame(
[
{
"Total": total,
"Passed": passed,
"Failed": failed,
"Success Rate": f"{(passed / total) * 100:.1f}%",
}
]
)
manager.add_dataframe(summary, title="Executive Summary")
# Detailed breakdown by category
cols = ["Module", "Test Case", "Result", "Time", "Message"]
if len(active_categories) > 1:
manager.add_header("Detailed Test Results by Category", level=2)
manager.add_divider("thin")
for cat in sorted(active_categories):
subset = df[df["Category"] == cat][cols]
manager.add_dataframe(subset, title=f"{cat} Tests")
else:
manager.add_alert_box(
"All tests passed successfully!",
box_type="success"
)
manager.save("report.md")
except Exception as e:
print(f"\nError generating report: {e}")
# --- Fixtures ---
@pytest.fixture(scope="function")
def manager() -> DatasetManager:
"""
Provides a instance of DatasetManager for each test.
"""
return DatasetManager()
@pytest.fixture(scope="function")
def fake_csv_data_dir(tmp_path: Path) -> Path:
"""
Creates a temporary directory structure mocking 'data/interim/features/clean-aug-soft-k5000'
and populates it with minimal, valid CSV files for testing.
Returns:
Path: The path to the *parent* of 'features' (e.g., the mocked INTERIM_DATA_DIR).
"""
interim_dir = tmp_path / "interim_test"
features_dir = interim_dir / "features" / "clean-aug-soft-k5000"
features_dir.mkdir(parents=True, exist_ok=True)
# Define minimal valid CSV content
csv_content = (
"combo,labels\n"
'"java code text","[1, 0, 0, 0, 0, 0, 0]"\n'
'"other java code","[0, 1, 0, 0, 0, 0, 0]"\n'
)
# Write mock files
(features_dir / "java_train.csv").write_text(csv_content)
(features_dir / "java_test.csv").write_text(csv_content)
# Return the root of the mocked interim directory
return interim_dir
@pytest.fixture(scope="session")
def mock_data():
"""
Provides a minimal, consistent, session-scoped dataset for model testing.
This simulates the (X, y) data structure used for training and evaluation.
"""
X = [
"this is java code for summary",
"python is great for parameters",
"a java example for usage",
"running python script for development notes",
"pharo is a language for intent",
"another java rational example",
]
# Mock labels for a 'java' model (7 categories)
# Shape (6 samples, 7 features)
y = np.array(
[
[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 1],
]
)
return {"X": X, "y": y}
@pytest.fixture(scope="module")
def trained_rf_model(mock_data, tmp_path_factory):
"""
Provides a fully-trained RandomForestTfIdf model instance.
"""
# Import locally to ensure proj_root is set
from modeling.models.randomForestTfIdf import RandomForestTfIdf
# Arrange
model = RandomForestTfIdf(language="java")
# Monkeypatch grid search parameters for maximum speed
model.grid_params = {
"tfidf__max_features": [10, 20], # Use minimal features
"clf__estimator__n_estimators": [2, 5], # Use minimal trees
}
model.params["cv_folds"] = 2 # Use minimal CV folds
# Create a persistent temp dir for this module's run
model_path = tmp_path_factory.mktemp("trained_rf_model")
# Act: Train the model
model.train(mock_data["X"], mock_data["y"], path=str(model_path), model_name="test_model")
# Yield the trained model and its save path
yield model, model_path
MODEL_CLASS_TO_TEST = train.MODEL_CLASS
MODEL_EXPERIMENT_NAME = train.EXP_NAME
MODEL_NAME_BASE = train.MODEL_NAME
@pytest.fixture(scope="session")
def get_predicted_labels():
def _helper(model, comment_sentence: str, lang: str) -> set:
if config.INPUT_COLUMN == "combo":
combo_input = f"DummyClass.{lang} | {comment_sentence}"
input_data = [combo_input]
else:
input_data = [comment_sentence]
prediction_array = model.predict(input_data)[0]
labels_map = config.LABELS_MAP[lang]
predicted_labels = {labels_map[i] for i, val in enumerate(prediction_array) if val == 1}
return predicted_labels
return _helper
@pytest.fixture(scope="module")
def java_model():
"""Loads the Java model from the config path"""
model_path = os.path.join(config.MODELS_DIR, MODEL_EXPERIMENT_NAME, f"{MODEL_NAME_BASE}_java")
if not os.path.exists(model_path):
pytest.skip(
"Production model not found. Skipping behavioral tests for Java.",
allow_module_level=True,
)
return MODEL_CLASS_TO_TEST(language="java", path=model_path)
@pytest.fixture(scope="module")
def python_model():
"""Loads the Python model from the config path"""
model_path = os.path.join(
config.MODELS_DIR, MODEL_EXPERIMENT_NAME, f"{MODEL_NAME_BASE}_python"
)
if not os.path.exists(model_path):
pytest.skip(
"Production model not found. Skipping behavioral tests for Python.",
allow_module_level=True,
)
return MODEL_CLASS_TO_TEST(language="python", path=model_path)
@pytest.fixture(scope="module")
def pharo_model():
"""Loads the Pharo model from the config path"""
model_path = os.path.join(config.MODELS_DIR, MODEL_EXPERIMENT_NAME, f"{MODEL_NAME_BASE}_pharo")
if not os.path.exists(model_path):
pytest.skip(
"Production model not found. Skipping behavioral tests for Pharo.",
allow_module_level=True,
)
return MODEL_CLASS_TO_TEST(language="pharo", path=model_path)
|