#!/usr/bin/env python3 """ Validation Script for Database and Cache Fixes ============================================ Tests the fixes for: 1. SQLite database path issues 2. Hugging Face cache permissions """ import os import sys import tempfile import shutil from pathlib import Path def test_database_path(): """Test database path creation and access""" print("šŸ” Testing database path fixes...") try: # Test the new database path from app.services.database_service import DatabaseManager # Test with default path (should be /app/data/legal_dashboard.db) db = DatabaseManager() print("āœ… Database manager initialized with default path") # Test if database directory exists db_dir = os.path.dirname(db.db_path) if os.path.exists(db_dir): print(f"āœ… Database directory exists: {db_dir}") else: print(f"āŒ Database directory missing: {db_dir}") return False # Test database connection if db.is_connected(): print("āœ… Database connection successful") else: print("āŒ Database connection failed") return False db.close() return True except Exception as e: print(f"āŒ Database test failed: {e}") return False def test_cache_directory(): """Test Hugging Face cache directory setup""" print("\nšŸ” Testing cache directory fixes...") try: # Check if cache directory is set cache_dir = os.environ.get("TRANSFORMERS_CACHE") if cache_dir: print(f"āœ… TRANSFORMERS_CACHE set to: {cache_dir}") else: print("āŒ TRANSFORMERS_CACHE not set") return False # Check if cache directory exists and is writable if os.path.exists(cache_dir): print(f"āœ… Cache directory exists: {cache_dir}") else: print(f"āŒ Cache directory missing: {cache_dir}") return False # Test write permissions test_file = os.path.join(cache_dir, "test_write.tmp") try: with open(test_file, 'w') as f: f.write("test") os.remove(test_file) print("āœ… Cache directory is writable") except Exception as e: print(f"āŒ Cache directory not writable: {e}") return False return True except Exception as e: print(f"āŒ Cache test failed: {e}") return False def test_dockerfile_updates(): """Test Dockerfile changes""" print("\nšŸ” Testing Dockerfile updates...") try: dockerfile_path = "Dockerfile" if not os.path.exists(dockerfile_path): print("āŒ Dockerfile not found") return False with open(dockerfile_path, 'r') as f: content = f.read() # Check for directory creation if "mkdir -p /app/data /app/cache" in content: print("āœ… Directory creation command found") else: print("āŒ Directory creation command missing") return False # Check for permissions if "chmod -R 777 /app/data /app/cache" in content: print("āœ… Permission setting command found") else: print("āŒ Permission setting command missing") return False # Check for environment variables if "ENV TRANSFORMERS_CACHE=/app/cache" in content: print("āœ… TRANSFORMERS_CACHE environment variable found") else: print("āŒ TRANSFORMERS_CACHE environment variable missing") return False if "ENV HF_HOME=/app/cache" in content: print("āœ… HF_HOME environment variable found") else: print("āŒ HF_HOME environment variable missing") return False return True except Exception as e: print(f"āŒ Dockerfile test failed: {e}") return False def test_main_py_updates(): """Test main.py updates""" print("\nšŸ” Testing main.py updates...") try: main_py_path = "app/main.py" if not os.path.exists(main_py_path): print("āŒ main.py not found") return False with open(main_py_path, 'r') as f: content = f.read() # Check for directory creation if "os.makedirs(\"/app/cache\", exist_ok=True)" in content: print("āœ… Cache directory creation found") else: print("āŒ Cache directory creation missing") return False if "os.makedirs(\"/app/data\", exist_ok=True)" in content: print("āœ… Data directory creation found") else: print("āŒ Data directory creation missing") return False # Check for environment variable setting if "os.environ[\"TRANSFORMERS_CACHE\"] = \"/app/cache\"" in content: print("āœ… TRANSFORMERS_CACHE environment variable setting found") else: print("āŒ TRANSFORMERS_CACHE environment variable setting missing") return False return True except Exception as e: print(f"āŒ main.py test failed: {e}") return False def test_dockerignore_updates(): """Test .dockerignore updates""" print("\nšŸ” Testing .dockerignore updates...") try: dockerignore_path = ".dockerignore" if not os.path.exists(dockerignore_path): print("āŒ .dockerignore not found") return False with open(dockerignore_path, 'r') as f: content = f.read() # Check for cache exclusions if "cache/" in content: print("āœ… Cache directory exclusion found") else: print("āŒ Cache directory exclusion missing") return False if "/app/cache/" in content: print("āœ… /app/cache exclusion found") else: print("āŒ /app/cache exclusion missing") return False return True except Exception as e: print(f"āŒ .dockerignore test failed: {e}") return False def main(): """Run all validation tests""" print("šŸš€ Legal Dashboard OCR - Fix Validation") print("=" * 50) # Change to project directory project_dir = Path(__file__).parent os.chdir(project_dir) # Run tests tests = [ test_database_path, test_cache_directory, test_dockerfile_updates, test_main_py_updates, test_dockerignore_updates ] results = [] for test in tests: try: result = test() results.append(result) except Exception as e: print(f"āŒ Test failed with exception: {e}") results.append(False) # Summary print("\n" + "=" * 50) print("šŸ“Š Validation Results Summary") print("=" * 50) passed = sum(results) total = len(results) print(f"āœ… Passed: {passed}/{total}") print(f"āŒ Failed: {total - passed}/{total}") if all(results): print("\nšŸŽ‰ All fixes validated successfully!") print("\nāœ… Runtime errors should be resolved:") print(" • SQLite database path fixed") print(" • Hugging Face cache permissions fixed") print(" • Environment variables properly set") print(" • Docker container ready for deployment") return 0 else: print("\nāš ļø Some fixes need attention. Please check the errors above.") return 1 if __name__ == "__main__": sys.exit(main())