Files
thechart/scripts/test_dose_calculation.py
William Valentin b76191d66d
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
feat: Implement dose calculation fix and enhance legend feature
- Fixed dose calculation logic in `_calculate_daily_dose` to correctly parse timestamps with multiple colons.
- Added comprehensive test cases for various dose formats and edge cases in `test_dose_calculation.py`.
- Enhanced graph legend to display individual medicines with average dosages and track medicines without dose data.
- Updated legend styling and positioning for better readability and organization.
- Created new tests for enhanced legend functionality, including handling of medicines with and without data.
- Improved mocking for matplotlib components in tests to prevent TypeErrors.
2025-07-30 14:22:07 -07:00

84 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""
Simple test script to verify dose calculation functionality.
"""
import os
import sys
# Add the src directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
import tkinter as tk
from graph_manager import GraphManager
def test_dose_calculation():
"""Test the dose calculation method directly."""
# Create a minimal tkinter setup for GraphManager
root = tk.Tk()
root.withdraw() # Hide the window
frame = tk.Frame(root)
try:
# Create GraphManager instance
gm = GraphManager(frame)
# Test cases
test_cases = [
# (input, expected_output, description)
("2025-07-28 18:59:45:150mg", 150.0, "Single dose with timestamp"),
(
"2025-07-28 18:59:45:150mg|2025-07-28 19:34:19:75mg",
225.0,
"Multiple doses",
),
("• • • • 2025-07-30 07:50:00:300", 300.0, "Dose with bullet symbols"),
(
"2025-07-28 18:59:45:12.5mg|2025-07-28 19:34:19:7.5mg",
20.0,
"Decimal doses",
),
("100mg|50mg", 150.0, "Doses without timestamps"),
("• 2025-07-30 22:50:00:10|75mg", 85.0, "Mixed format"),
("", 0.0, "Empty string"),
("nan", 0.0, "NaN value"),
("2025-07-28 18:59:45:10|2025-07-28 19:34:19:5", 15.0, "No units"),
]
print("Testing dose calculation...")
all_passed = True
for input_str, expected, description in test_cases:
result = gm._calculate_daily_dose(input_str)
passed = (
abs(result - expected) < 0.001
) # Allow for floating point precision
status = "PASS" if passed else "FAIL"
print(f"{status}: {description}")
print(f" Input: '{input_str}'")
print(f" Expected: {expected}, Got: {result}")
print()
if not passed:
all_passed = False
if all_passed:
print("All dose calculation tests PASSED!")
else:
print("Some dose calculation tests FAILED!")
return all_passed
finally:
root.destroy()
if __name__ == "__main__":
success = test_dose_calculation()
sys.exit(0 if success else 1)