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