Download tests/test_aggregation.py from YGCXLab/ux-research-agent-slackbot: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/tests/test_aggregation.py
- Command line
-
hf download hf://spaces/YGCXLab/ux-research-agent-slackbot/tests/test_aggregation.py
-
curl -L -o test_aggregation.py https://huggingface.co/spaces/YGCXLab/ux-research-agent-slackbot/resolve/main/tests/test_aggregation.py
10.5 kB
| """Tests for Retriever.aggregate() method.""" | |
| import pytest | |
| from unittest.mock import patch | |
| from src.engine.retriever import Retriever | |
| 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" | |