|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import re |
| 17 | +from google.api_core.gapic_v1.method_helpers import setup_request_id |
| 18 | + |
| 19 | + |
| 20 | +def test_setup_request_id(): |
| 21 | + class MockRequest: |
| 22 | + def __init__(self, **kwargs): |
| 23 | + for k, v in kwargs.items(): |
| 24 | + setattr(self, k, v) |
| 25 | + |
| 26 | + def __contains__(self, key): |
| 27 | + return hasattr(self, key) |
| 28 | + |
| 29 | + class MockProtoRequest: |
| 30 | + def __init__(self, **kwargs): |
| 31 | + for k, v in kwargs.items(): |
| 32 | + setattr(self, k, v) |
| 33 | + |
| 34 | + def HasField(self, key): |
| 35 | + return hasattr(self, key) |
| 36 | + |
| 37 | + # Test with proto3 optional field not in request |
| 38 | + request = MockRequest() |
| 39 | + setup_request_id(request, "request_id", True) |
| 40 | + assert re.match( |
| 41 | + r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", |
| 42 | + request.request_id, |
| 43 | + ) |
| 44 | + |
| 45 | + # Test with proto3 optional field already in request |
| 46 | + request = MockRequest(request_id="already_set") |
| 47 | + setup_request_id(request, "request_id", True) |
| 48 | + assert request.request_id == "already_set" |
| 49 | + |
| 50 | + # Test with non-proto3 optional field empty |
| 51 | + request = MockRequest(request_id="") |
| 52 | + setup_request_id(request, "request_id", False) |
| 53 | + assert re.match( |
| 54 | + r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", |
| 55 | + request.request_id, |
| 56 | + ) |
| 57 | + |
| 58 | + # Test with non-proto3 optional field already set |
| 59 | + request = MockRequest(request_id="already_set") |
| 60 | + setup_request_id(request, "request_id", False) |
| 61 | + assert request.request_id == "already_set" |
| 62 | + |
| 63 | + # Test with proto3 optional field not in request (MockProtoRequest) |
| 64 | + request = MockProtoRequest() |
| 65 | + setup_request_id(request, "request_id", True) |
| 66 | + assert re.match( |
| 67 | + r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", |
| 68 | + request.request_id, |
| 69 | + ) |
| 70 | + |
| 71 | + # Test with proto3 optional field already in request (MockProtoRequest) |
| 72 | + request = MockProtoRequest(request_id="already_set") |
| 73 | + setup_request_id(request, "request_id", True) |
| 74 | + assert request.request_id == "already_set" |
| 75 | + |
| 76 | + # Test with ValueError |
| 77 | + class MockValueErrorRequest: |
| 78 | + def HasField(self, key): |
| 79 | + raise ValueError("Mismatched field") |
| 80 | + |
| 81 | + def __contains__(self, key): |
| 82 | + return hasattr(self, key) |
| 83 | + |
| 84 | + request = MockValueErrorRequest() |
| 85 | + setup_request_id(request, "request_id", True) |
| 86 | + assert re.match( |
| 87 | + r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", |
| 88 | + request.request_id, |
| 89 | + ) |
| 90 | + |
| 91 | + # Test with dict and proto3 optional field not in request |
| 92 | + request = {} |
| 93 | + setup_request_id(request, "request_id", True) |
| 94 | + assert re.match( |
| 95 | + r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", |
| 96 | + request["request_id"], |
| 97 | + ) |
| 98 | + |
| 99 | + # Test with dict and proto3 optional field already in request |
| 100 | + request = {"request_id": "already_set"} |
| 101 | + setup_request_id(request, "request_id", True) |
| 102 | + assert request["request_id"] == "already_set" |
| 103 | + |
| 104 | + # Test with dict and non-proto3 optional field empty |
| 105 | + request = {"request_id": ""} |
| 106 | + setup_request_id(request, "request_id", False) |
| 107 | + assert re.match( |
| 108 | + r"[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}", |
| 109 | + request["request_id"], |
| 110 | + ) |
| 111 | + |
| 112 | + # Test with dict and non-proto3 optional field already set |
| 113 | + request = {"request_id": "already_set"} |
| 114 | + setup_request_id(request, "request_id", False) |
| 115 | + assert request["request_id"] == "already_set" |
0 commit comments