|
| 1 | +""" |
| 2 | +Tests for insights helper functions. |
| 3 | +
|
| 4 | +Directly tests the pure helper functions in api/routers/insights.py |
| 5 | +that don't require database or HTTP setup. |
| 6 | +""" |
| 7 | + |
| 8 | +from datetime import datetime, timezone |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from api.routers.insights import ( |
| 13 | + _collect_impl_tags, |
| 14 | + _flatten_tags, |
| 15 | + _parse_iso, |
| 16 | + _score_bucket, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class TestScoreBucket: |
| 21 | + """Tests for _score_bucket mapping.""" |
| 22 | + |
| 23 | + def test_minimum_score(self) -> None: |
| 24 | + assert _score_bucket(50) == "50-55" |
| 25 | + |
| 26 | + def test_maximum_score(self) -> None: |
| 27 | + assert _score_bucket(100) == "95-100" |
| 28 | + |
| 29 | + def test_middle_scores(self) -> None: |
| 30 | + assert _score_bucket(72) == "70-75" |
| 31 | + assert _score_bucket(85) == "85-90" |
| 32 | + assert _score_bucket(90) == "90-95" |
| 33 | + |
| 34 | + def test_boundary_at_55(self) -> None: |
| 35 | + assert _score_bucket(55) == "55-60" |
| 36 | + |
| 37 | + def test_below_50_clamped(self) -> None: |
| 38 | + assert _score_bucket(30) == "50-55" |
| 39 | + |
| 40 | + def test_above_100_clamped(self) -> None: |
| 41 | + assert _score_bucket(110) == "95-100" |
| 42 | + |
| 43 | + def test_exact_boundary(self) -> None: |
| 44 | + assert _score_bucket(75) == "75-80" |
| 45 | + assert _score_bucket(80) == "80-85" |
| 46 | + |
| 47 | + def test_fractional_score(self) -> None: |
| 48 | + assert _score_bucket(92.5) == "90-95" |
| 49 | + assert _score_bucket(87.9) == "85-90" |
| 50 | + |
| 51 | + |
| 52 | +class TestFlattenTags: |
| 53 | + """Tests for _flatten_tags.""" |
| 54 | + |
| 55 | + def test_none_tags(self) -> None: |
| 56 | + assert _flatten_tags(None) == set() |
| 57 | + |
| 58 | + def test_empty_dict(self) -> None: |
| 59 | + assert _flatten_tags({}) == set() |
| 60 | + |
| 61 | + def test_single_category(self) -> None: |
| 62 | + tags = {"plot_type": ["scatter"]} |
| 63 | + assert _flatten_tags(tags) == {"plot_type:scatter"} |
| 64 | + |
| 65 | + def test_multiple_categories(self) -> None: |
| 66 | + tags = {"plot_type": ["scatter", "line"], "domain": ["statistics"]} |
| 67 | + result = _flatten_tags(tags) |
| 68 | + assert result == {"plot_type:scatter", "plot_type:line", "domain:statistics"} |
| 69 | + |
| 70 | + def test_non_list_values_skipped(self) -> None: |
| 71 | + tags = {"plot_type": ["scatter"], "invalid": "not-a-list"} |
| 72 | + result = _flatten_tags(tags) |
| 73 | + assert result == {"plot_type:scatter"} |
| 74 | + |
| 75 | + def test_empty_list(self) -> None: |
| 76 | + tags = {"plot_type": []} |
| 77 | + assert _flatten_tags(tags) == set() |
| 78 | + |
| 79 | + |
| 80 | +class TestParseIso: |
| 81 | + """Tests for _parse_iso.""" |
| 82 | + |
| 83 | + def test_none_input(self) -> None: |
| 84 | + assert _parse_iso(None) is None |
| 85 | + |
| 86 | + def test_empty_string(self) -> None: |
| 87 | + assert _parse_iso("") is None |
| 88 | + |
| 89 | + def test_valid_iso_with_z(self) -> None: |
| 90 | + result = _parse_iso("2025-01-15T10:30:00Z") |
| 91 | + assert result is not None |
| 92 | + assert result.year == 2025 |
| 93 | + assert result.month == 1 |
| 94 | + assert result.tzinfo is not None |
| 95 | + |
| 96 | + def test_valid_iso_with_offset(self) -> None: |
| 97 | + result = _parse_iso("2025-01-15T10:30:00+02:00") |
| 98 | + assert result is not None |
| 99 | + assert result.tzinfo is not None |
| 100 | + |
| 101 | + def test_naive_datetime_gets_utc(self) -> None: |
| 102 | + result = _parse_iso("2025-01-15T10:30:00") |
| 103 | + assert result is not None |
| 104 | + assert result.tzinfo == timezone.utc |
| 105 | + |
| 106 | + def test_invalid_string(self) -> None: |
| 107 | + assert _parse_iso("not-a-date") is None |
| 108 | + |
| 109 | + def test_date_only(self) -> None: |
| 110 | + result = _parse_iso("2025-01-15") |
| 111 | + assert result is not None |
| 112 | + assert result.year == 2025 |
| 113 | + |
| 114 | + |
| 115 | +class TestCollectImplTags: |
| 116 | + """Tests for _collect_impl_tags.""" |
| 117 | + |
| 118 | + def test_spec_with_no_tags(self) -> None: |
| 119 | + from unittest.mock import MagicMock |
| 120 | + |
| 121 | + spec = MagicMock() |
| 122 | + spec.tags = None |
| 123 | + spec.impls = [] |
| 124 | + result = _collect_impl_tags(spec) |
| 125 | + assert result == set() |
| 126 | + |
| 127 | + def test_spec_with_tags_and_impl_tags(self) -> None: |
| 128 | + from unittest.mock import MagicMock |
| 129 | + |
| 130 | + impl = MagicMock() |
| 131 | + impl.library_id = "matplotlib" |
| 132 | + impl.impl_tags = {"techniques": ["annotations"]} |
| 133 | + |
| 134 | + spec = MagicMock() |
| 135 | + spec.tags = {"plot_type": ["scatter"]} |
| 136 | + spec.impls = [impl] |
| 137 | + |
| 138 | + result = _collect_impl_tags(spec) |
| 139 | + assert "plot_type:scatter" in result |
| 140 | + assert "techniques:annotations" in result |
| 141 | + |
| 142 | + def test_filter_by_library(self) -> None: |
| 143 | + from unittest.mock import MagicMock |
| 144 | + |
| 145 | + impl1 = MagicMock() |
| 146 | + impl1.library_id = "matplotlib" |
| 147 | + impl1.impl_tags = {"techniques": ["annotations"]} |
| 148 | + |
| 149 | + impl2 = MagicMock() |
| 150 | + impl2.library_id = "seaborn" |
| 151 | + impl2.impl_tags = {"techniques": ["regression"]} |
| 152 | + |
| 153 | + spec = MagicMock() |
| 154 | + spec.tags = {"plot_type": ["scatter"]} |
| 155 | + spec.impls = [impl1, impl2] |
| 156 | + |
| 157 | + result = _collect_impl_tags(spec, library="matplotlib") |
| 158 | + assert "techniques:annotations" in result |
| 159 | + assert "techniques:regression" not in result |
| 160 | + |
| 161 | + def test_impl_tags_none(self) -> None: |
| 162 | + from unittest.mock import MagicMock |
| 163 | + |
| 164 | + impl = MagicMock() |
| 165 | + impl.library_id = "matplotlib" |
| 166 | + impl.impl_tags = None |
| 167 | + |
| 168 | + spec = MagicMock() |
| 169 | + spec.tags = {"plot_type": ["scatter"]} |
| 170 | + spec.impls = [impl] |
| 171 | + |
| 172 | + result = _collect_impl_tags(spec) |
| 173 | + assert result == {"plot_type:scatter"} |
| 174 | + |
| 175 | + def test_impl_tags_not_dict(self) -> None: |
| 176 | + from unittest.mock import MagicMock |
| 177 | + |
| 178 | + impl = MagicMock() |
| 179 | + impl.library_id = "matplotlib" |
| 180 | + impl.impl_tags = "not-a-dict" |
| 181 | + |
| 182 | + spec = MagicMock() |
| 183 | + spec.tags = {} |
| 184 | + spec.impls = [impl] |
| 185 | + |
| 186 | + result = _collect_impl_tags(spec) |
| 187 | + assert result == set() |
0 commit comments