This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathtest_options.py
More file actions
176 lines (144 loc) · 5.27 KB
/
test_options.py
File metadata and controls
176 lines (144 loc) · 5.27 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright 2019 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 os
import pytest
import re
from unittest import mock
import warnings
from gapic.samplegen_utils import types
from gapic.utils import Options
def test_options_empty():
opts = Options.build('')
assert len(opts.templates) == 1
assert opts.templates[0].endswith('gapic/templates')
assert not opts.lazy_import
assert not opts.old_naming
def test_options_replace_templates():
opts = Options.build('python-gapic-templates=/foo/')
assert len(opts.templates) == 1
assert opts.templates[0] == '/foo'
def test_options_relative_templates():
opts = Options.build('python-gapic-templates=../../squid/clam')
expected = (os.path.abspath('../squid/clam'),)
assert opts.templates == expected
def test_options_unrecognized():
with mock.patch.object(warnings, 'warn') as warn:
Options.build('python-gapic-abc=xyz')
warn.assert_called_once_with('Unrecognized option: `python-gapic-abc`.')
def test_flags_unrecognized():
with mock.patch.object(warnings, 'warn') as warn:
Options.build('python-gapic-abc')
warn.assert_called_once_with('Unrecognized option: `python-gapic-abc`.')
def test_options_unrecognized_likely_typo():
with mock.patch.object(warnings, 'warn') as warn:
Options.build('go-gapic-abc=xyz')
assert len(warn.mock_calls) == 0
def test_options_trim_whitespace():
# When writing shell scripts, users may construct options strings with
# whitespace that needs to be trimmed after tokenizing.
opts = Options.build(
'''
python-gapic-templates=/squid/clam/whelk ,
python-gapic-name=mollusca ,
''')
assert opts.templates[0] == '/squid/clam/whelk'
assert opts.name == 'mollusca'
def test_options_no_valid_sample_config(fs):
fs.create_file("sampledir/not_a_config.yaml")
with pytest.raises(types.InvalidConfig):
Options.build("samples=sampledir/")
def test_options_service_config(fs):
opts = Options.build("")
assert opts.retry is None
# Default of None is okay, verify build can read a config.
service_config_fpath = "service_config.json"
fs.create_file(service_config_fpath, contents="""{
"methodConfig": [
{
"name": [
{
"service": "animalia.mollusca.v1beta1.Cephalopod",
"method": "IdentifySquid"
}
],
"retryPolicy": {
"maxAttempts": 5,
"maxBackoff": "3s",
"initialBackoff": "0.2s",
"backoffMultiplier": 2,
"retryableStatusCodes": [
"UNAVAILABLE",
"UNKNOWN"
]
},
"timeout": "5s"
}
]
}""")
opt_string = f"retry-config={service_config_fpath}"
opts = Options.build(opt_string)
# Verify the config was read in correctly.
expected_cfg = {
"methodConfig": [
{
"name": [
{
"service": "animalia.mollusca.v1beta1.Cephalopod",
"method": "IdentifySquid",
}
],
"retryPolicy": {
"maxAttempts": 5,
"maxBackoff": "3s",
"initialBackoff": "0.2s",
"backoffMultiplier": 2,
"retryableStatusCodes":
[
"UNAVAILABLE",
"UNKNOWN"
]
},
"timeout":"5s"
}
]
}
assert opts.retry == expected_cfg
def test_options_bool_flags():
# Most options are default False.
# New options should follow the dash-case/snake_case convention.
opt_str_to_attr_name = {
name: re.sub(r"-", "_", name)
for name in
["lazy-import",
"old-naming",
"add-iam-methods",
"metadata",
"warehouse-package-name",
]}
for opt, attr in opt_str_to_attr_name.items():
options = Options.build("")
assert not getattr(options, attr)
options = Options.build(opt)
assert getattr(options, attr)
# Check autogen-snippets separately, as it is default True
options = Options.build("")
assert options.autogen_snippets
options = Options.build("autogen-snippets=False")
assert not options.autogen_snippets
def test_options_autogen_snippets_false_for_old_naming():
# NOTE: Snippets are not currently correct for the alternative (Ads) templates
# so always disable snippetgen in that case
# https://github.com/googleapis/gapic-generator-python/issues/1052
options = Options.build("old-naming")
assert not options.autogen_snippets