|
| 1 | +import pytest |
| 2 | + |
| 3 | +from metrics.domain.charts.chart_settings.dual_category import DualCategoryChartSettings |
| 4 | +from metrics.domain.models import ChartGenerationPayload, PlotGenerationData |
| 5 | +from tests.conftest import fake_plot_data |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture() |
| 9 | +def fake_dual_category_chart_settings( |
| 10 | + fake_plot_data: PlotGenerationData, |
| 11 | +) -> DualCategoryChartSettings: |
| 12 | + payload = ChartGenerationPayload( |
| 13 | + chart_width=640, |
| 14 | + chart_height=400, |
| 15 | + plots=[fake_plot_data], |
| 16 | + x_axis_title="Date", |
| 17 | + y_axis_title="Cases", |
| 18 | + ) |
| 19 | + return DualCategoryChartSettings(chart_generation_payload=payload) |
| 20 | + |
| 21 | + |
| 22 | +class TestDualCategoryChartSettings: |
| 23 | + def test_get_legend_config( |
| 24 | + self, fake_dual_category_chart_settings: DualCategoryChartSettings |
| 25 | + ): |
| 26 | + """ |
| 27 | + Given an instance of `DualCategoryChartSettings` without a legend title |
| 28 | + When `_get_legend_config()` is called |
| 29 | + Then the legend configuration is returned without a title |
| 30 | + """ |
| 31 | + # Given |
| 32 | + chart_settings = fake_dual_category_chart_settings |
| 33 | + |
| 34 | + # When |
| 35 | + legend_config = chart_settings._get_legend_config() |
| 36 | + |
| 37 | + # Then |
| 38 | + assert legend_config == { |
| 39 | + "legend": { |
| 40 | + "font": chart_settings._get_tick_font_config(), |
| 41 | + "orientation": "h", |
| 42 | + "y": 1.0, |
| 43 | + "x": 0.5, |
| 44 | + "xanchor": "center", |
| 45 | + "yanchor": "bottom", |
| 46 | + "entrywidth": 80, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + def test_get_legend_config_includes_legend_title_when_provided( |
| 51 | + self, fake_dual_category_chart_settings: DualCategoryChartSettings |
| 52 | + ): |
| 53 | + """ |
| 54 | + Given an instance of `DualCategoryChartSettings` with a legend title |
| 55 | + When `_get_legend_config()` is called |
| 56 | + Then the legend configuration includes a formatted title |
| 57 | + """ |
| 58 | + # Given |
| 59 | + chart_settings = fake_dual_category_chart_settings |
| 60 | + legend_title = "Age group" |
| 61 | + chart_settings._chart_generation_payload.legend_title = legend_title |
| 62 | + |
| 63 | + # When |
| 64 | + legend_config = chart_settings._get_legend_config() |
| 65 | + |
| 66 | + # Then |
| 67 | + assert legend_config == { |
| 68 | + "legend": { |
| 69 | + "font": chart_settings._get_tick_font_config(), |
| 70 | + "orientation": "h", |
| 71 | + "y": 1.0, |
| 72 | + "x": 0.5, |
| 73 | + "xanchor": "center", |
| 74 | + "yanchor": "bottom", |
| 75 | + "entrywidth": 80, |
| 76 | + "title": { |
| 77 | + "text": f"<b>{legend_title}</b>", |
| 78 | + "side": "top", |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + def test_get_stacked_bar_chart_config( |
| 84 | + self, fake_dual_category_chart_settings: DualCategoryChartSettings |
| 85 | + ): |
| 86 | + """ |
| 87 | + Given an instance of `DualCategoryChartSettings` |
| 88 | + When `get_stacked_bar_chart_config()` is called |
| 89 | + Then stacked bar layout and legend settings are merged |
| 90 | + """ |
| 91 | + # Given |
| 92 | + chart_settings = fake_dual_category_chart_settings |
| 93 | + |
| 94 | + # When |
| 95 | + stacked_bar_config = chart_settings.get_stacked_bar_chart_config() |
| 96 | + |
| 97 | + expected_config = { |
| 98 | + **chart_settings._get_base_chart_config(), |
| 99 | + **chart_settings._get_legend_config(), |
| 100 | + "barmode": "stack", |
| 101 | + } |
| 102 | + |
| 103 | + # Then |
| 104 | + assert stacked_bar_config == expected_config |
0 commit comments