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