]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/query_processing/test_regex_replace.py
Update documentation, optimise regex_replace, add tests
[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) 2025 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     config.set_normalizer(None)
20
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
24     ]
25
26     proc = regex_replace.create(config)
27     return proc(query)
28
29
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']),
44 ])
45 def test_split_phrases(inp, outp):
46     query = [qmod.Phrase(qmod.PHRASE_ANY, text) for text in inp]
47
48     out = run_preprocessor_on(query)
49     expected_out = [qmod.Phrase(qmod.PHRASE_ANY, text) for text in outp]
50
51     assert out == expected_out, f"Expected {expected_out}, but got {out}"