File size: 10,467 Bytes
501bc8c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | """Tests for Retriever.aggregate() method."""
import pytest
from unittest.mock import patch
from src.engine.retriever import Retriever
@pytest.fixture
def retriever_with_data():
"""Create a Retriever with sample data, bypassing data loading."""
with patch("src.engine.retriever.load_internal_research", return_value=[]), \
patch("src.engine.retriever.load_baymard_guidelines", return_value=[]), \
patch("src.engine.retriever.load_nng_articles", return_value=[]):
r = Retriever.__new__(Retriever)
# Sample INTERNAL data
r.internal_data = [
{"id": "RES-001", "metadata": {"๋ฆฌ์์น ์ ๋ชฉ": "ํธํ
PDP", "๋ ์ง": "2023-06-23", "๋๋ฉ์ธ": "์์", "๋ฐฉ๋ฒ๋ก ": "UT", "๋ด๋น ๋ฆฌ์์ฒ": "Shane", "ํ๊ทธ": "#PDP #์์"}, "์์ธ ์ ๋ณด": {}},
{"id": "RES-002", "metadata": {"๋ฆฌ์์น ์ ๋ชฉ": "๊ฒฐ์ ํ๋ก์ฐ", "๋ ์ง": "2023-09-10", "๋๋ฉ์ธ": "๊ฒฐ์ ", "๋ฐฉ๋ฒ๋ก ": "์ค๋ฌธ", "๋ด๋น ๋ฆฌ์์ฒ": "Judy", "ํ๊ทธ": "#๊ฒฐ์ "}, "์์ธ ์ ๋ณด": {}},
{"id": "RES-003", "metadata": {"๋ฆฌ์์น ์ ๋ชฉ": "๊ฒ์ UX", "๋ ์ง": "2024-01-15", "๋๋ฉ์ธ": "๊ฒ์", "๋ฐฉ๋ฒ๋ก ": "UT", "๋ด๋น ๋ฆฌ์์ฒ": "Shane", "ํ๊ทธ": "#๊ฒ์ #UX"}, "์์ธ ์ ๋ณด": {}},
{"id": "RES-004", "metadata": {"๋ฆฌ์์น ์ ๋ชฉ": "์ฑ ์จ๋ณด๋ฉ", "๋ ์ง": "2024-03-20", "๋๋ฉ์ธ": "์์", "๋ฐฉ๋ฒ๋ก ": "์ธํฐ๋ทฐ", "๋ด๋น ๋ฆฌ์์ฒ": "Sonya", "ํ๊ทธ": "#์จ๋ณด๋ฉ"}, "์์ธ ์ ๋ณด": {}},
{"id": "RES-005", "metadata": {"๋ฆฌ์์น ์ ๋ชฉ": "๋ง์ดํ์ด์ง", "๋ ์ง": "2024-07-01", "๋๋ฉ์ธ": "๊ฒฐ์ ", "๋ฐฉ๋ฒ๋ก ": "UT", "๋ด๋น ๋ฆฌ์์ฒ": "Judy", "ํ๊ทธ": "#๋ง์ดํ์ด์ง"}, "์์ธ ์ ๋ณด": {}},
]
# Sample BAYMARD data
r.baymard_data = [
{"id": "237", "title": "Feature Broad Range", "topic": "Homepage", "theme": "Navigation", "platforms": ["desktop-web", "mobile-web"], "importance": 100},
{"id": "238", "title": "Cart Summary", "topic": "Cart", "theme": "Checkout", "platforms": ["desktop-web"], "importance": 80},
{"id": "239", "title": "Mobile Nav", "topic": "Homepage", "theme": "Navigation", "platforms": ["mobile-web", "mobile-app"], "importance": 100},
{"id": "240", "title": "Product Images", "topic": "PDP", "theme": "Product Page", "platforms": ["desktop-web", "mobile-web", "mobile-app"], "importance": 60},
]
# Sample NNG data
r.nng_data = [
{"id": "heuristic-eval", "title": "Heuristic Evaluation", "topics": ["Heuristic Evaluation", "Research Methods"], "authors": ["Kate Moran", "Kelley Gordon"], "date": "June 25, 2023"},
{"id": "card-sorting", "title": "Card Sorting", "topics": ["Research Methods"], "authors": ["Kate Moran"], "date": "March 10, 2024"},
{"id": "dark-patterns", "title": "Dark Patterns", "topics": ["Ethics"], "authors": ["Kelley Gordon"], "date": "January 5, 2023"},
{"id": "mobile-ux", "title": "Mobile UX", "topics": ["Mobile", "Research Methods"], "authors": ["Raluca Budiu"], "date": "December 12, 2024"},
]
return r
class TestInternalAggregation:
def test_group_by_year(self, retriever_with_data):
result = retriever_with_data.aggregate("INTERNAL", "year")
assert result["source"] == "INTERNAL"
assert result["group_by"] == "year"
assert result["total_docs"] == 5
counts = dict(result["counts"])
assert counts["2024"] == 3
assert counts["2023"] == 2
def test_group_by_domain(self, retriever_with_data):
result = retriever_with_data.aggregate("INTERNAL", "domain")
counts = dict(result["counts"])
assert counts["์์"] == 2
assert counts["๊ฒฐ์ "] == 2
assert counts["๊ฒ์"] == 1
def test_group_by_methodology(self, retriever_with_data):
result = retriever_with_data.aggregate("INTERNAL", "methodology")
counts = dict(result["counts"])
assert counts["UT"] == 3
assert counts["์ค๋ฌธ"] == 1
assert counts["์ธํฐ๋ทฐ"] == 1
def test_group_by_researcher(self, retriever_with_data):
result = retriever_with_data.aggregate("INTERNAL", "researcher")
counts = dict(result["counts"])
assert counts["Shane"] == 2
assert counts["Judy"] == 2
assert counts["Sonya"] == 1
def test_filter_and_group(self, retriever_with_data):
"""Filter by year=2024, then group by methodology."""
result = retriever_with_data.aggregate("INTERNAL", "methodology", filter_field="year", filter_value="2024")
assert result["total_docs"] == 3
counts = dict(result["counts"])
assert counts["UT"] == 2
assert counts["์ธํฐ๋ทฐ"] == 1
assert result["filter"]["field"] == "year"
assert result["filter"]["value"] == "2024"
def test_filter_researcher_group_year(self, retriever_with_data):
"""Filter by researcher=Shane, group by year."""
result = retriever_with_data.aggregate("INTERNAL", "year", filter_field="researcher", filter_value="Shane")
assert result["total_docs"] == 2
counts = dict(result["counts"])
assert counts["2023"] == 1
assert counts["2024"] == 1
class TestBaymardAggregation:
def test_group_by_topic(self, retriever_with_data):
result = retriever_with_data.aggregate("BAYMARD", "topic")
counts = dict(result["counts"])
assert counts["Homepage"] == 2
assert counts["Cart"] == 1
assert counts["PDP"] == 1
def test_group_by_platform_list_expansion(self, retriever_with_data):
"""platforms is a list field โ each platform value should be counted separately."""
result = retriever_with_data.aggregate("BAYMARD", "platform")
counts = dict(result["counts"])
assert counts["desktop-web"] == 3 # items 237, 238, 240
assert counts["mobile-web"] == 3 # items 237, 239, 240
assert counts["mobile-app"] == 2 # items 239, 240
def test_group_by_theme(self, retriever_with_data):
result = retriever_with_data.aggregate("BAYMARD", "theme")
counts = dict(result["counts"])
assert counts["Navigation"] == 2
assert counts["Checkout"] == 1
assert counts["Product Page"] == 1
class TestNngAggregation:
def test_group_by_year(self, retriever_with_data):
result = retriever_with_data.aggregate("NNG", "year")
counts = dict(result["counts"])
assert counts["2023"] == 2
assert counts["2024"] == 2
def test_group_by_author_list_expansion(self, retriever_with_data):
"""authors is a list field."""
result = retriever_with_data.aggregate("NNG", "author")
counts = dict(result["counts"])
assert counts["Kate Moran"] == 2
assert counts["Kelley Gordon"] == 2
assert counts["Raluca Budiu"] == 1
def test_group_by_topic_list_expansion(self, retriever_with_data):
"""topics is a list field."""
result = retriever_with_data.aggregate("NNG", "topic")
counts = dict(result["counts"])
assert counts["Research Methods"] == 3 # heuristic, card-sorting, mobile-ux
assert counts["Heuristic Evaluation"] == 1
assert counts["Ethics"] == 1
assert counts["Mobile"] == 1
def test_filter_year_group_author(self, retriever_with_data):
"""Filter by year=2024, group by author."""
result = retriever_with_data.aggregate("NNG", "author", filter_field="year", filter_value="2024")
assert result["total_docs"] == 2
counts = dict(result["counts"])
assert counts["Kate Moran"] == 1
assert counts["Raluca Budiu"] == 1
class TestAllSourceAggregation:
def test_all_source_counts(self, retriever_with_data):
result = retriever_with_data.aggregate("ALL", "source")
assert result["source"] == "ALL"
assert result["group_by"] == "source"
assert result["total_docs"] == 13 # 5 internal + 4 baymard + 4 nng
counts = dict(result["counts"])
assert counts["INTERNAL"] == 5
assert counts["BAYMARD"] == 4
assert counts["NNG"] == 4
def test_all_source_ignores_group_by(self, retriever_with_data):
"""group_by value is ignored for ALL โ always returns per-source counts."""
result = retriever_with_data.aggregate("ALL", "year")
assert result["group_by"] == "source"
assert result["total_docs"] == 13
def test_all_source_case_insensitive(self, retriever_with_data):
result = retriever_with_data.aggregate("all", "source")
assert result["source"] == "ALL"
class TestErrorCases:
def test_invalid_source(self, retriever_with_data):
result = retriever_with_data.aggregate("UNKNOWN", "year")
assert "error" in result
def test_invalid_group_by(self, retriever_with_data):
result = retriever_with_data.aggregate("INTERNAL", "nonexistent")
assert "error" in result
assert "nonexistent" in result["error"]
def test_invalid_filter_field(self, retriever_with_data):
result = retriever_with_data.aggregate("INTERNAL", "year", filter_field="nonexistent", filter_value="x")
assert "error" in result
def test_filter_no_match(self, retriever_with_data):
"""Filter that matches nothing should return empty counts."""
result = retriever_with_data.aggregate("INTERNAL", "year", filter_field="researcher", filter_value="Nobody")
assert result["total_docs"] == 0
assert result["counts"] == []
def test_case_insensitive_source(self, retriever_with_data):
"""Source should be case-insensitive."""
result = retriever_with_data.aggregate("internal", "year")
assert result["source"] == "INTERNAL"
assert result["total_docs"] == 5
class TestExtractYearFromText:
def test_standard_date(self):
assert Retriever._extract_year_from_text("June 25, 2023") == "2023"
def test_date_with_month_number(self):
assert Retriever._extract_year_from_text("12/15/2024") == "2024"
def test_empty_string(self):
assert Retriever._extract_year_from_text("") == "Unknown"
def test_no_year(self):
assert Retriever._extract_year_from_text("no date here") == "Unknown"
def test_iso_date(self):
assert Retriever._extract_year_from_text("2023-06-25") == "2023"
|