]> git.openstreetmap.org Git - nominatim.git/blob - test/python/cli/conftest.py
generalize fixtures for cli 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
23 @pytest.fixture
24 def cli_call(src_dir):
25     """ Call the nominatim main function with the correct paths set.
26         Returns a function that can be called with the desired CLI arguments.
27     """
28     def _call_nominatim(*args):
29         return nominatim.cli.nominatim(module_dir='MODULE NOT AVAILABLE',
30                                        osm2pgsql_path='OSM2PGSQL NOT AVAILABLE',
31                                        phplib_dir=str(src_dir / 'lib-php'),
32                                        data_dir=str(src_dir / 'data'),
33                                        phpcgi_path='/usr/bin/php-cgi',
34                                        sqllib_dir=str(src_dir / 'lib-sql'),
35                                        config_dir=str(src_dir / 'settings'),
36                                        cli_args=args)
37
38     return _call_nominatim
39
40
41 @pytest.fixture
42 def mock_run_legacy(monkeypatch):
43     mock = MockParamCapture()
44     monkeypatch.setattr(nominatim.cli, 'run_legacy_script', mock)
45     return mock
46
47
48 @pytest.fixture
49 def mock_func_factory(monkeypatch):
50     def get_mock(module, func):
51         mock = MockParamCapture()
52         mock.func_name = func
53         monkeypatch.setattr(module, func, mock)
54         return mock
55
56     return get_mock