]> git.openstreetmap.org Git - nominatim.git/blob - test/python/utils/test_json_writer.py
make Python frontend default and PHP optional
[nominatim.git] / test / python / utils / test_json_writer.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 Tests for the streaming JSON writer.
9 """
10 import json
11
12 import pytest
13
14 from nominatim.utils.json_writer import JsonWriter
15
16 @pytest.mark.parametrize("inval,outstr", [(None, 'null'),
17                                           (True, 'true'), (False, 'false'),
18                                           (23, '23'), (0, '0'), (-1.3, '-1.3'),
19                                           ('g\nä', '"g\\nä"'), ('"', '"\\\""'),
20                                           ({}, '{}'), ([], '[]')])
21 def test_simple_value(inval, outstr):
22     writer = JsonWriter()
23     writer.value(inval)
24
25     assert writer() == outstr
26     json.loads(writer())
27
28
29 def test_empty_array():
30     writer = JsonWriter().start_array().end_array()
31
32     assert writer() == '[]'
33     json.loads(writer())
34
35
36 def test_array_with_single_value():
37     writer = JsonWriter().start_array().value(None).end_array()
38
39     assert writer() == '[null]'
40     json.loads(writer())
41
42
43 @pytest.mark.parametrize("invals,outstr", [((1, ), '[1]'),
44                                            (('a', 'b'), '["a","b"]')])
45 def test_array_with_data(invals, outstr):
46     writer = JsonWriter()
47
48     writer.start_array()
49     for ival in invals:
50         writer.value(ival).next()
51     writer.end_array()
52
53     assert writer() == outstr
54     json.loads(writer())
55
56
57 def test_empty_object():
58     writer = JsonWriter().start_object().end_object()
59
60     assert writer() == '{}'
61     json.loads(writer())
62
63
64 def test_object_single_entry():
65     writer = JsonWriter()\
66                 .start_object()\
67                     .key('something')\
68                     .value(5)\
69                 .end_object()
70
71     assert writer() == '{"something":5}'
72     json.loads(writer())
73
74 def test_object_many_values():
75     writer = JsonWriter()\
76                 .start_object()\
77                     .keyval('foo', None)\
78                     .keyval('bar', {})\
79                     .keyval('baz', 'b\taz')\
80                 .end_object()
81
82     assert writer() == '{"foo":null,"bar":{},"baz":"b\\taz"}'
83     json.loads(writer())
84
85 def test_object_many_values_without_none():
86     writer = JsonWriter()\
87                 .start_object()\
88                     .keyval_not_none('foo', 0)\
89                     .keyval_not_none('bar', None)\
90                     .keyval_not_none('baz', '')\
91                     .keyval_not_none('eve', False,
92                                      transform = lambda v: 'yes' if v else 'no')\
93                 .end_object()
94
95     assert writer() == '{"foo":0,"baz":"","eve":"no"}'
96     json.loads(writer())
97
98
99 def test_raw_output():
100     writer = JsonWriter()\
101                 .start_array()\
102                     .raw('{ "nicely": "formatted here" }').next()\
103                     .value(1)\
104                 .end_array()
105
106     assert writer() == '[{ "nicely": "formatted here" },1]'