Files
Nominatim/test/python/config/test_config_load_module.py
2026-02-10 00:38:40 +05:30

83 lines
2.7 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Nominatim. (https://nominatim.org)
#
# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Test for loading extra Python modules.
"""
import sys
import pytest
from nominatim_db.config import Configuration
@pytest.fixture
def test_config(src_dir, tmp_path):
""" Create a configuration object with project and config directories
in a temporary directory.
"""
(tmp_path / 'project').mkdir()
(tmp_path / 'config').mkdir()
conf = Configuration(tmp_path / 'project')
conf.config_dir = tmp_path / 'config'
return conf
def test_load_default_module(test_config):
module = test_config.load_plugin_module('version', 'nominatim_db')
assert isinstance(module.NOMINATIM_VERSION, tuple)
def test_load_default_module_with_hyphen(test_config):
module = test_config.load_plugin_module('place-info', 'nominatim_db.data')
assert isinstance(module.PlaceInfo, object)
def test_load_plugin_module(test_config, tmp_path):
(tmp_path / 'project' / 'testpath').mkdir()
(tmp_path / 'project' / 'testpath' / 'mymod.py')\
.write_text("def my_test_function():\n return 'gjwitlsSG42TG%'", encoding='utf-8')
module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
assert module.my_test_function() == 'gjwitlsSG42TG%'
# also test reloading module
(tmp_path / 'project' / 'testpath' / 'mymod.py')\
.write_text("def my_test_function():\n return 'hjothjorhj'", encoding='utf-8')
module = test_config.load_plugin_module('testpath/mymod.py', 'private.something')
assert module.my_test_function() == 'gjwitlsSG42TG%'
def test_load_external_library_module(test_config, tmp_path, monkeypatch):
MODULE_NAME = 'foogurenqodr4'
pythonpath = tmp_path / 'priv-python'
pythonpath.mkdir()
(pythonpath / MODULE_NAME).mkdir()
(pythonpath / MODULE_NAME / '__init__.py').write_text('', encoding='utf-8')
(pythonpath / MODULE_NAME / 'tester.py')\
.write_text("def my_test_function():\n return 'gjwitlsSG42TG%'", encoding='utf-8')
monkeypatch.syspath_prepend(pythonpath)
module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
assert module.my_test_function() == 'gjwitlsSG42TG%'
# also test reloading module
(pythonpath / MODULE_NAME / 'tester.py')\
.write_text("def my_test_function():\n return 'dfigjreigj'", encoding='utf-8')
module = test_config.load_plugin_module(f'{MODULE_NAME}.tester', 'private.something')
assert module.my_test_function() == 'gjwitlsSG42TG%'
del sys.modules[f'{MODULE_NAME}.tester']