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.
177 lines
6.2 KiB
Markdown
177 lines
6.2 KiB
Markdown
# Test Updates for Enhanced Legend Feature
|
|
|
|
## Overview
|
|
Updated test suite to cover the new enhanced legend functionality that displays individual medicines with average dosages and tracks medicines without dose data.
|
|
|
|
## New Test Methods Added
|
|
|
|
### 1. `test_enhanced_legend_functionality`
|
|
**Purpose**: Tests that the enhanced legend displays correctly with medicine dose data.
|
|
|
|
**What it tests**:
|
|
- Legend is called with enhanced formatting parameters (ncol=2, fontsize='small', etc.)
|
|
- Medicine toggles are properly handled
|
|
- Legend configuration parameters are correctly applied
|
|
|
|
**Key assertions**:
|
|
- `mock_ax.legend.assert_called()`
|
|
- Verifies `ncol=2`, `fontsize='small'`, `frameon=True` parameters
|
|
|
|
### 2. `test_legend_with_medicines_without_data`
|
|
**Purpose**: Tests that medicines without dose data are properly tracked and displayed in legend info.
|
|
|
|
**What it tests**:
|
|
- Medicines with dose data vs. medicines without dose data
|
|
- Additional legend entries for "Tracked (no doses)" information
|
|
- Proper handling of mixed data scenarios
|
|
|
|
**Key assertions**:
|
|
- Legend has more labels than original when medicines without data are present
|
|
- `mock_ax.legend.assert_called()`
|
|
|
|
### 3. `test_average_dose_calculation_in_legend`
|
|
**Purpose**: Tests that average doses are correctly calculated and used in legend labels.
|
|
|
|
**What it tests**:
|
|
- Dose calculation accuracy for varying dose amounts
|
|
- Average calculation logic for medicines with multiple daily entries
|
|
- Proper dose processing and bar plotting
|
|
|
|
**Key assertions**:
|
|
- Direct dose calculation verification: `assert bup_avg == 100.0`
|
|
- Bar plotting verification: `mock_ax.bar.assert_called()`
|
|
|
|
### 4. `test_legend_positioning_and_styling`
|
|
**Purpose**: Tests that all legend styling parameters are correctly applied.
|
|
|
|
**What it tests**:
|
|
- Complete set of legend parameters (loc, bbox_to_anchor, ncol, fontsize, frameon, fancybox, shadow, framealpha)
|
|
- Parameter value accuracy
|
|
- Consistent application of styling
|
|
|
|
**Key assertions**:
|
|
```python
|
|
expected_params = {
|
|
'loc': 'upper left',
|
|
'bbox_to_anchor': (0, 1),
|
|
'ncol': 2,
|
|
'fontsize': 'small',
|
|
'frameon': True,
|
|
'fancybox': True,
|
|
'shadow': True,
|
|
'framealpha': 0.9
|
|
}
|
|
```
|
|
|
|
### 5. `test_medicine_tracking_lists`
|
|
**Purpose**: Tests that medicines are correctly categorized into medicines_with_data and medicines_without_data lists.
|
|
|
|
**What it tests**:
|
|
- Proper categorization of medicines based on dose data availability
|
|
- Toggle state handling for different medicine states
|
|
- Mixed scenarios with some medicines having data and others not
|
|
|
|
**Key assertions**:
|
|
- `mock_ax.bar.assert_called()` for medicines with data
|
|
- `mock_ax.legend.assert_called()` for legend creation
|
|
|
|
### 6. `test_legend_dummy_handle_creation`
|
|
**Purpose**: Tests that dummy handles are created for medicines without dose data in legend.
|
|
|
|
**What it tests**:
|
|
- Rectangle dummy handle creation for text-only legend entries
|
|
- Proper import and usage of matplotlib.patches.Rectangle
|
|
- Integration of dummy handles with existing legend system
|
|
|
|
**Key assertions**:
|
|
- `mock_rectangle.assert_called()` when medicines without data are present
|
|
|
|
### 7. `test_empty_dataframe_legend_handling`
|
|
**Purpose**: Tests that legend is handled correctly with empty DataFrame scenarios.
|
|
|
|
**What it tests**:
|
|
- No legend creation when no data is present
|
|
- Proper graph clearing and canvas redrawing
|
|
- Edge case handling
|
|
|
|
**Key assertions**:
|
|
- `mock_ax.legend.assert_not_called()` for empty data
|
|
- `mock_ax.clear.assert_called()` and `mock_canvas.draw.assert_called()`
|
|
|
|
## Test Data Enhancements
|
|
|
|
### Enhanced Sample DataFrames
|
|
Tests now use more comprehensive DataFrames that include:
|
|
- **Realistic dose data**: Multiple dose entries with varying amounts
|
|
- **Mixed scenarios**: Some medicines with data, others without
|
|
- **Average calculation data**: Varying doses across multiple days for accurate average testing
|
|
- **Edge cases**: Empty dose strings, missing data scenarios
|
|
|
|
### Example Test Data Structure:
|
|
```python
|
|
df_with_varying_doses = pd.DataFrame({
|
|
'bupropion_doses': ['100mg', '200mg', '150mg'], # Avg: 150mg
|
|
'propranolol_doses': ['10mg', '20mg', ''], # Avg: 15mg
|
|
'hydroxyzine_doses': ['', '', ''], # No data
|
|
})
|
|
```
|
|
|
|
## Mock Enhancements
|
|
|
|
### Legend-Specific Mocks:
|
|
- **`mock_ax.get_legend_handles_labels`**: Returns mock handles and labels
|
|
- **`matplotlib.patches.Rectangle`**: Mocked for dummy handle creation
|
|
- **Enhanced legend parameter verification**: Detailed parameter checking
|
|
|
|
### Integration Testing:
|
|
- Tests work with existing matplotlib mocking structure
|
|
- Compatible with existing GraphManager test patterns
|
|
- Maintains isolation between test methods
|
|
|
|
## Coverage Areas
|
|
|
|
### Legend Functionality:
|
|
✅ **Enhanced formatting**: Multi-column, styling, positioning
|
|
✅ **Medicine tracking**: With/without data categorization
|
|
✅ **Average calculations**: Accurate dose averaging in labels
|
|
✅ **Dummy handles**: Text-only legend entries
|
|
✅ **Parameter validation**: All styling parameters verified
|
|
|
|
### Edge Cases:
|
|
✅ **Empty DataFrames**: No legend creation
|
|
✅ **Mixed data scenarios**: Some medicines with/without data
|
|
✅ **Toggle combinations**: Various medicine toggle states
|
|
✅ **Import handling**: Matplotlib patches import testing
|
|
|
|
### Integration:
|
|
✅ **Existing functionality**: Compatible with previous tests
|
|
✅ **Mock consistency**: Uses established mocking patterns
|
|
✅ **Error handling**: Graceful handling of edge cases
|
|
|
|
## Running the Tests
|
|
|
|
```bash
|
|
# Run all graph manager tests
|
|
.venv/bin/python -m pytest tests/test_graph_manager.py -v
|
|
|
|
# Run only legend-related tests
|
|
.venv/bin/python -m pytest tests/test_graph_manager.py -k "legend" -v
|
|
|
|
# Run with coverage
|
|
.venv/bin/python -m pytest tests/test_graph_manager.py --cov=src.graph_manager --cov-report=html
|
|
```
|
|
|
|
## Benefits
|
|
|
|
### Test Quality:
|
|
- **Comprehensive coverage** of new legend functionality
|
|
- **Edge case testing** for robust error handling
|
|
- **Integration testing** with existing graph functionality
|
|
|
|
### Maintenance:
|
|
- **Clear test names** indicating specific functionality
|
|
- **Isolated test methods** for easy debugging
|
|
- **Consistent patterns** following existing test structure
|
|
|
|
The updated tests ensure that the enhanced legend functionality is thoroughly validated while maintaining compatibility with existing GraphManager features.
|