File size: 5,725 Bytes
c8df794
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Comprehensive test suite for the cleaned project
"""
import os
import sys
import importlib
import subprocess
from pathlib import Path

# Add project root to Python path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

def test_project_structure():
    """Test that all required files and directories exist"""
    print("πŸ” Testing project structure...")
    
    required_files = [
        "requirements.txt",
        "README.md",
        "setup.py",
        ".gitignore",
        "docker-compose.yml",
        "Makefile",
        "crop_disease_gui.py",
        "src/__init__.py",
        "src/model.py",
        "src/dataset.py",
        "src/train.py",
        "src/evaluate.py",
        "src/explain.py",
        "src/risk_level.py",
        "api/main.py",
        "api/Dockerfile",
        "models/crop_disease_v3_model.pth",
        "knowledge_base/disease_info.json",
        "test_leaf_sample.jpg"
    ]
    
    required_dirs = [
        "src",
        "api", 
        "models",
        "knowledge_base",
        "data",
        "outputs",
        "notebooks",
        "tests"
    ]
    
    missing_files = []
    missing_dirs = []
    
    for file_path in required_files:
        if not Path(file_path).exists():
            missing_files.append(file_path)
    
    for dir_path in required_dirs:
        if not Path(dir_path).exists():
            missing_dirs.append(dir_path)
    
    if missing_files:
        print(f"❌ Missing files: {missing_files}")
        return False
    
    if missing_dirs:
        print(f"❌ Missing directories: {missing_dirs}")
        return False
    
    print("βœ… Project structure is complete")
    return True

def test_imports():
    """Test that all Python modules can be imported"""
    print("\nπŸ” Testing Python imports...")
    
    modules_to_test = [
        "src.model",
        "src.dataset", 
        "src.train",
        "src.evaluate",
        "src.explain",
        "src.risk_level"
    ]
    
    failed_imports = []
    
    for module in modules_to_test:
        try:
            importlib.import_module(module)
            print(f"βœ… {module}")
        except ImportError as e:
            print(f"❌ {module}: {e}")
            failed_imports.append(module)
    
    if failed_imports:
        print(f"❌ Failed to import: {failed_imports}")
        return False
    
    print("βœ… All imports successful")
    return True

def test_api_startup():
    """Test that the API can start"""
    print("\nπŸ” Testing API startup...")
    
    try:
        # Test import of API main
        from api.main import app
        print("βœ… API imports successfully")
        
        # Test health endpoint exists
        print("βœ… API structure is valid")
        return True
        
    except Exception as e:
        print(f"❌ API startup failed: {e}")
        return False

def test_model_loading():
    """Test that the model can be loaded"""
    print("\nπŸ” Testing model loading...")
    
    try:
        import torch
        from src.model import CropDiseaseResNet50
        
        # Test model creation
        model = CropDiseaseResNet50(num_classes=17)
        print("βœ… Model architecture created")
        
        # Test model file exists
        model_path = "models/crop_disease_v3_model.pth"
        if Path(model_path).exists():
            # Try to load the model
            checkpoint = torch.load(model_path, map_location='cpu')
            print("βœ… Model file loads successfully")
            return True
        else:
            print("❌ Model file not found")
            return False
            
    except Exception as e:
        print(f"❌ Model loading failed: {e}")
        return False

def test_cleanup_completion():
    """Test that cleanup was successful"""
    print("\nπŸ” Testing cleanup completion...")
    
    # Check that cache files are gone
    cache_dirs = list(Path(".").rglob("__pycache__"))
    if cache_dirs:
        print(f"❌ Cache directories still exist: {cache_dirs}")
        return False
    
    # Check that backup files are gone
    backup_files = [
        "src/explain_backup.py"
    ]
    
    existing_backups = [f for f in backup_files if Path(f).exists()]
    if existing_backups:
        print(f"❌ Backup files still exist: {existing_backups}")
        return False
    
    print("βœ… Cleanup completed successfully")
    return True

def main():
    """Run all tests"""
    print("πŸ§ͺ COMPREHENSIVE PROJECT TEST SUITE")
    print("=" * 50)
    
    tests = [
        ("Project Structure", test_project_structure),
        ("Python Imports", test_imports),
        ("API Startup", test_api_startup),
        ("Model Loading", test_model_loading),
        ("Cleanup Completion", test_cleanup_completion)
    ]
    
    passed = 0
    total = len(tests)
    
    for test_name, test_func in tests:
        print(f"\n🎯 Running: {test_name}")
        print("-" * 30)
        if test_func():
            passed += 1
        else:
            print(f"❌ {test_name} FAILED")
    
    print("\n" + "=" * 50)
    print(f"πŸ“Š TEST RESULTS: {passed}/{total} tests passed")
    print("=" * 50)
    
    if passed == total:
        print("πŸŽ‰ ALL TESTS PASSED - Project is ready for development!")
        print("\nπŸ“ Next steps:")
        print("   1. Start API: python -m uvicorn api.main:app --reload")
        print("   2. Start GUI: python crop_disease_gui.py")
        print("   3. Run training: python src/train.py")
        return True
    else:
        print("⚠️  Some tests failed. Please check the issues above.")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)