#!/usr/bin/env python3 """ Manual test script to verify Notion database connectivity and basic operations. This script tests the core FoodWiseNotionClient functionality: 1. Add a test item to the Notion database 2. Query it back to verify it was created 3. Clean up by archiving the test item Run with: uv run python scripts/inventory_demo.py """ import sys import os from pathlib import Path from datetime import datetime, timedelta, date # Add src to path so we can import our modules sys.path.insert(0, str(Path(__file__).parent.parent / "src")) from dotenv import load_dotenv load_dotenv() from foodwise.notion.notion_client import FoodWiseNotionClient def run_notion_connection(): """Test basic Notion database operations.""" print("๐Ÿงช Testing Notion database connection...") try: # Initialize client # Prefer test DBs if provided to avoid modifying prod data test_inventory_db = os.getenv("NOTION_INVENTORY_DB_ID_TEST") if test_inventory_db: os.environ["NOTION_INVENTORY_DB_ID"] = test_inventory_db test_shopping_db = os.getenv("NOTION_SHOPPING_DB_ID_TEST") if test_shopping_db: os.environ["NOTION_SHOPPING_DB_ID"] = test_shopping_db client = FoodWiseNotionClient() print("โœ… Notion client initialized successfully") # Test data demo_item = { "name": "Hackathon Test Apple", "category": "Fruits", "storage_type": "Pantry", "quantity": 3, "unit": "pieces", "best_by_date": "2024-12-31", "purchase_date": "2024-12-01", "temperature_sensitive": False, "cooked_raw_status": "Raw", "tags": ["Organic", "Sale Item"] } print(f"๐Ÿ“ Adding test item: {demo_item['name']}") # Add item to database added_item = client.add_inventory_item(demo_item) print(f"โœ… Item created successfully!") print(f" ๐Ÿ“„ Notion URL: {added_item['url']}") print(f" ๐Ÿ†” Page ID: {added_item['id']}") # Query it back print("๐Ÿ” Searching for the test item...") search_results = client.search_items("Hackathon Test Apple") if search_results and len(search_results) > 0: found_item = search_results[0] print("โœ… Item found in search results!") print(f" ๐Ÿ“‹ Name: {found_item['name']}") print(f" ๐Ÿ“‚ Category: {found_item['category']}") print(f" ๐Ÿ“ฆ Storage: {found_item['storage_type']}") print(f" ๐Ÿ”ข Quantity: {found_item['quantity']} {found_item['unit']}") else: print("โŒ Item not found in search results") return False # Test expiring items query print("๐Ÿ“… Testing expiring items query...") expiring_items = client.get_expiring_items(30) # Next 30 days print(f"๐Ÿ“Š Found {len(expiring_items)} items expiring in next 30 days") # Clean up - archive the test item print("๐Ÿงน Cleaning up test item...") client.client.pages.update( page_id=added_item['id'], archived=True ) print("โœ… Test item archived successfully") print("\n๐ŸŽ‰ All tests passed! Notion integration is working correctly.") return True except Exception as e: print(f"โŒ Test failed with error: {e}") print("\n๐Ÿ”ง Troubleshooting:") print(" 1. Check that NOTION_SECRET is set in your .env file") print(" 2. Check that NOTION_INVENTORY_DB_ID is set in your .env file") print(" 3. Verify your Notion integration has access to the database") return False def run_query_operations(): """Test various query operations without adding new items.""" print("\n๐Ÿ” Testing query operations...") try: # Prefer test DBs if provided test_inventory_db = os.getenv("NOTION_INVENTORY_DB_ID_TEST") if test_inventory_db: os.environ["NOTION_INVENTORY_DB_ID"] = test_inventory_db client = FoodWiseNotionClient() # Test getting all inventory print("๐Ÿ“‹ Querying all inventory items...") all_items = client.query_inventory() print(f"๐Ÿ“Š Found {len(all_items)} total items in inventory") if all_items: sample_item = all_items[0] print(f" ๐Ÿ“ Sample item: {sample_item['name']}") # Test category filter print("๐Ÿฅ› Testing category filter (Dairy)...") dairy_items = client.query_inventory({ "property": "Category", "select": {"equals": "Dairy"} }) print(f"๐Ÿง€ Found {len(dairy_items)} dairy items") return True except Exception as e: print(f"โŒ Query test failed: {e}") return False def run_update_remove_operations(): """Test update and remove operations with proper cleanup.""" print("\n๐Ÿ”„ Testing update and remove operations...") try: # Prefer test DBs if provided test_inventory_db = os.getenv("NOTION_INVENTORY_DB_ID_TEST") if test_inventory_db: os.environ["NOTION_INVENTORY_DB_ID"] = test_inventory_db client = FoodWiseNotionClient() # Create a test item specifically for update/remove testing test_item = { "name": "Update Test Banana", "category": "Fruits", "storage_type": "Pantry", "quantity": 5.0, "unit": "pieces", "best_by_date": "2024-12-25", "purchase_date": "2024-12-01", "temperature_sensitive": False, "cooked_raw_status": "Raw" } print(f"๐Ÿ“ Adding test item for update/remove: {test_item['name']}") added_item = client.add_inventory_item(test_item) page_id = added_item['id'] print(f"โœ… Test item created with ID: {page_id}") # Test update operation print("๐Ÿ”„ Testing update operation...") update_data = { "quantity": 3.0, "best_by_date": "2024-12-20", "tags": ["Low Stock"] } updated_item = client.update_inventory_item(page_id, update_data) print("โœ… Update successful!") print(f" ๐Ÿ”ข Updated quantity: {updated_item['quantity']}") print(f" ๐Ÿ“… Updated best by: {updated_item['best_by_date']}") # Test remove operation print("๐Ÿ—‘๏ธ Testing remove operation...") remove_result = client.remove_inventory_item(page_id) print("โœ… Remove successful!") print(f" ๐Ÿ“‹ Result: {remove_result['message']}") print("๐ŸŽ‰ Update and remove operations working correctly!") return True except Exception as e: print(f"โŒ Update/remove test failed: {e}") # Try to clean up if item was created but operations failed try: if 'page_id' in locals(): print("๐Ÿงน Attempting cleanup...") client.remove_inventory_item(page_id) print("โœ… Cleanup successful") except: print("โš ๏ธ Cleanup failed - you may need to manually archive the test item") return False if __name__ == "__main__": print("๐Ÿฝ๏ธ FoodWise Inventory Demo (TEST DB)") print("=" * 40) # Run tests connection_success = run_notion_connection() if connection_success: query_success = run_query_operations() update_remove_success = run_update_remove_operations() if query_success and update_remove_success: print("\n๐ŸŽฏ All tests completed successfully!") print(" Your Notion integration is ready for the agent!") else: print("\nโš ๏ธ Some tests failed - check the output above") else: print("\n๐Ÿ’ฅ Connection test failed - fix setup before proceeding") sys.exit(1)