test input: use explicit package declaration (#345)

This commit is contained in:
Arun Babu Neelicattu 2022-03-03 14:34:53 +01:00 committed by GitHub
parent 74205e3319
commit bd69862a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
78 changed files with 141 additions and 37 deletions

View File

@ -13,6 +13,7 @@ jobs:
name: ${{ matrix.os }} / ${{ matrix.python-version }} name: ${{ matrix.os }} / ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}-latest runs-on: ${{ matrix.os }}-latest
strategy: strategy:
fail-fast: false
matrix: matrix:
os: [Ubuntu, MacOS, Windows] os: [Ubuntu, MacOS, Windows]
python-version: ['3.6.7', '3.7', '3.8', '3.9', '3.10'] python-version: ['3.6.7', '3.7', '3.8', '3.9', '3.10']
@ -56,9 +57,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
shell: bash shell: bash
run: | run: poetry install
poetry run python -m pip install pip -U
poetry install
- name: Generate code from proto files - name: Generate code from proto files
shell: bash shell: bash

View File

@ -1,3 +1,6 @@
import copy
import sys
import pytest import pytest
@ -10,3 +13,10 @@ def pytest_addoption(parser):
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def repeat(request): def repeat(request):
return request.config.getoption("repeat") return request.config.getoption("repeat")
@pytest.fixture
def reset_sys_path():
original = copy.deepcopy(sys.path)
yield
sys.path = original

View File

@ -78,7 +78,7 @@ async def generate_test_case_output(
""" """
test_case_output_path_reference = output_path_reference.joinpath(test_case_name) test_case_output_path_reference = output_path_reference.joinpath(test_case_name)
test_case_output_path_betterproto = output_path_betterproto.joinpath(test_case_name) test_case_output_path_betterproto = output_path_betterproto
os.makedirs(test_case_output_path_reference, exist_ok=True) os.makedirs(test_case_output_path_reference, exist_ok=True)
os.makedirs(test_case_output_path_betterproto, exist_ok=True) os.makedirs(test_case_output_path_betterproto, exist_ok=True)

View File

@ -7,12 +7,12 @@ import grpclib.server
import pytest import pytest
from betterproto.grpc.util.async_channel import AsyncChannel from betterproto.grpc.util.async_channel import AsyncChannel
from grpclib.testing import ChannelFor from grpclib.testing import ChannelFor
from tests.output_betterproto.service.service import ( from tests.output_betterproto.service import (
DoThingRequest, DoThingRequest,
DoThingResponse, DoThingResponse,
GetThingRequest, GetThingRequest,
) )
from tests.output_betterproto.service.service import TestStub as ThingServiceClient from tests.output_betterproto.service import TestStub as ThingServiceClient
from .thing_service import ThingService from .thing_service import ThingService

View File

@ -1,4 +1,4 @@
from tests.output_betterproto.service.service import ( from tests.output_betterproto.service import (
DoThingResponse, DoThingResponse,
DoThingRequest, DoThingRequest,
GetThingRequest, GetThingRequest,

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package bool;
message Test { message Test {
bool value = 1; bool value = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package bytes;
message Test { message Test {
bytes data = 1; bytes data = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package casing;
enum my_enum { enum my_enum {
ZERO = 0; ZERO = 0;
ONE = 1; ONE = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package casing_message_field_uppercase;
message Test { message Test {
int32 UPPERCASE = 1; int32 UPPERCASE = 1;
int32 UPPERCASE_V2 = 2; int32 UPPERCASE_V2 = 2;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package deprecated;
// Some documentation about the Test message. // Some documentation about the Test message.
message Test { message Test {
// Some documentation about the value. // Some documentation about the value.

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package deprecated_field;
// Some documentation about the Test message. // Some documentation about the Test message.
message Test { message Test {
// Some documentation about the value. // Some documentation about the value.

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package double;
message Test { message Test {
double count = 1; double count = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package empty_repeated;
message MessageA { message MessageA {
repeated float values = 1; repeated float values = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package enum;
// Tests that enums are correctly serialized and that it correctly handles skipped and out-of-order enum values // Tests that enums are correctly serialized and that it correctly handles skipped and out-of-order enum values
message Test { message Test {
Choice choice = 1; Choice choice = 1;

View File

@ -39,6 +39,8 @@
syntax = "proto2"; syntax = "proto2";
package example;
// package google.protobuf; // package google.protobuf;
option go_package = "google.golang.org/protobuf/types/descriptorpb"; option go_package = "google.golang.org/protobuf/types/descriptorpb";

View File

@ -2,7 +2,7 @@ from typing import AsyncIterable, AsyncIterator
import pytest import pytest
from grpclib.testing import ChannelFor from grpclib.testing import ChannelFor
from tests.output_betterproto.example_service.example_service import ( from tests.output_betterproto.example_service import (
ExampleRequest, ExampleRequest,
ExampleResponse, ExampleResponse,
TestBase, TestBase,

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package field_name_identical_to_type;
// Tests that messages may contain fields with names that are identical to their python types (PR #294) // Tests that messages may contain fields with names that are identical to their python types (PR #294)
message Test { message Test {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package fixed;
message Test { message Test {
fixed32 foo = 1; fixed32 foo = 1;
sfixed32 bar = 2; sfixed32 bar = 2;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package float;
// Some documentation about the Test message. // Some documentation about the Test message.
message Test { message Test {
double positive = 1; double positive = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package google_impl_behavior_equivalence;
message Foo{ message Foo{
int64 bar = 1; int64 bar = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes;
import "google/protobuf/duration.proto"; import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto"; import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto"; import "google/protobuf/wrappers.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes_response;
import "google/protobuf/wrappers.proto"; import "google/protobuf/wrappers.proto";
// Tests that wrapped values can be used directly as return values // Tests that wrapped values can be used directly as return values

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes_response_embedded;
import "google/protobuf/wrappers.proto"; import "google/protobuf/wrappers.proto";
// Tests that wrapped values are supported as part of output message // Tests that wrapped values are supported as part of output message

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes_service_returns_empty;
import "google/protobuf/empty.proto"; import "google/protobuf/empty.proto";
service Test { service Test {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes_service_returns_googletype;
import "google/protobuf/empty.proto"; import "google/protobuf/empty.proto";
import "google/protobuf/struct.proto"; import "google/protobuf/struct.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes_struct;
import "google/protobuf/struct.proto"; import "google/protobuf/struct.proto";
message Test { message Test {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package googletypes_value;
import "google/protobuf/struct.proto"; import "google/protobuf/struct.proto";
// Tests that fields of type google.protobuf.Value can contain arbitrary JSON-values. // Tests that fields of type google.protobuf.Value can contain arbitrary JSON-values.

View File

@ -1,7 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package Capitalized; package import_capitalized_package.Capitalized;
message Message { message Message {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_capitalized_package;
import "capitalized.proto"; import "capitalized.proto";
// Tests that we can import from a package with a capital name, that looks like a nested type, but isn't. // Tests that we can import from a package with a capital name, that looks like a nested type, but isn't.

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package package.childpackage; package import_child_package_from_package.package.childpackage;
message ChildMessage { message ChildMessage {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_child_package_from_package;
import "package_message.proto"; import "package_message.proto";
// Tests generated imports when a message in a package refers to a message in a nested child package. // Tests generated imports when a message in a package refers to a message in a nested child package.

View File

@ -2,7 +2,7 @@ syntax = "proto3";
import "child.proto"; import "child.proto";
package package; package import_child_package_from_package.package;
message PackageMessage { message PackageMessage {
package.childpackage.ChildMessage c = 1; package.childpackage.ChildMessage c = 1;

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package childpackage; package import_child_package_from_root.childpackage;
message Message { message Message {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_child_package_from_root;
import "child.proto"; import "child.proto";
// Tests generated imports when a message in root refers to a message in a child package. // Tests generated imports when a message in root refers to a message in a child package.

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_circular_dependency;
import "root.proto"; import "root.proto";
import "other.proto"; import "other.proto";

View File

@ -1,7 +1,7 @@
syntax = "proto3"; syntax = "proto3";
import "root.proto"; import "root.proto";
package other; package import_circular_dependency.other;
message OtherPackageMessage { message OtherPackageMessage {
RootPackageMessage rootPackageMessage = 1; RootPackageMessage rootPackageMessage = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_circular_dependency;
message RootPackageMessage { message RootPackageMessage {
} }

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package cousin.cousin_subpackage; package import_cousin_package.cousin.cousin_subpackage;
message CousinMessage { message CousinMessage {
} }

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package test.subpackage; package import_cousin_package.test.subpackage;
import "cousin.proto"; import "cousin.proto";

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package cousin.subpackage; package import_cousin_package_same_name.cousin.subpackage;
message CousinMessage { message CousinMessage {
} }

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package test.subpackage; package import_cousin_package_same_name.test.subpackage;
import "cousin.proto"; import "cousin.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_packages_same_name;
import "users_v1.proto"; import "users_v1.proto";
import "posts_v1.proto"; import "posts_v1.proto";

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package posts.v1; package import_packages_same_name.posts.v1;
message Post { message Post {

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package users.v1; package import_packages_same_name.users.v1;
message User { message User {

View File

@ -2,7 +2,7 @@ syntax = "proto3";
import "parent_package_message.proto"; import "parent_package_message.proto";
package parent.child; package import_parent_package_from_child.parent.child;
// Tests generated imports when a message refers to a message defined in its parent package // Tests generated imports when a message refers to a message defined in its parent package

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package parent; package import_parent_package_from_child.parent;
message ParentPackageMessage { message ParentPackageMessage {
} }

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package child; package import_root_package_from_child.child;
import "root.proto"; import "root.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_root_package_from_child;
message RootMessage { message RootMessage {
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_root_sibling;
import "sibling.proto"; import "sibling.proto";
// Tests generated imports when a message in the root package refers to another message in the root package // Tests generated imports when a message in the root package refers to another message in the root package

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_root_sibling;
message SiblingMessage { message SiblingMessage {
} }

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package child; package import_service_input_message.child;
message ChildRequestMessage { message ChildRequestMessage {
int32 child_argument = 1; int32 child_argument = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_service_input_message;
import "request_message.proto"; import "request_message.proto";
import "child_package_request_message.proto"; import "child_package_request_message.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package import_service_input_message;
message RequestMessage { message RequestMessage {
int32 argument = 1; int32 argument = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package int32;
// Some documentation about the Test message. // Some documentation about the Test message.
message Test { message Test {
// Some documentation about the count. // Some documentation about the count.

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package map;
message Test { message Test {
map<string, int32> counts = 1; map<string, int32> counts = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package mapmessage;
message Test { message Test {
map<string, Nested> items = 1; map<string, Nested> items = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package namespace_builtin_types;
// Tests that messages may contain fields with names that are python types // Tests that messages may contain fields with names that are python types
message Test { message Test {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package namespace_keywords;
// Tests that messages may contain fields that are Python keywords // Tests that messages may contain fields that are Python keywords
// //
// Generated with Python 3.7.6 // Generated with Python 3.7.6

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package nested;
// A test message with a nested message inside of it. // A test message with a nested message inside of it.
message Test { message Test {
// This is the nested type. // This is the nested type.

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package nested2;
import "package.proto"; import "package.proto";
message Game { message Game {

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package equipment; package nested2.equipment;
message Weapon { message Weapon {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package nestedtwice;
message Test { message Test {
message Top { message Top {
message Middle { message Middle {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package oneof;
message Test { message Test {
oneof foo { oneof foo {
int32 pitied = 1; int32 pitied = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package oneof_default_value_serialization;
import "google/protobuf/duration.proto"; import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto"; import "google/protobuf/timestamp.proto";
import "google/protobuf/wrappers.proto"; import "google/protobuf/wrappers.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package oneof_empty;
message Nothing {} message Nothing {}
message MaybeNothing { message MaybeNothing {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package oneof_enum;
message Test { message Test {
oneof action { oneof action {
Signal signal = 1; Signal signal = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package proto3_field_presence;
import "google/protobuf/timestamp.proto"; import "google/protobuf/timestamp.proto";
message InnerTest { message InnerTest {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package proto3_field_presence_oneof;
message Test { message Test {
oneof kind { oneof kind {
Nested nested = 1; Nested nested = 1;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package recursivemessage;
message Test { message Test {
string name = 1; string name = 1;
Test child = 2; Test child = 2;

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package ref;
import "repeatedmessage.proto"; import "repeatedmessage.proto";
message Test { message Test {

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package repeated;
message Test { message Test {
repeated string names = 1; repeated string names = 1;
} }

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package repeated_duration_timestamp;
import "google/protobuf/duration.proto"; import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto"; import "google/protobuf/timestamp.proto";

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package repeatedpacked;
message Test { message Test {
repeated int32 counts = 1; repeated int32 counts = 1;
repeated sint64 signed = 2; repeated sint64 signed = 2;

View File

@ -3,7 +3,7 @@ syntax = "proto3";
import "google/protobuf/duration.proto"; import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto"; import "google/protobuf/timestamp.proto";
package things.messages; package service_separate_packages.things.messages;
message DoThingRequest { message DoThingRequest {
string name = 1; string name = 1;

View File

@ -2,7 +2,7 @@ syntax = "proto3";
import "messages.proto"; import "messages.proto";
package things.service; package service_separate_packages.things.service;
service Test { service Test {
rpc DoThing (things.messages.DoThingRequest) returns (things.messages.DoThingResponse); rpc DoThing (things.messages.DoThingRequest) returns (things.messages.DoThingResponse);

View File

@ -1,5 +1,7 @@
syntax = "proto3"; syntax = "proto3";
package signed;
message Test { message Test {
// todo: rename fields after fixing bug where 'signed_32_positive' will map to 'signed_32Positive' as output json // todo: rename fields after fixing bug where 'signed_32_positive' will map to 'signed_32Positive' as output json
sint32 signed32 = 1; // signed_32_positive sint32 signed32 = 1; // signed_32_positive

View File

@ -480,7 +480,7 @@ def test_iso_datetime_list():
def test_service_argument__expected_parameter(): def test_service_argument__expected_parameter():
from tests.output_betterproto.service.service import TestStub from tests.output_betterproto.service import TestStub
sig = signature(TestStub.do_thing) sig = signature(TestStub.do_thing)
do_thing_request_parameter = sig.parameters["do_thing_request"] do_thing_request_parameter = sig.parameters["do_thing_request"]

View File

@ -23,8 +23,6 @@ from tests.util import (
# break things because we can't properly reset the symbol database. # break things because we can't properly reset the symbol database.
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
from google.protobuf import symbol_database
from google.protobuf.descriptor_pool import DescriptorPool
from google.protobuf.json_format import Parse from google.protobuf.json_format import Parse
@ -125,14 +123,9 @@ def dict_replace_nans(input_dict: Dict[Any, Any]) -> Dict[Any, Any]:
@pytest.fixture @pytest.fixture
def test_data(request): def test_data(request, reset_sys_path):
test_case_name = request.param test_case_name = request.param
# Reset the internal symbol database so we can import the `Test` message
# multiple times. Ugh.
sym = symbol_database.Default()
sym.pool = DescriptorPool()
reference_module_root = os.path.join( reference_module_root = os.path.join(
*reference_output_package.split("."), test_case_name *reference_output_package.split("."), test_case_name
) )
@ -158,8 +151,6 @@ def test_data(request):
) )
) )
sys.path.remove(reference_module_root)
@pytest.mark.parametrize("test_data", test_cases.messages, indirect=True) @pytest.mark.parametrize("test_data", test_cases.messages, indirect=True)
def test_message_can_instantiated(test_data: TestData) -> None: def test_message_can_instantiated(test_data: TestData) -> None: