File size: 5,933 Bytes
8a08300
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Quick verification script for Phase 1 implementation.
Tests imports and basic module functionality without requiring Redis.
"""


def test_imports():
    """Test that all Phase 1 modules can be imported."""
    print("Testing imports...")

    try:
        from src.data.ingest import TransactionSchema, InferenceTransactionSchema, load_dataset

        print("  ✓ Data ingestion module imports successfully")
    except ImportError as e:
        print(f"  ✗ Data ingestion import failed: {e}")
        return False

    try:
        from src.features.store import RedisFeatureStore

        print("  ✓ Feature store module imports successfully")
    except ImportError as e:
        print(f"  ✗ Feature store import failed: {e}")
        return False

    try:
        from src.features.constants import category_names, job_names

        print(
            f"  ✓ Constants module loaded ({len(category_names)} categories, {len(job_names)} jobs)"
        )
    except ImportError as e:
        print(f"  ✗ Constants import failed: {e}")
        return False

    return True


def test_pydantic_validation():
    """Test Pydantic validation logic."""
    print("\nTesting Pydantic validation...")

    from src.data.ingest import TransactionSchema, InferenceTransactionSchema
    from pydantic import ValidationError

    # Test valid transaction
    try:
        valid_data = {
            "trans_date_trans_time": "2019-01-01 00:00:18",
            "cc_num": 2703186189652095,
            "merchant": "Test Merchant",
            "category": "misc_net",
            "amt": 100.50,
            "first": "John",
            "last": "Doe",
            "gender": "M",
            "street": "123 Main St",
            "city": "Springfield",
            "state": "IL",
            "zip": 62701,
            "lat": 39.7817,
            "long": -89.6501,
            "city_pop": 100000,
            "job": "Engineer, biomedical",
            "dob": "1990-01-01",
            "trans_num": "abc123",
            "unix_time": 1325376018,
            "merch_lat": 39.7900,
            "merch_long": -89.6600,
            "is_fraud": 0,
        }
        schema = TransactionSchema(**valid_data)
        print(f"  ✓ Valid transaction accepted (amt: ${schema.amt:.2f})")
    except ValidationError as e:
        print(f"  ✗ Valid transaction rejected: {e}")
        return False

    # Test invalid amount
    try:
        invalid_data = valid_data.copy()
        invalid_data["amt"] = -50.00
        schema = TransactionSchema(**invalid_data)
        print("  ✗ Negative amount not rejected!")
        return False
    except ValidationError:
        print("  ✓ Negative amount correctly rejected")

    # Test invalid coordinates
    try:
        invalid_data = valid_data.copy()
        invalid_data["lat"] = 200.0
        schema = TransactionSchema(**invalid_data)
        print("  ✗ Invalid latitude not rejected!")
        return False
    except ValidationError:
        print("  ✓ Invalid latitude correctly rejected")

    # Test inference schema
    try:
        inference_data = {
            "user_id": "u12345",
            "amt": 150.00,
            "lat": 40.7128,
            "long": -74.0060,
            "category": "grocery_pos",
            "job": "Engineer, biomedical",
            "merch_lat": 40.7200,
            "merch_long": -74.0100,
            "unix_time": 1234567890,
        }
        schema = InferenceTransactionSchema(**inference_data)
        print(f"  ✓ Inference schema works (user: {schema.user_id})")
    except ValidationError as e:
        print(f"  ✗ Inference schema failed: {e}")
        return False

    return True


def test_constants():
    """Test that constants are properly loaded."""
    print("\nTesting constants...")

    from src.features.constants import category_names, job_names

    # Check categories
    expected_categories = ["misc_net", "gas_transport", "grocery_pos", "entertainment"]
    for cat in expected_categories:
        if cat not in category_names:
            print(f"  ✗ Missing category: {cat}")
            return False
    print(f"  ✓ All expected categories present ({len(category_names)} total)")

    # Check jobs
    expected_jobs = ["Engineer, biomedical", "Data scientist", "Mechanical engineer"]
    found_jobs = [j for j in expected_jobs if j in job_names]
    print(
        f"  ✓ Job list loaded ({len(job_names)} jobs, {len(found_jobs)}/{len(expected_jobs)} test jobs found)"
    )

    return True


def main():
    """Run all verification tests."""
    print("=" * 60)
    print("Phase 1 Verification Script")
    print("=" * 60)

    tests = [
        ("Imports", test_imports),
        ("Pydantic Validation", test_pydantic_validation),
        ("Constants", test_constants),
    ]

    results = []
    for name, test_func in tests:
        try:
            result = test_func()
            results.append((name, result))
        except Exception as e:
            print(f"\n  ✗ {name} test crashed: {e}")
            results.append((name, False))

    print("\n" + "=" * 60)
    print("Summary")
    print("=" * 60)

    for name, passed in results:
        status = "✓ PASS" if passed else "✗ FAIL"
        print(f"{status:>10} - {name}")

    all_passed = all(r[1] for r in results)

    print("\n" + "=" * 60)
    if all_passed:
        print("✅ All verification tests passed!")
        print("\nPhase 1 implementation is working correctly.")
        print("\nNext steps:")
        print("  1. Install dependencies: uv sync")
        print("  2. Start Redis: docker-compose -f docker/docker-compose.yml up -d redis")
        print("  3. Run full test suite: pytest tests/ -v")
        print("  4. Try demo: python scripts/demo_phase1.py")
    else:
        print("❌ Some tests failed. Check the output above.")
    print("=" * 60)


if __name__ == "__main__":
    main()