From 1cf12f86a58aa318ab32700dab8043b8b0caaf7c Mon Sep 17 00:00:00 2001 From: Gaole Meng Date: Mon, 30 Oct 2023 20:26:07 -0700 Subject: [PATCH] test: add e2e test for default value use case for stream writer (#2288) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: add e2e test for default value use case for stream writer * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot --- .../it/ITBigQueryWriteManualClientTest.java | 66 +++++++++++++++++++ .../src/test/proto/test.proto | 6 ++ 2 files changed, 72 insertions(+) diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryWriteManualClientTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryWriteManualClientTest.java index d08713de53..7cae021fff 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryWriteManualClientTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryWriteManualClientTest.java @@ -856,6 +856,72 @@ private void testJsonStreamWriterForDefaultValue(JsonStreamWriter jsonStreamWrit assertEquals(false, iter.hasNext()); } + @Test + public void testStreamWriterWithDefaultValue() throws ExecutionException, InterruptedException { + String tableName = "streamWriterWithDefaultValue"; + String exclusiveTableId = + String.format( + "projects/%s/datasets/%s/tables/%s", + ServiceOptions.getDefaultProjectId(), DATASET, tableName); + tableInfo = + TableInfo.newBuilder(TableId.of(DATASET, tableName), defaultValueTableDefinition).build(); + bigquery.create(tableInfo); + try (StreamWriter streamWriter = + StreamWriter.newBuilder(exclusiveTableId + "/_default") + .setWriterSchema( + ProtoSchemaConverter.convert(SimpleTypeForDefaultValue.getDescriptor())) + .setDefaultMissingValueInterpretation(MissingValueInterpretation.DEFAULT_VALUE) + .setEnableConnectionPool(true) + .build()) { + // 1. row has both fields set. + SimpleTypeForDefaultValue simpleTypeForDefaultValue1 = + SimpleTypeForDefaultValue.newBuilder() + .setFooWithDefault("foo_value") + .setBarWithoutDefault("bar_value") + .setDateWithDefaultToCurrent("2022-02-02 01:02:03") + .build(); + + // 2. row without any column set, expect default value to be filled + SimpleTypeForDefaultValue simpleTypeForDefaultValue2 = + SimpleTypeForDefaultValue.newBuilder().build(); + ProtoRows rows = + ProtoRows.newBuilder() + .addSerializedRows(simpleTypeForDefaultValue1.toByteString()) + .addSerializedRows(simpleTypeForDefaultValue2.toByteString()) + .build(); + + // Start insertion and validation. + ApiFuture response1 = streamWriter.append(rows); + response1.get(); + TableResult result = + bigquery.listTableData( + tableInfo.getTableId(), BigQuery.TableDataListOption.startIndex(0L)); + Iterator iter = result.getValues().iterator(); + + FieldValueList currentRow = iter.next(); + assertEquals("foo_value", currentRow.get(0).getStringValue()); + assertEquals("bar_value", currentRow.get(1).getStringValue()); + assertEquals( + Timestamp.valueOf("2022-02-02 01:02:03") + .toLocalDateTime() + .atZone(ZoneId.of("UTC")) + .toInstant() + .toEpochMilli(), + Double.valueOf(currentRow.get(2).getStringValue()).longValue() * 1000); + + currentRow = iter.next(); + assertEquals("default_value_for_test", currentRow.get(0).getStringValue()); + assertEquals(null, currentRow.get(1).getValue()); + assertFalse(currentRow.get(2).getStringValue().isEmpty()); + // Check whether the recorded value is up to date enough. + Instant parsedInstant = + Instant.ofEpochSecond(Double.valueOf(currentRow.get(2).getStringValue()).longValue()); + assertTrue(parsedInstant.isAfter(Instant.now().minus(1, ChronoUnit.HOURS))); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + // This test runs about 1 min. @Test public void testJsonStreamWriterWithMessagesOver10M() diff --git a/google-cloud-bigquerystorage/src/test/proto/test.proto b/google-cloud-bigquerystorage/src/test/proto/test.proto index 191f641f25..8b77a4eb47 100644 --- a/google-cloud-bigquerystorage/src/test/proto/test.proto +++ b/google-cloud-bigquerystorage/src/test/proto/test.proto @@ -50,6 +50,12 @@ message ComplicateType { optional InnerType inner_type = 2; } +message SimpleTypeForDefaultValue { + optional string foo_with_default = 1; + optional string bar_without_default = 2; + optional string date_with_default_to_current = 3; +} + message ContainsRecursive { optional RecursiveType field = 1; }