]> git.openstreetmap.org Git - nominatim.git/blob - test/python/api/test_result_formatting_v1.py
95472916926bd4177b6f4fe6c5b0f40768b24048
[nominatim.git] / test / python / api / test_result_formatting_v1.py
1 # SPDX-License-Identifier: GPL-2.0-only
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 Tests for formatting results for the V1 API.
9 """
10 import datetime as dt
11 import pytest
12
13 import nominatim.api.v1 as api_impl
14 from nominatim.api import StatusResult
15 from nominatim.version import NOMINATIM_VERSION
16
17 STATUS_FORMATS = {'text', 'json'}
18
19 # StatusResult
20
21 def test_status_format_list():
22     assert set(api_impl.list_formats(StatusResult)) == STATUS_FORMATS
23
24
25 @pytest.mark.parametrize('fmt', list(STATUS_FORMATS))
26 def test_status_supported(fmt):
27     assert api_impl.supports_format(StatusResult, fmt)
28
29
30 def test_status_unsupported():
31     assert not api_impl.supports_format(StatusResult, 'gagaga')
32
33
34 def test_status_format_text():
35     assert api_impl.format_result(StatusResult(0, 'message here'), 'text') == 'OK'
36
37
38 def test_status_format_text():
39     assert api_impl.format_result(StatusResult(500, 'message here'), 'text') == 'ERROR: message here'
40
41
42 def test_status_format_json_minimal():
43     status = StatusResult(700, 'Bad format.')
44
45     result = api_impl.format_result(status, 'json')
46
47     assert result == '{"status": 700, "message": "Bad format.", "software_version": "%s"}' % (NOMINATIM_VERSION, )
48
49
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'
54
55     result = api_impl.format_result(status, 'json')
56
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, )