#!/usr/bin/env python3 """ Test the dose tracking functionality of the edit window. """ import os import sys import tkinter as tk sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src")) from medicine_manager import MedicineManager from pathology_manager import PathologyManager from ui_manager import UIManager def test_dose_tracking_ui(): """Test that the dose tracking UI functionality works.""" print("Testing dose tracking UI functionality...") # Initialize managers medicine_manager = MedicineManager() pathology_manager = PathologyManager() # Create a simple logger import logging logger = logging.getLogger("test") logger.setLevel(logging.INFO) # Create root window root = tk.Tk() root.withdraw() # Initialize UI manager ui_manager = UIManager( root=root, logger=logger, medicine_manager=medicine_manager, pathology_manager=pathology_manager, ) # Test data with existing doses test_values = ["2025-07-31"] # date # Add pathology values pathologies = pathology_manager.get_all_pathologies() for _pathology_key, _pathology in pathologies.items(): test_values.append(5) # pathology value # Add medicine values and doses with some existing data medicines = medicine_manager.get_all_medicines() for _medicine_key in medicines: test_values.append(1) # medicine checkbox value test_values.append("08:00: 150mg\n14:00: 25mg") # existing doses test_values.append("Test dose tracking") # note print(f"Created test data with {len(test_values)} values") # Mock callbacks that will check the dose data dose_tracking_working = False saved_dose_data = None def mock_save_callback(*args): nonlocal dose_tracking_working, saved_dose_data if len(args) >= 2: saved_dose_data = args[-1] # dose_data should be last argument print(f"✅ Save called with dose data: {saved_dose_data}") # Check if dose data contains all expected medicines expected_medicines = set(medicines.keys()) actual_medicines = ( set(saved_dose_data.keys()) if isinstance(saved_dose_data, dict) else set() ) if expected_medicines == actual_medicines: dose_tracking_working = True print("✅ All medicines present in dose data") else: print( f"❌ Medicine mismatch. Expected: {expected_medicines}, " f"Got: {actual_medicines}" ) else: print("❌ Save callback called with insufficient arguments") def mock_delete_callback(win): print("✅ Delete callback called") win.destroy() callbacks = {"save": mock_save_callback, "delete": mock_delete_callback} try: # Create edit window edit_window = ui_manager.create_edit_window(tuple(test_values), callbacks) print("✅ Edit window with dose tracking created successfully") # Test the mock save to check dose data structure print("\nTesting dose data extraction...") mock_save_callback( edit_window, # window "2025-07-31", # date *[5] * len(pathologies), # pathology values *[1] * len(medicines), # medicine values "Test note", # note {med: "08:00: 150mg\n14:00: 25mg" for med in medicines}, # dose_data ) # Check that dose tracking variables are properly created print("\nChecking dose tracking UI elements...") medicine_count = len(medicines) print(f"✅ Should have dose tracking for {medicine_count} medicines:") for medicine_key, medicine in medicines.items(): print(f" - {medicine_key}: {medicine.display_name}") print(f"✅ Expected dose entry fields: {medicine_count}") print(f"✅ Expected dose history areas: {medicine_count}") print(f"✅ Expected punch buttons: {medicine_count}") if dose_tracking_working and saved_dose_data: print("✅ Dose tracking data structure is correct") # Verify each medicine has dose data for medicine_key in medicines: if medicine_key in saved_dose_data: dose_value = saved_dose_data[medicine_key] print(f"✅ {medicine_key} dose data: '{dose_value}'") else: print(f"❌ Missing dose data for {medicine_key}") return False edit_window.destroy() root.quit() return dose_tracking_working except Exception as e: print(f"❌ Error testing dose tracking: {e}") import traceback traceback.print_exc() return False if __name__ == "__main__": success = test_dose_tracking_ui() if success: print("\n🎉 Dose tracking UI functionality test passed!") print("Dose tracking is working with the dynamic system!") else: print("\n💥 Dose tracking UI functionality test failed!") sys.exit(1)