|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Fix test files to expect domain exceptions instead of ToolError.""" |
| 3 | + |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# Map of exception patterns to their correct exception types |
| 8 | +EXCEPTION_MAPPINGS = [ |
| 9 | + (r'pytest\.raises\(ToolError, match="Column.*not found"\)', 'pytest.raises(ColumnNotFoundError, match="Column.*not found")'), |
| 10 | + (r'pytest\.raises\(ToolError, match=".*not found"\)', 'pytest.raises(ColumnNotFoundError, match=".*not found")'), |
| 11 | + (r'pytest\.raises\(ToolError, match="No data loaded in session"\)', 'pytest.raises(NoDataLoadedError, match="No data loaded in session")'), |
| 12 | + (r'pytest\.raises\(ToolError, match="Invalid value"\)', 'pytest.raises(InvalidParameterError, match="Invalid value")'), |
| 13 | + (r'pytest\.raises\(ToolError, match="Session.*not found"\)', 'pytest.raises(SessionNotFoundError, match="Session.*not found")'), |
| 14 | +] |
| 15 | + |
| 16 | +# Import lines to add if ToolError is imported |
| 17 | +IMPORTS_TO_ADD = { |
| 18 | + "from databeak.exceptions import ColumnNotFoundError, InvalidParameterError, NoDataLoadedError, SessionNotFoundError", |
| 19 | +} |
| 20 | + |
| 21 | +def fix_test_file(file_path: Path) -> bool: |
| 22 | + """Fix a single test file.""" |
| 23 | + content = file_path.read_text() |
| 24 | + original_content = content |
| 25 | + |
| 26 | + # Check if file uses ToolError |
| 27 | + if "from fastmcp.exceptions import ToolError" not in content: |
| 28 | + return False |
| 29 | + |
| 30 | + # Remove ToolError import |
| 31 | + content = re.sub(r"from fastmcp\.exceptions import ToolError\n", "", content) |
| 32 | + |
| 33 | + # Add domain exception imports if not present |
| 34 | + if "from databeak.exceptions import" not in content: |
| 35 | + # Find where to insert (after fastmcp imports) |
| 36 | + insert_pos = content.find("\n# Ensure full module coverage") |
| 37 | + if insert_pos == -1: |
| 38 | + insert_pos = content.find("\nimport databeak") |
| 39 | + if insert_pos == -1: |
| 40 | + insert_pos = content.find("\nfrom databeak") |
| 41 | + |
| 42 | + if insert_pos != -1: |
| 43 | + import_line = "\nfrom databeak.exceptions import ColumnNotFoundError, InvalidParameterError, NoDataLoadedError, SessionNotFoundError\n" |
| 44 | + content = content[:insert_pos] + import_line + content[insert_pos:] |
| 45 | + |
| 46 | + # Apply exception replacements |
| 47 | + for pattern, replacement in EXCEPTION_MAPPINGS: |
| 48 | + content = re.sub(pattern, replacement, content) |
| 49 | + |
| 50 | + # Write back if changed |
| 51 | + if content != original_content: |
| 52 | + file_path.write_text(content) |
| 53 | + print(f"✓ Fixed {file_path.name}") |
| 54 | + return True |
| 55 | + |
| 56 | + return False |
| 57 | + |
| 58 | +def main(): |
| 59 | + """Fix all test files in servers directory.""" |
| 60 | + test_dir = Path(__file__).parent.parent / "tests" / "unit" / "servers" |
| 61 | + |
| 62 | + files_to_fix = [ |
| 63 | + "test_discovery_server.py", |
| 64 | + "test_io_server.py", |
| 65 | + "test_io_server_coverage_fixes.py", |
| 66 | + "test_row_operations_server.py", |
| 67 | + "test_statistics_server.py", |
| 68 | + "test_statistics_server_coverage.py", |
| 69 | + "test_transformation_server.py", |
| 70 | + "test_validation_server.py", |
| 71 | + "test_system_server.py", |
| 72 | + ] |
| 73 | + |
| 74 | + fixed_count = 0 |
| 75 | + for filename in files_to_fix: |
| 76 | + file_path = test_dir / filename |
| 77 | + if file_path.exists(): |
| 78 | + if fix_test_file(file_path): |
| 79 | + fixed_count += 1 |
| 80 | + else: |
| 81 | + print(f"✗ File not found: {filename}") |
| 82 | + |
| 83 | + print(f"\nFixed {fixed_count} files") |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments