To reproduce, add a fragment file:
# In tests/fragments/broken.proto
syntax = "proto3";
package google.fragment;
import "google/api/client.proto";
import "import.proto";
service MyService {
option (google.api.default_host) = "my.example.com";
rpc MyMethod(MethodRequest) returns (MethodResponse) {}
}
message MethodRequest {
string input = 1;
}
message Container {
Import import = 1;
}
message MethodResponse {
oneof data {
Container container = 1;
}
}
Then run nox -s fragment-3.7 -- tests/fragments/broken.proto. The generated unit test fails with NameError: name 'import_' is not defined.
Some variants of similar protos do not fail:
- If
oneof is removed.
- If
Container container = 1; is replaced with just Import import = 1;.
- If
Import import = 1; is replaced with a field of primitive data type, e.g. string import = 1;.
- If
MethodResponse is empty, and the oneof is moved into MethodRequest. (This seems to be incidental to the implementation of unit test generation.)
Two pieces of generator code are relevant here:
- Import statements https://github.com/googleapis/gapic-generator-python/blob/v1.10.0/gapic/schema/wrappers.py#L1489: This populates the types referenced in a method,
_ref_types. It is used in the template to add import statements: https://github.com/googleapis/gapic-generator-python/blob/v1.10.0/gapic/templates/tests/unit/gapic/%25name_%25version/%25sub/test_%25service.py.j2#L65
- Response https://github.com/googleapis/gapic-generator-python/blob/v1.10.0/gapic/schema/wrappers.py#L176: This recursively populates a mock message. It is used in the template for
call.return_value: https://github.com/googleapis/gapic-generator-python/blob/v1.10.0/gapic/templates/tests/unit/gapic/%25name_%25version/%25sub/test_macros.j2#L35
In the example above, it seems that the response message was properly populated with import_.Import(...) within its container field, but the method's _ref_types attribute did not include import_.
To reproduce, add a fragment file:
Then run
nox -s fragment-3.7 -- tests/fragments/broken.proto. The generated unit test fails withNameError: name 'import_' is not defined.Some variants of similar protos do not fail:
oneofis removed.Container container = 1;is replaced with justImport import = 1;.Import import = 1;is replaced with a field of primitive data type, e.g.string import = 1;.MethodResponseis empty, and theoneofis moved intoMethodRequest. (This seems to be incidental to the implementation of unit test generation.)Two pieces of generator code are relevant here:
_ref_types. It is used in the template to add import statements: https://github.com/googleapis/gapic-generator-python/blob/v1.10.0/gapic/templates/tests/unit/gapic/%25name_%25version/%25sub/test_%25service.py.j2#L65call.return_value: https://github.com/googleapis/gapic-generator-python/blob/v1.10.0/gapic/templates/tests/unit/gapic/%25name_%25version/%25sub/test_macros.j2#L35In the example above, it seems that the response message was properly populated with
import_.Import(...)within itscontainerfield, but the method's_ref_typesattribute did not includeimport_.