]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_sp_csv_loader.py
Merge pull request #2562 from lonvia/copyright-headers
[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 def test_parse_csv(sp_csv_loader):
16     """
17         Test method parse_csv()
18         Should return the right SpecialPhrase objects.
19     """
20     phrases = sp_csv_loader.parse_csv()
21     assert check_phrases_content(phrases)
22
23 def test_next(sp_csv_loader):
24     """
25         Test objects returned from the next() method.
26         It should return all SpecialPhrases objects of
27         the sp_csv_test.csv special phrases.
28     """
29     phrases = next(sp_csv_loader)
30     assert check_phrases_content(phrases)
31
32 def test_check_csv_validity(sp_csv_loader):
33     """
34         Test method check_csv_validity()
35         It should raise an exception when file with a
36         different exception than .csv is given.
37     """
38     sp_csv_loader.csv_path = 'test.csv'
39     sp_csv_loader.check_csv_validity()
40     sp_csv_loader.csv_path = 'test.wrong'
41     with pytest.raises(UsageError):
42         assert sp_csv_loader.check_csv_validity()
43
44 def check_phrases_content(phrases):
45     """
46         Asserts that the given phrases list contains
47         the right phrases of the sp_csv_test.csv special phrases.
48     """
49     return  len(phrases) > 1 \
50             and any(p.p_label == 'Billboard'
51                     and p.p_class == 'advertising'
52                     and p.p_type == 'billboard'
53                     and p.p_operator == '-' for p in phrases) \
54             and any(p.p_label == 'Zip Lines'
55                     and p.p_class == 'aerialway'
56                     and p.p_type == 'zip_line'
57                     and p.p_operator == '-' for p in phrases)
58
59 @pytest.fixture
60 def sp_csv_loader(src_dir):
61     """
62         Return an instance of SPCsvLoader.
63     """
64     csv_path = (src_dir / 'test' / 'testdata' / 'sp_csv_test.csv').resolve()
65     loader = SPCsvLoader(csv_path)
66     return loader