2 Tests for creating new tokenizers.
7 from nominatim.db import properties
8 from nominatim.tokenizer import factory
9 from nominatim.errors import UsageError
10 import dummy_tokenizer
13 def test_config(def_config, tmp_path):
14 def_config.project_dir = tmp_path
19 def tokenizer_import(monkeypatch):
20 monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
22 def _import_dummy(module, *args, **kwargs):
23 return dummy_tokenizer
25 monkeypatch.setattr(importlib, "import_module", _import_dummy)
28 def test_setup_dummy_tokenizer(temp_db_conn, test_config,
29 tokenizer_import, property_table):
30 tokenizer = factory.create_tokenizer(test_config)
32 assert isinstance(tokenizer, dummy_tokenizer.DummyTokenizer)
33 assert tokenizer.init_state == "new"
34 assert (test_config.project_dir / 'tokenizer').is_dir()
36 assert properties.get_property(temp_db_conn, 'tokenizer') == 'dummy'
39 def test_setup_tokenizer_dir_exists(test_config, tokenizer_import, property_table):
40 (test_config.project_dir / 'tokenizer').mkdir()
42 tokenizer = factory.create_tokenizer(test_config)
44 assert isinstance(tokenizer, dummy_tokenizer.DummyTokenizer)
45 assert tokenizer.init_state == "new"
48 def test_setup_tokenizer_dir_failure(test_config, tokenizer_import, property_table):
49 (test_config.project_dir / 'tokenizer').write_text("foo")
51 with pytest.raises(UsageError):
52 factory.create_tokenizer(test_config)
55 def test_setup_bad_tokenizer_name(test_config, monkeypatch):
56 monkeypatch.setenv('NOMINATIM_TOKENIZER', 'dummy')
58 with pytest.raises(UsageError):
59 factory.create_tokenizer(test_config)
61 def test_load_tokenizer(temp_db_conn, test_config,
62 tokenizer_import, property_table):
63 factory.create_tokenizer(test_config)
65 tokenizer = factory.get_tokenizer_for_db(test_config)
67 assert isinstance(tokenizer, dummy_tokenizer.DummyTokenizer)
68 assert tokenizer.init_state == "loaded"
71 def test_load_no_tokenizer_dir(test_config, tokenizer_import, property_table):
72 factory.create_tokenizer(test_config)
74 test_config.project_dir = test_config.project_dir / 'foo'
76 with pytest.raises(UsageError):
77 factory.get_tokenizer_for_db(test_config)
80 def test_load_missing_propoerty(temp_db_cursor, test_config, tokenizer_import, property_table):
81 factory.create_tokenizer(test_config)
83 temp_db_cursor.execute("TRUNCATE TABLE nominatim_properties")
85 with pytest.raises(UsageError):
86 factory.get_tokenizer_for_db(test_config)