1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for replacing values in an input using custom regex.
12 import nominatim_api.search.query as qmod
13 from nominatim_api.query_preprocessing.config import QueryConfig
14 from nominatim_api.query_preprocessing import regex_replace
15 from nominatim_api.errors import UsageError
18 def run_preprocessor_on(query):
19 config = QueryConfig()
21 config['replacements'] = [
22 {'pattern': r'\b(?:\d{1,3}\.){3}\d{1,3}\b', 'replace': ''}, # IPv4
23 {'pattern': r'https?://\S+', 'replace': ''} # HTTP/HTTPS URLs
26 proc = regex_replace.create(config)
30 @pytest.mark.parametrize('inp,outp', [
31 (['45.67.89.101'], []),
32 (['198.51.100.23'], []),
33 (['203.0.113.255'], []),
34 (['http://www.openstreetmap.org'], []),
35 (['https://www.openstreetmap.org/edit'], []),
36 (['http://osm.org'], []),
37 (['https://www.openstreetmap.org/user/abc'], []),
38 (['https://tile.openstreetmap.org/12/2048/2048.png'], []),
39 (['Check the map at https://www.openstreetmap.org'], ['Check the map at ']),
40 (['Use 203.0.113.255 for routing'], ['Use for routing']),
41 (['Find maps at https://osm.org and http://openstreetmap.org'], ['Find maps at and ']),
42 (['203.0.113.255', 'Some Address'], ['Some Address']),
43 (['https://osm.org', 'Another Place'], ['Another Place']),
45 def test_split_phrases(inp, outp):
46 query = [qmod.Phrase(qmod.PHRASE_ANY, text) for text in inp]
48 out = run_preprocessor_on(query)
49 assert out == [qmod.Phrase(qmod.PHRASE_ANY, text) for text in outp]
52 def test_missing_replacement_section():
53 with pytest.raises(UsageError, match="'replacements' missing"):
54 regex_replace.create(QueryConfig())
57 def test_bad_type_for_replacement():
58 with pytest.raises(UsageError, match="'replacements' must be of type list"):
59 regex_replace.create(QueryConfig({'replacements': 'something'}))