#!/usr/bin/env python3 """ Test script to verify multiple dose punching and saving behavior. """ 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_multiple_punch_and_save(): """Test multiple dose punching followed by save.""" print("Testing multiple dose punching and save functionality...") # Create a test Tkinter root root = tk.Tk() root.title("Test Root Window") root.geometry("200x100") # Small root 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", "hydroxyzine": "", "gabapentin": "", "propranolol": "", } # 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 0, # hydroxyzine sample_dose_data["hydroxyzine"], # hydroxyzine_doses 0, # gabapentin sample_dose_data["gabapentin"], # gabapentin_doses 0, # propranolol sample_dose_data["propranolol"], # propranolol_doses "Test entry for multiple punch testing", # note ) # Track save calls save_calls = [] # Define test callbacks def test_save(*args): save_calls.append(args) print(f"✓ Save called with {len(args)} arguments") # Print dose data specifically if len(args) >= 12: # Should have dose_data as last argument dose_data = args[-1] # Last argument should be dose_data print(" Dose data received:") for med, doses in dose_data.items(): print(f" {med}: {doses}") # Close 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) edit_window.geometry("600x400") # Set a reasonable size edit_window.lift() # Bring to front edit_window.focus_force() # Force focus print("✓ Edit window created") print("✓ Now simulating multiple dose punches...") # Let's simulate the manual process print("\n=== Manual Test Instructions ===") print("1. In the Bupropion field, enter '150mg' and click 'Take Bupropion'") print("2. Enter '300mg' and click 'Take Bupropion' again") print("3. You should see both doses in the text area") print("4. Click 'Save' to persist the changes") print("5. Check if both doses are saved to the CSV") print("\nWindow will stay open for manual testing...") # Wait for user to manually test edit_window.wait_window() # Check if save was called if save_calls: print("✓ Save was called successfully") return True else: print("✗ Save was not called") return False except Exception as e: print(f"✗ Error during test: {e}") import traceback traceback.print_exc() return False finally: root.destroy() if __name__ == "__main__": print("Testing Multiple Dose Punching and Save") print("=" * 40) success = test_multiple_punch_and_save() if success: print("\n✅ Multiple punch and save test completed!") else: print("\n❌ Multiple punch and save test failed!") sys.exit(1)