|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | + |
| 5 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../src"))) |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +from src.data.loader import DataLoader |
| 9 | +from src.data.preprocessor import DataPreprocessor |
| 10 | + |
| 11 | + |
| 12 | +def test_data_loader_real(): |
| 13 | + orig_train = os.path.join("data", "processed", "train.csv") |
| 14 | + orig_test = os.path.join("data", "processed", "test.csv") |
| 15 | + |
| 16 | + df_train = pd.read_csv(orig_train, nrows=200) |
| 17 | + df_test = pd.read_csv(orig_test, nrows=100) |
| 18 | + |
| 19 | + with tempfile.TemporaryDirectory() as tmp: |
| 20 | + df_train.to_csv(os.path.join(tmp, "train.csv"), index=False) |
| 21 | + df_test.to_csv(os.path.join(tmp, "test.csv"), index=False) |
| 22 | + |
| 23 | + loader = DataLoader(data_dir=tmp) |
| 24 | + loader.load_data("train.csv", "test.csv") |
| 25 | + train, valid, test = loader.train_valid_split(0.2) |
| 26 | + |
| 27 | + # Train dataset should remain the same, valid and test should be split |
| 28 | + assert train.shape[0] == 200 |
| 29 | + assert valid.shape[0] == 20 |
| 30 | + assert test.shape[0] == 80 |
| 31 | + |
| 32 | + |
| 33 | +def detect_date_columns(df): |
| 34 | + date_columns = [] |
| 35 | + for col in df.columns: |
| 36 | + try: |
| 37 | + temp_series = pd.to_datetime(df[col], errors="coerce") |
| 38 | + if temp_series.notna().any(): |
| 39 | + date_columns.append(col) |
| 40 | + except Exception: |
| 41 | + pass |
| 42 | + return date_columns |
| 43 | + |
| 44 | + |
| 45 | +def test_preprocessor(): |
| 46 | + |
| 47 | + d = DataLoader("data/processed") |
| 48 | + |
| 49 | + d.load_data("train.csv", "test.csv") |
| 50 | + |
| 51 | + train, val, test = d.train_valid_split(0.2) |
| 52 | + |
| 53 | + date_columns = detect_date_columns(train.copy()) |
| 54 | + |
| 55 | + categorical = [ |
| 56 | + col |
| 57 | + for col in train.select_dtypes(include=["object"]).columns |
| 58 | + if col not in date_columns |
| 59 | + ] |
| 60 | + numerical = train.select_dtypes(include=["float64", "int64"]).columns |
| 61 | + |
| 62 | + p = DataPreprocessor(categorical, numerical, "is_fraud", date_columns) |
| 63 | + |
| 64 | + p = p.fit(train) |
| 65 | + X, y = p.transform(train) |
| 66 | + |
| 67 | + assert X.shape[0] == train.shape[0] |
| 68 | + assert y.shape[0] == train.shape[0] |
| 69 | + |
| 70 | + assert X.shape[1] == 9 |
| 71 | + |
| 72 | + |
| 73 | +def main(): |
| 74 | + try: |
| 75 | + test_data_loader_real() |
| 76 | + except Exception as e: |
| 77 | + print(f"Test failed: {e}", file=sys.stderr) |
| 78 | + sys.exit(1) |
| 79 | + |
| 80 | + try: |
| 81 | + test_preprocessor() |
| 82 | + except Exception as e: |
| 83 | + print(f"Test failed: {e}", file=sys.stderr) |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | + print("All DataLoader & Preprocessor tests passed.") |
| 87 | + sys.exit(0) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments