]> git.openstreetmap.org Git - nominatim.git/blob - test/python/config/test_config_load_module.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / config / test_config_load_module.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 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 import sys
11
12 import pytest
13
14 from nominatim_db.config import Configuration
15
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')
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_db')
31
32     assert isinstance(module.NOMINATIM_VERSION, tuple)
33
34
35 def test_load_default_module_with_hyphen(test_config):
36     module = test_config.load_plugin_module('place-info', 'nominatim_db.data')
37
38     assert isinstance(module.PlaceInfo, object)
39
40
41 def test_load_plugin_module(test_config, tmp_path):
42     (tmp_path / 'project' / 'testpath').mkdir()
43     (tmp_path / 'project' / 'testpath' / 'mymod.py')\
44         .write_text("def my_test_function():\n  return 'gjwitlsSG42TG%'")
45
46     module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
47
48     assert module.my_test_function() == 'gjwitlsSG42TG%'
49
50     # also test reloading module
51     (tmp_path / 'project' / 'testpath' / 'mymod.py')\
52         .write_text("def my_test_function():\n  return 'hjothjorhj'")
53
54     module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
55
56     assert module.my_test_function() == 'gjwitlsSG42TG%'
57
58
59 def test_load_external_library_module(test_config, tmp_path, monkeypatch):
60     MODULE_NAME = 'foogurenqodr4'
61     pythonpath = tmp_path / 'priv-python'
62     pythonpath.mkdir()
63     (pythonpath / MODULE_NAME).mkdir()
64     (pythonpath / MODULE_NAME / '__init__.py').write_text('')
65     (pythonpath / MODULE_NAME / 'tester.py')\
66         .write_text("def my_test_function():\n  return 'gjwitlsSG42TG%'")
67
68     monkeypatch.syspath_prepend(pythonpath)
69
70     module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
71
72     assert module.my_test_function() == 'gjwitlsSG42TG%'
73
74     # also test reloading module
75     (pythonpath / MODULE_NAME / 'tester.py')\
76         .write_text("def my_test_function():\n  return 'dfigjreigj'")
77
78     module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
79
80     assert module.my_test_function() == 'gjwitlsSG42TG%'
81
82     del sys.modules[f'{MODULE_NAME}.tester']