1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
9 import nominatim_db.cli
11 class MockParamCapture:
12 """ Mock that records the parameters with which a function was called
13 as well as the number of calls.
15 def __init__(self, retval=0):
17 self.return_value = retval
19 self.last_kwargs = None
21 def __call__(self, *args, **kwargs):
24 self.last_kwargs = kwargs
25 return self.return_value
28 class AsyncMockParamCapture:
29 """ Mock that records the parameters with which a function was called
30 as well as the number of calls.
32 def __init__(self, retval=0):
34 self.return_value = retval
36 self.last_kwargs = None
38 async def __call__(self, *args, **kwargs):
41 self.last_kwargs = kwargs
42 return self.return_value
46 def __init__(self, *args, **kwargs):
47 self.update_sql_functions_called = False
48 self.finalize_import_called = False
49 self.update_statistics_called = False
50 self.update_word_tokens_called = False
52 def update_sql_functions(self, *args, **kwargs):
53 self.update_sql_functions_called = True
55 def finalize_import(self, *args, **kwargs):
56 self.finalize_import_called = True
58 def update_statistics(self, *args, **kwargs):
59 self.update_statistics_called = True
61 def update_word_tokens(self, *args, **kwargs):
62 self.update_word_tokens_called = True
67 """ Call the nominatim main function with the correct paths set.
68 Returns a function that can be called with the desired CLI arguments.
70 def _call_nominatim(*args):
71 return nominatim_db.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
72 osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
75 return _call_nominatim
79 def mock_func_factory(monkeypatch):
80 def get_mock(module, func):
81 mock = MockParamCapture()
83 monkeypatch.setattr(module, func, mock)
90 def async_mock_func_factory(monkeypatch):
91 def get_mock(module, func):
92 mock = AsyncMockParamCapture()
94 monkeypatch.setattr(module, func, mock)
101 def cli_tokenizer_mock(monkeypatch):
102 tok = DummyTokenizer()
103 monkeypatch.setattr(nominatim_db.tokenizer.factory, 'get_tokenizer_for_db',
105 monkeypatch.setattr(nominatim_db.tokenizer.factory, 'create_tokenizer',