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:
146
test_final_workflow.py
Normal file
146
test_final_workflow.py
Normal file
@@ -0,0 +1,146 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test the complete dose tracking workflow after fixing the parsing issues."""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pandas as pd
|
||||
|
||||
sys.path.append("src")
|
||||
from data_manager import DataManager
|
||||
from ui_manager import UIManager
|
||||
|
||||
|
||||
def test_dose_workflow():
|
||||
"""Test that doses are preserved when editing through the UI."""
|
||||
|
||||
# Create temporary directory and CSV file
|
||||
temp_dir = tempfile.mkdtemp()
|
||||
csv_file = os.path.join(temp_dir, "test.csv")
|
||||
print(f"Using temporary CSV: {csv_file}")
|
||||
|
||||
try:
|
||||
# Create mock managers with known configurations
|
||||
mock_medicine_manager = Mock()
|
||||
mock_medicine_manager.get_dynamic_columns.return_value = [
|
||||
"Medicine A",
|
||||
"Medicine B",
|
||||
]
|
||||
mock_medicine_manager.get_medicines.return_value = {
|
||||
"med1": {"name": "Medicine A"},
|
||||
"med2": {"name": "Medicine B"},
|
||||
}
|
||||
mock_medicine_manager.get_medicine_keys.return_value = ["med1", "med2"]
|
||||
|
||||
mock_pathology_manager = Mock()
|
||||
mock_pathology_manager.get_dynamic_columns.return_value = [
|
||||
"Pathology X",
|
||||
"Pathology Y",
|
||||
]
|
||||
mock_pathology_manager.get_pathology_keys.return_value = ["path1", "path2"]
|
||||
|
||||
# Create DataManager and UIManager
|
||||
import logging
|
||||
import tkinter as tk
|
||||
|
||||
logger = logging.getLogger("test")
|
||||
root = tk.Tk()
|
||||
root.withdraw() # Hide the window during testing
|
||||
|
||||
data_manager = DataManager(
|
||||
csv_file, logger, mock_medicine_manager, mock_pathology_manager
|
||||
)
|
||||
ui_manager = UIManager(
|
||||
root, logger, mock_medicine_manager, mock_pathology_manager
|
||||
)
|
||||
|
||||
# Add initial entry with some doses
|
||||
print("\n1. Adding initial entry with two doses...")
|
||||
initial_data = {
|
||||
"Date": "2025-01-30",
|
||||
"Depression": 3,
|
||||
"Anxiety": 4,
|
||||
"Sleep": 7,
|
||||
"Appetite": 6,
|
||||
"Medicine A": "2025-01-30 08:00:00:150mg|2025-01-30 14:00:00:25mg",
|
||||
"Medicine B": "",
|
||||
"Pathology X": 2,
|
||||
"Pathology Y": 1,
|
||||
"Notes": "Initial entry",
|
||||
}
|
||||
data_manager.add_entry(initial_data)
|
||||
|
||||
# Check what was saved
|
||||
df = pd.read_csv(csv_file)
|
||||
print(f'Medicine A after initial save: "{df.iloc[0]["Medicine A"]}"')
|
||||
|
||||
# Now simulate the UI editing workflow
|
||||
print("\n2. Simulating UI edit workflow...")
|
||||
|
||||
# Get the saved data (as it would appear in edit window)
|
||||
saved_medicine_a = df.iloc[0]["Medicine A"]
|
||||
print(f'Saved Medicine A doses: "{saved_medicine_a}"')
|
||||
|
||||
# Create mock text widget to simulate _populate_dose_history
|
||||
class MockText:
|
||||
def __init__(self):
|
||||
self.content = ""
|
||||
|
||||
def configure(self, state):
|
||||
pass
|
||||
|
||||
def delete(self, start, end):
|
||||
self.content = ""
|
||||
|
||||
def insert(self, pos, text):
|
||||
self.content = text
|
||||
|
||||
def get(self, start, end):
|
||||
return self.content
|
||||
|
||||
mock_text = MockText()
|
||||
ui_manager._populate_dose_history(mock_text, saved_medicine_a)
|
||||
print(f'UI display after _populate_dose_history: "{mock_text.content}"')
|
||||
|
||||
# Simulate user adding a new dose to the text widget
|
||||
user_edited_content = mock_text.content + "\n• 06:00 PM - 50mg"
|
||||
print(f'User adds new dose, text widget now contains: "{user_edited_content}"')
|
||||
|
||||
# Parse this back for saving
|
||||
parsed_doses = ui_manager._parse_dose_history_for_saving(
|
||||
user_edited_content, "2025-01-30"
|
||||
)
|
||||
print(f'Parsed for saving: "{parsed_doses}"')
|
||||
|
||||
# Update the entry
|
||||
update_data = initial_data.copy()
|
||||
update_data["Medicine A"] = parsed_doses
|
||||
data_manager.update_entry("2025-01-30", update_data)
|
||||
|
||||
# Check final result
|
||||
df = pd.read_csv(csv_file)
|
||||
final_medicine_a = df.iloc[0]["Medicine A"]
|
||||
print(f'\n3. Final Medicine A after update: "{final_medicine_a}"')
|
||||
|
||||
# Count doses
|
||||
dose_count = len([d for d in final_medicine_a.split("|") if d.strip()])
|
||||
print(f"Final dose count: {dose_count}")
|
||||
|
||||
if dose_count == 3:
|
||||
print("✅ SUCCESS: All doses preserved!")
|
||||
return True
|
||||
else:
|
||||
print("❌ FAILURE: Doses were lost!")
|
||||
return False
|
||||
|
||||
finally:
|
||||
# Clean up
|
||||
shutil.rmtree(temp_dir)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_dose_workflow()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user