mirror of
https://github.com/osm-search/Nominatim.git
synced 2024-12-25 14:02:12 +03:00
test: avoid use of tempfile module
Use the tmp_path fixture instead which provides automatic cleanup.
This commit is contained in:
parent
f93d0fa957
commit
af52eed0dd
@ -1,6 +1,5 @@
|
|||||||
import itertools
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
@ -88,6 +87,9 @@ def temp_db_cursor(temp_db):
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def table_factory(temp_db_cursor):
|
def table_factory(temp_db_cursor):
|
||||||
|
""" A fixture that creates new SQL tables, potentially filled with
|
||||||
|
content.
|
||||||
|
"""
|
||||||
def mk_table(name, definition='id INT', content=None):
|
def mk_table(name, definition='id INT', content=None):
|
||||||
temp_db_cursor.execute('CREATE TABLE {} ({})'.format(name, definition))
|
temp_db_cursor.execute('CREATE TABLE {} ({})'.format(name, definition))
|
||||||
if content is not None:
|
if content is not None:
|
||||||
@ -127,18 +129,11 @@ def cli_call():
|
|||||||
return _call_nominatim
|
return _call_nominatim
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def tmp_phplib_dir():
|
|
||||||
with tempfile.TemporaryDirectory() as phpdir:
|
|
||||||
(Path(phpdir) / 'admin').mkdir()
|
|
||||||
|
|
||||||
yield Path(phpdir)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def property_table(table_factory):
|
def property_table(table_factory):
|
||||||
table_factory('nominatim_properties', 'property TEXT, value TEXT')
|
table_factory('nominatim_properties', 'property TEXT, value TEXT')
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def status_table(temp_db_conn):
|
def status_table(temp_db_conn):
|
||||||
""" Create an empty version of the status table and
|
""" Create an empty version of the status table and
|
||||||
|
@ -3,14 +3,19 @@ Tests for tools.exec_utils module.
|
|||||||
"""
|
"""
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import nominatim.tools.exec_utils as exec_utils
|
import nominatim.tools.exec_utils as exec_utils
|
||||||
|
|
||||||
@pytest.fixture
|
class TestRunLegacyScript:
|
||||||
def nominatim_env(tmp_phplib_dir, def_config):
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def setup_nominatim_env(self, tmp_path, def_config):
|
||||||
|
tmp_phplib_dir = tmp_path / 'phplib'
|
||||||
|
tmp_phplib_dir.mkdir()
|
||||||
|
(tmp_phplib_dir / 'admin').mkdir()
|
||||||
|
|
||||||
class _NominatimEnv:
|
class _NominatimEnv:
|
||||||
config = def_config
|
config = def_config
|
||||||
phplib_dir = tmp_phplib_dir
|
phplib_dir = tmp_phplib_dir
|
||||||
@ -21,78 +26,66 @@ def nominatim_env(tmp_phplib_dir, def_config):
|
|||||||
module_dir = 'module'
|
module_dir = 'module'
|
||||||
osm2pgsql_path = 'osm2pgsql'
|
osm2pgsql_path = 'osm2pgsql'
|
||||||
|
|
||||||
return _NominatimEnv
|
self.testenv = _NominatimEnv
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def test_script(nominatim_env):
|
def mk_script(self, code):
|
||||||
def _create_file(code):
|
codefile = self.testenv.phplib_dir / 'admin' / 't.php'
|
||||||
with (nominatim_env.phplib_dir / 'admin' / 't.php').open(mode='w') as fd:
|
codefile.write_text('<?php\n' + code + '\n')
|
||||||
fd.write('<?php\n')
|
|
||||||
fd.write(code + '\n')
|
|
||||||
|
|
||||||
return 't.php'
|
return 't.php'
|
||||||
|
|
||||||
return _create_file
|
|
||||||
|
|
||||||
@pytest.fixture(params=[0, 1, 15, 255])
|
@pytest.mark.parametrize("return_code", (0, 1, 15, 255))
|
||||||
def return_code(request):
|
def test_run_legacy_return_exit_code(self, return_code):
|
||||||
return request.param
|
fname = self.mk_script('exit({});'.format(return_code))
|
||||||
|
assert return_code == \
|
||||||
### run_legacy_script
|
exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
|
||||||
|
|
||||||
def test_run_legacy_return_exit_code(nominatim_env, test_script, return_code):
|
|
||||||
fname = test_script('exit({});'.format(return_code))
|
|
||||||
assert return_code == exec_utils.run_legacy_script(fname,
|
|
||||||
nominatim_env=nominatim_env)
|
|
||||||
|
|
||||||
|
|
||||||
def test_run_legacy_return_throw_on_fail(nominatim_env, test_script):
|
def test_run_legacy_return_throw_on_fail(self):
|
||||||
fname = test_script('exit(11);')
|
fname = self.mk_script('exit(11);')
|
||||||
with pytest.raises(subprocess.CalledProcessError):
|
with pytest.raises(subprocess.CalledProcessError):
|
||||||
exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
|
exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
|
||||||
throw_on_fail=True)
|
throw_on_fail=True)
|
||||||
|
|
||||||
|
|
||||||
def test_run_legacy_return_dont_throw_on_success(nominatim_env, test_script):
|
def test_run_legacy_return_dont_throw_on_success(self):
|
||||||
fname = test_script('exit(0);')
|
fname = self.mk_script('exit(0);')
|
||||||
assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env,
|
assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv,
|
||||||
throw_on_fail=True)
|
throw_on_fail=True)
|
||||||
|
|
||||||
def test_run_legacy_use_given_module_path(nominatim_env, test_script):
|
def test_run_legacy_use_given_module_path(self):
|
||||||
fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
|
fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == '' ? 0 : 23);")
|
||||||
|
|
||||||
assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
|
assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
|
||||||
|
|
||||||
|
|
||||||
def test_run_legacy_do_not_overwrite_module_path(nominatim_env, test_script, monkeypatch):
|
def test_run_legacy_do_not_overwrite_module_path(self, monkeypatch):
|
||||||
monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
|
monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', 'other')
|
||||||
fname = test_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
|
fname = self.mk_script("exit($_SERVER['NOMINATIM_DATABASE_MODULE_PATH'] == 'other' ? 0 : 1);")
|
||||||
|
|
||||||
assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=nominatim_env)
|
assert 0 == exec_utils.run_legacy_script(fname, nominatim_env=self.testenv)
|
||||||
|
|
||||||
### run_api_script
|
|
||||||
|
|
||||||
@pytest.fixture
|
class TestRunApiScript:
|
||||||
def tmp_project_dir():
|
|
||||||
with tempfile.TemporaryDirectory() as tempd:
|
@pytest.fixture(autouse=True)
|
||||||
project_dir = Path(tempd)
|
def setup_project_dir(self, tmp_path):
|
||||||
webdir = project_dir / 'website'
|
webdir = tmp_path / 'website'
|
||||||
webdir.mkdir()
|
webdir.mkdir()
|
||||||
|
(webdir / 'test.php').write_text("<?php\necho 'OK\n';")
|
||||||
|
|
||||||
with (webdir / 'test.php').open(mode='w') as fd:
|
|
||||||
fd.write("<?php\necho 'OK\n';")
|
|
||||||
|
|
||||||
yield project_dir
|
def test_run_api(self, tmp_path):
|
||||||
|
assert 0 == exec_utils.run_api_script('test', tmp_path)
|
||||||
|
|
||||||
def test_run_api(tmp_project_dir):
|
def test_run_api_execution_error(self, tmp_path):
|
||||||
assert 0 == exec_utils.run_api_script('test', tmp_project_dir)
|
assert 0 != exec_utils.run_api_script('badname', tmp_path)
|
||||||
|
|
||||||
def test_run_api_execution_error(tmp_project_dir):
|
def test_run_api_with_extra_env(self, tmp_path):
|
||||||
assert 0 != exec_utils.run_api_script('badname', tmp_project_dir)
|
extra_env = dict(SCRIPT_FILENAME=str(tmp_path / 'website' / 'test.php'))
|
||||||
|
assert 0 == exec_utils.run_api_script('badname', tmp_path,
|
||||||
def test_run_api_with_extra_env(tmp_project_dir):
|
|
||||||
extra_env = dict(SCRIPT_FILENAME=str(tmp_project_dir / 'website' / 'test.php'))
|
|
||||||
assert 0 == exec_utils.run_api_script('badname', tmp_project_dir,
|
|
||||||
extra_env=extra_env)
|
extra_env=extra_env)
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,15 +2,13 @@
|
|||||||
Tests for import special phrases methods
|
Tests for import special phrases methods
|
||||||
of the class SPImporter.
|
of the class SPImporter.
|
||||||
"""
|
"""
|
||||||
from nominatim.errors import UsageError
|
|
||||||
from pathlib import Path
|
|
||||||
import tempfile
|
|
||||||
from shutil import copyfile
|
from shutil import copyfile
|
||||||
import pytest
|
import pytest
|
||||||
from nominatim.tools.special_phrases.sp_importer import SPImporter
|
from nominatim.tools.special_phrases.sp_importer import SPImporter
|
||||||
from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
|
from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
|
||||||
from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
|
from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
|
||||||
from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
|
from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
|
||||||
|
from nominatim.errors import UsageError
|
||||||
|
|
||||||
from cursor import CursorForTesting
|
from cursor import CursorForTesting
|
||||||
|
|
||||||
@ -93,19 +91,18 @@ def test_load_white_and_black_lists(sp_importer):
|
|||||||
|
|
||||||
assert isinstance(black_list, dict) and isinstance(white_list, dict)
|
assert isinstance(black_list, dict) and isinstance(white_list, dict)
|
||||||
|
|
||||||
def test_convert_php_settings(sp_importer, testfile_dir):
|
def test_convert_php_settings(sp_importer, testfile_dir, tmp_path):
|
||||||
"""
|
"""
|
||||||
Test that _convert_php_settings_if_needed() convert the given
|
Test that _convert_php_settings_if_needed() convert the given
|
||||||
php file to a json file.
|
php file to a json file.
|
||||||
"""
|
"""
|
||||||
php_file = (testfile_dir / 'phrase_settings.php').resolve()
|
php_file = (testfile_dir / 'phrase_settings.php').resolve()
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
temp_settings = (tmp_path / 'phrase_settings.php').resolve()
|
||||||
temp_settings = (Path(temp_dir) / 'phrase_settings.php').resolve()
|
|
||||||
copyfile(php_file, temp_settings)
|
copyfile(php_file, temp_settings)
|
||||||
sp_importer._convert_php_settings_if_needed(temp_settings)
|
sp_importer._convert_php_settings_if_needed(temp_settings)
|
||||||
|
|
||||||
assert (Path(temp_dir) / 'phrase_settings.json').is_file()
|
assert (tmp_path / 'phrase_settings.json').is_file()
|
||||||
|
|
||||||
def test_convert_settings_wrong_file(sp_importer):
|
def test_convert_settings_wrong_file(sp_importer):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user