File size: 6,086 Bytes
79ea999 |
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 |
#!/usr/bin/env python3
"""
Backward Compatibility Verification Script
This script verifies that the enhanced config.py maintains 100% backward
compatibility with existing code and API calls.
"""
import sys
import os
def test_imports():
"""Test that all import patterns work"""
print("=" * 60)
print("Testing Import Patterns")
print("=" * 60)
# Test 1: from config import settings
try:
from config import settings
assert hasattr(settings, 'hf_token')
assert hasattr(settings, 'hf_cache_dir')
assert hasattr(settings, 'db_path')
print("β
'from config import settings' - PASSED")
except Exception as e:
print(f"β 'from config import settings' - FAILED: {e}")
return False
# Test 2: from src.config import settings
try:
from src.config import settings
assert hasattr(settings, 'hf_token')
assert hasattr(settings, 'hf_cache_dir')
print("β
'from src.config import settings' - PASSED")
except Exception as e:
print(f"β 'from src.config import settings' - FAILED: {e}")
return False
# Test 3: from .config import settings (relative import)
try:
import src
from src.config import settings
assert hasattr(settings, 'hf_token')
print("β
Relative import - PASSED")
except Exception as e:
print(f"β Relative import - FAILED: {e}")
return False
return True
def test_attributes():
"""Test that all attributes work as expected"""
print("\n" + "=" * 60)
print("Testing Attribute Access")
print("=" * 60)
from config import settings
# Test hf_token
try:
token = settings.hf_token
assert isinstance(token, str)
print(f"β
settings.hf_token: {type(token).__name__} - PASSED")
except Exception as e:
print(f"β settings.hf_token - FAILED: {e}")
return False
# Test hf_cache_dir
try:
cache_dir = settings.hf_cache_dir
assert isinstance(cache_dir, str)
assert len(cache_dir) > 0
print(f"β
settings.hf_cache_dir: {cache_dir} - PASSED")
except Exception as e:
print(f"β settings.hf_cache_dir - FAILED: {e}")
return False
# Test db_path
try:
db_path = settings.db_path
assert isinstance(db_path, str)
print(f"β
settings.db_path: {db_path} - PASSED")
except Exception as e:
print(f"β settings.db_path - FAILED: {e}")
return False
# Test max_workers
try:
max_workers = settings.max_workers
assert isinstance(max_workers, int)
assert 1 <= max_workers <= 16
print(f"β
settings.max_workers: {max_workers} - PASSED")
except Exception as e:
print(f"β settings.max_workers - FAILED: {e}")
return False
# Test all other attributes
attributes = [
'cache_ttl', 'faiss_index_path', 'session_timeout',
'max_session_size_mb', 'mobile_max_tokens', 'mobile_timeout',
'gradio_port', 'gradio_host', 'log_level', 'log_format',
'default_model', 'embedding_model', 'classification_model'
]
for attr in attributes:
try:
value = getattr(settings, attr)
print(f"β
settings.{attr}: {type(value).__name__} - PASSED")
except Exception as e:
print(f"β settings.{attr} - FAILED: {e}")
return False
return True
def test_context_manager_compatibility():
"""Test that context_manager can import settings"""
print("\n" + "=" * 60)
print("Testing Context Manager Compatibility")
print("=" * 60)
try:
# Simulate what context_manager does
from config import settings
db_path = settings.db_path
assert isinstance(db_path, str)
print(f"β
Context manager import pattern works - PASSED")
print(f" db_path: {db_path}")
return True
except Exception as e:
print(f"β Context manager compatibility - FAILED: {e}")
return False
def test_cache_directory():
"""Test cache directory functionality"""
print("\n" + "=" * 60)
print("Testing Cache Directory Management")
print("=" * 60)
try:
from src.config import settings
cache_dir = settings.hf_cache_dir
# Verify directory exists
assert os.path.exists(cache_dir), f"Cache directory does not exist: {cache_dir}"
print(f"β
Cache directory exists: {cache_dir}")
# Verify write access
test_file = os.path.join(cache_dir, ".test_write")
try:
with open(test_file, 'w') as f:
f.write("test")
os.remove(test_file)
print(f"β
Cache directory is writable")
except PermissionError:
print(f"β οΈ Cache directory not writable (may need permissions)")
return True
except Exception as e:
print(f"β Cache directory test - FAILED: {e}")
return False
def main():
"""Run all compatibility tests"""
print("Backward Compatibility Verification")
print("=" * 60)
print()
results = []
results.append(("Imports", test_imports()))
results.append(("Attributes", test_attributes()))
results.append(("Context Manager", test_context_manager_compatibility()))
results.append(("Cache Directory", test_cache_directory()))
print("\n" + "=" * 60)
print("Test Summary")
print("=" * 60)
all_passed = True
for test_name, passed in results:
status = "β
PASSED" if passed else "β FAILED"
print(f"{test_name}: {status}")
if not passed:
all_passed = False
print("=" * 60)
if all_passed:
print("β
ALL TESTS PASSED - Backward compatibility verified!")
return 0
else:
print("β SOME TESTS FAILED - Please review errors above")
return 1
if __name__ == "__main__":
sys.exit(main())
|