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_snippet_index.py
More file actions
229 lines (173 loc) · 7.78 KB
/
test_snippet_index.py
File metadata and controls
229 lines (173 loc) · 7.78 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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
#
# http://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 json
from google.protobuf import json_format
import pytest
from gapic.samplegen_utils import snippet_metadata_pb2
from gapic.samplegen_utils import snippet_index, types
from common_types import DummyApiSchema, DummyService, DummyMethod
@pytest.fixture
def sample_str():
return """# [START mollusc_classify_sync]
from molluscs.v1 import molluscclient
def sample_classify(video, location):
# Create a client
client = molluscclient.MolluscServiceClient()
# Initialize request argument(s)
classify_target = molluscclient.ClassifyTarget()
# video = "path/to/mollusc/video.mkv"
with open(video, "rb") as f:
classify_target.video = f.read()
# location = "New Zealand"
classify_target.location_annotation = location
request = molluscclient.molluscs.v1.ClassifyRequest(
classify_target=classify_target,
)
# Make the request
response = client.classify(request=request)
# Handle response
print(f"Mollusc is a \"{response.taxonomy}\"")
# [END mollusc_classify_sync]"""
def test_snippet_init(sample_str):
# We are not trying to exhaustively test the snippet metadata protobuf,
# just checking that fields are not unset
sample_metadata = snippet_metadata_pb2.Snippet(title="classify_squid.py")
sample_metadata.language = snippet_metadata_pb2.Language.PYTHON
snippet = snippet_index.Snippet(sample_str, sample_metadata)
assert snippet.sample_str == sample_str
# It's easier to eyeball diffs on the dictionary representation
assert json_format.MessageToDict(snippet.metadata) == {
"language": "PYTHON",
"title": "classify_squid.py",
"segments": [
{"end": 28, "start": 2, "type": "FULL"},
{"end": 28, "start": 2, "type": "SHORT"},
{"end": 8, "start": 6, "type": "CLIENT_INITIALIZATION"},
{"end": 22, "start": 9, "type": "REQUEST_INITIALIZATION"},
{"end": 25, "start": 23, "type": "REQUEST_EXECUTION"},
{"end": 29, "start": 26, "type": "RESPONSE_HANDLING"},
]
}
# This is the same as the sample_str above, minus the # [START ...]
# and # [END ...] lines
expected_full_snipppet = """from molluscs.v1 import molluscclient
def sample_classify(video, location):
# Create a client
client = molluscclient.MolluscServiceClient()
# Initialize request argument(s)
classify_target = molluscclient.ClassifyTarget()
# video = "path/to/mollusc/video.mkv"
with open(video, "rb") as f:
classify_target.video = f.read()
# location = "New Zealand"
classify_target.location_annotation = location
request = molluscclient.molluscs.v1.ClassifyRequest(
classify_target=classify_target,
)
# Make the request
response = client.classify(request=request)
# Handle response
print(f"Mollusc is a \"{response.taxonomy}\"")
"""
assert snippet.full_snippet == expected_full_snipppet
def test_add_snippet_no_matching_service(sample_str):
snippet_metadata = snippet_metadata_pb2.Snippet(
)
snippet_metadata.client_method.method.service.short_name = "Clam"
snippet = snippet_index.Snippet(sample_str, snippet_metadata)
# No 'Clam' service in API Schema
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(name="Squid", methods={})}
))
with pytest.raises(types.UnknownService):
index.add_snippet(snippet)
def test_add_snippet_no_matching_rpc(sample_str):
snippet_metadata = snippet_metadata_pb2.Snippet(
)
snippet_metadata.client_method.method.service.short_name = "Squid"
snippet_metadata.client_method.full_name = "classify"
snippet = snippet_index.Snippet(sample_str, snippet_metadata)
# No 'classify' method in 'Squid' service
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(name="Squid", methods={"list": None})}
))
with pytest.raises(types.RpcMethodNotFound):
index.add_snippet(snippet)
def test_get_snippet_no_matching_service():
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(
name="Squid", methods={"classify": DummyMethod()})}
))
# No 'Clam' service in API Schema
with pytest.raises(types.UnknownService):
index.get_snippet(service_name="Clam", rpc_name="classify")
def test_get_snippet_no_matching_rpc():
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(
name="Squid", methods={"classify": DummyMethod()})}
))
# No 'list' RPC in 'Squid' service
with pytest.raises(types.RpcMethodNotFound):
index.get_snippet(service_name="Squid", rpc_name="list")
def test_add_and_get_snippet_sync(sample_str):
snippet_metadata = snippet_metadata_pb2.Snippet()
snippet_metadata.client_method.method.service.short_name = "Squid"
snippet_metadata.client_method.method.full_name = "classify"
snippet = snippet_index.Snippet(sample_str, snippet_metadata)
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(
name="Squid", methods={"classify": DummyMethod()})}
))
index.add_snippet(snippet)
index.get_snippet(service_name="Squid", rpc_name="classify")
def test_add_and_get_snippet_async(sample_str):
snippet_metadata = snippet_metadata_pb2.Snippet()
snippet_metadata.client_method.method.service.short_name = "Squid"
snippet_metadata.client_method.method.full_name = "classify"
setattr(snippet_metadata.client_method, "async", True)
snippet = snippet_index.Snippet(sample_str, snippet_metadata)
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(
name="Squid", methods={"classify": DummyMethod()})}
))
index.add_snippet(snippet)
index.get_snippet(service_name="Squid", rpc_name="classify", sync=False)
def test_get_metadata_json(sample_str):
snippet_metadata = snippet_metadata_pb2.Snippet()
snippet_metadata.client_method.method.service.short_name = "Squid"
snippet_metadata.client_method.method.full_name = "classify"
snippet = snippet_index.Snippet(sample_str, snippet_metadata)
index = snippet_index.SnippetIndex(api_schema=DummyApiSchema(
services={"Squid": DummyService(
name="Squid", methods={"classify": DummyMethod()})}
))
index.add_snippet(snippet)
assert json.loads(index.get_metadata_json()) == {
'snippets': [{'clientMethod': {'method': {'fullName': 'classify',
'service': {'shortName': 'Squid'}}},
'segments': [{'end': 28, 'start': 2, 'type': 'FULL'},
{'end': 28, 'start': 2, 'type': 'SHORT'},
{'end': 8,
'start': 6,
'type': 'CLIENT_INITIALIZATION'},
{'end': 22,
'start': 9,
'type': 'REQUEST_INITIALIZATION'},
{'end': 25,
'start': 23,
'type': 'REQUEST_EXECUTION'},
{'end': 29,
'start': 26,
'type': 'RESPONSE_HANDLING'}]}]
}