]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_import_special_phrases.py
Stop creating the place_classtype tables from special phrases
[nominatim.git] / test / python / tools / test_import_special_phrases.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 import special phrases methods
9     of the class SPImporter.
10 """
11 import pytest
12 from nominatim_db.tools.special_phrases.sp_importer import SPImporter
13 from nominatim_db.tools.special_phrases.sp_wiki_loader import SPWikiLoader
14 from nominatim_db.tools.special_phrases.special_phrase import SpecialPhrase
15
16
17 @pytest.fixture
18 def sp_importer(temp_db_conn, def_config, monkeypatch):
19     """
20         Return an instance of SPImporter.
21     """
22     monkeypatch.setenv('NOMINATIM_LANGUAGES', 'en')
23     loader = SPWikiLoader(def_config)
24     return SPImporter(def_config, temp_db_conn, loader)
25
26
27 @pytest.fixture
28 def xml_wiki_content(src_dir):
29     """
30         return the content of the static xml test file.
31     """
32     xml_test_content = src_dir / 'test' / 'testdata' / 'special_phrases_test_content.txt'
33     return xml_test_content.read_text(encoding='utf-8')
34
35
36 def test_check_sanity_class(sp_importer):
37     """
38         Check for _check_sanity() method.
39         If a wrong class or type is given, an UsageError should raise.
40         If a good class and type are given, nothing special happens.
41     """
42
43     assert not sp_importer._check_sanity(SpecialPhrase('en', '', 'type', ''))
44     assert not sp_importer._check_sanity(SpecialPhrase('en', 'class', '', ''))
45
46     assert sp_importer._check_sanity(SpecialPhrase('en', 'class', 'type', ''))
47
48
49 def test_load_white_and_black_lists(sp_importer):
50     """
51         Test that _load_white_and_black_lists() well return
52         black list and white list and that they are of dict type.
53     """
54     black_list, white_list = sp_importer._load_white_and_black_lists()
55
56     assert isinstance(black_list, dict) and isinstance(white_list, dict)
57
58
59 @pytest.mark.parametrize("should_replace", [(True), (False)])
60 def test_import_phrases(monkeypatch, sp_importer, tokenizer_mock,
61                         xml_wiki_content, should_replace):
62     """
63         Check that the main import_phrases() method is well executed.
64         It should pass all phrases of the wiki content on to the tokenizer.
65     """
66     monkeypatch.setattr('nominatim_db.tools.special_phrases.sp_wiki_loader._get_wiki_content',
67                         lambda lang: xml_wiki_content)
68
69     tokenizer = tokenizer_mock()
70     sp_importer.import_phrases(tokenizer, should_replace)
71
72     assert len(tokenizer.analyser_cache['special_phrases']) == 19
73     assert ('Zip Line', 'aerialway', 'zip_line', '-') in sp_importer.word_phrases