]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_sp_csv_loader.py
4bf7137e796ded0b7cdbf29e6b082e3f6c0a8815
[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' and p.p_class == 'advertising' and p.p_type == 'billboard'
45                     and p.p_operator == '-' for p in phrases) \
46             and any(p.p_label == 'Zip Lines' and p.p_class == 'aerialway' and p.p_type == 'zip_line'
47                     and p.p_operator == '-' for p in phrases)
48
49 @pytest.fixture
50 def sp_csv_loader(src_dir):
51     """
52         Return an instance of SPCsvLoader.
53     """
54     csv_path = (src_dir / 'test' / 'testdata' / 'sp_csv_test.csv').resolve()
55     loader = SPCsvLoader(csv_path)
56     return loader