1# Copyright 2021 Google LLC 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 15import pytest 16 17from google.api_core import rest_helpers 18 19 20def test_flatten_simple_value(): 21 with pytest.raises(TypeError): 22 rest_helpers.flatten_query_params("abc") 23 24 25def test_flatten_list(): 26 with pytest.raises(TypeError): 27 rest_helpers.flatten_query_params(["abc", "def"]) 28 29 30def test_flatten_none(): 31 assert rest_helpers.flatten_query_params(None) == [] 32 33 34def test_flatten_empty_dict(): 35 assert rest_helpers.flatten_query_params({}) == [] 36 37 38def test_flatten_simple_dict(): 39 obj = {"a": "abc", "b": "def", "c": True, "d": False, "e": 10, "f": -3.76} 40 assert rest_helpers.flatten_query_params(obj) == [ 41 ("a", "abc"), 42 ("b", "def"), 43 ("c", True), 44 ("d", False), 45 ("e", 10), 46 ("f", -3.76), 47 ] 48 49 50def test_flatten_simple_dict_strict(): 51 obj = {"a": "abc", "b": "def", "c": True, "d": False, "e": 10, "f": -3.76} 52 assert rest_helpers.flatten_query_params(obj, strict=True) == [ 53 ("a", "abc"), 54 ("b", "def"), 55 ("c", "true"), 56 ("d", "false"), 57 ("e", "10"), 58 ("f", "-3.76"), 59 ] 60 61 62def test_flatten_repeated_field(): 63 assert rest_helpers.flatten_query_params({"a": ["x", "y", "z", None]}) == [ 64 ("a", "x"), 65 ("a", "y"), 66 ("a", "z"), 67 ] 68 69 70def test_flatten_nested_dict(): 71 obj = {"a": {"b": {"c": ["x", "y", "z"]}}, "d": {"e": "uvw"}} 72 expected_result = [("a.b.c", "x"), ("a.b.c", "y"), ("a.b.c", "z"), ("d.e", "uvw")] 73 74 assert rest_helpers.flatten_query_params(obj) == expected_result 75 76 77def test_flatten_repeated_dict(): 78 obj = { 79 "a": {"b": {"c": [{"v": 1}, {"v": 2}]}}, 80 "d": "uvw", 81 } 82 83 with pytest.raises(ValueError): 84 rest_helpers.flatten_query_params(obj) 85 86 87def test_flatten_repeated_list(): 88 obj = { 89 "a": {"b": {"c": [["e", "f"], ["g", "h"]]}}, 90 "d": "uvw", 91 } 92 93 with pytest.raises(ValueError): 94 rest_helpers.flatten_query_params(obj) 95