|
| 1 | +import pytest |
| 2 | + |
| 3 | +from cms.dynamic_content.blocks import ( |
| 4 | + POPULAR_TOPICS_BOTTOM_RIGHT_COLUMN_COUNT, |
| 5 | + POPULAR_TOPICS_HEADLINE_NUMBER_BLOCK_COUNT, |
| 6 | + PopularTopicsMetricNumberBlockTypes, |
| 7 | + PopularTopicsRightColumnBottomRowBlockTypes, |
| 8 | +) |
| 9 | +from cms.dynamic_content.cards import ( |
| 10 | + POPULAR_TOPICS_RIGHT_COLUMN_BOTTOM_ROW_SEGMENT_COUNT, |
| 11 | + PopularTopicsCard, |
| 12 | + PopularTopicsLeftColumnBlockTypes, |
| 13 | + PopularTopicsRightColumnTopRowBlockTypes, |
| 14 | + SimplifiedChartWithLink, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class TestPopularTopicsFirstColumnBlockTypes: |
| 19 | + @pytest.mark.parametrize( |
| 20 | + "expected_field_name", |
| 21 | + ( |
| 22 | + "weather_health_alert_card", |
| 23 | + "chart_card_with_description", |
| 24 | + ), |
| 25 | + ) |
| 26 | + def test_has_expected_child_blocks(self, expected_field_name: str) -> None: |
| 27 | + """ |
| 28 | + Given an instance of `PopularTopicsLeftColumnBlockTypes` |
| 29 | + When inspecting child blocks |
| 30 | + Then the expected field(s) are available |
| 31 | + """ |
| 32 | + # Given |
| 33 | + first_column_block_types = PopularTopicsLeftColumnBlockTypes() |
| 34 | + |
| 35 | + # When |
| 36 | + selected_field = first_column_block_types.child_blocks.get(expected_field_name) |
| 37 | + |
| 38 | + # Then |
| 39 | + assert selected_field is not None |
| 40 | + |
| 41 | + |
| 42 | +class TestPopularTopicsSecondColumnTopRightBlockTypes: |
| 43 | + def test_has_expected_chart_card_block(self) -> None: |
| 44 | + """ |
| 45 | + Given an instance of `PopularTopicsRightColumnTopRowBlockTypes` |
| 46 | + When inspecting child blocks |
| 47 | + Then the chart card field is available and correctly typed |
| 48 | + """ |
| 49 | + # Given |
| 50 | + top_right_block_types = PopularTopicsRightColumnTopRowBlockTypes() |
| 51 | + |
| 52 | + # When |
| 53 | + selected_field = top_right_block_types.child_blocks.get("chart_card") |
| 54 | + |
| 55 | + # Then |
| 56 | + assert isinstance(selected_field, SimplifiedChartWithLink) |
| 57 | + |
| 58 | + |
| 59 | +class TestPopularTopicsSecondColumnBottomRowBlockTypes: |
| 60 | + def test_has_expected_headline_metric_card_block(self) -> None: |
| 61 | + """ |
| 62 | + Given an instance of `PopularTopicsRightColumnBottomRowBlockTypes` |
| 63 | + When inspecting child blocks |
| 64 | + Then the headline metric card field is available and correctly typed |
| 65 | + """ |
| 66 | + # Given |
| 67 | + bottom_row_block_types = PopularTopicsRightColumnBottomRowBlockTypes() |
| 68 | + |
| 69 | + # When |
| 70 | + selected_field = bottom_row_block_types.child_blocks.get("headline_metric_card") |
| 71 | + |
| 72 | + # Then |
| 73 | + assert isinstance(selected_field, PopularTopicsMetricNumberBlockTypes) |
| 74 | + assert selected_field.meta.min_num == POPULAR_TOPICS_BOTTOM_RIGHT_COLUMN_COUNT |
| 75 | + assert selected_field.meta.max_num == POPULAR_TOPICS_BOTTOM_RIGHT_COLUMN_COUNT |
| 76 | + |
| 77 | + |
| 78 | +class TestPopularTopicsCard: |
| 79 | + @pytest.mark.parametrize( |
| 80 | + "field_name, expected_block_type", |
| 81 | + ( |
| 82 | + ("left_column", PopularTopicsLeftColumnBlockTypes), |
| 83 | + ("right_column_top_row", PopularTopicsRightColumnTopRowBlockTypes), |
| 84 | + ("right_column_bottom_row", PopularTopicsRightColumnBottomRowBlockTypes), |
| 85 | + ), |
| 86 | + ) |
| 87 | + def test_has_expected_child_blocks( |
| 88 | + self, |
| 89 | + field_name: str, |
| 90 | + expected_block_type: type, |
| 91 | + ) -> None: |
| 92 | + """ |
| 93 | + Given an instance of `PopularTopicsCard` |
| 94 | + When inspecting child blocks |
| 95 | + Then the expected field(s) use the correct block type |
| 96 | + """ |
| 97 | + # Given |
| 98 | + popular_topics_card = PopularTopicsCard() |
| 99 | + |
| 100 | + # When |
| 101 | + selected_field = popular_topics_card.child_blocks.get(field_name) |
| 102 | + |
| 103 | + # Then |
| 104 | + assert isinstance(selected_field, expected_block_type) |
| 105 | + |
| 106 | + @pytest.mark.parametrize( |
| 107 | + "field_name, expected_min_num, expected_max_num", |
| 108 | + ( |
| 109 | + ("left_column", 1, 1), |
| 110 | + ("right_column_top_row", 1, 1), |
| 111 | + ( |
| 112 | + "right_column_bottom_row", |
| 113 | + POPULAR_TOPICS_RIGHT_COLUMN_BOTTOM_ROW_SEGMENT_COUNT, |
| 114 | + POPULAR_TOPICS_RIGHT_COLUMN_BOTTOM_ROW_SEGMENT_COUNT, |
| 115 | + ), |
| 116 | + ), |
| 117 | + ) |
| 118 | + def test_card_columns_have_expected_required_counts( |
| 119 | + self, |
| 120 | + field_name: str, |
| 121 | + expected_min_num: int, |
| 122 | + expected_max_num: int, |
| 123 | + ) -> None: |
| 124 | + """ |
| 125 | + Given an instance of `PopularTopicsCard` |
| 126 | + When inspecting stream count constraints for each card area |
| 127 | + Then each field has the expected min and max values |
| 128 | + """ |
| 129 | + # Given |
| 130 | + popular_topics_card = PopularTopicsCard() |
| 131 | + |
| 132 | + # When |
| 133 | + selected_field = popular_topics_card.child_blocks[field_name] |
| 134 | + |
| 135 | + # Then |
| 136 | + assert selected_field.meta.min_num == expected_min_num |
| 137 | + assert selected_field.meta.max_num == expected_max_num |
| 138 | + |
| 139 | + |
| 140 | +class TestPopularTopicsMetricNumberBlockTypes: |
| 141 | + def test_headline_metrics_are_required_and_allow_exactly_two_entries(self) -> None: |
| 142 | + """ |
| 143 | + Given an instance of `PopularTopicsMetricNumberBlockTypes` |
| 144 | + When inspecting the headline_metrics field configuration |
| 145 | + Then headline_metrics are required and constrained to exactly two entries |
| 146 | + """ |
| 147 | + # Given |
| 148 | + metric_number_block = PopularTopicsMetricNumberBlockTypes() |
| 149 | + |
| 150 | + # When |
| 151 | + headline_metrics_block = metric_number_block.child_blocks["headline_metrics"] |
| 152 | + |
| 153 | + # Then |
| 154 | + assert headline_metrics_block.meta.required is True |
| 155 | + assert ( |
| 156 | + headline_metrics_block.meta.min_num |
| 157 | + == POPULAR_TOPICS_HEADLINE_NUMBER_BLOCK_COUNT |
| 158 | + ) |
| 159 | + assert ( |
| 160 | + headline_metrics_block.meta.max_num |
| 161 | + == POPULAR_TOPICS_HEADLINE_NUMBER_BLOCK_COUNT |
| 162 | + ) |
0 commit comments