Skip to content

Commit

Permalink
feat: Complete test for BaseTypeInfoConverterTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ekawinataa committed Oct 11, 2024
1 parent d0228dd commit 14bdd12
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.gotocompany.depot.maxcompute;

import com.aliyun.odps.type.TypeInfo;
import com.google.protobuf.Descriptors;
import com.gotocompany.depot.TestMaxComputeTypeInfo;
import com.gotocompany.depot.maxcompute.converter.BaseTypeInfoConverter;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class BaseTypeInfoConverterTest {

private final Descriptors.Descriptor descriptor = TestMaxComputeTypeInfo.TestRoot.getDescriptor();
private final BaseTypeInfoConverter baseTypeInfoConverter = new BaseTypeInfoConverter();

@Test
public void shouldConvertPayloadToTypeInfo() {
String expectedStringTypeInfoRepresentation = "STRING";
String expectedMessageTypeRepresentation = "STRUCT<string_field:STRING,another_inner_field:STRUCT<string_field:STRING>,another_inner_list_field:ARRAY<STRUCT<string_field:STRING>>>";
String expectedRepeatedMessageTypeRepresentation = String.format("ARRAY<%s>", expectedMessageTypeRepresentation);
String expectedTimestampTypeInfoRepresentation = "TIMESTAMP_NTZ";
String expectedDurationTypeInfoRepresentation = "STRUCT<seconds:BIGINT,nanos:INT>";
String expectedStructTypeInfoRepresentation = "STRING";

TypeInfo stringTypeInfo = baseTypeInfoConverter.convert(descriptor.findFieldByName("string_field"));
TypeInfo messageTypeInfo = baseTypeInfoConverter.convert(descriptor.findFieldByName("inner_field"));
TypeInfo repeatedTypeInfo = baseTypeInfoConverter.convert(descriptor.findFieldByName("inner_list_field"));
TypeInfo timestampTypeInfo = baseTypeInfoConverter.convert(descriptor.findFieldByName("timestamp_field"));
TypeInfo durationTypeInfo = baseTypeInfoConverter.convert(descriptor.findFieldByName("duration_field"));
TypeInfo structTypeInfo = baseTypeInfoConverter.convert(descriptor.findFieldByName("struct_field"));

Assertions.assertEquals(expectedStringTypeInfoRepresentation, stringTypeInfo.toString());
Assertions.assertEquals(expectedMessageTypeRepresentation, messageTypeInfo.toString());
Assertions.assertEquals(expectedRepeatedMessageTypeRepresentation, repeatedTypeInfo.toString());
Assertions.assertEquals(expectedTimestampTypeInfoRepresentation, timestampTypeInfo.toString());
Assertions.assertEquals(expectedDurationTypeInfoRepresentation, durationTypeInfo.toString());
Assertions.assertEquals(expectedStructTypeInfoRepresentation, structTypeInfo.toString());
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowIllegalArgumentExceptionForUnsupportedType() {
Descriptors.FieldDescriptor unsupportedFieldDescriptor = descriptor.findFieldByName("empty_field");
baseTypeInfoConverter.convert(unsupportedFieldDescriptor);
}

}
2 changes: 2 additions & 0 deletions src/test/proto/TestMaxComputeTypeInfo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.gotocompany.depot;
import "google/protobuf/timestamp.proto";
import "google/protobuf/struct.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/empty.proto";

message TestFields {
bytes bytes_field = 1;
Expand Down Expand Up @@ -32,6 +33,7 @@ message TestRoot {
google.protobuf.Timestamp timestamp_field = 4;
google.protobuf.Struct struct_field = 5;
google.protobuf.Duration duration_field = 6;
google.protobuf.Empty empty_field = 7;
}

message TestInner {
Expand Down

0 comments on commit 14bdd12

Please sign in to comment.