"""Test CRUD operations for Milestone 4.""" from src.database.db_manager import DatabaseManager from src.services.recipe_service import RecipeService def test_crud(): """Test all CRUD operations.""" print("=" * 60) print("Testing Recipe CRUD Operations (Milestone 4)") print("=" * 60) # Initialize database db = DatabaseManager("database/chef.db") db.init_database() service = RecipeService(db) print("\n1. Testing CREATE operation...") new_recipe = service.create_recipe( name="Test Recipe - CRUD Test", prompt_text="This is a test prompt for CRUD validation.", tags="test,crud,milestone4", description="A recipe created to test CRUD operations", notes="Created during Milestone 4 testing" ) if new_recipe: print(f" ✓ CREATE: Successfully created recipe ID {new_recipe.id}") print(f" Name: {new_recipe.name}") print(f" Tags: {new_recipe.tags}") recipe_id = new_recipe.id else: print(" ✗ CREATE: Failed to create recipe") db.close() return print("\n2. Testing READ operation...") retrieved = service.get_recipe(recipe_id) if retrieved: print(f" ✓ READ: Successfully retrieved recipe ID {recipe_id}") print(f" Name: {retrieved.name}") print(f" Description: {retrieved.description}") else: print(f" ✗ READ: Failed to retrieve recipe ID {recipe_id}") print("\n3. Testing UPDATE operation...") success = service.update_recipe( recipe_id, name="Test Recipe - CRUD Test (Updated)", description="Description updated during testing", tags="test,crud,milestone4,updated" ) if success: print(f" ✓ UPDATE: Successfully updated recipe ID {recipe_id}") updated = service.get_recipe(recipe_id) if updated: print(f" New Name: {updated.name}") print(f" New Tags: {updated.tags}") print(f" New Description: {updated.description}") else: print(f" ✗ UPDATE: Failed to update recipe ID {recipe_id}") print("\n4. Testing SEARCH operation...") results = service.search_recipes("CRUD") if results: print(f" ✓ SEARCH: Found {len(results)} recipe(s) matching 'CRUD'") for r in results: print(f" - {r.name}") else: print(" ✗ SEARCH: No results found") print("\n5. Testing FILTER BY TAGS operation...") tag_results = service.filter_by_tags(["milestone4"]) if tag_results: print(f" ✓ FILTER: Found {len(tag_results)} recipe(s) with tag 'milestone4'") for r in tag_results: print(f" - {r.name}") else: print(" ✗ FILTER: No results found") print("\n6. Testing GET ALL operation...") all_recipes = service.get_all_recipes() print(f" ✓ GET ALL: Retrieved {len(all_recipes)} total recipes") print("\n7. Testing NAME UNIQUENESS CHECK...") is_unique = service.check_name_unique("Test Recipe - CRUD Test (Updated)") if not is_unique: print(" ✓ UNIQUENESS: Correctly detected duplicate name") else: print(" ✗ UNIQUENESS: Failed to detect duplicate name") is_unique_new = service.check_name_unique("This Name Does Not Exist Yet") if is_unique_new: print(" ✓ UNIQUENESS: Correctly detected unique name") else: print(" ✗ UNIQUENESS: Incorrectly marked unique name as duplicate") print("\n8. Testing VALIDATION (empty name)...") invalid_recipe = service.create_recipe(name="", prompt_text="Test") if invalid_recipe is None: print(" ✓ VALIDATION: Correctly rejected empty name") else: print(" ✗ VALIDATION: Failed to reject empty name") print("\n9. Testing VALIDATION (empty prompt)...") invalid_recipe2 = service.create_recipe(name="Test", prompt_text="") if invalid_recipe2 is None: print(" ✓ VALIDATION: Correctly rejected empty prompt") else: print(" ✗ VALIDATION: Failed to reject empty prompt") print("\n10. Testing DELETE operation...") success = service.delete_recipe(recipe_id) if success: print(f" ✓ DELETE: Successfully deleted recipe ID {recipe_id}") # Verify it's gone deleted = service.get_recipe(recipe_id) if deleted is None: print(" ✓ DELETE: Verified recipe no longer exists") else: print(" ✗ DELETE: Recipe still exists after deletion!") else: print(f" ✗ DELETE: Failed to delete recipe ID {recipe_id}") # Close database db.close() print("\n" + "=" * 60) print("CRUD Testing Complete!") print("=" * 60) if __name__ == "__main__": test_crud()