]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/conftest.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / cli / conftest.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 import pytest
8
9 import nominatim.cli
10
11 class MockParamCapture:
12     """ Mock that records the parameters with which a function was called
13         as well as the number of calls.
14     """
15     def __init__(self, retval=0):
16         self.called = 0
17         self.return_value = retval
18         self.last_args = None
19         self.last_kwargs = None
20
21     def __call__(self, *args, **kwargs):
22         self.called += 1
23         self.last_args = args
24         self.last_kwargs = kwargs
25         return self.return_value
26
27
28 class DummyTokenizer:
29     def __init__(self, *args, **kwargs):
30         self.update_sql_functions_called = False
31         self.finalize_import_called = False
32         self.update_statistics_called = False
33         self.update_word_tokens_called = False
34
35     def update_sql_functions(self, *args):
36         self.update_sql_functions_called = True
37
38     def finalize_import(self, *args):
39         self.finalize_import_called = True
40
41     def update_statistics(self):
42         self.update_statistics_called = True
43
44     def update_word_tokens(self):
45         self.update_word_tokens_called = True
46
47
48 @pytest.fixture
49 def cli_call(src_dir):
50     """ Call the nominatim main function with the correct paths set.
51         Returns a function that can be called with the desired CLI arguments.
52     """
53     def _call_nominatim(*args):
54         return nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
55                                        osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
56                                        phplib_dir=str(src_dir / 'lib-php'),
57                                        data_dir=str(src_dir / 'data'),
58                                        phpcgi_path='/usr/bin/php-cgi',
59                                        sqllib_dir=str(src_dir / 'lib-sql'),
60                                        config_dir=str(src_dir / 'settings'),
61                                        cli_args=args)
62
63     return _call_nominatim
64
65
66 @pytest.fixture
67 def mock_run_legacy(monkeypatch):
68     mock = MockParamCapture()
69     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
70     return mock
71
72
73 @pytest.fixture
74 def mock_func_factory(monkeypatch):
75     def get_mock(module, func):
76         mock = MockParamCapture()
77         mock.func_name = func
78         monkeypatch.setattr(module, func, mock)
79         return mock
80
81     return get_mock
82
83
84 @pytest.fixture
85 def cli_tokenizer_mock(monkeypatch):
86     tok = DummyTokenizer()
87     monkeypatch.setattr(nominatim.tokenizer.factory, 'get_tokenizer_for_db',
88                         lambda *args: tok)
89     monkeypatch.setattr(nominatim.tokenizer.factory, 'create_tokenizer',
90                         lambda *args: tok)
91
92     return tok