49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify the encoding fix for CSV imports.
|
|
"""
|
|
|
|
import os
|
|
import tempfile
|
|
import sys
|
|
|
|
# Add the project directory to the path
|
|
sys.path.insert(0, '/Users/hotswap/Documents/projects/delphi-database-v2')
|
|
|
|
from app.import_legacy import open_text_with_fallbacks
|
|
|
|
def test_encoding_fix():
|
|
"""Test that the encoding fix can handle problematic files."""
|
|
|
|
# Create a test file with problematic encoding (copyright symbol at position 3738)
|
|
test_content = "Plan_Id,Plan_Name\n" + "test" * 1000 + "©" + "test" * 1000
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', delete=False, suffix='.csv') as f:
|
|
f.write(test_content)
|
|
temp_file = f.name
|
|
|
|
try:
|
|
# Test that our function can open the file successfully
|
|
file_obj, encoding = open_text_with_fallbacks(temp_file)
|
|
print(f"Successfully opened file with encoding: {encoding}")
|
|
|
|
# Read the content to verify it works
|
|
content = file_obj.read()
|
|
file_obj.close()
|
|
|
|
print(f"Content length: {len(content)}")
|
|
print("Test passed: Encoding fix works correctly")
|
|
|
|
except Exception as e:
|
|
print(f"Test failed: {e}")
|
|
return False
|
|
finally:
|
|
# Clean up
|
|
os.unlink(temp_file)
|
|
|
|
return True
|
|
|
|
if __name__ == "__main__":
|
|
success = test_encoding_fix()
|
|
sys.exit(0 if success else 1)
|