]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_sp_csv_loader.py
b5069a52a166633c5a52d0c9926afff54b8d4d7a
[nominatim.git] / test / python / tools / test_sp_csv_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 SPCsvLoader class.
9 """
10 import pytest
11
12 from nominatim.errors import UsageError
13 from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
14
15 @pytest.fixture
16 def sp_csv_loader(src_dir):
17     """
18         Return an instance of SPCsvLoader.
19     """
20     csv_path = (src_dir / 'test' / 'testdata' / 'sp_csv_test.csv').resolve()
21     loader = SPCsvLoader(csv_path)
22     return loader
23
24
25 def test_generate_phrases(sp_csv_loader):
26     """
27         Test method parse_csv()
28         Should return the right SpecialPhrase objects.
29     """
30     phrases = list(sp_csv_loader.generate_phrases())
31
32     assert len(phrases) == 41
33     assert len(set(phrases)) == 41
34
35     assert any(p.p_label == 'Billboard'
36                and p.p_class == 'advertising'
37                and p.p_type == 'billboard'
38                and p.p_operator == '-' for p in phrases)
39     assert any(p.p_label == 'Zip Lines'
40                and p.p_class == 'aerialway'
41                and p.p_type == 'zip_line'
42                and p.p_operator == '-' for p in phrases)
43
44
45 def test_invalid_cvs_file():
46     """
47         Test method check_csv_validity()
48         It should raise an exception when file with a
49         different exception than .csv is given.
50     """
51     loader = SPCsvLoader('test.wrong')
52
53     with pytest.raises(UsageError, match='not a csv file'):
54         next(loader.generate_phrases())