|
| 1 | +import pytest |
| 2 | + |
| 3 | +from memos.search.search_service import resolve_filter_for_cube |
| 4 | + |
| 5 | + |
| 6 | +class TestResolveFilterForCube: |
| 7 | + """Tests for resolve_filter_for_cube — multi-cube filter routing.""" |
| 8 | + |
| 9 | + # ── None passthrough ── |
| 10 | + |
| 11 | + def test_none_returns_none(self): |
| 12 | + assert resolve_filter_for_cube(None, "cube_001") is None |
| 13 | + |
| 14 | + # ── Unified filter (filter2): top-level and/or ── |
| 15 | + |
| 16 | + def test_unified_and_returns_same_for_any_cube(self): |
| 17 | + f = {"and": [{"tags": {"contains": "阅读"}}, {"created_at": {"gte": "2025-01-01"}}]} |
| 18 | + assert resolve_filter_for_cube(f, "cube_001") is f |
| 19 | + assert resolve_filter_for_cube(f, "cube_999") is f |
| 20 | + |
| 21 | + def test_unified_or_returns_same_for_any_cube(self): |
| 22 | + f = {"or": [{"tags": {"contains": "A"}}, {"tags": {"contains": "B"}}]} |
| 23 | + assert resolve_filter_for_cube(f, "cube_001") is f |
| 24 | + |
| 25 | + # ── Per-cube filter (filter1 / filter4) ── |
| 26 | + |
| 27 | + def test_per_cube_returns_matching_sub_filter(self): |
| 28 | + sub_a = {"and": [{"tags": {"contains": "阅读"}}]} |
| 29 | + sub_b = {"and": [{"tags": {"contains": "工作"}}]} |
| 30 | + f = {"cube_A": sub_a, "cube_B": sub_b} |
| 31 | + |
| 32 | + assert resolve_filter_for_cube(f, "cube_A") is sub_a |
| 33 | + assert resolve_filter_for_cube(f, "cube_B") is sub_b |
| 34 | + |
| 35 | + def test_per_cube_missing_key_returns_none(self): |
| 36 | + f = { |
| 37 | + "cube_A": {"and": [{"tags": {"contains": "阅读"}}]}, |
| 38 | + "cube_B": {"and": [{"tags": {"contains": "工作"}}]}, |
| 39 | + } |
| 40 | + assert resolve_filter_for_cube(f, "cube_C") is None |
| 41 | + |
| 42 | + def test_per_cube_single_key(self): |
| 43 | + sub = {"and": [{"created_at": {"gte": "2025-01-01"}}]} |
| 44 | + f = {"cube_only": sub} |
| 45 | + assert resolve_filter_for_cube(f, "cube_only") is sub |
| 46 | + assert resolve_filter_for_cube(f, "other") is None |
| 47 | + |
| 48 | + # ── Mixed (filter3): illegal ── |
| 49 | + |
| 50 | + def test_mixed_and_with_cube_key_raises(self): |
| 51 | + f = { |
| 52 | + "and": [{"tags": {"contains": "阅读"}}], |
| 53 | + "cube_A": {"and": [{"tags": {"contains": "工作"}}]}, |
| 54 | + } |
| 55 | + with pytest.raises(ValueError, match="cannot coexist"): |
| 56 | + resolve_filter_for_cube(f, "cube_A") |
| 57 | + |
| 58 | + def test_mixed_or_with_cube_key_raises(self): |
| 59 | + f = { |
| 60 | + "or": [{"tags": {"contains": "阅读"}}], |
| 61 | + "cube_B": {"and": [{"tags": {"contains": "工作"}}]}, |
| 62 | + } |
| 63 | + with pytest.raises(ValueError, match="cannot coexist"): |
| 64 | + resolve_filter_for_cube(f, "cube_B") |
| 65 | + |
| 66 | + # ── Edge cases ── |
| 67 | + |
| 68 | + def test_empty_dict_returns_none(self): |
| 69 | + assert resolve_filter_for_cube({}, "cube_001") is None |
| 70 | + |
| 71 | + def test_per_cube_with_empty_sub_filter(self): |
| 72 | + f = {"cube_A": {}} |
| 73 | + result = resolve_filter_for_cube(f, "cube_A") |
| 74 | + assert result == {} |
| 75 | + |
| 76 | + def test_unified_and_empty_list(self): |
| 77 | + f = {"and": []} |
| 78 | + assert resolve_filter_for_cube(f, "any") is f |
0 commit comments