]> 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-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 import pytest
8
9 import nominatim_db.cli
10
11
12 class MockParamCapture:
13     """ Mock that records the parameters with which a function was called
14         as well as the number of calls.
15     """
16     def __init__(self, retval=0):
17         self.called = 0
18         self.return_value = retval
19         self.last_args = None
20         self.last_kwargs = None
21
22     def __call__(self, *args, **kwargs):
23         self.called += 1
24         self.last_args = args
25         self.last_kwargs = kwargs
26         return self.return_value
27
28
29 class AsyncMockParamCapture:
30     """ Mock that records the parameters with which a function was called
31         as well as the number of calls.
32     """
33     def __init__(self, retval=0):
34         self.called = 0
35         self.return_value = retval
36         self.last_args = None
37         self.last_kwargs = None
38
39     async def __call__(self, *args, **kwargs):
40         self.called += 1
41         self.last_args = args
42         self.last_kwargs = kwargs
43         return self.return_value
44
45
46 class DummyTokenizer:
47     def __init__(self, *args, **kwargs):
48         self.update_sql_functions_called = False
49         self.finalize_import_called = False
50         self.update_statistics_called = False
51         self.update_word_tokens_called = False
52
53     def update_sql_functions(self, *args, **kwargs):
54         self.update_sql_functions_called = True
55
56     def finalize_import(self, *args, **kwargs):
57         self.finalize_import_called = True
58
59     def update_statistics(self, *args, **kwargs):
60         self.update_statistics_called = True
61
62     def update_word_tokens(self, *args, **kwargs):
63         self.update_word_tokens_called = True
64
65
66 @pytest.fixture
67 def cli_call():
68     """ Call the nominatim main function with the correct paths set.
69         Returns a function that can be called with the desired CLI arguments.
70     """
71     def _call_nominatim(*args):
72         return nominatim_db.cli.nominatim(cli_args=args)
73
74     return _call_nominatim
75
76
77 @pytest.fixture
78 def mock_func_factory(monkeypatch):
79     def get_mock(module, func):
80         mock = MockParamCapture()
81         mock.func_name = func
82         monkeypatch.setattr(module, func, mock)
83         return mock
84
85     return get_mock
86
87
88 @pytest.fixture
89 def async_mock_func_factory(monkeypatch):
90     def get_mock(module, func):
91         mock = AsyncMockParamCapture()
92         mock.func_name = func
93         monkeypatch.setattr(module, func, mock)
94         return mock
95
96     return get_mock
97
98
99 @pytest.fixture
100 def cli_tokenizer_mock(monkeypatch):
101     tok = DummyTokenizer()
102     monkeypatch.setattr(nominatim_db.tokenizer.factory, 'get_tokenizer_for_db',
103                         lambda *args: tok)
104     monkeypatch.setattr(nominatim_db.tokenizer.factory, 'create_tokenizer',
105                         lambda *args: tok)
106
107     return tok