|
6 | 6 |
|
7 | 7 | import json |
8 | 8 |
|
| 9 | +from lodstorage.prefix_config import PrefixConfigs |
9 | 10 | from lodstorage.prefixes import Prefixes |
10 | 11 | from lodstorage.query import EndpointManager, QueryManager |
11 | 12 | from lodstorage.sparql import SPARQL |
| 13 | + |
12 | 14 | from lodstorage.yaml_path import YamlPath |
13 | 15 | from tests.basetest import Basetest |
14 | 16 | from tests.endpoint_test import EndpointTest |
@@ -70,119 +72,42 @@ def test_issue151_prefix_sets_refactoring(self): |
70 | 72 | """ |
71 | 73 | Test Issue #151: Simplify endpoints.yaml by referencing prefix sets. |
72 | 74 |
|
73 | | - Verifies: |
74 | | - 1. Endpoints use prefix_sets (List[str]) instead of inline prefixes |
75 | | - 2. Prefix resolution works correctly via Prefixes.getPrefixes() |
76 | | - 3. Queries using resolved prefixes execute successfully |
77 | | -
|
78 | | - Uses queries_olympics.yaml as test data. |
| 75 | + Verifies new add_endpoint_prefixes() merges prefix_sets → query.query. |
79 | 76 | """ |
80 | | - debug = self.debug |
81 | | - debug = True |
82 | | - # Get QLever Olympics endpoint |
83 | | - endpoint_path = YamlPath.getSamplePath("endpoints_qlever.yaml") |
84 | | - endpoints = EndpointManager.getEndpoints( |
85 | | - endpointPath=endpoint_path, lang="sparql", with_default=False |
86 | | - ) |
87 | | - ep_name = "olympics-qlever" |
88 | | - self.assertIn(ep_name, endpoints, f"{ep_name} endpoint not found") |
89 | | - endpoint = endpoints[ep_name] |
90 | | - |
91 | | - # Load Olympics queries which use prefix_sets |
92 | | - olympics_queries_path = f"{self.sampledata_dir}/queries/queries_olympics.yaml" |
93 | | - qm = QueryManager( |
94 | | - queriesPath=olympics_queries_path, |
95 | | - with_default=False, |
96 | | - lang="sparql", |
97 | | - debug=False, |
98 | | - ) |
| 77 | + debug = False |
99 | 78 |
|
100 | | - if debug: |
101 | | - print(f"\n=== Testing Issue #151: Prefix Sets Refactoring ===") |
102 | | - print(f"Endpoint: {endpoint.name}") |
103 | | - print(f"URL: {endpoint.endpoint}") |
| 79 | + endpoint_path = YamlPath.getSamplePath("endpoints_qlever.yaml") |
| 80 | + endpoints = EndpointManager.getEndpoints(endpointPath=endpoint_path, lang="sparql", with_default=False) |
| 81 | + endpoint_name = "olympics-qlever" |
| 82 | + endpoint = endpoints[endpoint_name] |
104 | 83 |
|
105 | | - # 1. Verify endpoint has prefix_sets field |
106 | | - if debug: |
107 | | - print(f"\n1. Checking endpoint structure...") |
108 | | - print(f" Has prefix_sets: {hasattr(endpoint, 'prefix_sets')}") |
109 | | - if hasattr(endpoint, "prefix_sets"): |
110 | | - print(f" Prefix sets: {endpoint.prefix_sets}") |
111 | | - |
112 | | - self.assertTrue( |
113 | | - hasattr(endpoint, "prefix_sets"), |
114 | | - f"Endpoint {endpoint.name} lacks 'prefix_sets' field", |
115 | | - ) |
116 | | - self.assertIsInstance( |
117 | | - endpoint.prefix_sets, list, f"prefix_sets for {endpoint.name} is not a list" |
118 | | - ) |
119 | | - |
120 | | - # 2. Verify prefix resolution |
| 84 | + # Verify prefix_sets |
121 | 85 | if debug: |
122 | | - print(f"\n2. Testing prefix resolution...") |
123 | | - |
124 | | - combined_prefixes = Prefixes.getPrefixes(endpoint.prefix_sets) |
125 | | - |
126 | | - if debug: |
127 | | - print(f" Combined prefixes from {endpoint.prefix_sets}:") |
128 | | - for line in combined_prefixes.split("\n")[:5]: # Show first 5 |
129 | | - print(f" {line}") |
130 | | - print(f" ... (total {len(combined_prefixes.split(chr(10)))} lines)") |
131 | | - |
132 | | - # Verify expected prefixes are present |
133 | | - expected_prefixes = [ |
134 | | - "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>", |
135 | | - "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>", |
136 | | - "PREFIX olympics: <https://olympics.qlever.cs.uni-freiburg.de/>", |
137 | | - ] |
138 | | - |
139 | | - for expected in expected_prefixes: |
140 | | - if debug: |
141 | | - print(f" Checking for: {expected[:50]}...") |
142 | | - self.assertIn( |
143 | | - expected, combined_prefixes, f"Expected prefix missing: {expected}" |
144 | | - ) |
145 | | - |
146 | | - # 3. Test query execution with resolved prefixes |
147 | | - if debug: |
148 | | - print(f"\n3. Testing query execution...") |
| 86 | + print(f"prefix_sets for {endpoint_name}: {endpoint.prefix_sets}") |
| 87 | + self.assertTrue(hasattr(endpoint, "prefix_sets")) |
| 88 | + self.assertEqual(endpoint.prefix_sets, ["rdf", "olympics"]) |
149 | 89 |
|
| 90 | + olympics_queries_path = f"{self.sampledata_dir}/queries/queries_olympics.yaml" |
| 91 | + qm = QueryManager(queriesPath=olympics_queries_path, with_default=False, lang="sparql", debug=False) |
150 | 92 | query = qm.queriesByName["Athletes_by_gold_medals"] |
151 | 93 |
|
152 | 94 | if debug: |
153 | | - print(f" Query: {query.name}") |
154 | | - print(f" Query prefix_sets: {query.prefix_sets}") |
| 95 | + print(f"Query before add_endpoint_prefixes:\n{query.query[:200]}...") |
155 | 96 |
|
156 | | - # Resolve query-specific prefixes |
157 | | - query_prefixes = Prefixes.getPrefixes(query.prefix_sets) |
158 | | - query.prefixes = query_prefixes.split("\n") |
| 97 | + query.add_endpoint_prefixes(endpoint, PrefixConfigs.get_instance()) |
159 | 98 |
|
160 | 99 | if debug: |
161 | | - print(f" Resolved {len(query.prefixes)} prefix lines for query") |
162 | | - |
163 | | - # Execute query |
164 | | - sparql = SPARQL(endpoint.endpoint, method=endpoint.method) |
165 | | - |
166 | | - try: |
167 | | - if debug: |
168 | | - print(f" Executing query at {endpoint.endpoint}...") |
| 100 | + print(f"Query after (has rdf?: {'PREFIX rdf:' in query.query}, olympics?: {'PREFIX olympics:' in query.query})") |
| 101 | + print(f"Full query:\n{query.query}") |
169 | 102 |
|
170 | | - qlod = sparql.queryAsListOfDicts(query.query) |
| 103 | + self.assertIn("PREFIX rdf:", query.query) |
| 104 | + self.assertIn("PREFIX olympics:", query.query) |
171 | 105 |
|
172 | | - if debug: |
173 | | - print(f" ✅ Query succeeded: {len(qlod)} results") |
174 | | - if qlod: |
175 | | - print(f" Sample result: {qlod[0]}") |
176 | | - |
177 | | - self.assertIsInstance(qlod, list, "Query result is not a list") |
178 | | - self.assertGreater( |
179 | | - len(qlod), 0, "Query with assembled prefixes returned no results" |
180 | | - ) |
181 | | - |
182 | | - except Exception as ex: |
183 | | - if debug: |
184 | | - print(f" ❌ Query failed: {ex}") |
185 | | - self.fail(f"Query failed with refactored prefixes: {ex}") |
| 106 | + sparql = SPARQL(endpoint.endpoint, method=endpoint.method) |
| 107 | + qlod = sparql.queryAsListOfDicts(query.query) |
186 | 108 |
|
187 | 109 | if debug: |
188 | | - print(f"\n=== Issue #151 Test Complete ===\n") |
| 110 | + print(f"Query results: {len(qlod)} rows") |
| 111 | + |
| 112 | + self.assertIsInstance(qlod, list) |
| 113 | + self.assertGreater(len(qlod), 0) |
0 commit comments