ux-research-agent-slackbot / tests /test_aggregation.py
debug-log
Claude Opus 4.6
feat: Insight Repository โ€” GitLab ์ดˆ๊ธฐํ™” + HF ์ž๋™ ๋ฐฐํฌ
501bc8c
Raw History Blame Contribute Delete
10.5 kB
"""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"