| |
| """ |
| Test script for EpiPred web application |
| """ |
|
|
| import os |
| import sys |
| import tempfile |
| import json |
| from pathlib import 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: |
| predictor = EpitopePredictor() |
| print("β Model predictor created successfully") |
| |
| |
| 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 |
| |
| 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_fasta = """>Test_Sequence_1 |
| MKLLILTCLVAVALARPKHPIKHQGLPQEVLNENLLRFFVAPFPEVFGKEKVNEL |
| |
| >Test_Sequence_2 |
| CARFASLIYGKFVRQPQVWLRIQNYSVMDICDEHQGVMVPGVGVPQALQKYNPD""" |
| |
| sequences = parse_fasta_text(test_fasta) |
| print(f"β Parsed {len(sequences)} sequences") |
| |
| |
| 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: |
| |
| 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 |
| |
| |
| 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 |
| |
| |
| 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: |
| 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) |
|
|