1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Test for loading extra Python modules.
10 from pathlib import Path
15 from nominatim.config import Configuration
18 def test_config(src_dir, tmp_path):
19 """ Create a configuration object with project and config directories
20 in a temporary directory.
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'
29 def test_load_default_module(test_config):
30 module = test_config.load_plugin_module('version', 'nominatim')
32 assert isinstance(module.NOMINATIM_VERSION, tuple)
35 def test_load_plugin_module(test_config, tmp_path):
36 (tmp_path / 'project' / 'testpath').mkdir()
37 (tmp_path / 'project' / 'testpath' / 'mymod.py')\
38 .write_text("def my_test_function():\n return 'gjwitlsSG42TG%'")
40 module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
42 assert module.my_test_function() == 'gjwitlsSG42TG%'
44 # also test reloading module
45 (tmp_path / 'project' / 'testpath' / 'mymod.py')\
46 .write_text("def my_test_function():\n return 'hjothjorhj'")
48 module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
50 assert module.my_test_function() == 'gjwitlsSG42TG%'
53 def test_load_external_library_module(test_config, tmp_path, monkeypatch):
54 MODULE_NAME = 'foogurenqodr4'
55 pythonpath = tmp_path / 'priv-python'
57 (pythonpath / MODULE_NAME).mkdir()
58 (pythonpath / MODULE_NAME / '__init__.py').write_text('')
59 (pythonpath / MODULE_NAME / 'tester.py')\
60 .write_text("def my_test_function():\n return 'gjwitlsSG42TG%'")
62 monkeypatch.syspath_prepend(pythonpath)
64 module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
66 assert module.my_test_function() == 'gjwitlsSG42TG%'
68 # also test reloading module
69 (pythonpath / MODULE_NAME / 'tester.py')\
70 .write_text("def my_test_function():\n return 'dfigjreigj'")
72 module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
74 assert module.my_test_function() == 'gjwitlsSG42TG%'
76 del sys.modules[f'{MODULE_NAME}.tester']