Spaces:
Running
Running
File size: 11,474 Bytes
9eecab5 | 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | import shutil
import os
import pandas as pd
from data.registry import DatasetRegistry
from data.schema_extractor import extract_schema
from agents.transformer_agent import TransformerAgent
DATASETS_DIR = "data/datasets"
METADATA_DIR = "data/metadata"
DATASETS_BACKUP = "data/datasets_backup"
METADATA_BACKUP = "data/metadata_backup"
passed = 0
failed = 0
def backup():
shutil.copytree(DATASETS_DIR, DATASETS_BACKUP, dirs_exist_ok=True)
shutil.copytree(METADATA_DIR, METADATA_BACKUP, dirs_exist_ok=True)
def restore():
shutil.rmtree(DATASETS_DIR)
shutil.rmtree(METADATA_DIR)
shutil.copytree(DATASETS_BACKUP, DATASETS_DIR)
shutil.copytree(METADATA_BACKUP, METADATA_DIR)
shutil.rmtree(DATASETS_BACKUP, ignore_errors=True)
shutil.rmtree(METADATA_BACKUP, ignore_errors=True)
def fresh():
return DatasetRegistry(), None
def fresh_agent():
registry = DatasetRegistry()
return registry, TransformerAgent(registry)
def run_test(label, query, check_fn, agent):
global passed, failed
result = agent.handle(query)
try:
ok = check_fn(result, agent)
except Exception as e:
ok = False
print(f" [check error] {e}")
tag = "[PASS]" if ok else "[FAIL]"
print(f"{tag} {label}")
print(f" Query : {query}")
print(f" Result : {result}\n")
if ok:
passed += 1
else:
failed += 1
print("=" * 60)
print(" Transformer Agent Test Suite")
print("=" * 60)
backup()
try:
# ββ SAFETY: ORIGINAL IS NEVER MODIFIED βββββββββββββββββ
print("--- Safety: original dataset is never modified ---\n")
registry, agent = fresh_agent()
original_shape = registry.load_dataframe("products").shape
agent.handle("drop duplicates in products")
original_after = registry.load_dataframe("products").shape
clean_exists = "products_clean" in registry.list_datasets()
ok = (original_after == original_shape) and clean_exists
print(f"{'[PASS]' if ok else '[FAIL]'} Original unchanged; products_clean created")
print(f" Original shape before : {original_shape}")
print(f" Original shape after : {original_after}")
print(f" products_clean exists : {clean_exists}\n")
passed += ok
failed += (not ok)
restore(); backup()
# ββ CLEANING: DROP DUPLICATES βββββββββββββββββββββββββββ
print("--- Cleaning: Drop Duplicates ---\n")
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df_with_dups = pd.concat([df, df.head(10)], ignore_index=True)
registry.update_dataset("products", df_with_dups, extract_schema(df_with_dups))
run_test(
label="Drop 10 injected duplicate rows",
query="drop duplicates in products",
check_fn=lambda result, ag: (
"dropped 10" in result.lower() and
ag.registry.load_dataframe("products_clean").duplicated().sum() == 0
),
agent=agent,
)
restore(); backup()
registry, agent = fresh_agent()
run_test(
label="No duplicates present β reports 0 dropped",
query="drop duplicates in products",
check_fn=lambda result, ag: "dropped 0" in result.lower(),
agent=agent,
)
restore(); backup()
# ββ CLEANING: FILL NULLS ββββββββββββββββββββββββββββββββ
print("--- Cleaning: Fill Nulls ---\n")
# symmetric numeric (|skew| < 1) β mean
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df.loc[0:9, "Price"] = None
registry.update_dataset("products", df, extract_schema(df))
run_test(
label="Fill symmetric Price column β uses mean",
query="fill price in products",
check_fn=lambda result, ag: (
"mean" in result.lower() and
ag.registry.load_dataframe("products_clean")["Price"].isnull().sum() == 0
),
agent=agent,
)
restore(); backup()
# skewed numeric (|skew| >= 1) β median
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df["Price"] = df["Price"].astype(float)
df.loc[0:9, "Price"] = None
df.loc[10:, "Price"] = df.loc[10:, "Price"] ** 3
registry.update_dataset("products", df, extract_schema(df))
run_test(
label="Fill skewed Price column β uses median",
query="fill price in products",
check_fn=lambda result, ag: (
"median" in result.lower() and
ag.registry.load_dataframe("products_clean")["Price"].isnull().sum() == 0
),
agent=agent,
)
restore(); backup()
# categorical β mode
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df.loc[0:9, "Category"] = None
registry.update_dataset("products", df, extract_schema(df))
run_test(
label="Fill categorical Category column β uses mode",
query="fill category in products",
check_fn=lambda result, ag: (
"mode" in result.lower() and
ag.registry.load_dataframe("products_clean")["Category"].isnull().sum() == 0
),
agent=agent,
)
restore(); backup()
# fill all columns at once
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df.loc[0:9, "Price"] = None
df.loc[0:4, "Category"] = None
registry.update_dataset("products", df, extract_schema(df))
run_test(
label="Fill all nulls across every column in one call",
query="fill nulls in products",
check_fn=lambda result, ag: (
"filled" in result.lower() and
ag.registry.load_dataframe("products_clean").isnull().sum().sum() == 0
),
agent=agent,
)
restore(); backup()
# column with no nulls
registry, agent = fresh_agent()
run_test(
label="Fill column with no nulls β no-op message",
query="fill price in products",
check_fn=lambda result, ag: "no missing" in result.lower(),
agent=agent,
)
restore(); backup()
# ββ CLEANING: DROP CONSTANT COLUMNS ββββββββββββββββββββ
print("--- Cleaning: Drop Constant Columns ---\n")
# Currency is constant (USD) in the original products data
registry, agent = fresh_agent()
run_test(
label="Drop existing constant column (Currency=USD)",
query="drop constant columns in products",
check_fn=lambda result, ag: (
"currency" in result.lower() and
"Currency" not in ag.registry.load_dataframe("products_clean").columns
),
agent=agent,
)
restore(); backup()
# inject an additional constant column
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df["TestConst"] = 0
registry.update_dataset("products", df, extract_schema(df))
run_test(
label="Drop multiple constant columns (Currency + injected TestConst)",
query="drop constant columns in products",
check_fn=lambda result, ag: (
"testconst" in result.lower() and
"TestConst" not in ag.registry.load_dataframe("products_clean").columns and
"Currency" not in ag.registry.load_dataframe("products_clean").columns
),
agent=agent,
)
restore(); backup()
# ββ CLEANING: STRIP WHITESPACE ββββββββββββββββββββββββββ
print("--- Cleaning: Strip Whitespace ---\n")
registry, agent = fresh_agent()
df = registry.load_dataframe("products")
df["Name"] = " " + df["Name"].astype(str) + " "
registry.update_dataset("products", df, extract_schema(df))
run_test(
label="Strip whitespace from string columns",
query="strip whitespace in products",
check_fn=lambda result, ag: (
"stripped" in result.lower() and
not ag.registry.load_dataframe("products_clean")["Name"]
.str.startswith(" ").any()
),
agent=agent,
)
restore(); backup()
# ββ CLEANING: DROP COLUMN βββββββββββββββββββββββββββββββ
print("--- Cleaning: Drop Column ---\n")
registry, agent = fresh_agent()
run_test(
label="Drop Description column",
query="drop description in products",
check_fn=lambda result, ag: (
"dropped" in result.lower() and
"Description" not in ag.registry.load_dataframe("products_clean").columns
),
agent=agent,
)
run_test(
label="Drop non-existent column β not found",
query="drop ghostcol in products",
check_fn=lambda result, ag: "not found" in result.lower(),
agent=agent,
)
restore(); backup()
# ββ TRANSFORMATIONS βββββββββββββββββββββββββββββββββββββ
print("--- Transformations (secondary) ---\n")
registry, agent = fresh_agent()
run_test(
label="Normalize Price β [0, 1]",
query="normalize price in products",
check_fn=lambda result, ag: (
"normalized" in result.lower() and
ag.registry.load_dataframe("products_clean")["Price"].between(0, 1).all()
),
agent=agent,
)
run_test(
label="Normalize non-numeric column β blocked",
query="normalize category in products",
check_fn=lambda result, ag: "not numeric" in result.lower(),
agent=agent,
)
restore(); backup()
registry, agent = fresh_agent()
run_test(
label="Encode Category β integer codes",
query="encode category in products",
check_fn=lambda result, ag: (
"label-encoded" in result.lower() and
pd.api.types.is_integer_dtype(
ag.registry.load_dataframe("products_clean")["Category"]
)
),
agent=agent,
)
run_test(
label="Encode numeric column β blocked",
query="encode price in products",
check_fn=lambda result, ag: "not categorical" in result.lower(),
agent=agent,
)
restore(); backup()
registry, agent = fresh_agent()
run_test(
label="Rename Stock to inventory",
query="rename stock to inventory in products",
check_fn=lambda result, ag: (
"renamed" in result.lower() and
"inventory" in ag.registry.load_dataframe("products_clean").columns and
"Stock" not in ag.registry.load_dataframe("products_clean").columns
),
agent=agent,
)
restore(); backup()
# ββ EDGE CASES ββββββββββββββββββββββββββββββββββββββββββ
print("--- Edge Cases ---\n")
registry, agent = fresh_agent()
run_test(
label="Unknown operation β fallback message",
query="sort price in products",
check_fn=lambda result, ag: "not understood" in result.lower(),
agent=agent,
)
finally:
restore()
print("=" * 60)
print(f"Results: {passed} passed, {failed} failed")
if failed == 0:
print("All tests passed.")
print("=" * 60)
|