BrailleMenuGenV2 / tests /test_braille.py
Chamin09's picture
initial commit
93c4f75 verified
import os
import sys
import time
# Add the parent directory to the path so we can import our modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from models.braille_translator import text_to_braille, get_braille_metadata
def test_braille_translation(text):
"""
Test Braille translation on a given text.
Args:
text: Text to translate to Braille
Returns:
Dictionary with test results
"""
start_time = time.time()
# Translate to Braille
try:
result = text_to_braille(text, use_context=True)
success = result['success']
braille_text = result.get('formatted_braille', '')
error = result.get('error', None)
except Exception as e:
success = False
braille_text = ''
error = str(e)
end_time = time.time()
# Get metadata
metadata = get_braille_metadata(text)
# Compile results
test_results = {
'original_text': text,
'success': success,
'processing_time': end_time - start_time,
'braille_text': braille_text[:100] + '...' if len(braille_text) > 100 else braille_text,
'word_count': metadata['word_count'],
'character_count': metadata['character_count'],
'line_count': metadata['line_count']
}
if not success:
test_results['error'] = error
return test_results
def run_braille_tests():
"""
Run tests on sample menu texts.
Returns:
List of test results
"""
# Sample menu texts
sample_texts = [
# Simple menu item
"Cheeseburger - $10.99\nServed with fries and a pickle.",
# Menu section
"APPETIZERS\n-----------\nMozzarella Sticks - $7.99\nLoaded Nachos - $9.99\nBuffalo Wings - $12.99",
# Complex menu with formatting
"""MAIN COURSE
-------------
Grilled Salmon - $18.99
Fresh Atlantic salmon served with seasonal vegetables and rice pilaf.
Filet Mignon - $24.99
8oz center-cut filet served with mashed potatoes and asparagus.
Vegetable Pasta - $14.99
Penne pasta with seasonal vegetables in a creamy garlic sauce."""
]
results = []
for i, text in enumerate(sample_texts):
print(f"\nTesting sample {i+1}...")
result = test_braille_translation(text)
results.append(result)
# Print progress
status = "SUCCESS" if result['success'] else "FAILED"
print(f"Sample {i+1}: {status}")
print(f"Words: {result['word_count']}, Time: {result['processing_time']:.2f}s")
print(f"Braille sample: {result['braille_text'][:50]}...")
return results
if __name__ == "__main__":
print("Testing Braille translation functionality...")
results = run_braille_tests()
# Print summary
success_count = sum(1 for r in results if r['success'])
print(f"\nSummary: {success_count}/{len(results)} tests passed")
if results:
avg_time = sum(r['processing_time'] for r in results) / len(results)
print(f"Average processing time: {avg_time:.2f} seconds")