]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_sp_wiki_loader.py
move SearchDescription building into tokens
[nominatim.git] / test / python / test_tools_sp_wiki_loader.py
1 """
2     Tests for methods of the SPWikiLoader class.
3 """
4 import pytest
5 from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
6
7 @pytest.fixture
8 def xml_wiki_content(src_dir):
9     """
10         return the content of the static xml test file.
11     """
12     xml_test_content = src_dir / 'test' / 'testdata' / 'special_phrases_test_content.txt'
13     return xml_test_content.read_text()
14
15
16 @pytest.fixture
17 def sp_wiki_loader(monkeypatch, def_config, xml_wiki_content):
18     """
19         Return an instance of SPWikiLoader.
20     """
21     loader = SPWikiLoader(def_config, ['en'])
22     monkeypatch.setattr('nominatim.tools.special_phrases.sp_wiki_loader.SPWikiLoader._get_wiki_content',
23                         lambda self, lang: xml_wiki_content)
24     return loader
25
26
27 def test_parse_xml(sp_wiki_loader, xml_wiki_content):
28     """
29         Test method parse_xml()
30         Should return the right SpecialPhrase objects.
31     """
32     phrases = sp_wiki_loader.parse_xml(xml_wiki_content)
33     assert check_phrases_content(phrases)
34
35
36 def test_next(sp_wiki_loader):
37     """
38         Test objects returned from the next() method.
39         It should return all SpecialPhrases objects of
40         the 'en' special phrases.
41     """
42     phrases = next(sp_wiki_loader)
43     assert check_phrases_content(phrases)
44
45 def check_phrases_content(phrases):
46     """
47         Asserts that the given phrases list contains
48         the right phrases of the 'en' special phrases.
49     """
50     return  len(phrases) > 1 \
51             and any(p.p_label == 'Embassies' and p.p_class == 'amenity' and p.p_type == 'embassy'
52                     and p.p_operator == '-' for p in phrases) \
53             and any(p.p_label == 'Zip Line' and p.p_class == 'aerialway' and p.p_type == 'zip_line'
54                     and p.p_operator == '-' for p in phrases)