]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/query_processing/test_regex_replace.py
better naming for sanitized names
[nominatim.git] / test / python / api / query_processing / test_regex_replace.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 '''
8 Tests for replacing values in an input using custom regex.
9 '''
10 import pytest
11
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
16
17 def run_preprocessor_on(query):
18     config = QueryConfig()
19
20     config['replacements'] = [
21         {'pattern': r'\b(?:\d{1,3}\.){3}\d{1,3}\b', 'replace': ''},  # IPv4
22         {'pattern': r'https?://\S+', 'replace': ''}  # HTTP/HTTPS URLs
23     ]
24
25     proc = regex_replace.create(config)
26     return proc(query)
27
28
29 @pytest.mark.parametrize('inp,outp', [
30     (['45.67.89.101'], []),
31     (['198.51.100.23'], []),
32     (['203.0.113.255'], []),
33     (['http://www.openstreetmap.org'], []),
34     (['https://www.openstreetmap.org/edit'], []),
35     (['http://osm.org'], []),
36     (['https://www.openstreetmap.org/user/abc'], []),
37     (['https://tile.openstreetmap.org/12/2048/2048.png'], []),
38     (['Check the map at https://www.openstreetmap.org'], ['Check the map at ']),
39     (['Use 203.0.113.255 for routing'], ['Use  for routing']),
40     (['Find maps at https://osm.org and http://openstreetmap.org'], ['Find maps at  and ']),
41     (['203.0.113.255', 'Some Address'], ['Some Address']),
42     (['https://osm.org', 'Another Place'], ['Another Place']),
43 ])
44 def test_split_phrases(inp, outp):
45     query = [qmod.Phrase(qmod.PHRASE_ANY, text) for text in inp]
46
47     out = run_preprocessor_on(query)
48     assert out == [qmod.Phrase(qmod.PHRASE_ANY, text) for text in outp]