Spaces:
Sleeping
Sleeping
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)
|