feat: Add comprehensive tests for punch button functionality and multiple dose handling

This commit is contained in:
William Valentin
2025-07-28 23:10:04 -07:00
parent 26fc74b580
commit bce6c8c27d
8 changed files with 1088 additions and 1 deletions

View File

@@ -513,8 +513,10 @@ class UIManager:
def save_with_doses():
# Extract dose data from the text widgets
dose_data = {}
for medicine in ["bupropion", "hydroxyzine", "gabapentin", "propranolol"]:
dose_text_key = f"{medicine}_doses_text"
if dose_text_key in vars_dict and isinstance(
vars_dict[dose_text_key], tk.Text
):
@@ -524,6 +526,7 @@ class UIManager:
)
else:
dose_data[medicine] = ""
dose_data[medicine] = ""
callbacks["save"](
parent,
@@ -593,11 +596,19 @@ class UIManager:
dose_vars[f"{medicine}_doses_text"] = dose_text
# Punch button to record dose immediately
def create_punch_command(med_name, entry_var, text_widget):
"""Create a punch command that captures the specific widgets."""
def punch_command():
self._punch_dose_direct(med_name, entry_var, text_widget)
return punch_command
punch_button = ttk.Button(
dose_frame,
text=f"Take {medicine.title()}",
width=15,
command=lambda med=medicine: self._punch_dose_in_edit(med, dose_vars),
command=create_punch_command(medicine, dose_entry_var, dose_text),
)
punch_button.grid(row=idx, column=3, sticky="w", padx=5, pady=2)
@@ -634,6 +645,52 @@ class UIManager:
return dose_vars
def _punch_dose_direct(
self,
medicine_name: str,
dose_entry_var: tk.StringVar,
dose_text_widget: tk.Text,
) -> None:
"""Handle punch dose button with direct widget references."""
dose = dose_entry_var.get().strip()
# Find the parent edit window
parent_window = dose_text_widget.winfo_toplevel()
if not dose:
messagebox.showerror(
"Error",
f"Please enter a dose amount for {medicine_name}",
parent=parent_window,
)
return
# Get current time
now = datetime.now()
time_str = now.strftime("%H:%M")
# Get current content
current_content = dose_text_widget.get(1.0, tk.END).strip()
# Add new dose entry
new_dose_line = f"{time_str}: {dose}"
if current_content == "No doses recorded" or not current_content:
dose_text_widget.delete(1.0, tk.END)
dose_text_widget.insert(1.0, new_dose_line)
else:
dose_text_widget.insert(tk.END, f"\n{new_dose_line}")
# Clear the entry field
dose_entry_var.set("")
# Show success message
messagebox.showinfo(
"Success",
f"{medicine_name.title()} dose recorded: {dose} at {time_str}",
parent=parent_window,
)
def _punch_dose_in_edit(self, medicine_name: str, dose_vars: dict) -> None:
"""Handle punch dose button in edit window."""
dose_entry_var = dose_vars.get(f"{medicine_name}_entry_var")