]> git.openstreetmap.org Git - nominatim.git/blob - test/python/config/test_config_load_module.py
add support for external sanitizer modules
[nominatim.git] / test / python / config / test_config_load_module.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Test for loading extra Python modules.
9 """
10 from pathlib import Path
11 import sys
12
13 import pytest
14
15 from nominatim.config import Configuration
16
17 @pytest.fixture
18 def test_config(src_dir, tmp_path):
19     """ Create a configuration object with project and config directories
20         in a temporary directory.
21     """
22     (tmp_path / 'project').mkdir()
23     (tmp_path / 'config').mkdir()
24     conf = Configuration(tmp_path / 'project', src_dir / 'settings')
25     conf.config_dir = tmp_path / 'config'
26     return conf
27
28
29 def test_load_default_module(test_config):
30     module = test_config.load_plugin_module('version', 'nominatim')
31
32     assert isinstance(module.NOMINATIM_VERSION, tuple)
33
34 def test_load_default_module_with_hyphen(test_config):
35     module = test_config.load_plugin_module('place-info', 'nominatim.data')
36
37     assert isinstance(module.PlaceInfo, object)
38
39
40 def test_load_plugin_module(test_config, tmp_path):
41     (tmp_path / 'project' / 'testpath').mkdir()
42     (tmp_path / 'project' / 'testpath' / 'mymod.py')\
43         .write_text("def my_test_function():\n  return 'gjwitlsSG42TG%'")
44
45     module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
46
47     assert module.my_test_function() == 'gjwitlsSG42TG%'
48
49     # also test reloading module
50     (tmp_path / 'project' / 'testpath' / 'mymod.py')\
51         .write_text("def my_test_function():\n  return 'hjothjorhj'")
52
53     module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
54
55     assert module.my_test_function() == 'gjwitlsSG42TG%'
56
57
58 def test_load_external_library_module(test_config, tmp_path, monkeypatch):
59     MODULE_NAME = 'foogurenqodr4'
60     pythonpath = tmp_path / 'priv-python'
61     pythonpath.mkdir()
62     (pythonpath / MODULE_NAME).mkdir()
63     (pythonpath / MODULE_NAME / '__init__.py').write_text('')
64     (pythonpath / MODULE_NAME / 'tester.py')\
65         .write_text("def my_test_function():\n  return 'gjwitlsSG42TG%'")
66
67     monkeypatch.syspath_prepend(pythonpath)
68
69     module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
70
71     assert module.my_test_function() == 'gjwitlsSG42TG%'
72
73     # also test reloading module
74     (pythonpath / MODULE_NAME / 'tester.py')\
75         .write_text("def my_test_function():\n  return 'dfigjreigj'")
76
77     module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
78
79     assert module.my_test_function() == 'gjwitlsSG42TG%'
80
81     del sys.modules[f'{MODULE_NAME}.tester']