Skip to content

Commit

Permalink
fix: samplegen always produces sample dicts with "response" (#914)
Browse files Browse the repository at this point in the history
- fix samplegen logic to always set a `sample[response]`
- enable `autogen-snippets` for the golden integration tests that are run through Bazel
- install some extra stub packages for mypy that are needed with the new release
  • Loading branch information
busunkim96 authored Jun 9, 2021
1 parent 5225b67 commit 0b168f2
Show file tree
Hide file tree
Showing 72 changed files with 2,826 additions and 17 deletions.
7 changes: 5 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ config_setting(
py_runtime(
name = "pyenv3_runtime",
interpreter = ":pyenv3wrapper.sh",
python_version="PY3",
python_version = "PY3",
)

py_runtime_pair(
Expand All @@ -52,7 +52,10 @@ toolchain(
py_binary(
name = "gapic_plugin",
srcs = glob(["gapic/**/*.py"]),
data = [":pandoc_binary"] + glob(["gapic/**/*.j2", "gapic/**/.*.j2"]),
data = [":pandoc_binary"] + glob([
"gapic/**/*.j2",
"gapic/**/.*.j2",
]),
main = "gapic/cli/generate_with_pandoc.py",
python_version = "PY3",
visibility = ["//visibility:public"],
Expand Down
22 changes: 16 additions & 6 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ Execute unit tests by running one of the sessions prefixed with `unit-`.

- Run a single integration test for one API. This generates Python source code
with the microgenerator and compares them to the golden files in
`test/integration/goldens/asset`.
`tests/integration/goldens/asset`.

```sh
bazel test //test/integration:asset
bazel test //tests/integration:asset
```

- Update goldens files. This overwrites the golden files in
`test/integration/goldens/asset`.
- Run integration tests for all APIs.

```sh
bazel run //test/integration:asset_update
```
bazel test //tests/integration:all
```

- Update all goldens files. This overwrites the golden files in
`tests/integration/goldens/`.

```sh
bazel run //tests/integration:asset_update
bazel run //tests/integration:credentials_update
bazel run //tests/integration:logging_update
bazel run //tests/integration:redis_update
```

3 changes: 3 additions & 0 deletions gapic/samplegen/samplegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ def preprocess_sample(sample, api_schema: api.API, rpc: wrappers.Method):
# Add reasonable defaults depending on the type of the sample
if not rpc.void:
sample.setdefault("response", [{"print": ["%s", "$resp"]}])
else:
sample.setdefault("response", [])

@utils.cached_property
def flattenable_fields(self) -> FrozenSet[str]:
Expand Down Expand Up @@ -1000,6 +1002,7 @@ def generate_sample(sample, api_schema, sample_template: jinja2.Template) -> str
sample["request"] = v.validate_and_transform_request(
calling_form, sample["request"]
)

v.validate_response(sample["response"])

return sample_template.render(
Expand Down
2 changes: 2 additions & 0 deletions gapic/templates/examples/feature_fragments.j2
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,12 @@ client.{{ sample.rpc|snake_case }}({{ render_request_params_unary(sample.request
{% if calling_form == calling_form_enum.Request %}
response = {{ method_invocation_text|trim }}

{% if response_statements %}
# Handle response
{% for statement in response_statements %}
{{ dispatch_statement(statement)|trim }}
{% endfor %}
{% endif %}
{% elif calling_form == calling_form_enum.RequestPagedAll %}
page_result = {{ method_invocation_text|trim }}
for response in page_result:
Expand Down
2 changes: 1 addition & 1 deletion gapic/templates/noxfile.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def cover(session):
@nox.session(python=['3.6', '3.7'])
def mypy(session):
"""Run the type checker."""
session.install('mypy')
session.install('mypy', 'types-pkg_resources')
session.install('.')
session.run(
'mypy',
Expand Down
9 changes: 7 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def showcase_mypy(
"""Perform typecheck analysis on the generated Showcase library."""

# Install pytest and gapic-generator-python
session.install("mypy")
session.install("mypy", "types-pkg-resources")

with showcase_library(session, templates=templates, other_opts=other_opts) as lib:
session.chdir(lib)
Expand Down Expand Up @@ -340,6 +340,11 @@ def docs(session):
def mypy(session):
"""Perform typecheck analysis."""

session.install("mypy")
session.install(
"mypy",
"types-protobuf",
"types-PyYAML",
"types-dataclasses"
)
session.install(".")
session.run("mypy", "gapic")
10 changes: 10 additions & 0 deletions tests/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,19 @@ py_gapic_library(
name = "asset_py_gapic",
srcs = ["@com_google_googleapis//google/cloud/asset/v1:asset_proto"],
grpc_service_config = "cloudasset_grpc_service_config.json",
opt_args = [
"autogen-snippets",
],
)

# Credentials.
py_gapic_library(
name = "credentials_py_gapic",
srcs = ["@com_google_googleapis//google/iam/credentials/v1:credentials_proto"],
grpc_service_config = "iamcredentials_grpc_service_config.json",
opt_args = [
"autogen-snippets",
],
)

# Logging.
Expand All @@ -68,11 +74,15 @@ py_gapic_library(
opt_args = [
"python-gapic-namespace=google.cloud",
"python-gapic-name=logging",
"autogen-snippets",
],
)

py_gapic_library(
name = "redis_py_gapic",
srcs = ["@com_google_googleapis//google/cloud/redis/v1:redis_proto"],
grpc_service_config = "redis_grpc_service_config.json",
opt_args = [
"autogen-snippets",
],
)
2 changes: 1 addition & 1 deletion tests/integration/goldens/asset/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def cover(session):
@nox.session(python=['3.6', '3.7'])
def mypy(session):
"""Run the type checker."""
session.install('mypy')
session.install('mypy', 'types-pkg_resources')
session.install('.')
session.run(
'mypy',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for AnalyzeIamPolicy
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
from google.cloud import asset_v1


def sample_analyze_iam_policy():
"""Snippet for analyze_iam_policy"""

# Create a client
client = asset_v1.AssetServiceClient()

# Initialize request argument(s)
request = asset_v1.AnalyzeIamPolicyRequest(
)

# Make the request
response = client.analyze_iam_policy(request=request)

# Handle response
print("{}".format(response))

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicy_grpc]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for AnalyzeIamPolicyLongrunning
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
from google.cloud import asset_v1


def sample_analyze_iam_policy_longrunning():
"""Snippet for analyze_iam_policy_longrunning"""

# Create a client
client = asset_v1.AssetServiceClient()

# Initialize request argument(s)
request = asset_v1.AnalyzeIamPolicyLongrunningRequest(
)

# Make the request
operation = client.analyze_iam_policy_longrunning(request=request)

print("Waiting for operation to complete...")

response = operation.result()
print("{}".format(response))

# [END cloudasset_generated_asset_v1_AssetService_AnalyzeIamPolicyLongrunning_grpc]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for BatchGetAssetsHistory
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_grpc]
from google.cloud import asset_v1


def sample_batch_get_assets_history():
"""Snippet for batch_get_assets_history"""

# Create a client
client = asset_v1.AssetServiceClient()

# Initialize request argument(s)
request = asset_v1.BatchGetAssetsHistoryRequest(
)

# Make the request
response = client.batch_get_assets_history(request=request)

# Handle response
print("{}".format(response))

# [END cloudasset_generated_asset_v1_AssetService_BatchGetAssetsHistory_grpc]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for CreateFeed
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_CreateFeed_grpc]
from google.cloud import asset_v1


def sample_create_feed():
"""Snippet for create_feed"""

# Create a client
client = asset_v1.AssetServiceClient()

# Initialize request argument(s)
request = asset_v1.CreateFeedRequest(
)

# Make the request
response = client.create_feed(request=request)

# Handle response
print("{}".format(response))

# [END cloudasset_generated_asset_v1_AssetService_CreateFeed_grpc]
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for DeleteFeed
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install google-cloud-asset


# [START cloudasset_generated_asset_v1_AssetService_DeleteFeed_grpc]
from google.cloud import asset_v1


def sample_delete_feed():
"""Snippet for delete_feed"""

# Create a client
client = asset_v1.AssetServiceClient()

# Initialize request argument(s)
request = asset_v1.DeleteFeedRequest(
)

# Make the request
response = client.delete_feed(request=request)


# [END cloudasset_generated_asset_v1_AssetService_DeleteFeed_grpc]
Loading

0 comments on commit 0b168f2

Please sign in to comment.