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