1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for formatting results for the V1 API.
13 import nominatim.result_formatter.v1 as format_module
14 from nominatim.apicmd.status import StatusResult
15 from nominatim.version import version_str
17 STATUS_FORMATS = {'text', 'json'}
19 class TestStatusResultFormat:
22 @pytest.fixture(autouse=True)
23 def make_formatter(self):
24 self.formatter = format_module.create(StatusResult)
27 def test_format_list(self):
28 assert set(self.formatter.list_formats()) == STATUS_FORMATS
31 @pytest.mark.parametrize('fmt', list(STATUS_FORMATS))
32 def test_supported(self, fmt):
33 assert self.formatter.supports_format(fmt)
36 def test_unsupported(self):
37 assert not self.formatter.supports_format('gagaga')
40 def test_format_text(self):
41 assert self.formatter.format(StatusResult(0, 'message here'), 'text') == 'OK'
44 def test_format_text(self):
45 assert self.formatter.format(StatusResult(500, 'message here'), 'text') == 'ERROR: message here'
48 def test_format_json_minimal(self):
49 status = StatusResult(700, 'Bad format.')
51 result = self.formatter.format(status, 'json')
53 assert result == '{"status": 700, "message": "Bad format.", "software_version": "%s"}' % (version_str())
56 def test_format_json_full(self):
57 status = StatusResult(0, 'OK')
58 status.data_updated = dt.datetime(2010, 2, 7, 20, 20, 3, 0, tzinfo=dt.timezone.utc)
59 status.database_version = '5.6'
61 result = self.formatter.format(status, 'json')
63 assert result == '{"status": 0, "message": "OK", "data_updated": "2010-02-07T20:20:03+00:00", "software_version": "%s", "database_version": "5.6"}' % (version_str())