File size: 6,877 Bytes
292ca6d | 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 | #!/usr/bin/env python3
"""
Test script for EpiPred web application
"""
import os
import sys
import tempfile
import json
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
def test_imports():
"""Test if all required modules can be imported"""
print("Testing imports...")
try:
import flask
print(f"β Flask {flask.__version__}")
except ImportError as e:
print(f"β Flask import failed: {e}")
return False
try:
import tensorflow as tf
print(f"β TensorFlow {tf.__version__}")
except ImportError as e:
print(f"β TensorFlow import failed: {e}")
return False
try:
import numpy as np
print(f"β NumPy {np.__version__}")
except ImportError as e:
print(f"β NumPy import failed: {e}")
return False
try:
import pandas as pd
print(f"β Pandas {pd.__version__}")
except ImportError as e:
print(f"β Pandas import failed: {e}")
return False
return True
def test_app_creation():
"""Test if Flask app can be created"""
print("\nTesting Flask app creation...")
try:
from app import app
print("β Flask app created successfully")
return True
except Exception as e:
print(f"β Flask app creation failed: {e}")
return False
def test_model_predictor():
"""Test model predictor initialization"""
print("\nTesting model predictor...")
try:
from model_predictor import EpitopePredictor
# Try to create predictor (may fail if no model files)
try:
predictor = EpitopePredictor()
print("β Model predictor created successfully")
# Test model info
info = predictor.get_model_info()
print(f"β Model info retrieved: {len(info)} parameters")
return True
except Exception as e:
print(f"β Model predictor creation failed (expected if no model files): {e}")
return True # This is expected without model files
except Exception as e:
print(f"β Model predictor import failed: {e}")
return False
def test_fasta_parsing():
"""Test FASTA parsing functionality"""
print("\nTesting FASTA parsing...")
try:
from app import parse_fasta_text, validate_sequences
# Test valid FASTA
test_fasta = """>Test_Sequence_1
MKLLILTCLVAVALARPKHPIKHQGLPQEVLNENLLRFFVAPFPEVFGKEKVNEL
>Test_Sequence_2
CARFASLIYGKFVRQPQVWLRIQNYSVMDICDEHQGVMVPGVGVPQALQKYNPD"""
sequences = parse_fasta_text(test_fasta)
print(f"β Parsed {len(sequences)} sequences")
# Test validation
errors = validate_sequences(sequences)
if not errors:
print("β Sequence validation passed")
else:
print(f"β Validation warnings: {errors}")
return True
except Exception as e:
print(f"β FASTA parsing test failed: {e}")
return False
def test_routes():
"""Test Flask routes"""
print("\nTesting Flask routes...")
try:
from app import app
with app.test_client() as client:
# Test main page
response = client.get('/')
if response.status_code == 200:
print("β Main page route works")
else:
print(f"β Main page returned status {response.status_code}")
return False
# Test instructions page
response = client.get('/instructions')
if response.status_code == 200:
print("β Instructions page route works")
else:
print(f"β Instructions page returned status {response.status_code}")
return False
# Test about page
response = client.get('/about')
if response.status_code == 200:
print("β About page route works")
else:
print(f"β About page returned status {response.status_code}")
return False
return True
except Exception as e:
print(f"β Route testing failed: {e}")
return False
def test_file_structure():
"""Test if all required files exist"""
print("\nTesting file structure...")
required_files = [
'app.py',
'model_predictor.py',
'run.py',
'requirements.txt',
'templates/base.html',
'templates/index.html',
'templates/results.html',
'templates/instructions.html',
'templates/about.html',
'static/css/style.css',
'static/js/main.js'
]
missing_files = []
for file_path in required_files:
if not os.path.exists(file_path):
missing_files.append(file_path)
if missing_files:
print(f"β Missing files: {', '.join(missing_files)}")
return False
else:
print(f"β All {len(required_files)} required files present")
return True
def main():
"""Run all tests"""
print("EpiPred Web Application Test Suite")
print("=" * 40)
tests = [
("File Structure", test_file_structure),
("Python Imports", test_imports),
("Flask App Creation", test_app_creation),
("Model Predictor", test_model_predictor),
("FASTA Parsing", test_fasta_parsing),
("Flask Routes", test_routes)
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
print(f"\n{test_name}:")
print("-" * len(test_name))
try:
if test_func():
passed += 1
print(f"β {test_name} PASSED")
else:
print(f"β {test_name} FAILED")
except Exception as e:
print(f"β {test_name} ERROR: {e}")
print("\n" + "=" * 40)
print(f"Test Results: {passed}/{total} tests passed")
if passed == total:
print("π All tests passed! The application should work correctly.")
print("\nTo start the application, run:")
print(" python run.py")
print("\nThen open http://localhost:5000 in your browser.")
else:
print("β Some tests failed. Please check the errors above.")
if passed >= total - 1: # Allow model predictor to fail
print("\nThe application may still work if only model loading failed.")
print("Make sure model files are available for full functionality.")
return passed == total
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)
|