]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_sp_csv_loader.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tools / test_sp_csv_loader.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 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_db.errors import UsageError
13 from nominatim_db.tools.special_phrases.sp_csv_loader import SPCsvLoader
14 from nominatim_db.tools.special_phrases.special_phrase import SpecialPhrase
15
16
17 @pytest.fixture
18 def sp_csv_loader(src_dir):
19     """
20         Return an instance of SPCsvLoader.
21     """
22     csv_path = (src_dir / 'test' / 'testdata' / 'sp_csv_test.csv').resolve()
23     loader = SPCsvLoader(csv_path)
24     return loader
25
26
27 def test_generate_phrases(sp_csv_loader):
28     """
29         Test method parse_csv()
30         Should return the right SpecialPhrase objects.
31     """
32     phrases = list(sp_csv_loader.generate_phrases())
33
34     assert len(phrases) == 42
35     assert len(set(phrases)) == 41
36
37     assert SpecialPhrase('Billboard', 'advertising', 'billboard', '-') in phrases
38     assert SpecialPhrase('Zip Lines', 'aerialway', 'zip_line', '-') in phrases
39
40
41 def test_invalid_cvs_file():
42     """
43         Test method check_csv_validity()
44         It should raise an exception when file with a
45         different exception than .csv is given.
46     """
47     loader = SPCsvLoader('test.wrong')
48
49     with pytest.raises(UsageError, match='not a csv file'):
50         next(loader.generate_phrases())