]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/test_factory.py
only print non-empty search tables
[nominatim.git] / test / python / tokenizer / test_factory.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 creating new tokenizers.
9 """
10 import pytest
11
12 from nominatim.db import properties
13 from nominatim.tokenizer import factory
14 from nominatim.errors import UsageError
15 from dummy_tokenizer import DummyTokenizer
16
17
18 def test_setup_bad_tokenizer_name(project_env, monkeypatch):
19     monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
20
21     with pytest.raises(UsageError):
22         factory.create_tokenizer(project_env)
23
24
25 class TestFactory:
26     @pytest.fixture(autouse=True)
27     def init_env(self, project_env, property_table, tokenizer_mock):
28         self.config = project_env
29
30
31     def test_setup_dummy_tokenizer(self, temp_db_conn):
32         tokenizer = factory.create_tokenizer(self.config)
33
34         assert isinstance(tokenizer, DummyTokenizer)
35         assert tokenizer.init_state == "new"
36         assert (self.config.project_dir / 'tokenizer').is_dir()
37
38         assert properties.get_property(temp_db_conn, 'tokenizer') == 'dummy'
39
40
41     def test_setup_tokenizer_dir_exists(self):
42         (self.config.project_dir / 'tokenizer').mkdir()
43
44         tokenizer = factory.create_tokenizer(self.config)
45
46         assert isinstance(tokenizer, DummyTokenizer)
47         assert tokenizer.init_state == "new"
48
49
50     def test_setup_tokenizer_dir_failure(self):
51         (self.config.project_dir / 'tokenizer').write_text("foo")
52
53         with pytest.raises(UsageError):
54             factory.create_tokenizer(self.config)
55
56
57     def test_load_tokenizer(self):
58         factory.create_tokenizer(self.config)
59
60         tokenizer = factory.get_tokenizer_for_db(self.config)
61
62         assert isinstance(tokenizer, DummyTokenizer)
63         assert tokenizer.init_state == "loaded"
64
65
66     def test_load_repopulate_tokenizer_dir(self):
67         factory.create_tokenizer(self.config)
68
69         self.config.project_dir = self.config.project_dir
70
71         factory.get_tokenizer_for_db(self.config)
72         assert (self.config.project_dir / 'tokenizer').exists()
73
74
75     def test_load_missing_property(self, temp_db_cursor):
76         factory.create_tokenizer(self.config)
77
78         temp_db_cursor.execute("TRUNCATE TABLE nominatim_properties")
79
80         with pytest.raises(UsageError):
81             factory.get_tokenizer_for_db(self.config)