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_config(def_config, tmp_path, property_table, tokenizer_mock):
13 def_config.project_dir = tmp_path
17 def test_setup_dummy_tokenizer(temp_db_conn, test_config):
18 tokenizer = factory.create_tokenizer(test_config)
20 assert isinstance(tokenizer, DummyTokenizer)
21 assert tokenizer.init_state == "new"
22 assert (test_config.project_dir / 'tokenizer').is_dir()
24 assert properties.get_property(temp_db_conn, 'tokenizer') == 'dummy'
27 def test_setup_tokenizer_dir_exists(test_config):
28 (test_config.project_dir / 'tokenizer').mkdir()
30 tokenizer = factory.create_tokenizer(test_config)
32 assert isinstance(tokenizer, DummyTokenizer)
33 assert tokenizer.init_state == "new"
36 def test_setup_tokenizer_dir_failure(test_config):
37 (test_config.project_dir / 'tokenizer').write_text("foo")
39 with pytest.raises(UsageError):
40 factory.create_tokenizer(test_config)
43 def test_setup_bad_tokenizer_name(def_config, tmp_path, monkeypatch):
44 def_config.project_dir = tmp_path
45 monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
47 with pytest.raises(UsageError):
48 factory.create_tokenizer(def_config)
51 def test_load_tokenizer(test_config):
52 factory.create_tokenizer(test_config)
54 tokenizer = factory.get_tokenizer_for_db(test_config)
56 assert isinstance(tokenizer, DummyTokenizer)
57 assert tokenizer.init_state == "loaded"
60 def test_load_no_tokenizer_dir(test_config):
61 factory.create_tokenizer(test_config)
63 test_config.project_dir = test_config.project_dir / 'foo'
65 with pytest.raises(UsageError):
66 factory.get_tokenizer_for_db(test_config)
69 def test_load_missing_propoerty(temp_db_cursor, test_config):
70 factory.create_tokenizer(test_config)
72 temp_db_cursor.execute("TRUNCATE TABLE nominatim_properties")
74 with pytest.raises(UsageError):
75 factory.get_tokenizer_for_db(test_config)