]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_sp_csv_loader.py
move SearchDescription building into tokens
[nominatim.git] / test / python / test_tools_sp_csv_loader.py
1 """
2     Tests for methods of the SPCsvLoader class.
3 """
4 import pytest
5
6 from nominatim.errors import UsageError
7 from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
8
9 def test_parse_csv(sp_csv_loader):
10     """
11         Test method parse_csv()
12         Should return the right SpecialPhrase objects.
13     """
14     phrases = sp_csv_loader.parse_csv()
15     assert check_phrases_content(phrases)
16
17 def test_next(sp_csv_loader):
18     """
19         Test objects returned from the next() method.
20         It should return all SpecialPhrases objects of
21         the sp_csv_test.csv special phrases.
22     """
23     phrases = next(sp_csv_loader)
24     assert check_phrases_content(phrases)
25
26 def test_check_csv_validity(sp_csv_loader):
27     """
28         Test method check_csv_validity()
29         It should raise an exception when file with a
30         different exception than .csv is given.
31     """
32     sp_csv_loader.csv_path = 'test.csv'
33     sp_csv_loader.check_csv_validity()
34     sp_csv_loader.csv_path = 'test.wrong'
35     with pytest.raises(UsageError):
36         assert sp_csv_loader.check_csv_validity()
37
38 def check_phrases_content(phrases):
39     """
40         Asserts that the given phrases list contains
41         the right phrases of the sp_csv_test.csv special phrases.
42     """
43     return  len(phrases) > 1 \
44             and any(p.p_label == 'Billboard'
45                     and p.p_class == 'advertising'
46                     and p.p_type == 'billboard'
47                     and p.p_operator == '-' for p in phrases) \
48             and any(p.p_label == 'Zip Lines'
49                     and p.p_class == 'aerialway'
50                     and p.p_type == 'zip_line'
51                     and p.p_operator == '-' for p in phrases)
52
53 @pytest.fixture
54 def sp_csv_loader(src_dir):
55     """
56         Return an instance of SPCsvLoader.
57     """
58     csv_path = (src_dir / 'test' / 'testdata' / 'sp_csv_test.csv').resolve()
59     loader = SPCsvLoader(csv_path)
60     return loader