Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/googleapis/java-spanner i…
Browse files Browse the repository at this point in the history
…nto adds-query-optimizer-statistics-support
  • Loading branch information
thiagotnunes committed Sep 7, 2020
2 parents a65c801 + cdad9e1 commit 69e8934
Showing 1 changed file with 120 additions and 20 deletions.
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.ByteArray;
import com.google.cloud.Date;
Expand All @@ -37,7 +38,10 @@
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.Value;
import com.google.cloud.spanner.testing.EmulatorSpannerHelper;
import com.google.common.collect.ImmutableList;
import io.grpc.Context;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand All @@ -60,6 +64,47 @@
@RunWith(JUnit4.class)
public class ITWriteTest {
@ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();

// TODO: Remove when the emulator supports NUMERIC
private static final String SCHEMA_WITH_NUMERIC =
"CREATE TABLE T ("
+ " K STRING(MAX) NOT NULL,"
+ " BoolValue BOOL,"
+ " Int64Value INT64,"
+ " Float64Value FLOAT64,"
+ " StringValue STRING(MAX),"
+ " BytesValue BYTES(MAX),"
+ " TimestampValue TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ " DateValue DATE,"
+ " NumericValue NUMERIC,"
+ " BoolArrayValue ARRAY<BOOL>,"
+ " Int64ArrayValue ARRAY<INT64>,"
+ " Float64ArrayValue ARRAY<FLOAT64>,"
+ " StringArrayValue ARRAY<STRING(MAX)>,"
+ " BytesArrayValue ARRAY<BYTES(MAX)>,"
+ " TimestampArrayValue ARRAY<TIMESTAMP>,"
+ " DateArrayValue ARRAY<DATE>,"
+ " NumericArrayValue ARRAY<NUMERIC>,"
+ ") PRIMARY KEY (K)";
private static final String SCHEMA_WITHOUT_NUMERIC =
"CREATE TABLE T ("
+ " K STRING(MAX) NOT NULL,"
+ " BoolValue BOOL,"
+ " Int64Value INT64,"
+ " Float64Value FLOAT64,"
+ " StringValue STRING(MAX),"
+ " BytesValue BYTES(MAX),"
+ " TimestampValue TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ " DateValue DATE,"
+ " BoolArrayValue ARRAY<BOOL>,"
+ " Int64ArrayValue ARRAY<INT64>,"
+ " Float64ArrayValue ARRAY<FLOAT64>,"
+ " StringArrayValue ARRAY<STRING(MAX)>,"
+ " BytesArrayValue ARRAY<BYTES(MAX)>,"
+ " TimestampArrayValue ARRAY<TIMESTAMP>,"
+ " DateArrayValue ARRAY<DATE>,"
+ ") PRIMARY KEY (K)";

private static Database db;
/** Sequence used to generate unique keys. */
private static int seq;
Expand All @@ -68,26 +113,12 @@ public class ITWriteTest {

@BeforeClass
public static void setUpDatabase() {
db =
env.getTestHelper()
.createTestDatabase(
"CREATE TABLE T ("
+ " K STRING(MAX) NOT NULL,"
+ " BoolValue BOOL,"
+ " Int64Value INT64,"
+ " Float64Value FLOAT64,"
+ " StringValue STRING(MAX),"
+ " BytesValue BYTES(MAX),"
+ " TimestampValue TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ " DateValue DATE,"
+ " BoolArrayValue ARRAY<BOOL>,"
+ " Int64ArrayValue ARRAY<INT64>,"
+ " Float64ArrayValue ARRAY<FLOAT64>,"
+ " StringArrayValue ARRAY<STRING(MAX)>,"
+ " BytesArrayValue ARRAY<BYTES(MAX)>,"
+ " TimestampArrayValue ARRAY<TIMESTAMP>,"
+ " DateArrayValue ARRAY<DATE>,"
+ ") PRIMARY KEY (K)");
if (EmulatorSpannerHelper.isUsingEmulator()) {
// The emulator does not yet support NUMERIC.
db = env.getTestHelper().createTestDatabase(SCHEMA_WITHOUT_NUMERIC);
} else {
db = env.getTestHelper().createTestDatabase(SCHEMA_WITH_NUMERIC);
}
client = env.getTestHelper().getDatabaseClient(db);
}

Expand Down Expand Up @@ -354,6 +385,23 @@ public void writeDateNull() {
assertThat(row.isNull(0)).isTrue();
}

@Test
public void writeNumeric() {
assumeFalse("Emulator does not yet support NUMERIC", EmulatorSpannerHelper.isUsingEmulator());
write(baseInsert().set("NumericValue").to(new BigDecimal("3.141592")).build());
Struct row = readLastRow("NumericValue");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBigDecimal(0)).isEqualTo(BigDecimal.valueOf(3141592, 6));
}

@Test
public void writeNumericNull() {
assumeFalse("Emulator does not yet support NUMERIC", EmulatorSpannerHelper.isUsingEmulator());
write(baseInsert().set("NumericValue").to((Long) null).build());
Struct row = readLastRow("NumericValue");
assertThat(row.isNull(0)).isTrue();
}

@Test
public void writeBoolArrayNull() {
write(baseInsert().set("BoolArrayValue").toBoolArray((boolean[]) null).build());
Expand Down Expand Up @@ -577,6 +625,58 @@ public void writeDateArray() {
assertThat(row.getDateList(0)).containsExactly(d1, null, d2).inOrder();
}

@Test
public void writeNumericArrayNull() {
assumeFalse("Emulator does not yet support NUMERIC", EmulatorSpannerHelper.isUsingEmulator());
write(baseInsert().set("NumericArrayValue").toNumericArray(null).build());
Struct row = readLastRow("NumericArrayValue");
assertThat(row.isNull(0)).isTrue();
}

@Test
public void writeNumericArrayEmpty() {
assumeFalse("Emulator does not yet support NUMERIC", EmulatorSpannerHelper.isUsingEmulator());
write(
baseInsert()
.set("NumericArrayValue")
.toNumericArray(ImmutableList.<BigDecimal>of())
.build());
Struct row = readLastRow("NumericArrayValue");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBigDecimalList(0)).containsExactly();
}

@Test
public void writeNumericArray() {
assumeFalse("Emulator does not yet support NUMERIC", EmulatorSpannerHelper.isUsingEmulator());
write(
baseInsert()
.set("NumericArrayValue")
.toNumericArray(
Arrays.asList(new BigDecimal("3.141592"), new BigDecimal("6.626"), null))
.build());
Struct row = readLastRow("NumericArrayValue");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBigDecimalList(0))
.containsExactly(BigDecimal.valueOf(3141592, 6), BigDecimal.valueOf(6626, 3), null)
.inOrder();
}

@Test
public void writeNumericArrayNoNulls() {
assumeFalse("Emulator does not yet support NUMERIC", EmulatorSpannerHelper.isUsingEmulator());
write(
baseInsert()
.set("NumericArrayValue")
.toNumericArray(Arrays.asList(new BigDecimal("3.141592"), new BigDecimal("6.626")))
.build());
Struct row = readLastRow("NumericArrayValue");
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBigDecimalList(0))
.containsExactly(BigDecimal.valueOf(3141592, 6), BigDecimal.valueOf(6626, 3))
.inOrder();
}

@Test
public void tableNotFound() {
// TODO(user): More precise matchers! Customer code needs to discern table not found, column
Expand Down

0 comments on commit 69e8934

Please sign in to comment.