|
| 1 | +# Copyright (c) 2024 Microsoft Corporation. |
| 2 | +# Licensed under the MIT License |
| 3 | + |
| 4 | +"""Tests for dynamic community selection with type handling.""" |
| 5 | + |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +from graphrag.data_model.community import Community |
| 9 | +from graphrag.data_model.community_report import CommunityReport |
| 10 | +from graphrag.query.context_builder.dynamic_community_selection import ( |
| 11 | + DynamicCommunitySelection, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def create_mock_tokenizer() -> MagicMock: |
| 16 | + """Create a mock tokenizer.""" |
| 17 | + tokenizer = MagicMock() |
| 18 | + tokenizer.encode.return_value = [1, 2, 3] |
| 19 | + return tokenizer |
| 20 | + |
| 21 | + |
| 22 | +def create_mock_model() -> MagicMock: |
| 23 | + """Create a mock chat model.""" |
| 24 | + return MagicMock() |
| 25 | + |
| 26 | + |
| 27 | +def test_dynamic_community_selection_handles_int_children(): |
| 28 | + """Test that DynamicCommunitySelection correctly handles children IDs as integers. |
| 29 | +
|
| 30 | + This tests the fix for issue #2004 where children IDs could be integers |
| 31 | + while self.reports keys are strings, causing child communities to be skipped. |
| 32 | + """ |
| 33 | + # Create communities with integer children (simulating the bug scenario) |
| 34 | + # Note: Even though the type annotation says list[str], actual data may have ints |
| 35 | + communities = [ |
| 36 | + Community( |
| 37 | + id="comm-0", |
| 38 | + short_id="0", |
| 39 | + title="Root Community", |
| 40 | + level="0", |
| 41 | + parent="", |
| 42 | + children=[1, 2], # type: ignore[list-item] # Integer children - testing bug fix |
| 43 | + ), |
| 44 | + Community( |
| 45 | + id="comm-1", |
| 46 | + short_id="1", |
| 47 | + title="Child Community 1", |
| 48 | + level="1", |
| 49 | + parent="0", |
| 50 | + children=[], |
| 51 | + ), |
| 52 | + Community( |
| 53 | + id="comm-2", |
| 54 | + short_id="2", |
| 55 | + title="Child Community 2", |
| 56 | + level="1", |
| 57 | + parent="0", |
| 58 | + children=[], |
| 59 | + ), |
| 60 | + ] |
| 61 | + |
| 62 | + # Create community reports with string community_id |
| 63 | + reports = [ |
| 64 | + CommunityReport( |
| 65 | + id="report-0", |
| 66 | + short_id="0", |
| 67 | + title="Report 0", |
| 68 | + community_id="0", |
| 69 | + summary="Root community summary", |
| 70 | + full_content="Root community full content", |
| 71 | + rank=1.0, |
| 72 | + ), |
| 73 | + CommunityReport( |
| 74 | + id="report-1", |
| 75 | + short_id="1", |
| 76 | + title="Report 1", |
| 77 | + community_id="1", |
| 78 | + summary="Child 1 summary", |
| 79 | + full_content="Child 1 full content", |
| 80 | + rank=1.0, |
| 81 | + ), |
| 82 | + CommunityReport( |
| 83 | + id="report-2", |
| 84 | + short_id="2", |
| 85 | + title="Report 2", |
| 86 | + community_id="2", |
| 87 | + summary="Child 2 summary", |
| 88 | + full_content="Child 2 full content", |
| 89 | + rank=1.0, |
| 90 | + ), |
| 91 | + ] |
| 92 | + |
| 93 | + model = create_mock_model() |
| 94 | + tokenizer = create_mock_tokenizer() |
| 95 | + |
| 96 | + selector = DynamicCommunitySelection( |
| 97 | + community_reports=reports, |
| 98 | + communities=communities, |
| 99 | + model=model, |
| 100 | + tokenizer=tokenizer, |
| 101 | + threshold=1, |
| 102 | + keep_parent=False, |
| 103 | + max_level=2, |
| 104 | + ) |
| 105 | + |
| 106 | + # Verify that reports are keyed by string |
| 107 | + assert "0" in selector.reports |
| 108 | + assert "1" in selector.reports |
| 109 | + assert "2" in selector.reports |
| 110 | + |
| 111 | + # Verify that communities are keyed by string short_id |
| 112 | + assert "0" in selector.communities |
| 113 | + assert "1" in selector.communities |
| 114 | + assert "2" in selector.communities |
| 115 | + |
| 116 | + # Verify that the children are properly accessible |
| 117 | + # Before the fix, int children would fail the `in self.reports` check |
| 118 | + root_community = selector.communities["0"] |
| 119 | + for child in root_community.children: |
| 120 | + child_id = str(child) |
| 121 | + # This should now work with the fix |
| 122 | + assert child_id in selector.reports, ( |
| 123 | + f"Child {child} (as '{child_id}') should be found in reports" |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +def test_dynamic_community_selection_handles_str_children(): |
| 128 | + """Test that DynamicCommunitySelection works correctly with string children IDs.""" |
| 129 | + communities = [ |
| 130 | + Community( |
| 131 | + id="comm-0", |
| 132 | + short_id="0", |
| 133 | + title="Root Community", |
| 134 | + level="0", |
| 135 | + parent="", |
| 136 | + children=["1", "2"], # String children - expected type |
| 137 | + ), |
| 138 | + Community( |
| 139 | + id="comm-1", |
| 140 | + short_id="1", |
| 141 | + title="Child Community 1", |
| 142 | + level="1", |
| 143 | + parent="0", |
| 144 | + children=[], |
| 145 | + ), |
| 146 | + Community( |
| 147 | + id="comm-2", |
| 148 | + short_id="2", |
| 149 | + title="Child Community 2", |
| 150 | + level="1", |
| 151 | + parent="0", |
| 152 | + children=[], |
| 153 | + ), |
| 154 | + ] |
| 155 | + |
| 156 | + reports = [ |
| 157 | + CommunityReport( |
| 158 | + id="report-0", |
| 159 | + short_id="0", |
| 160 | + title="Report 0", |
| 161 | + community_id="0", |
| 162 | + summary="Root community summary", |
| 163 | + full_content="Root community full content", |
| 164 | + rank=1.0, |
| 165 | + ), |
| 166 | + CommunityReport( |
| 167 | + id="report-1", |
| 168 | + short_id="1", |
| 169 | + title="Report 1", |
| 170 | + community_id="1", |
| 171 | + summary="Child 1 summary", |
| 172 | + full_content="Child 1 full content", |
| 173 | + rank=1.0, |
| 174 | + ), |
| 175 | + CommunityReport( |
| 176 | + id="report-2", |
| 177 | + short_id="2", |
| 178 | + title="Report 2", |
| 179 | + community_id="2", |
| 180 | + summary="Child 2 summary", |
| 181 | + full_content="Child 2 full content", |
| 182 | + rank=1.0, |
| 183 | + ), |
| 184 | + ] |
| 185 | + |
| 186 | + model = create_mock_model() |
| 187 | + tokenizer = create_mock_tokenizer() |
| 188 | + |
| 189 | + selector = DynamicCommunitySelection( |
| 190 | + community_reports=reports, |
| 191 | + communities=communities, |
| 192 | + model=model, |
| 193 | + tokenizer=tokenizer, |
| 194 | + threshold=1, |
| 195 | + keep_parent=False, |
| 196 | + max_level=2, |
| 197 | + ) |
| 198 | + |
| 199 | + # Verify that children can be found in reports |
| 200 | + root_community = selector.communities["0"] |
| 201 | + for child in root_community.children: |
| 202 | + child_id = str(child) |
| 203 | + assert child_id in selector.reports, ( |
| 204 | + f"Child {child} (as '{child_id}') should be found in reports" |
| 205 | + ) |
0 commit comments