1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 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.api.v1 as api_impl
14 from nominatim.api import StatusResult
15 from nominatim.version import NOMINATIM_VERSION
17 STATUS_FORMATS = {'text', 'json'}
21 def test_status_format_list():
22 assert set(api_impl.list_formats(StatusResult)) == STATUS_FORMATS
25 @pytest.mark.parametrize('fmt', list(STATUS_FORMATS))
26 def test_status_supported(fmt):
27 assert api_impl.supports_format(StatusResult, fmt)
30 def test_status_unsupported():
31 assert not api_impl.supports_format(StatusResult, 'gagaga')
34 def test_status_format_text():
35 assert api_impl.format_result(StatusResult(0, 'message here'), 'text', {}) == 'OK'
38 def test_status_format_text():
39 assert api_impl.format_result(StatusResult(500, 'message here'), 'text', {}) == 'ERROR: message here'
42 def test_status_format_json_minimal():
43 status = StatusResult(700, 'Bad format.')
45 result = api_impl.format_result(status, 'json', {})
47 assert result == '{"status":700,"message":"Bad format.","software_version":"%s"}' % (NOMINATIM_VERSION, )
50 def test_status_format_json_full():
51 status = StatusResult(0, 'OK')
52 status.data_updated = dt.datetime(2010, 2, 7, 20, 20, 3, 0, tzinfo=dt.timezone.utc)
53 status.database_version = '5.6'
55 result = api_impl.format_result(status, 'json', {})
57 assert result == '{"status":0,"message":"OK","data_updated":"2010-02-07T20:20:03+00:00","software_version":"%s","database_version":"5.6"}' % (NOMINATIM_VERSION, )