Add comprehensive tests for dose tracking functionality

- Implemented `test_dose_parsing_simple.py` to validate the dose parsing workflow.
- Created `test_dose_save.py` to verify the saving functionality of dose tracking.
- Added `test_dose_save_simple.py` for programmatic testing of dose saving without UI interaction.
- Developed `test_final_workflow.py` to test the complete dose tracking workflow, ensuring doses are preserved during edits.
- Enhanced `conftest.py` with a mock pathology manager for testing.
- Updated `test_data_manager.py` to include pathology manager in DataManager tests and ensure compatibility with new features.
This commit is contained in:
William Valentin
2025-07-31 09:50:45 -07:00
parent b8600ae57a
commit c755f0affc
22 changed files with 2801 additions and 394 deletions
+46 -87
View File
@@ -5,33 +5,43 @@ import os
import pandas as pd
from medicine_manager import MedicineManager
from pathology_manager import PathologyManager
class DataManager:
"""Handle all data operations for the application."""
def __init__(
self, filename: str, logger: logging.Logger, medicine_manager: MedicineManager
self,
filename: str,
logger: logging.Logger,
medicine_manager: MedicineManager,
pathology_manager: PathologyManager,
) -> None:
self.filename: str = filename
self.logger: logging.Logger = logger
self.medicine_manager = medicine_manager
self.pathology_manager = pathology_manager
self._initialize_csv_file()
def _get_csv_headers(self) -> list[str]:
"""Get CSV headers based on current medicine configuration."""
base_headers = ["date", "depression", "anxiety", "sleep", "appetite"]
"""Get CSV headers based on current pathology and medicine configuration."""
# Start with date
headers = ["date"]
# Add pathology headers
for pathology_key in self.pathology_manager.get_pathology_keys():
headers.append(pathology_key)
# Add medicine headers
medicine_headers = []
for medicine_key in self.medicine_manager.get_medicine_keys():
medicine_headers.extend([medicine_key, f"{medicine_key}_doses"])
headers.extend([medicine_key, f"{medicine_key}_doses"])
return base_headers + medicine_headers + ["note"]
return headers + ["note"]
def _initialize_csv_file(self) -> None:
"""Create CSV file with headers if it doesn't exist."""
if not os.path.exists(self.filename):
"""Create CSV file with headers if it doesn't exist or is empty."""
if not os.path.exists(self.filename) or os.path.getsize(self.filename) == 0:
with open(self.filename, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(self._get_csv_headers())
@@ -44,14 +54,11 @@ class DataManager:
try:
# Build dtype dictionary dynamically
dtype_dict = {
"depression": int,
"anxiety": int,
"sleep": int,
"appetite": int,
"date": str,
"note": str,
}
dtype_dict = {"date": str, "note": str}
# Add pathology types
for pathology_key in self.pathology_manager.get_pathology_keys():
dtype_dict[pathology_key] = int
# Add medicine types
for medicine_key in self.medicine_manager.get_medicine_keys():
@@ -99,69 +106,24 @@ class DataManager:
)
return False
# Find the row to update using original_date as a unique identifier
# Handle both old format (10 columns) and new format (16 columns)
if len(values) == 16:
# New format with all dose columns including quetiapine
df.loc[
df["date"] == original_date,
[
"date",
"depression",
"anxiety",
"sleep",
"appetite",
"bupropion",
"bupropion_doses",
"hydroxyzine",
"hydroxyzine_doses",
"gabapentin",
"gabapentin_doses",
"propranolol",
"propranolol_doses",
"quetiapine",
"quetiapine_doses",
"note",
],
] = values
elif len(values) == 14:
# Format without quetiapine
df.loc[
df["date"] == original_date,
[
"date",
"depression",
"anxiety",
"sleep",
"appetite",
"bupropion",
"bupropion_doses",
"hydroxyzine",
"hydroxyzine_doses",
"gabapentin",
"gabapentin_doses",
"propranolol",
"propranolol_doses",
"note",
],
] = values
else:
# Old format - only update the user-editable columns
df.loc[
df["date"] == original_date,
[
"date",
"depression",
"anxiety",
"sleep",
"appetite",
"bupropion",
"hydroxyzine",
"gabapentin",
"propranolol",
"note",
],
] = values
# Get current CSV headers to match with values
headers = self._get_csv_headers()
# Ensure we have the right number of values
if len(values) != len(headers):
self.logger.warning(
f"Value count mismatch: expected {len(headers)}, got {len(values)}"
)
# Pad with defaults if too few values
while len(values) < len(headers):
header = headers[len(values)]
if header == "note" or header.endswith("_doses"):
values.append("")
else:
values.append(0)
# Update the row using column names
df.loc[df["date"] == original_date, headers] = values
df.to_csv(self.filename, index=False)
return True
except Exception as e:
@@ -193,14 +155,11 @@ class DataManager:
# Find or create entry for the given date
if df.empty or date not in df["date"].values:
# Create new entry for today with default values
new_entry = {
"date": date,
"depression": 0,
"anxiety": 0,
"sleep": 0,
"appetite": 0,
"note": "",
}
new_entry = {"date": date, "note": ""}
# Add pathology columns with default values
for pathology_key in self.pathology_manager.get_pathology_keys():
new_entry[pathology_key] = 0
# Add medicine columns dynamically
for medicine_key in self.medicine_manager.get_medicine_keys():