"""Test output management functionality for Milestone 5.""" from src.database.db_manager import DatabaseManager from src.database.models import Recipe from src.services.output_service import OutputService from src.utils.file_utils import get_recipe_output_dir, format_file_size from pathlib import Path def test_output_management(): """Test all output management operations.""" print("=" * 60) print("Testing Output Management (Milestone 5)") print("=" * 60) # Initialize database db = DatabaseManager("database/chef.db") db.init_database() output_service = OutputService(db) # Get first recipe for testing recipes = Recipe.get_all(db) if not recipes: print("No recipes found! Please run create_test_recipes.py first") db.close() return recipe = recipes[0] print(f"\nUsing recipe: {recipe.name} (ID: {recipe.id})") print("\n1. Testing get_recipe_output_dir()...") output_dir = get_recipe_output_dir(recipe.id) print(f" ✓ Output directory: {output_dir}") print(f" ✓ Directory exists: {output_dir.exists()}") print("\n2. Testing save_output()...") # Create a test file test_file = Path("/tmp/test_output.txt") if test_file.exists(): output = output_service.save_output( recipe.id, str(test_file), "Test output created during Milestone 5 testing" ) if output: print(f" ✓ Output saved: {output.filename}") print(f" ID: {output.id}") print(f" Size: {format_file_size(output.file_size)}") print(f" Type: {output.file_type}") print(f" Path: {output.filepath}") output_id = output.id else: print(" ✗ Failed to save output") db.close() return else: print(" ✗ Test file not found at /tmp/test_output.txt") db.close() return print("\n3. Testing get_recipe_outputs()...") outputs = output_service.get_recipe_outputs(recipe.id) print(f" ✓ Found {len(outputs)} output(s) for recipe {recipe.id}") for out in outputs: print(f" - {out.filename} ({format_file_size(out.file_size)})") print("\n4. Testing get_output_by_id()...") retrieved = output_service.get_output_by_id(output_id) if retrieved: print(f" ✓ Retrieved output: {retrieved.filename}") print(f" Notes: {retrieved.execution_notes}") else: print(f" ✗ Failed to retrieve output {output_id}") print("\n5. Testing get_all_outputs()...") all_outputs = output_service.get_all_outputs() print(f" ✓ Total outputs in database: {len(all_outputs)}") print("\n6. Testing update_execution_notes()...") success = output_service.update_execution_notes( output_id, "Updated notes: This output was modified during testing" ) if success: updated = output_service.get_output_by_id(output_id) print(f" ✓ Notes updated successfully") print(f" New notes: {updated.execution_notes}") else: print(" ✗ Failed to update notes") print("\n7. Testing search_outputs()...") search_results = output_service.search_outputs("test") print(f" ✓ Search 'test' found {len(search_results)} output(s)") for out in search_results: print(f" - {out.filename}") print("\n8. Testing filesystem organization...") # Check if file exists in correct location output_file = Path(retrieved.filepath) if output_file.exists(): print(f" ✓ Output file exists at: {output_file}") print(f" Size: {format_file_size(output_file.stat().st_size)}") else: print(f" ✗ Output file not found at: {output_file}") print("\n9. Testing format_file_size()...") test_sizes = [512, 1024, 1024*1024, 1024*1024*10] for size in test_sizes: formatted = format_file_size(size) print(f" {size:>10} bytes = {formatted}") print("\n10. Testing delete_output()...") # Only delete if we created it in this test reply = input(f"\n Delete test output '{retrieved.filename}'? (yes/no): ") if reply.lower() == 'yes': success = output_service.delete_output(output_id) if success: print(f" ✓ Output deleted successfully") # Verify file is gone if not output_file.exists(): print(f" ✓ File deleted from filesystem") else: print(f" ✗ File still exists in filesystem") # Verify DB record is gone deleted = output_service.get_output_by_id(output_id) if deleted is None: print(f" ✓ Database record deleted") else: print(f" ✗ Database record still exists") else: print(f" ✗ Failed to delete output") else: print(" Skipped deletion") # Close database db.close() print("\n" + "=" * 60) print("Output Management Testing Complete!") print("=" * 60) if __name__ == "__main__": test_output_management()