Skip to content

Commit fce5338

Browse files
committed
add tests
1 parent 5288c54 commit fce5338

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

sdks/java/extensions/protobuf/src/test/java/org/apache/beam/sdk/extensions/protobuf/ProtoMessageSchemaTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.NULL_MAP_PRIMITIVE_ROW;
3030
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.NULL_REPEATED_PROTO;
3131
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.NULL_REPEATED_ROW;
32+
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.NULLABLE_PRIMITIVE_PROTO;
33+
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.NULLABLE_PRIMITIVE_ROW;
34+
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.NULLABLE_PRIMITIVE_SCHEMA;
3235
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.ONEOF_PROTO_BOOL;
3336
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.ONEOF_PROTO_INT32;
3437
import static org.apache.beam.sdk.extensions.protobuf.TestProtoSchemas.ONEOF_PROTO_PRIMITIVE;
@@ -75,6 +78,7 @@
7578
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.MapPrimitive;
7679
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Nested;
7780
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.NonContiguousOneOf;
81+
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.NullablePrimitive;
7882
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.OneOf;
7983
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.OuterOneOf;
8084
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Primitive;
@@ -116,6 +120,26 @@ public void testPrimitiveRowToProto() {
116120
assertEquals(PRIMITIVE_PROTO, fromRow.apply(PRIMITIVE_ROW));
117121
}
118122

123+
@Test
124+
public void testNullablePrimitiveSchema() {
125+
Schema schema = new ProtoMessageSchema().schemaFor(TypeDescriptor.of(NullablePrimitive.class));
126+
assertEquals(NULLABLE_PRIMITIVE_SCHEMA, schema);
127+
}
128+
129+
@Test
130+
public void testNullablePrimitiveProtoToRow() {
131+
SerializableFunction<NullablePrimitive, Row> toRow =
132+
new ProtoMessageSchema().toRowFunction(TypeDescriptor.of(NullablePrimitive.class));
133+
assertEquals(NULLABLE_PRIMITIVE_ROW, toRow.apply(NULLABLE_PRIMITIVE_PROTO));
134+
}
135+
136+
@Test
137+
public void testNullablePrimitiveRowToProto() {
138+
SerializableFunction<Row, NullablePrimitive> fromRow =
139+
new ProtoMessageSchema().fromRowFunction(TypeDescriptor.of(NullablePrimitive.class));
140+
assertEquals(NULLABLE_PRIMITIVE_PROTO, fromRow.apply(NULLABLE_PRIMITIVE_ROW));
141+
}
142+
119143
@Test
120144
public void testOptionalPrimitiveSchema() {
121145
Schema schema = new ProtoMessageSchema().schemaFor(TypeDescriptor.of(OptionalPrimitive.class));

sdks/java/extensions/protobuf/src/test/java/org/apache/beam/sdk/extensions/protobuf/TestProtoSchemas.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.MapPrimitive;
4545
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Nested;
4646
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.NonContiguousOneOf;
47+
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.NullablePrimitive;
4748
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.OneOf;
4849
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.OuterOneOf;
4950
import org.apache.beam.sdk.extensions.protobuf.Proto3SchemaMessages.Primitive;
@@ -111,6 +112,47 @@ static Schema.Options withTypeName(String typeName) {
111112
"proto3_schema_messages.Primitive"))
112113
.build();
113114

115+
static final Schema NULLABLE_PRIMITIVE_SCHEMA =
116+
Schema.builder()
117+
.addField(withFieldNumber("primitive_double", FieldType.DOUBLE, 1).withNullable(true))
118+
.addField(withFieldNumber("primitive_float", FieldType.FLOAT, 2).withNullable(true))
119+
.addField(withFieldNumber("primitive_int32", FieldType.INT32, 3).withNullable(true))
120+
.addField(withFieldNumber("primitive_int64", FieldType.INT64, 4).withNullable(true))
121+
.addField(
122+
withFieldNumber("primitive_uint32", FieldType.logicalType(new UInt32()), 5)
123+
.withNullable(true))
124+
.addField(
125+
withFieldNumber("primitive_uint64", FieldType.logicalType(new UInt64()), 6)
126+
.withNullable(true))
127+
.addField(
128+
withFieldNumber("primitive_sint32", FieldType.logicalType(new SInt32()), 7)
129+
.withNullable(true))
130+
.addField(
131+
withFieldNumber("primitive_sint64", FieldType.logicalType(new SInt64()), 8)
132+
.withNullable(true))
133+
.addField(
134+
withFieldNumber("primitive_fixed32", FieldType.logicalType(new Fixed32()), 9)
135+
.withNullable(true))
136+
.addField(
137+
withFieldNumber("primitive_fixed64", FieldType.logicalType(new Fixed64()), 10)
138+
.withNullable(true))
139+
.addField(
140+
withFieldNumber("primitive_sfixed32", FieldType.logicalType(new SFixed32()), 11)
141+
.withNullable(true))
142+
.addField(
143+
withFieldNumber("primitive_sfixed64", FieldType.logicalType(new SFixed64()), 12)
144+
.withNullable(true))
145+
.addField(withFieldNumber("primitive_bool", FieldType.BOOLEAN, 13).withNullable(true))
146+
.addField(withFieldNumber("primitive_string", FieldType.STRING, 14).withNullable(true))
147+
.addField(withFieldNumber("primitive_bytes", FieldType.BYTES, 15).withNullable(true))
148+
.setOptions(
149+
Schema.Options.builder()
150+
.setOption(
151+
SCHEMA_OPTION_META_TYPE_NAME,
152+
FieldType.STRING,
153+
"proto3_schema_messages.NullablePrimitive"))
154+
.build();
155+
114156
static final Schema OPTIONAL_PRIMITIVE_SCHEMA =
115157
Schema.builder()
116158
.addField(withFieldNumber("primitive_int32", FieldType.INT32, 1))
@@ -181,6 +223,10 @@ static Schema.Options withTypeName(String typeName) {
181223
.setPrimitiveBytes(ByteString.copyFrom(BYTE_ARRAY))
182224
.build();
183225

226+
static final NullablePrimitive NULLABLE_PRIMITIVE_PROTO = NullablePrimitive.newBuilder().build();
227+
228+
static final Row NULLABLE_PRIMITIVE_ROW = Row.nullRow(NULLABLE_PRIMITIVE_SCHEMA);
229+
184230
// A sample instance of the row.
185231
static final Row OPTIONAL_PRIMITIVE_ROW =
186232
Row.withSchema(OPTIONAL_PRIMITIVE_SCHEMA).addValues(32, true, "horsey", BYTE_ARRAY).build();

sdks/java/extensions/protobuf/src/test/proto/proto3_schema_messages.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ message Primitive {
5151
bytes primitive_bytes = 15;
5252
}
5353

54+
message NullablePrimitive {
55+
optional double primitive_double = 1;
56+
optional float primitive_float = 2;
57+
optional int32 primitive_int32 = 3;
58+
optional int64 primitive_int64 = 4;
59+
optional uint32 primitive_uint32 = 5;
60+
optional uint64 primitive_uint64 = 6;
61+
optional sint32 primitive_sint32 = 7;
62+
optional sint64 primitive_sint64 = 8;
63+
optional fixed32 primitive_fixed32 = 9;
64+
optional fixed64 primitive_fixed64 = 10;
65+
optional sfixed32 primitive_sfixed32 = 11;
66+
optional sfixed64 primitive_sfixed64 = 12;
67+
optional bool primitive_bool = 13;
68+
optional string primitive_string = 14;
69+
optional bytes primitive_bytes = 15;
70+
}
71+
5472
message RepeatPrimitive {
5573
repeated double repeated_double = 1;
5674
repeated float repeated_float = 2;

0 commit comments

Comments
 (0)