feat: Implement dose calculation fix and enhance legend feature
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
- 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.
This commit is contained in:
@@ -89,3 +89,33 @@ def sample_dose_data():
|
||||
'nan_value': 'nan', # Should be 0
|
||||
'no_units': '2025-07-28 18:59:45:10|2025-07-28 19:34:19:5', # Should sum to 15
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def legend_test_dataframe():
|
||||
"""DataFrame specifically designed for testing legend functionality."""
|
||||
return pd.DataFrame({
|
||||
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
|
||||
'depression': [3, 2, 4],
|
||||
'anxiety': [2, 3, 1],
|
||||
'sleep': [4, 3, 5],
|
||||
'appetite': [3, 4, 2],
|
||||
# Medicine with consistent doses for average testing
|
||||
'bupropion': [1, 1, 1],
|
||||
'bupropion_doses': ['2024-01-01 08:00:00:100mg',
|
||||
'2024-01-02 08:00:00:200mg',
|
||||
'2024-01-03 08:00:00:150mg'], # Average: 150mg
|
||||
# Medicine with varying doses
|
||||
'propranolol': [1, 1, 0],
|
||||
'propranolol_doses': ['2024-01-01 12:00:00:10mg',
|
||||
'2024-01-02 12:00:00:20mg',
|
||||
''], # Average: 15mg (10+20)/2
|
||||
# Medicines without dose data
|
||||
'hydroxyzine': [0, 0, 0],
|
||||
'hydroxyzine_doses': ['', '', ''],
|
||||
'gabapentin': [0, 0, 0],
|
||||
'gabapentin_doses': ['', '', ''],
|
||||
'quetiapine': [0, 0, 0],
|
||||
'quetiapine_doses': ['', '', ''],
|
||||
'note': ['Test note 1', 'Test note 2', 'Test note 3']
|
||||
})
|
||||
|
||||
@@ -433,6 +433,329 @@ class TestGraphManager:
|
||||
assert mock_ax.clear.call_count >= 2
|
||||
assert mock_canvas.draw.call_count >= 2
|
||||
|
||||
def test_enhanced_legend_functionality(self, parent_frame):
|
||||
"""Test that the enhanced legend displays correctly with medicine data."""
|
||||
df_with_doses = pd.DataFrame({
|
||||
'date': ['2024-01-01', '2024-01-02'],
|
||||
'depression': [3, 2],
|
||||
'anxiety': [2, 3],
|
||||
'sleep': [4, 3],
|
||||
'appetite': [3, 4],
|
||||
'bupropion': [1, 1],
|
||||
'bupropion_doses': ['2024-01-01 08:00:00:150mg', '2024-01-02 08:00:00:200mg'],
|
||||
'hydroxyzine': [0, 0],
|
||||
'hydroxyzine_doses': ['', ''],
|
||||
'gabapentin': [0, 0],
|
||||
'gabapentin_doses': ['', ''],
|
||||
'propranolol': [1, 1],
|
||||
'propranolol_doses': ['2024-01-01 12:00:00:10mg', '2024-01-02 12:00:00:15mg'],
|
||||
'quetiapine': [0, 0],
|
||||
'quetiapine_doses': ['', ''],
|
||||
})
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
mock_ax.get_legend_handles_labels.return_value = ([], [])
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
|
||||
# Enable some medicine toggles
|
||||
gm.toggle_vars["bupropion"].set(True)
|
||||
gm.toggle_vars["propranolol"].set(True)
|
||||
gm.toggle_vars["hydroxyzine"].set(True) # No dose data
|
||||
|
||||
gm.update_graph(df_with_doses)
|
||||
|
||||
# Verify that legend is called with enhanced parameters
|
||||
mock_ax.legend.assert_called()
|
||||
legend_call = mock_ax.legend.call_args
|
||||
|
||||
# Check that enhanced legend parameters are used
|
||||
assert 'ncol' in legend_call.kwargs
|
||||
assert legend_call.kwargs['ncol'] == 2
|
||||
assert 'fontsize' in legend_call.kwargs
|
||||
assert legend_call.kwargs['fontsize'] == 'small'
|
||||
assert 'frameon' in legend_call.kwargs
|
||||
assert legend_call.kwargs['frameon'] is True
|
||||
|
||||
def test_legend_with_medicines_without_data(self, parent_frame):
|
||||
"""Test that medicines without dose data are properly tracked in legend."""
|
||||
df_with_partial_doses = pd.DataFrame({
|
||||
'date': ['2024-01-01'],
|
||||
'depression': [3],
|
||||
'anxiety': [2],
|
||||
'sleep': [4],
|
||||
'appetite': [3],
|
||||
'bupropion': [1],
|
||||
'bupropion_doses': ['2024-01-01 08:00:00:150mg'],
|
||||
'hydroxyzine': [0],
|
||||
'hydroxyzine_doses': [''], # No dose data
|
||||
'gabapentin': [0],
|
||||
'gabapentin_doses': [''], # No dose data
|
||||
'propranolol': [0],
|
||||
'propranolol_doses': [''],
|
||||
'quetiapine': [0],
|
||||
'quetiapine_doses': [''],
|
||||
})
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
|
||||
# Mock the legend handles and labels
|
||||
original_handles = [Mock()]
|
||||
original_labels = ['Bupropion (avg: 150.0mg)']
|
||||
mock_ax.get_legend_handles_labels.return_value = (original_handles, original_labels)
|
||||
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
|
||||
# Enable medicines with and without data
|
||||
gm.toggle_vars["bupropion"].set(True) # Has data
|
||||
gm.toggle_vars["hydroxyzine"].set(True) # No data
|
||||
gm.toggle_vars["gabapentin"].set(True) # No data
|
||||
|
||||
gm.update_graph(df_with_partial_doses)
|
||||
|
||||
# Verify legend was called
|
||||
mock_ax.legend.assert_called()
|
||||
|
||||
# Check that the legend call includes additional handles/labels
|
||||
legend_call = mock_ax.legend.call_args
|
||||
handles, labels = legend_call.args[:2]
|
||||
|
||||
# Should have more labels than just the original ones
|
||||
assert len(labels) > len(original_labels)
|
||||
|
||||
def test_average_dose_calculation_in_legend(self, parent_frame):
|
||||
"""Test that average doses are correctly calculated and displayed in legend."""
|
||||
df_with_varying_doses = pd.DataFrame({
|
||||
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
|
||||
'depression': [3, 2, 4],
|
||||
'anxiety': [2, 3, 1],
|
||||
'sleep': [4, 3, 5],
|
||||
'appetite': [3, 4, 2],
|
||||
'bupropion': [1, 1, 1],
|
||||
'bupropion_doses': ['2024-01-01 08:00:00:100mg',
|
||||
'2024-01-02 08:00:00:200mg',
|
||||
'2024-01-03 08:00:00:150mg'], # Average should be 150mg
|
||||
'propranolol': [1, 1, 0],
|
||||
'propranolol_doses': ['2024-01-01 12:00:00:10mg',
|
||||
'2024-01-02 12:00:00:20mg',
|
||||
''], # Average should be 15mg
|
||||
'hydroxyzine': [0, 0, 0],
|
||||
'hydroxyzine_doses': ['', '', ''],
|
||||
'gabapentin': [0, 0, 0],
|
||||
'gabapentin_doses': ['', '', ''],
|
||||
'quetiapine': [0, 0, 0],
|
||||
'quetiapine_doses': ['', '', ''],
|
||||
})
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
|
||||
# Test the average calculation directly
|
||||
bup_avg = gm._calculate_daily_dose('2024-01-01 08:00:00:100mg')
|
||||
assert bup_avg == 100.0
|
||||
|
||||
prop_avg = gm._calculate_daily_dose('2024-01-01 12:00:00:10mg')
|
||||
assert prop_avg == 10.0
|
||||
|
||||
# Test with full data
|
||||
gm.toggle_vars["bupropion"].set(True)
|
||||
gm.toggle_vars["propranolol"].set(True)
|
||||
gm.update_graph(df_with_varying_doses)
|
||||
|
||||
# Verify that bars were plotted (indicating dose data was processed)
|
||||
mock_ax.bar.assert_called()
|
||||
|
||||
def test_legend_positioning_and_styling(self, parent_frame):
|
||||
"""Test that legend positioning and styling parameters are correctly applied."""
|
||||
df_simple = pd.DataFrame({
|
||||
'date': ['2024-01-01'],
|
||||
'depression': [3],
|
||||
'anxiety': [2],
|
||||
'sleep': [4],
|
||||
'appetite': [3],
|
||||
'bupropion': [1],
|
||||
'bupropion_doses': ['2024-01-01 08:00:00:150mg'],
|
||||
'hydroxyzine': [0],
|
||||
'hydroxyzine_doses': [''],
|
||||
'gabapentin': [0],
|
||||
'gabapentin_doses': [''],
|
||||
'propranolol': [0],
|
||||
'propranolol_doses': [''],
|
||||
'quetiapine': [0],
|
||||
'quetiapine_doses': [''],
|
||||
})
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
mock_ax.get_legend_handles_labels.return_value = ([Mock()], ['Test Label'])
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
gm.update_graph(df_simple)
|
||||
|
||||
# Verify legend styling parameters
|
||||
mock_ax.legend.assert_called()
|
||||
legend_call = mock_ax.legend.call_args
|
||||
|
||||
expected_params = {
|
||||
'loc': 'upper left',
|
||||
'bbox_to_anchor': (0, 1),
|
||||
'ncol': 2,
|
||||
'fontsize': 'small',
|
||||
'frameon': True,
|
||||
'fancybox': True,
|
||||
'shadow': True,
|
||||
'framealpha': 0.9
|
||||
}
|
||||
|
||||
for param, expected_value in expected_params.items():
|
||||
assert param in legend_call.kwargs
|
||||
assert legend_call.kwargs[param] == expected_value
|
||||
|
||||
def test_medicine_tracking_lists(self, parent_frame):
|
||||
"""Test that medicines are correctly categorized into with_data and without_data lists."""
|
||||
df_mixed_data = pd.DataFrame({
|
||||
'date': ['2024-01-01', '2024-01-02'],
|
||||
'depression': [3, 2],
|
||||
'anxiety': [2, 3],
|
||||
'sleep': [4, 3],
|
||||
'appetite': [3, 4],
|
||||
# Medicines with data
|
||||
'bupropion': [1, 1],
|
||||
'bupropion_doses': ['2024-01-01 08:00:00:150mg', '2024-01-02 08:00:00:200mg'],
|
||||
'propranolol': [1, 1],
|
||||
'propranolol_doses': ['2024-01-01 12:00:00:10mg', '2024-01-02 12:00:00:15mg'],
|
||||
# Medicines without data (but toggled on)
|
||||
'hydroxyzine': [0, 0],
|
||||
'hydroxyzine_doses': ['', ''],
|
||||
'gabapentin': [0, 0],
|
||||
'gabapentin_doses': ['', ''],
|
||||
'quetiapine': [0, 0],
|
||||
'quetiapine_doses': ['', ''],
|
||||
})
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
mock_ax.get_legend_handles_labels.return_value = ([], [])
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
|
||||
# Enable all medicines
|
||||
gm.toggle_vars["bupropion"].set(True) # Has data
|
||||
gm.toggle_vars["propranolol"].set(True) # Has data
|
||||
gm.toggle_vars["hydroxyzine"].set(True) # No data
|
||||
gm.toggle_vars["gabapentin"].set(True) # No data
|
||||
gm.toggle_vars["quetiapine"].set(False) # Disabled
|
||||
|
||||
gm.update_graph(df_mixed_data)
|
||||
|
||||
# Verify that the method was called and plotting occurred
|
||||
mock_ax.bar.assert_called() # Should be called for medicines with data
|
||||
mock_ax.legend.assert_called() # Legend should be created
|
||||
|
||||
def test_legend_dummy_handle_creation(self, parent_frame):
|
||||
"""Test that dummy handles are created for medicines without data."""
|
||||
df_no_dose_data = pd.DataFrame({
|
||||
'date': ['2024-01-01'],
|
||||
'depression': [3],
|
||||
'anxiety': [2],
|
||||
'sleep': [4],
|
||||
'appetite': [3],
|
||||
'bupropion': [0],
|
||||
'bupropion_doses': [''],
|
||||
'hydroxyzine': [0],
|
||||
'hydroxyzine_doses': [''],
|
||||
'gabapentin': [0],
|
||||
'gabapentin_doses': [''],
|
||||
'propranolol': [0],
|
||||
'propranolol_doses': [''],
|
||||
'quetiapine': [0],
|
||||
'quetiapine_doses': [''],
|
||||
})
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
mock_ax.get_legend_handles_labels.return_value = ([Mock()], ['Depression'])
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
# Mock Rectangle import for dummy handle creation
|
||||
with patch('matplotlib.patches.Rectangle') as mock_rectangle:
|
||||
mock_dummy_handle = Mock()
|
||||
mock_rectangle.return_value = mock_dummy_handle
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
|
||||
# Enable some medicines without data
|
||||
gm.toggle_vars["hydroxyzine"].set(True)
|
||||
gm.toggle_vars["gabapentin"].set(True)
|
||||
|
||||
gm.update_graph(df_no_dose_data)
|
||||
|
||||
# If there are medicines without data, Rectangle should be called
|
||||
# to create dummy handles
|
||||
if gm.toggle_vars["hydroxyzine"].get() or gm.toggle_vars["gabapentin"].get():
|
||||
mock_rectangle.assert_called()
|
||||
|
||||
def test_empty_dataframe_legend_handling(self, parent_frame):
|
||||
"""Test that legend is handled correctly with empty DataFrame."""
|
||||
empty_df = pd.DataFrame()
|
||||
|
||||
with patch('matplotlib.pyplot.subplots') as mock_subplots:
|
||||
mock_fig = Mock()
|
||||
mock_ax = Mock()
|
||||
mock_subplots.return_value = (mock_fig, mock_ax)
|
||||
|
||||
with patch('graph_manager.FigureCanvasTkAgg') as mock_canvas_class:
|
||||
mock_canvas = Mock()
|
||||
mock_canvas_class.return_value = mock_canvas
|
||||
|
||||
gm = GraphManager(parent_frame)
|
||||
gm.update_graph(empty_df)
|
||||
|
||||
# With empty data, legend should not be called
|
||||
mock_ax.legend.assert_not_called()
|
||||
mock_ax.clear.assert_called()
|
||||
mock_canvas.draw.assert_called()
|
||||
|
||||
def test_dose_calculation_comprehensive(self, parent_frame, sample_dose_data):
|
||||
"""Test dose calculation with comprehensive test cases."""
|
||||
gm = GraphManager(parent_frame)
|
||||
|
||||
Reference in New Issue
Block a user