]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_sp_wiki_loader.py
0a64cd563362cf70c325e4e9c43553d2782307c4
[nominatim.git] / test / python / tools / test_sp_wiki_loader.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8     Tests for methods of the SPWikiLoader class.
9 """
10 import pytest
11 from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
12
13 @pytest.fixture
14 def xml_wiki_content(src_dir):
15     """
16         return the content of the static xml test file.
17     """
18     xml_test_content = src_dir / 'test' / 'testdata' / 'special_phrases_test_content.txt'
19     return xml_test_content.read_text()
20
21
22 @pytest.fixture
23 def sp_wiki_loader(monkeypatch, def_config, xml_wiki_content):
24     """
25         Return an instance of SPWikiLoader.
26     """
27     monkeypatch.setenv('NOMINATIM_LANGUAGES', 'en')
28     loader = SPWikiLoader(def_config)
29     monkeypatch.setattr('nominatim.tools.special_phrases.sp_wiki_loader.SPWikiLoader._get_wiki_content',
30                         lambda self, lang: xml_wiki_content)
31     return loader
32
33
34 def test_parse_xml(sp_wiki_loader, xml_wiki_content):
35     """
36         Test method parse_xml()
37         Should return the right SpecialPhrase objects.
38     """
39     phrases = sp_wiki_loader.parse_xml(xml_wiki_content)
40     check_phrases_content(phrases)
41
42
43 def test_next(sp_wiki_loader):
44     """
45         Test objects returned from the next() method.
46         It should return all SpecialPhrases objects of
47         the 'en' special phrases.
48     """
49     phrases = next(sp_wiki_loader)
50     check_phrases_content(phrases)
51
52 def check_phrases_content(phrases):
53     """
54         Asserts that the given phrases list contains
55         the right phrases of the 'en' special phrases.
56     """
57     assert set((p.p_label, p.p_class, p.p_type, p.p_operator) for p in phrases) ==\
58               {('Zip Line', 'aerialway', 'zip_line', '-'),
59                ('Zip Lines', 'aerialway', 'zip_line', '-'),
60                ('Zip Line in', 'aerialway', 'zip_line', 'in'),
61                ('Zip Lines in', 'aerialway', 'zip_line', 'in'),
62                ('Zip Line near', 'aerialway', 'zip_line', 'near'),
63                ('Animal shelter', 'amenity', 'animal_shelter', '-'),
64                ('Animal shelters', 'amenity', 'animal_shelter', '-'),
65                ('Animal shelter in', 'amenity', 'animal_shelter', 'in'),
66                ('Animal shelters in', 'amenity', 'animal_shelter', 'in'),
67                ('Animal shelter near', 'amenity', 'animal_shelter', 'near'),
68                ('Animal shelters near', 'amenity', 'animal_shelter', 'near'),
69                ('Drinking Water near', 'amenity', 'drinking_water', 'near'),
70                ('Water', 'amenity', 'drinking_water', '-'),
71                ('Water in', 'amenity', 'drinking_water', 'in'),
72                ('Water near', 'amenity', 'drinking_water', 'near'),
73                ('Embassy', 'amenity', 'embassy', '-'),
74                ('Embassys', 'amenity', 'embassy', '-'),
75                ('Embassies', 'amenity', 'embassy', '-')}