From f4bc871b91d8d528031fbc6060f27e01871984ae Mon Sep 17 00:00:00 2001 From: Dov Shlachter Date: Tue, 28 Jan 2020 14:03:21 -0800 Subject: [PATCH] Provide 'types' and 'enums' submodule view (#273) This is a minor hack to preserve the module interface from the monocode: types and enums for a service live in distinct submodules. Includes tests for enums --- .../%namespace/%name_%version/%sub/enums.py.j2 | 13 +++++++++++++ .../%name_%version/%sub/types/__init__.py.j2 | 13 +++++++++++++ .../unit/%name_%version/%sub/test_%service.py.j2 | 12 ++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 gapic/templates/%namespace/%name_%version/%sub/enums.py.j2 create mode 100644 gapic/templates/%namespace/%name_%version/%sub/types/__init__.py.j2 diff --git a/gapic/templates/%namespace/%name_%version/%sub/enums.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/enums.py.j2 new file mode 100644 index 000000000..567e8fe1c --- /dev/null +++ b/gapic/templates/%namespace/%name_%version/%sub/enums.py.j2 @@ -0,0 +1,13 @@ +{% extends '_base.py.j2' %} + +{% block content %} +{% for p in api.protos.values() if p.file_to_generate and p.enums -%} +from .types.{{ p.module_name }} import ({% for e in p.enums.values() %}{{ e.name }}, {% endfor %}) +{% endfor %} + +__all__ = ( +{%- for p in api.protos.values() if p.file_to_generate %}{% for e in p.enums.values() %} + '{{ e.name }}', +{%- endfor %}{% endfor %} +) +{% endblock %} diff --git a/gapic/templates/%namespace/%name_%version/%sub/types/__init__.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/types/__init__.py.j2 new file mode 100644 index 000000000..7b464a970 --- /dev/null +++ b/gapic/templates/%namespace/%name_%version/%sub/types/__init__.py.j2 @@ -0,0 +1,13 @@ +{% extends '_base.py.j2' %} + +{% block content %} +{% for p in api.protos.values() if p.file_to_generate and p.messages -%} +from .{{ p.module_name }} import ({% for m in p.messages.values() %}{{ m.name }}, {% endfor %}) +{% endfor %} + +__all__ = ( +{%- for p in api.protos.values() if p.file_to_generate %}{% for m in p.messages.values() %} + '{{ m.name }}', +{%- endfor %}{% endfor %} +) +{% endblock %} diff --git a/gapic/templates/tests/unit/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/%name_%version/%sub/test_%service.py.j2 index 0be731c98..45f0b35cb 100644 --- a/gapic/templates/tests/unit/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/%name_%version/%sub/test_%service.py.j2 @@ -20,6 +20,9 @@ from google.api_core import future from google.api_core import operations_v1 from google.longrunning import operations_pb2 {% endif -%} +{% if api.protos.values()|selectattr('enums') -%} +from {{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }} import enums +{% endif -%} {% for method in service.methods.values() -%} {% for ref_type in method.ref_types if not ((ref_type.ident.python_import.package == ('google', 'api_core') and ref_type.ident.python_import.module == 'operation') @@ -431,4 +434,13 @@ def test_{{ message.name|snake_case }}_path(): {% endwith -%} {% endfor -%} + +{% with enums = [] -%}{% for p in api.protos.values() if p.file_to_generate -%}{% for e in p.enums.values() if e.ident.module == service.meta.address.module -%}{% do enums.append(e) %} +{% if enums -%} +def test_enum_path(): +{%- for e in enums %} + assert enums.{{ e.name }} == {{ e.ident.module }}.{{ e.name }} +{%- endfor %} +{% endif -%} +{% endfor -%}{% endfor -%}{% endwith -%} {% endblock %}