#!/usr/bin/env python3 """ Quick test to verify the save functionality works correctly. """ import os import sys import tkinter as tk # Add the src directory to the path so we can import our modules sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) import logging from ui_manager import UIManager def test_save_functionality(): """Test that the save button works without errors.""" print("Testing save functionality in edit window...") # Create a test Tkinter root root = tk.Tk() root.withdraw() # Hide the main window # Create a logger logger = logging.getLogger("test_logger") logger.setLevel(logging.DEBUG) # Create UIManager ui_manager = UIManager(root, logger) # Sample dose data for testing sample_dose_data = { "bupropion": "2025-01-15 08:00:00:300mg|2025-01-15 20:00:00:150mg", "hydroxyzine": "2025-01-15 22:00:00:25mg", "gabapentin": "", "propranolol": "2025-01-15 09:30:00:10mg", } # Sample values for the edit window (14 fields for new CSV format) sample_values = ( "01/15/2025", # date 5, # depression 3, # anxiety 7, # sleep 6, # appetite 1, # bupropion sample_dose_data["bupropion"], # bupropion_doses 1, # hydroxyzine sample_dose_data["hydroxyzine"], # hydroxyzine_doses 0, # gabapentin sample_dose_data["gabapentin"], # gabapentin_doses 1, # propranolol sample_dose_data["propranolol"], # propranolol_doses "Test entry for save functionality", # note ) # Track if save was called successfully save_called = False save_args = None # Define test callbacks def test_save(*args): nonlocal save_called, save_args save_called = True save_args = args print("✓ Save callback executed successfully") print(f" Arguments received: {len(args)} args") # Close the edit window after save if args and hasattr(args[0], "destroy"): args[0].destroy() def test_delete(*args): print("Delete callback triggered") if args and hasattr(args[0], "destroy"): args[0].destroy() callbacks = { "save": test_save, "delete": test_delete, } try: # Create the edit window edit_window = ui_manager.create_edit_window(sample_values, callbacks) print("✓ Edit window created successfully") print("✓ Testing automatic save...") # Simulate clicking save button by calling the save function directly # First, we need to get the vars_dict from the window # We'll trigger a save by simulating the button press # Find the save button and trigger it def find_save_button(widget): """Recursively find the save button.""" if isinstance(widget, tk.Button) and widget.cget("text") == "Save": return widget for child in widget.winfo_children(): result = find_save_button(child) if result: return result return None # Wait a moment for the window to fully initialize edit_window.update_idletasks() # Find and click the save button save_button = find_save_button(edit_window) if save_button: print("✓ Found save button, triggering click...") save_button.invoke() else: print("✗ Could not find save button") edit_window.destroy() return False # Check if save was called if save_called: print("✓ Save functionality test PASSED") print( f"✓ Save was called with {len(save_args) if save_args else 0} arguments" ) return True else: print("✗ Save functionality test FAILED - save was not called") return False except Exception as e: print(f"✗ Error during save test: {e}") import traceback traceback.print_exc() return False finally: root.destroy() if __name__ == "__main__": print("Testing Save Functionality") print("=" * 30) success = test_save_functionality() if success: print("\n✅ Save functionality test completed successfully!") else: print("\n❌ Save functionality test failed!") sys.exit(1)