Skip to content

Commit c70dec9

Browse files
committed
Test protobuf import stability
1 parent 3bc2559 commit c70dec9

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_elements/scalar_type_extension.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ def protobuf(type:, import: nil, comment: nil)
6363
def initialize_proto_extension
6464
original_name = type_ref.with_reverted_override.name
6565
if (proto_options = BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME[original_name])
66-
protobuf(**proto_options)
66+
protobuf(
67+
type: proto_options.fetch(:type),
68+
import: proto_options[:import],
69+
comment: proto_options[:comment]
70+
)
6771
end
6872

6973
yield

elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/api_extension_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ module SchemaDefinition
3232
end
3333

3434
it "maps every built-in scalar to a proto field type" do
35+
built_in_scalar_options = SchemaElements::ScalarTypeExtension::BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME
3536
field_types = []
3637
proto = define_proto_schema do |s|
3738
s.object_type "Widget" do |t|
38-
SchemaElements::ScalarTypeExtension::BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME.each_key do |type_name|
39+
built_in_scalar_options.each_key do |type_name|
3940
t.field type_name.downcase, type_name
4041
end
4142
field_types = t.graphql_fields_by_name.values.map { |field| field.type.name }
4243
t.index "widgets"
4344
end
4445
end
4546

46-
proto_options_by_name = SchemaElements::ScalarTypeExtension::BUILT_IN_SCALAR_PROTO_OPTIONS_BY_NAME
47-
expect(field_types).to match_array(proto_options_by_name.keys)
47+
expect(field_types).to match_array(built_in_scalar_options.keys)
4848
expect(proto).to include('import "google/protobuf/timestamp.proto";')
49-
proto_options_by_name.each.with_index(1) do |(type_name, proto_options), field_number|
50-
expect(proto).to include("#{proto_options.fetch(:type)} #{type_name.downcase} = #{field_number};")
49+
built_in_scalar_options.each.with_index(1) do |(type_name, options), field_number|
50+
expect(proto).to include("#{options.fetch(:type)} #{type_name.downcase} = #{field_number};")
5151
end
5252
end
5353

elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/schema_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,38 @@ module SchemaDefinition
267267
PROTO
268268
end
269269

270+
it "sorts and de-duplicates imports shared by distinct protobuf types" do
271+
proto = define_proto_schema do |s|
272+
s.scalar_type "FirstZType" do |t|
273+
t.mapping type: "keyword"
274+
t.protobuf type: "example.FirstZType", import: "z/types.proto"
275+
end
276+
277+
s.scalar_type "SecondZType" do |t|
278+
t.mapping type: "keyword"
279+
t.protobuf type: "example.SecondZType", import: "z/types.proto"
280+
end
281+
282+
s.scalar_type "AType" do |t|
283+
t.mapping type: "keyword"
284+
t.protobuf type: "example.AType", import: "a/types.proto"
285+
end
286+
287+
s.object_type "Event" do |t|
288+
t.field "id", "ID"
289+
t.field "first_z", "FirstZType"
290+
t.field "second_z", "SecondZType"
291+
t.field "a", "AType"
292+
t.index "events"
293+
end
294+
end
295+
296+
expect(proto.lines.grep(/\Aimport /).map(&:chomp)).to eq([
297+
%(import "a/types.proto";),
298+
%(import "z/types.proto";)
299+
])
300+
end
301+
270302
it "renders format comments on fields whose scalar type documents one" do
271303
proto = define_proto_schema do |s|
272304
s.object_type "Person" do |t|

0 commit comments

Comments
 (0)