| |
| """ |
| 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...") |
|
|
| |
| current_dir = os.path.dirname(os.path.abspath(__file__)) |
| if current_dir not in sys.path: |
| sys.path.insert(0, current_dir) |
|
|
| |
| 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: |
| |
| from streamlit_app.core.cache_manager import CacheManager, get_cache_manager |
|
|
| cache = get_cache_manager(max_memory_mb=10, persistent=False) |
| cache_stats = cache.get_stats() |
| print(f"β
Cache Manager: {cache_stats}") |
|
|
| |
| 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") |
|
|
| |
| from streamlit_app.utils.config import ConfigManager |
|
|
| config = ConfigManager() |
| config_dict = config.get_config() |
| print(f"β
Config Manager: {len(config_dict)} configuration sections") |
|
|
| |
| 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") |
|
|
| |
| 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 |
|
|
| |
| if not test_file_structure(): |
| all_passed = False |
|
|
| |
| if not test_imports(): |
| all_passed = False |
|
|
| |
| 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) |
|
|