Files
thechart/scripts/test_dose_tracking_ui.py

118 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""
Test script for dose tracking UI in edit window.
Tests the specific issue where adding new doses replaces existing ones.
"""
# ruff: noqa: E402
import sys
import tkinter as tk
from datetime import datetime
from pathlib import Path
def _ensure_src_on_path() -> None:
src_dir = Path(__file__).resolve().parent.parent / "src"
if str(src_dir) not in sys.path:
sys.path.insert(0, str(src_dir))
_ensure_src_on_path()
from thechart.core.constants import LOG_LEVEL
from thechart.core.logger import init_logger
from thechart.managers import Medicine, MedicineManager, PathologyManager
from thechart.ui import ThemeManager
from thechart.ui.ui_manager import UIManager
logger = init_logger(__name__, testing_mode=(LOG_LEVEL == "DEBUG"))
def test_dose_tracking():
"""Test the dose tracking functionality."""
# Create test window
root = tk.Tk()
root.title("Dose Tracking Test")
root.geometry("800x600")
# Initialize managers
medicine_manager = MedicineManager(logger=logger)
pathology_manager = PathologyManager(logger=logger)
theme_manager = ThemeManager(root, logger)
ui_manager = UIManager(
root, logger, medicine_manager, pathology_manager, theme_manager
)
# Add a test medicine if none exist
medicines = medicine_manager.get_all_medicines()
if not medicines:
test_medicine = Medicine(
key="bupropion",
display_name="Bupropion",
dosage="150mg",
color="#4CAF50",
quick_doses=["150", "300"],
is_default=True,
)
medicine_manager.add_medicine(test_medicine)
print("Added test medicine: Bupropion")
# Test data - simulate existing doses for today
test_date = datetime.now().strftime("%Y-%m-%d")
existing_doses = {"bupropion": "• 08:00 AM - 150mg\n• 12:00 PM - 150mg"}
# Create test callbacks
def test_save_callback(edit_win, *args):
print(f"Save callback called with {len(args)} arguments")
print(f"Arguments: {args}")
# Don't actually save, just print for testing
def test_delete_callback(edit_win):
print("Delete callback called")
edit_win.destroy()
callbacks = {"save": test_save_callback, "delete": test_delete_callback}
# Test values to populate the edit window
test_values = (
test_date, # date
0, # pathology score (if any)
1, # medicine taken (bupropion)
existing_doses["bupropion"], # existing doses
"Test note", # note
)
print(f"Creating edit window with test values: {test_values}")
# Create the edit window
_ = ui_manager.create_edit_window(test_values, callbacks)
# Add instructions label
instructions = tk.Label(
root,
text="Instructions:\n"
"1. The edit window should show existing doses: 08:00 AM and 12:00 PM\n"
"2. Enter a new dose (e.g., 150) and click 'Take Bupropion'\n"
"3. The new dose should be ADDED to existing doses, not replace them\n"
"4. Click Save to see the final dose data in console",
justify=tk.LEFT,
wraplength=500,
bg="lightyellow",
padx=10,
pady=10,
)
instructions.pack(pady=10, padx=10, fill=tk.X)
print("Test setup complete. Check the edit window for dose tracking behavior.")
print(
"Expected behavior: New doses should be added to existing ones, "
"not replace them."
)
root.mainloop()
if __name__ == "__main__":
test_dose_tracking()