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 import special phrases methods
9 of the class SPImporter.
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
18 def sp_importer(temp_db_conn, def_config, monkeypatch):
20 Return an instance of SPImporter.
22 monkeypatch.setenv('NOMINATIM_LANGUAGES', 'en')
23 loader = SPWikiLoader(def_config)
24 return SPImporter(def_config, temp_db_conn, loader)
28 def xml_wiki_content(src_dir):
30 return the content of the static xml test file.
32 xml_test_content = src_dir / 'test' / 'testdata' / 'special_phrases_test_content.txt'
33 return xml_test_content.read_text(encoding='utf-8')
36 def test_check_sanity_class(sp_importer):
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.
43 assert not sp_importer._check_sanity(SpecialPhrase('en', '', 'type', ''))
44 assert not sp_importer._check_sanity(SpecialPhrase('en', 'class', '', ''))
46 assert sp_importer._check_sanity(SpecialPhrase('en', 'class', 'type', ''))
49 def test_load_white_and_black_lists(sp_importer):
51 Test that _load_white_and_black_lists() well return
52 black list and white list and that they are of dict type.
54 black_list, white_list = sp_importer._load_white_and_black_lists()
56 assert isinstance(black_list, dict) and isinstance(white_list, dict)
59 @pytest.mark.parametrize("should_replace", [(True), (False)])
60 def test_import_phrases(monkeypatch, sp_importer, tokenizer_mock,
61 xml_wiki_content, should_replace):
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.
66 monkeypatch.setattr('nominatim_db.tools.special_phrases.sp_wiki_loader._get_wiki_content',
67 lambda lang: xml_wiki_content)
69 tokenizer = tokenizer_mock()
70 sp_importer.import_phrases(tokenizer, should_replace)
72 assert len(tokenizer.analyser_cache['special_phrases']) == 19
73 assert ('Zip Line', 'aerialway', 'zip_line', '-') in sp_importer.word_phrases