l / test_app_imports.py
Princess3's picture
Upload 25 files
c089ca4 verified
#!/usr/bin/env python3
"""
Test script to validate Streamlit app imports and basic functionality
"""
import sys
import os
from pathlib import Path
def test_imports():
"""Test that all required modules can be imported"""
print("πŸ” Testing Streamlit app imports...")
# Add current directory to Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
if current_dir not in sys.path:
sys.path.insert(0, current_dir)
# Test core modules
modules_to_test = [
'streamlit',
'pandas',
'plotly',
'psutil',
'numpy',
'streamlit_app.core.cache_manager',
'streamlit_app.core.text_processor',
'streamlit_app.core.llm_analyzer',
'streamlit_app.core.dataset_builder',
'streamlit_app.utils.config',
'streamlit_app.utils.performance',
'streamlit_app.utils.ui_helpers'
]
failed_imports = []
for module in modules_to_test:
try:
__import__(module)
print(f"βœ… {module}")
except ImportError as e:
print(f"❌ {module}: {e}")
failed_imports.append(module)
except Exception as e:
print(f"⚠️ {module}: Unexpected error - {e}")
if failed_imports:
print(f"\n❌ Failed to import {len(failed_imports)} modules:")
for module in failed_imports:
print(f" - {module}")
return False
print(f"\nβœ… All {len(modules_to_test)} modules imported successfully!")
return True
def test_core_functionality():
"""Test basic functionality of core modules"""
print("\nπŸ”§ Testing core functionality...")
try:
# Test cache manager
from streamlit_app.core.cache_manager import CacheManager, get_cache_manager
cache = get_cache_manager(max_memory_mb=10, persistent=False) # Small cache for testing
cache_stats = cache.get_stats()
print(f"βœ… Cache Manager: {cache_stats}")
# Test text processor
from streamlit_app.core.text_processor import TextProcessor
processor = TextProcessor()
test_text = "This is a test of the New Zealand legislation analysis system."
cleaned = processor.clean_text(test_text)
chunks = processor.chunk_text(cleaned, chunk_size=50, overlap=10)
print(f"βœ… Text Processor: {len(chunks)} chunks created")
# Test configuration manager
from streamlit_app.utils.config import ConfigManager
config = ConfigManager()
config_dict = config.get_config()
print(f"βœ… Config Manager: {len(config_dict)} configuration sections")
# Test performance monitor
from streamlit_app.utils.performance import PerformanceMonitor
perf = PerformanceMonitor(max_history=10)
stats = perf.get_stats()
print(f"βœ… Performance Monitor: Memory usage {stats['memory_usage_mb']:.1f} MB")
# Test UI helpers (basic instantiation)
from streamlit_app.utils.ui_helpers import UIHelpers
helper = UIHelpers()
print("βœ… UI Helpers: Module loaded")
print("\nπŸŽ‰ All core functionality tests passed!")
return True
except Exception as e:
print(f"\n❌ Core functionality test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_file_structure():
"""Test that all required files exist"""
print("\nπŸ“ Testing file structure...")
required_files = [
'streamlit_app/app.py',
'streamlit_app/core/cache_manager.py',
'streamlit_app/core/text_processor.py',
'streamlit_app/core/llm_analyzer.py',
'streamlit_app/core/dataset_builder.py',
'streamlit_app/utils/config.py',
'streamlit_app/utils/performance.py',
'streamlit_app/utils/ui_helpers.py',
'requirements.txt',
'run_streamlit_app.py',
'README_Streamlit_App.md'
]
missing_files = []
for file_path in required_files:
if not Path(file_path).exists():
missing_files.append(file_path)
print(f"❌ Missing: {file_path}")
else:
print(f"βœ… Found: {file_path}")
if missing_files:
print(f"\n❌ Missing {len(missing_files)} files:")
for file_path in missing_files:
print(f" - {file_path}")
return False
print(f"\nβœ… All {len(required_files)} files present!")
return True
def main():
"""Main test function"""
print("πŸ›οΈ NZ Legislation Loophole Analysis - App Validation")
print("=" * 60)
all_passed = True
# Test file structure
if not test_file_structure():
all_passed = False
# Test imports
if not test_imports():
all_passed = False
# Test core functionality
if not test_core_functionality():
all_passed = False
print("\n" + "=" * 60)
if all_passed:
print("πŸŽ‰ VALIDATION COMPLETE - App is ready to run!")
print("\nπŸš€ To start the application:")
print(" python run_streamlit_app.py")
print("\nπŸ“± Then visit: http://localhost:8501")
else:
print("❌ VALIDATION FAILED - Please check the errors above")
print("\nπŸ”§ Troubleshooting:")
print(" - Ensure all dependencies are installed: pip install -r requirements.txt")
print(" - Check Python version (3.8+ required)")
print(" - Verify file permissions")
return all_passed
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)