Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit ebc38bf

Browse files
Fix pager test assertions for ListValue and Struct types
Relax assertion checks in generated pager tests to allow `Sequence` for `struct_pb2.ListValue` and `Mapping` for `struct_pb2.Struct`, as `proto-plus` converts these WKTs to native Python types. Also handles `struct_pb2.Value` mapping to `Any`. This fixes an `AssertionError` where tests expected `struct_pb2.ListValue` instances but received list-like objects (e.g. `RepeatedComposite` or `list`).
1 parent b75b8f4 commit ebc38bf

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ from google.protobuf import json_format
2626
import json
2727
import math
2828
import pytest
29+
from collections.abc import Sequence, Mapping
2930
from google.api_core import api_core_version
3031
from proto.marshal.rules.dates import DurationRule, TimestampRule
3132
from proto.marshal.rules import wrappers

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,18 @@ def test_{{ method_name }}_pager(transport_name: str = "grpc"):
752752

753753
results = list(pager)
754754
assert len(results) == 6
755+
{% if method.paged_result_field.type.ident|string == 'struct_pb2.ListValue' %}
756+
assert all(isinstance(i, Sequence)
757+
for i in results)
758+
{% elif method.paged_result_field.type.ident|string == 'struct_pb2.Struct' %}
759+
assert all(isinstance(i, Mapping)
760+
for i in results)
761+
{% elif method.paged_result_field.type.ident|string == 'struct_pb2.Value' %}
762+
assert all(True for i in results)
763+
{% else %}
755764
assert all(isinstance(i, {{ method.paged_result_field.type.ident }})
756765
for i in results)
766+
{% endif %}
757767
{% endif %}
758768
def test_{{ method_name }}_pages(transport_name: str = "grpc"):
759769
client = {{ service.client_name }}(
@@ -913,9 +923,19 @@ async def test_{{ method_name }}_async_pager():
913923
assert async_pager.get('a') is None
914924
assert isinstance(async_pager.get('h'), {{ method.paged_result_field.type.fields.get('value').ident }})
915925
{% else %}
926+
{% if method.paged_result_field.type.ident|string == 'struct_pb2.ListValue' %}
927+
assert all(isinstance(i, Sequence)
928+
for i in responses)
929+
{% elif method.paged_result_field.type.ident|string == 'struct_pb2.Struct' %}
930+
assert all(isinstance(i, Mapping)
931+
for i in responses)
932+
{% elif method.paged_result_field.type.ident|string == 'struct_pb2.Value' %}
933+
assert all(True for i in responses)
934+
{% else %}
916935
assert all(isinstance(i, {{ method.paged_result_field.type.ident }})
917936
for i in responses)
918937
{% endif %}
938+
{% endif %}
919939

920940

921941
@pytest.mark.asyncio
@@ -1412,9 +1432,19 @@ def test_{{ method_name }}_rest_pager(transport: str = 'rest'):
14121432
assert pager.get('a') is None
14131433
assert isinstance(pager.get('h'), {{ method.paged_result_field.type.fields.get('value').ident }})
14141434
{% else %}
1435+
{% if method.paged_result_field.type.ident|string == 'struct_pb2.ListValue' %}
1436+
assert all(isinstance(i, Sequence)
1437+
for i in results)
1438+
{% elif method.paged_result_field.type.ident|string == 'struct_pb2.Struct' %}
1439+
assert all(isinstance(i, Mapping)
1440+
for i in results)
1441+
{% elif method.paged_result_field.type.ident|string == 'struct_pb2.Value' %}
1442+
assert all(True for i in results)
1443+
{% else %}
14151444
assert all(isinstance(i, {{ method.paged_result_field.type.ident }})
14161445
for i in results)
14171446
{% endif %}
1447+
{% endif %}
14181448

14191449
pages = list(client.{{ method_name }}(request=sample_request).pages)
14201450
for page_, token in zip(pages, ['abc','def','ghi', '']):

0 commit comments

Comments
 (0)