#!/usr/bin/env python3 """ Step-by-step test to demonstrate multiple dose functionality. """ import os import sys import tkinter as tk import pandas as pd # 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 demonstrate_multiple_doses(): """Demonstrate the complete multiple dose workflow.""" print("๐Ÿงช Multiple Dose Demonstration") print("=" * 40) # Check current CSV state try: df = pd.read_csv("thechart_data.csv") print(f"๐Ÿ“‹ Current CSV has {len(df)} entries") latest = df.iloc[-1] print(f"๐Ÿ“… Latest entry date: {latest['date']}") # Show current dose state for latest entry dose_columns = [col for col in df.columns if col.endswith("_doses")] print("๐Ÿ’Š Current doses in latest entry:") for dose_col in dose_columns: medicine = dose_col.replace("_doses", "") dose_data = str(latest[dose_col]) if dose_data and dose_data != "nan" and dose_data.strip(): dose_count = len(dose_data.split("|")) print(f" {medicine}: {dose_count} dose(s)") else: print(f" {medicine}: No doses") except Exception as e: print(f"โŒ Error reading CSV: {e}") return print("\n๐Ÿ”ฌ Testing Edit Window Workflow:") print("1. Create edit window for latest entry") print("2. Add multiple doses using punch buttons") print("3. Save and verify CSV is updated") print("\nStarting test...") # Create test environment root = tk.Tk() root.title("Dose Test") root.geometry("300x200") logger = logging.getLogger("dose_test") logger.setLevel(logging.DEBUG) ui_manager = UIManager(root, logger) # Use the actual latest CSV data for testing if len(latest) >= 14: sample_values = tuple(latest.iloc[:14]) else: # Pad with empty values if needed sample_values = tuple(list(latest) + [""] * (14 - len(latest))) # Track save operations save_called = False saved_dose_data = None def test_save(*args): nonlocal save_called, saved_dose_data save_called = True if len(args) >= 12: saved_dose_data = args[-1] # dose_data is last argument print("\nโœ… Save called!") print("๐Ÿ’พ Dose data being saved:") for med, doses in saved_dose_data.items(): if doses: dose_count = len(doses.split("|")) if "|" in doses else 1 print(f" {med}: {dose_count} dose(s) - {doses}") else: print(f" {med}: No doses") # Close the window if args and hasattr(args[0], "destroy"): args[0].destroy() def test_delete(*args): print("๐Ÿ—‘๏ธ Delete called") if args and hasattr(args[0], "destroy"): args[0].destroy() callbacks = { "save": test_save, "delete": test_delete, } try: # Create edit window edit_window = ui_manager.create_edit_window(sample_values, callbacks) edit_window.geometry("700x500") edit_window.lift() edit_window.focus_force() print("\n๐Ÿ“ INSTRUCTIONS:") print("1. In any medicine dose field, enter a dose amount (e.g., '100mg')") print("2. Click the 'Take [Medicine]' button") print("3. Enter another dose amount") print("4. Click the 'Take [Medicine]' button again") print("5. You should see both doses in the text area") print("6. Click 'Save' to persist changes") print("\nโณ Waiting for your interaction...") # Wait for user interaction edit_window.wait_window() if save_called: print("\n๐ŸŽ‰ SUCCESS: Save operation completed!") print("๐Ÿ“Š Multiple doses should now be saved to CSV") # Verify the save actually updated the CSV try: df_after = pd.read_csv("thechart_data.csv") if len(df_after) > len(df): print("โœ… New entry added to CSV") else: print("โœ… Existing entry updated in CSV") print("\n๐Ÿ” Verifying saved data...") latest_after = df_after.iloc[-1] for dose_col in dose_columns: medicine = dose_col.replace("_doses", "") dose_data = str(latest_after[dose_col]) if dose_data and dose_data != "nan" and dose_data.strip(): dose_count = len(dose_data.split("|")) print(f" {medicine}: {dose_count} dose(s) in CSV") except Exception as e: print(f"โŒ Error verifying CSV: {e}") return True else: print("\nโŒ Save was not called - test incomplete") 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__": os.chdir("/home/will/Code/thechart") success = demonstrate_multiple_doses() if success: print("\n๐ŸŽฏ Multiple dose functionality verified!") else: print("\nโ“ Test incomplete or failed")