]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/fake_adaptor.py
reintroduce file logging for Python frontend
[nominatim.git] / test / python / api / fake_adaptor.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) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Provides dummy implementations of ASGIAdaptor for testing.
9 """
10 from collections import namedtuple
11
12 import nominatim.api.v1.server_glue as glue
13 from nominatim.config import Configuration
14
15 class FakeError(BaseException):
16
17     def __init__(self, msg, status):
18         self.msg = msg
19         self.status = status
20
21     def __str__(self):
22         return f'{self.status} -- {self.msg}'
23
24 FakeResponse = namedtuple('FakeResponse', ['status', 'output', 'content_type'])
25
26 class FakeAdaptor(glue.ASGIAdaptor):
27
28     def __init__(self, params=None, headers=None, config=None):
29         self.params = params or {}
30         self.headers = headers or {}
31         self._config = config or Configuration(None)
32
33
34     def get(self, name, default=None):
35         return self.params.get(name, default)
36
37
38     def get_header(self, name, default=None):
39         return self.headers.get(name, default)
40
41
42     def error(self, msg, status=400):
43         return FakeError(msg, status)
44
45
46     def create_response(self, status, output, num_results):
47         return FakeResponse(status, output, self.content_type)
48
49
50     def config(self):
51         return self._config
52