|
| 1 | +# Copyright 2024 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import json |
| 19 | +from google.cloud.spanner_v1.data_types import JsonObject |
| 20 | + |
| 21 | + |
| 22 | +class Test_JsonObject_serde(unittest.TestCase): |
| 23 | + def test_w_dict(self): |
| 24 | + data = {"foo": "bar"} |
| 25 | + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) |
| 26 | + data_jsonobject = JsonObject(data) |
| 27 | + self.assertEqual(data_jsonobject.serialize(), expected) |
| 28 | + |
| 29 | + def test_w_list_of_dict(self): |
| 30 | + data = [{"foo1": "bar1"}, {"foo2": "bar2"}] |
| 31 | + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) |
| 32 | + data_jsonobject = JsonObject(data) |
| 33 | + self.assertEqual(data_jsonobject.serialize(), expected) |
| 34 | + |
| 35 | + def test_w_JsonObject_of_dict(self): |
| 36 | + data = {"foo": "bar"} |
| 37 | + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) |
| 38 | + data_jsonobject = JsonObject(JsonObject(data)) |
| 39 | + self.assertEqual(data_jsonobject.serialize(), expected) |
| 40 | + |
| 41 | + def test_w_JsonObject_of_list_of_dict(self): |
| 42 | + data = [{"foo1": "bar1"}, {"foo2": "bar2"}] |
| 43 | + expected = json.dumps(data, sort_keys=True, separators=(",", ":")) |
| 44 | + data_jsonobject = JsonObject(JsonObject(data)) |
| 45 | + self.assertEqual(data_jsonobject.serialize(), expected) |
0 commit comments