simple-text-analyzer / test /test_memory_handling.py
egumasa's picture
removed temporary file writing
ca02ec3
#!/usr/bin/env python3
"""Test script to verify in-memory file handling works correctly."""
import sys
sys.path.append('.')
from io import StringIO
import pandas as pd
# Test 1: Verify StringIO works with pandas
def test_stringio_pandas():
print("Test 1: StringIO with pandas")
csv_content = """word,frequency
apple,100
banana,80
cherry,60"""
content_io = StringIO(csv_content)
df = pd.read_csv(content_io)
print(f"βœ“ Successfully read CSV from StringIO: {len(df)} rows")
print(df)
print()
# Test 2: Test custom config parsing
def test_custom_config():
print("Test 2: Custom config parsing")
from text_analyzer.lexical_sophistication import LexicalSophisticationAnalyzer
analyzer = LexicalSophisticationAnalyzer()
config = {
'content': """word\tfreq
hello\t500
world\t400
test\t300""",
'word_column': 'word',
'freq_column': 'freq',
'delimiter': '\t',
'is_custom_config': True
}
result = analyzer._parse_custom_config(config)
print(f"βœ“ Successfully parsed custom config: {len(result)} entries")
print(f"Sample entries: {list(result.items())[:3]}")
print()
# Test 3: Test file content extraction
def test_file_extraction():
print("Test 3: File content extraction")
from web_app.handlers.analysis_handlers import AnalysisHandlers
# Simulate uploaded file
class MockUploadedFile:
def __init__(self, name, content):
self.name = name
self._content = content.encode('utf-8')
self._position = 0
def read(self):
self._position = len(self._content)
return self._content
def seek(self, position):
self._position = position
mock_file = MockUploadedFile("test.txt", "This is a test file content.")
file_contents = AnalysisHandlers.extract_uploaded_files([mock_file])
print(f"βœ“ Successfully extracted {len(file_contents)} files")
for filename, content in file_contents:
print(f" - {filename}: {content[:50]}...")
print()
if __name__ == "__main__":
print("Testing in-memory file handling...\n")
try:
test_stringio_pandas()
test_custom_config()
test_file_extraction()
print("βœ… All tests passed!")
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()