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 assert rest_helpers.flatten_query_params({"a": "abc", "b": "def"}) == [ 40 ("a", "abc"), 41 ("b", "def"), 42 ] 43 44 45def test_flatten_repeated_field(): 46 assert rest_helpers.flatten_query_params({"a": ["x", "y", "z", None]}) == [ 47 ("a", "x"), 48 ("a", "y"), 49 ("a", "z"), 50 ] 51 52 53def test_flatten_nested_dict(): 54 obj = {"a": {"b": {"c": ["x", "y", "z"]}}, "d": {"e": "uvw"}} 55 expected_result = [("a.b.c", "x"), ("a.b.c", "y"), ("a.b.c", "z"), ("d.e", "uvw")] 56 57 assert rest_helpers.flatten_query_params(obj) == expected_result 58 59 60def test_flatten_repeated_dict(): 61 obj = { 62 "a": {"b": {"c": [{"v": 1}, {"v": 2}]}}, 63 "d": "uvw", 64 } 65 66 with pytest.raises(ValueError): 67 rest_helpers.flatten_query_params(obj) 68 69 70def test_flatten_repeated_list(): 71 obj = { 72 "a": {"b": {"c": [["e", "f"], ["g", "h"]]}}, 73 "d": "uvw", 74 } 75 76 with pytest.raises(ValueError): 77 rest_helpers.flatten_query_params(obj) 78