2 Tests for creating new tokenizers.
6 from nominatim.db import properties
7 from nominatim.tokenizer import factory
8 from nominatim.errors import UsageError
9 from dummy_tokenizer import DummyTokenizer
12 def test_setup_bad_tokenizer_name(project_env, monkeypatch):
13 monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
15 with pytest.raises(UsageError):
16 factory.create_tokenizer(project_env)
20 @pytest.fixture(autouse=True)
21 def init_env(self, project_env, property_table, tokenizer_mock):
22 self.config = project_env
25 def test_setup_dummy_tokenizer(self, temp_db_conn):
26 tokenizer = factory.create_tokenizer(self.config)
28 assert isinstance(tokenizer, DummyTokenizer)
29 assert tokenizer.init_state == "new"
30 assert (self.config.project_dir / 'tokenizer').is_dir()
32 assert properties.get_property(temp_db_conn, 'tokenizer') == 'dummy'
35 def test_setup_tokenizer_dir_exists(self):
36 (self.config.project_dir / 'tokenizer').mkdir()
38 tokenizer = factory.create_tokenizer(self.config)
40 assert isinstance(tokenizer, DummyTokenizer)
41 assert tokenizer.init_state == "new"
44 def test_setup_tokenizer_dir_failure(self):
45 (self.config.project_dir / 'tokenizer').write_text("foo")
47 with pytest.raises(UsageError):
48 factory.create_tokenizer(self.config)
51 def test_load_tokenizer(self):
52 factory.create_tokenizer(self.config)
54 tokenizer = factory.get_tokenizer_for_db(self.config)
56 assert isinstance(tokenizer, DummyTokenizer)
57 assert tokenizer.init_state == "loaded"
60 def test_load_no_tokenizer_dir(self):
61 factory.create_tokenizer(self.config)
63 self.config.project_dir = self.config.project_dir / 'foo'
65 with pytest.raises(UsageError):
66 factory.get_tokenizer_for_db(self.config)
69 def test_load_missing_property(self, temp_db_cursor):
70 factory.create_tokenizer(self.config)
72 temp_db_cursor.execute("TRUNCATE TABLE nominatim_properties")
74 with pytest.raises(UsageError):
75 factory.get_tokenizer_for_db(self.config)