2 Test for legacy tokenizer.
8 from nominatim.tokenizer import legacy_tokenizer
9 from nominatim.db import properties
10 from nominatim.errors import UsageError
13 def test_config(def_config, tmp_path):
14 def_config.project_dir = tmp_path / 'project'
15 def_config.project_dir.mkdir()
17 module_dir = tmp_path / 'module_src'
19 (module_dir / 'nominatim.so').write_text('TEST nomiantim.so')
21 def_config.lib_dir.module = module_dir
23 sqldir = tmp_path / 'sql'
25 (sqldir / 'tokenizer').mkdir()
26 (sqldir / 'tokenizer' / 'legacy_tokenizer.sql').write_text("SELECT 'a'")
27 (sqldir / 'words.sql').write_text("SELECT 'a'")
28 shutil.copy(str(def_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer_tables.sql'),
29 str(sqldir / 'tokenizer' / 'legacy_tokenizer_tables.sql'))
31 def_config.lib_dir.sql = sqldir
32 def_config.lib_dir.data = sqldir
39 def tokenizer_factory(dsn, tmp_path, monkeypatch):
42 return legacy_tokenizer.create(dsn, tmp_path / 'tokenizer')
47 def tokenizer_setup(tokenizer_factory, test_config, property_table,
48 monkeypatch, sql_preprocessor):
49 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
50 tok = tokenizer_factory()
51 tok.init_new_db(test_config)
54 def test_init_new(tokenizer_factory, test_config, property_table, monkeypatch,
55 temp_db_conn, sql_preprocessor):
56 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', 'xxvv')
57 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
59 tok = tokenizer_factory()
60 tok.init_new_db(test_config)
62 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) == 'xxvv'
64 outfile = test_config.project_dir / 'module' / 'nominatim.so'
66 assert outfile.exists()
67 assert outfile.read_text() == 'TEST nomiantim.so'
68 assert outfile.stat().st_mode == 33261
71 def test_init_module_load_failed(tokenizer_factory, test_config, property_table,
72 monkeypatch, temp_db_conn):
73 tok = tokenizer_factory()
75 with pytest.raises(UsageError):
76 tok.init_new_db(test_config)
79 def test_init_module_custom(tokenizer_factory, test_config, property_table,
80 monkeypatch, tmp_path, sql_preprocessor):
81 module_dir = (tmp_path / 'custom').resolve()
83 (module_dir/ 'nominatim.so').write_text('CUSTOM nomiantim.so')
85 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', str(module_dir))
86 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
88 tok = tokenizer_factory()
89 tok.init_new_db(test_config)
91 assert not (test_config.project_dir / 'module').exists()
94 def test_init_from_project(tokenizer_setup, tokenizer_factory):
95 tok = tokenizer_factory()
97 tok.init_from_project()
99 assert tok.normalization is not None