|
4 | 4 | @author: wf |
5 | 5 | """ |
6 | 6 |
|
| 7 | +from argparse import Namespace |
| 8 | +from contextlib import redirect_stdout |
7 | 9 | import copy |
8 | 10 | import io |
9 | 11 | import json |
10 | 12 | import os |
11 | 13 | import traceback |
12 | | -from argparse import Namespace |
13 | | -from contextlib import redirect_stdout |
14 | 14 |
|
15 | | -import tests.test_sqlite3 |
16 | | -from tests.action_stats import ActionStats |
| 15 | +from lodstorage.prefix_config import PrefixConfigs |
17 | 16 | from lodstorage.prefixes import Prefixes |
18 | 17 | from lodstorage.query import ( |
19 | 18 | EndpointManager, |
|
27 | 26 | from lodstorage.querymain import QueryMain |
28 | 27 | from lodstorage.querymain import main as queryMain |
29 | 28 | from lodstorage.sparql import SPARQL |
| 29 | + |
| 30 | +from tests.action_stats import ActionStats |
30 | 31 | from tests.endpoint_test import EndpointTest |
| 32 | +import tests.test_sqlite3 |
| 33 | + |
31 | 34 |
|
32 | 35 | class TestQueries(EndpointTest): |
33 | 36 | """ |
@@ -171,6 +174,64 @@ def testQueryCommandLine(self): |
171 | 174 | print(f"{resultFormat}:{result}") |
172 | 175 | self.assertTrue(expected in result, f"{expected}({resultFormat})") |
173 | 176 |
|
| 177 | + def test_add_endpoint_prefixes(self): |
| 178 | + """ |
| 179 | + Test Query.add_endpoint_prefixes() merges endpoint prefixes without duplicates. |
| 180 | + https://github.com/WolfgangFahl/pyLoDStorage/issues/140 (related prefix handling) |
| 181 | + """ |
| 182 | + debug = self.debug |
| 183 | + #debug=True |
| 184 | + endpoints = EndpointManager.getEndpoints(lang="sparql") |
| 185 | + wikidata_ep = endpoints["wikidata"] |
| 186 | + prefix_configs = PrefixConfigs.get_instance() |
| 187 | + |
| 188 | + qm = QueryManager(lang="sparql", debug=False) |
| 189 | + cities_query = qm.queriesByName["cities"] # Has rdfs, wd, wdt |
| 190 | + |
| 191 | + original_query = cities_query.query |
| 192 | + original_prefixes = Prefixes.extract_prefixes(original_query) |
| 193 | + self.assertEqual(3, len(original_prefixes)) # rdfs, wd, wdt |
| 194 | + |
| 195 | + # Add endpoint prefixes (Wikidata full set → overlaps) |
| 196 | + cities_query.add_endpoint_prefixes(wikidata_ep, prefix_configs) |
| 197 | + |
| 198 | + # Debug: Always show merged state (before asserts → visible on fail) |
| 199 | + merged_query = cities_query.query |
| 200 | + prefix_dict = Prefixes.extract_prefixes(merged_query) |
| 201 | + prefix_lines = [line for line in merged_query.splitlines() if line.strip().startswith('PREFIX')] |
| 202 | + if debug: |
| 203 | + print("Merged query prefixes:") |
| 204 | + print('\n'.join(prefix_lines)) |
| 205 | + print(f"Unique prefixes: {len(prefix_dict)}") |
| 206 | + print(f"self.prefixes len: {len(cities_query.prefixes)}") |
| 207 | + print("self.prefixes sample:", cities_query.prefixes[:3]) |
| 208 | + |
| 209 | + # Verify: Unique prefixes (no dups like 'rdfs') |
| 210 | + self.assertEqual(len(prefix_dict), len(prefix_lines)) # 1:1 → no dup lines |
| 211 | + |
| 212 | + # Specific: 'rdfs', 'wd', 'wdt' appear exactly once |
| 213 | + for prefix in ['rdfs', 'wd', 'wdt']: |
| 214 | + count = sum(1 for line in prefix_lines if f'{prefix}: ' in line) |
| 215 | + self.assertEqual(1, count) |
| 216 | + |
| 217 | + # Full Wikidata set merged (many more) |
| 218 | + self.assertGreater(len(prefix_dict), 10) |
| 219 | + |
| 220 | + # Query body preserved |
| 221 | + self.assertIn('SELECT ?city_id ?name', merged_query) |
| 222 | + |
| 223 | + # self.prefixes: Unique PREFIX lines list (len + sorted names match) |
| 224 | + self.assertEqual(len(prefix_lines), len(cities_query.prefixes)) # same count |
| 225 | + def get_prefix_names(lines): |
| 226 | + return sorted([line.split(':')[0].strip('PREFIX ').strip() for line in lines]) |
| 227 | + self.assertEqual( |
| 228 | + get_prefix_names(prefix_lines), |
| 229 | + get_prefix_names(cities_query.prefixes) |
| 230 | + ) # same set (order-insensitive) |
| 231 | + used_prefixes = {line.split(':')[0].strip() for line in cities_query.prefixes} |
| 232 | + self.assertEqual(len(used_prefixes), len(cities_query.prefixes)) # no dups |
| 233 | + |
| 234 | + |
174 | 235 | def testQueryEndpoints(self): |
175 | 236 | """ |
176 | 237 | tests the sparql endpoint commandline endpoint selection |
|
0 commit comments