"""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"