Skip to content

Commit 3713a4b

Browse files
authored
fix: convert datetime back to proto for unit tests (#511)
1 parent e353e61 commit 3713a4b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from unittest import mock
77
import grpc
88
import math
99
import pytest
10+
from proto.marshal.rules.dates import DurationRule, TimestampRule
1011

1112
{# Import the service itself as well as every proto module that it imports. -#}
1213
{% filter sort_lines -%}
@@ -371,7 +372,13 @@ def test_{{ method.name|snake_case }}_flattened():
371372
assert len(call.mock_calls) == 1
372373
_, args, _ = call.mock_calls[0]
373374
{% for key, field in method.flattened_fields.items() -%}{%- if not field.oneof or field.proto3_optional %}
375+
{% if field.ident|string() == 'timestamp.Timestamp' -%}
376+
assert TimestampRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
377+
{% elif field.ident|string() == 'duration.Duration' -%}
378+
assert DurationRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
379+
{% else -%}
374380
assert args[0].{{ key }} == {{ field.mock_value }}
381+
{% endif %}
375382
{% endif %}{% endfor %}
376383
{%- for oneofs in method.flattened_oneof_fields().values() %}
377384
{%- with field = oneofs[-1] %}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import grpc
88
from grpc.experimental import aio
99
import math
1010
import pytest
11+
from proto.marshal.rules.dates import DurationRule, TimestampRule
1112

1213
{# Import the service itself as well as every proto module that it imports. -#}
1314
{% filter sort_lines -%}
@@ -574,7 +575,13 @@ def test_{{ method.name|snake_case }}_flattened():
574575
assert len(call.mock_calls) == 1
575576
_, args, _ = call.mock_calls[0]
576577
{% for key, field in method.flattened_fields.items() -%}{%- if not field.oneof or field.proto3_optional %}
578+
{% if field.ident|string() == 'timestamp.Timestamp' -%}
579+
assert TimestampRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
580+
{% elif field.ident|string() == 'duration.Duration' -%}
581+
assert DurationRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
582+
{% else -%}
577583
assert args[0].{{ key }} == {{ field.mock_value }}
584+
{% endif %}
578585
{% endif %}{% endfor %}
579586
{%- for oneofs in method.flattened_oneof_fields().values() %}
580587
{%- with field = oneofs[-1] %}
@@ -653,7 +660,13 @@ async def test_{{ method.name|snake_case }}_flattened_async():
653660
assert len(call.mock_calls)
654661
_, args, _ = call.mock_calls[0]
655662
{% for key, field in method.flattened_fields.items() -%}{%- if not field.oneof or field.proto3_optional %}
663+
{% if field.ident|string() == 'timestamp.Timestamp' -%}
664+
assert TimestampRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
665+
{% elif field.ident|string() == 'duration.Duration' -%}
666+
assert DurationRule().to_proto(args[0].{{ key }}) == {{ field.mock_value }}
667+
{% else -%}
656668
assert args[0].{{ key }} == {{ field.mock_value }}
669+
{% endif %}
657670
{% endif %}{% endfor %}
658671
{%- for oneofs in method.flattened_oneof_fields().values() %}
659672
{%- with field = oneofs[-1] %}

0 commit comments

Comments
 (0)