This repository was archived by the owner on Feb 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_fields_mitigate_collision.py
More file actions
81 lines (64 loc) · 2.84 KB
/
Copy pathtest_fields_mitigate_collision.py
File metadata and controls
81 lines (64 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import proto
import pytest
# Underscores may be appended to field names
# that collide with python or proto-plus keywords.
# In case a key only exists with a `_` suffix, coerce the key
# to include the `_` suffix. It's not possible to
# natively define the same field with a trailing underscore in protobuf.
# See related issue
# https://github.com/googleapis/python-api-core/issues/227
def test_fields_mitigate_collision():
class TestMessage(proto.Message):
spam_ = proto.Field(proto.STRING, number=1)
eggs = proto.Field(proto.STRING, number=2)
class TextStream(proto.Message):
text_stream = proto.Field(TestMessage, number=1)
obj = TestMessage(spam_="has_spam")
obj.eggs = "has_eggs"
assert obj.spam_ == "has_spam"
# Test that `spam` is coerced to `spam_`
modified_obj = TestMessage({"spam": "has_spam", "eggs": "has_eggs"})
assert modified_obj.spam_ == "has_spam"
# Test get and set
modified_obj.spam = "no_spam"
assert modified_obj.spam == "no_spam"
modified_obj.spam_ = "yes_spam"
assert modified_obj.spam_ == "yes_spam"
modified_obj.spam = "maybe_spam"
assert modified_obj.spam_ == "maybe_spam"
modified_obj.spam_ = "maybe_not_spam"
assert modified_obj.spam == "maybe_not_spam"
# Try nested values
modified_obj = TextStream(
text_stream=TestMessage({"spam": "has_spam", "eggs": "has_eggs"})
)
assert modified_obj.text_stream.spam_ == "has_spam"
# Test get and set for nested values
modified_obj.text_stream.spam = "no_spam"
assert modified_obj.text_stream.spam == "no_spam"
modified_obj.text_stream.spam_ = "yes_spam"
assert modified_obj.text_stream.spam_ == "yes_spam"
modified_obj.text_stream.spam = "maybe_spam"
assert modified_obj.text_stream.spam_ == "maybe_spam"
modified_obj.text_stream.spam_ = "maybe_not_spam"
assert modified_obj.text_stream.spam == "maybe_not_spam"
with pytest.raises(AttributeError):
assert modified_obj.text_stream.attribute_does_not_exist == "n/a"
with pytest.raises(AttributeError):
modified_obj.text_stream.attribute_does_not_exist = "n/a"
# Try using dict
modified_obj = TextStream(text_stream={"spam": "has_spam", "eggs": "has_eggs"})
assert modified_obj.text_stream.spam_ == "has_spam"