Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TypeTest.javai #2315

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,207 +1 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.common;

import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.arrayType;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.bytesType;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.int64Type;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.mapType;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.stringType;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.structField;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.structType;
import static com.google.cloud.bigtable.data.v2.stub.sql.SqlProtoFactory.timestampType;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.cloud.bigtable.common.Type.SchemalessStruct;
import com.google.cloud.bigtable.common.Type.StructWithSchema;
import com.google.cloud.bigtable.data.v2.models.sql.SqlType;
import com.google.cloud.bigtable.data.v2.models.sql.Struct;
import com.google.common.testing.EqualsTester;
import com.google.protobuf.ByteString;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class TypeTest {

@Test
public void simpleTypes_TypeToString() {
assertThat(Type.String.create().toString()).isEqualTo("STRING");
assertThat(Type.Bytes.create().toString()).isEqualTo("BYTES");
assertThat(Type.Int64.create().toString()).isEqualTo("INT64");
assertThat(Type.Float64.create().toString()).isEqualTo("FLOAT64");
assertThat(Type.Float32.create().toString()).isEqualTo("FLOAT32");
assertThat(Type.Bool.create().toString()).isEqualTo("BOOL");
assertThat(Type.Timestamp.create().toString()).isEqualTo("TIMESTAMP");
assertThat(Type.Date.create().toString()).isEqualTo("DATE");
assertThat(Type.SchemalessStruct.create().toString()).isEqualTo("STRUCT");
}

@Test
public void simpleTypes_equals() {
assertThat(Type.String.create()).isEqualTo(Type.String.create());
assertThat(Type.Bytes.create()).isEqualTo(Type.Bytes.create());
assertThat(Type.Int64.create()).isEqualTo(Type.Int64.create());
assertThat(Type.Float32.create()).isEqualTo(Type.Float32.create());
assertThat(Type.Float64.create()).isEqualTo(Type.Float64.create());
assertThat(Type.Bool.create()).isEqualTo(Type.Bool.create());
assertThat(Type.Timestamp.create()).isEqualTo(Type.Timestamp.create());
assertThat(Type.Date.create()).isEqualTo(Type.Date.create());
assertThat(Type.SchemalessStruct.create()).isEqualTo(Type.SchemalessStruct.create());

assertThat(Type.String.create()).isNotEqualTo(Type.Bytes.create());
assertThat(Type.Bytes.create()).isNotEqualTo(Type.String.create());
assertThat(Type.Int64.create()).isNotEqualTo(Type.String.create());
assertThat(Type.Float32.create()).isNotEqualTo(Type.String.create());
assertThat(Type.Float64.create()).isNotEqualTo(Type.String.create());
assertThat(Type.Bool.create()).isNotEqualTo(Type.String.create());
assertThat(Type.Timestamp.create()).isNotEqualTo(Type.String.create());
assertThat(Type.Date.create()).isNotEqualTo(Type.String.create());
assertThat(Type.SchemalessStruct.create()).isNotEqualTo(Type.String.create());
}

@Test
public void array_equals() {
assertThat(Type.Array.create(Type.String.create()))
.isEqualTo(Type.Array.create(Type.String.create()));
assertThat(Type.Array.create(Type.String.create()))
.isNotEqualTo(Type.Array.create(Type.Bytes.create()));
// Nested arrays
assertThat(Type.Array.create(Type.Array.create(Type.String.create())))
.isEqualTo(Type.Array.create(Type.Array.create(Type.String.create())));
assertThat(Type.Array.create(Type.Array.create(Type.String.create())))
.isNotEqualTo(Type.Array.create(Type.Array.create(Type.Bytes.create())));
}

@Test
public void map_equals() {
assertThat(Type.Map.create(Type.Bytes.create(), Type.String.create()))
.isEqualTo(Type.Map.create(Type.Bytes.create(), Type.String.create()));
assertThat(Type.Map.create(Type.Bytes.create(), Type.String.create()))
.isNotEqualTo(Type.Map.create(Type.String.create(), Type.String.create()));
assertThat(Type.Map.create(Type.Bytes.create(), Type.String.create()))
.isNotEqualTo(Type.Map.create(Type.Bytes.create(), Type.Bytes.create()));
// Nested Maps
assertThat(
Type.Map.create(
Type.Bytes.create(), Type.Map.create(Type.String.create(), Type.Bytes.create())))
.isEqualTo(
Type.Map.create(
Type.Bytes.create(), Type.Map.create(Type.String.create(), Type.Bytes.create())));
assertThat(
Type.Map.create(
Type.Bytes.create(), Type.Map.create(Type.String.create(), Type.Bytes.create())))
.isNotEqualTo(
Type.Map.create(
Type.Bytes.create(), Type.Map.create(Type.String.create(), Type.String.create())));
}

@Test
public void structWithSchema_equals() {
com.google.bigtable.v2.Type structProto =
structType(structField("timestamp", timestampType()), structField("value", bytesType()));
com.google.bigtable.v2.Type complexStructProto =
structType(
structField("map", mapType(stringType(), bytesType())),
structField("array", arrayType(stringType())));
new EqualsTester()
.addEqualityGroup(
StructWithSchema.fromProto(structProto.getStructType()),
StructWithSchema.fromProto(structProto.getStructType()))
.addEqualityGroup(
StructWithSchema.fromProto(complexStructProto.getStructType()),
StructWithSchema.fromProto(complexStructProto.getStructType()));
}

@Test
public void structWithSchema_fields() {
StructWithSchema struct =
StructWithSchema.fromProto(
structType(structField("timestamp", timestampType()), structField("value", bytesType()))
.getStructType());
assertThat(struct.getFields()).hasSize(2);
assertThat(struct.getFields().get(0).name()).isEqualTo("timestamp");
assertThat(struct.getFields().get(0).type()).isEqualTo(Type.Timestamp.create());
assertThat(struct.getType(0)).isEqualTo(Type.Timestamp.create());
assertThat(struct.getType("timestamp")).isEqualTo(Type.Timestamp.create());
assertThat(struct.getColumnIndex("timestamp")).isEqualTo(0);

assertThat(struct.getFields().get(1).name()).isEqualTo("value");
assertThat(struct.getFields().get(1).type()).isEqualTo(Type.Bytes.create());
assertThat(struct.getType(1)).isEqualTo(Type.Bytes.create());
assertThat(struct.getType("value")).isEqualTo(Type.Bytes.create());
assertThat(struct.getColumnIndex("value")).isEqualTo(1);
}

@Test
public void structWithSchema_handlesAmbiguousFields() {
StructWithSchema struct =
StructWithSchema.fromProto(
structType(structField("foo", timestampType()), structField("foo", bytesType()))
.getStructType());
assertThat(struct.getFields()).hasSize(2);
assertThat(struct.getType(0)).isEqualTo(Type.Timestamp.create());
assertThat(struct.getType(1)).isEqualTo(Type.Bytes.create());

assertThrows(IllegalArgumentException.class, () -> struct.getType("foo"));
assertThrows(IllegalArgumentException.class, () -> struct.getColumnIndex("foo"));
}

@Test
public void structWithSchema_toString() {
StructWithSchema struct =
StructWithSchema.fromProto(
structType(structField("test", stringType()), structField("test2", int64Type()))
.getStructType());
assertThat(struct.toString())
.isEqualTo("STRUCT{fields=[Field{name=test, type=STRING}, Field{name=test2, type=INT64}]}");
}

@Test
public void schemalessStruct_throwsExceptionOnSchemaAccess() {
SchemalessStruct struct = Type.SchemalessStruct.create();

assertThrows(UnsupportedOperationException.class, () -> struct.getType("foo"));
assertThrows(UnsupportedOperationException.class, () -> struct.getType(0));
assertThrows(UnsupportedOperationException.class, () -> struct.getColumnIndex("foo"));
assertThrows(UnsupportedOperationException.class, struct::getFields);
}

@Test
public void array_toString() {
Type array = Type.Array.create(Type.String.create());

assertThat(array.toString()).isEqualTo("ARRAY{elementType=STRING}");
}

@Test
public void simpleMap_toString() {
Type map = Type.Map.create(Type.Bytes.create(), Type.String.create());

assertThat(map.toString()).isEqualTo("MAP{keyType=BYTES, valueType=STRING}");
}

@Test
public void historicalMap_toString() {
SqlType.Map<ByteString, List<Struct>> historicalMap = SqlType.historicalMap();

assertThat(historicalMap.toString())
.isEqualTo("MAP{keyType=BYTES, valueType=ARRAY{elementType=STRUCT}}");
}
}