From 117b49e92abcea275d8a98dde54f5563ec13135c Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 28 May 2020 22:02:08 +0530 Subject: [PATCH] chore: cover code coverage (#395) Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary) Fixes #330 --- .../cloud/bigquery/BigQueryImplTest.java | 258 +++++++++++++++++- .../cloud/bigquery/BigtableOptionsTest.java | 57 ++++ .../bigquery/ExternalTableDefinitionTest.java | 12 + .../com/google/cloud/bigquery/JobTest.java | 2 + .../bigquery/LoadJobConfigurationTest.java | 11 + .../bigquery/ModelTableDefinitionTest.java | 109 ++++++++ .../google/cloud/bigquery/PolicyTagsTest.java | 60 ++++ .../bigquery/QueryJobConfigurationTest.java | 42 +++ .../bigquery/QueryParameterValueTest.java | 29 ++ .../google/cloud/bigquery/QueryStageTest.java | 7 + .../cloud/bigquery/RoutineInfoTest.java | 1 + .../bigquery/StandardTableDefinitionTest.java | 13 + .../com/google/cloud/bigquery/TableTest.java | 15 + .../cloud/bigquery/TimePartitioningTest.java | 3 + .../cloud/bigquery/TimelineSampleTest.java | 10 + .../cloud/bigquery/ViewDefinitionTest.java | 16 +- 16 files changed, 643 insertions(+), 2 deletions(-) create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ModelTableDefinitionTest.java create mode 100644 google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PolicyTagsTest.java diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index ac11f40d0..6435ff91a 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -53,6 +53,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import java.io.IOException; import java.math.BigInteger; import java.util.Collections; import java.util.List; @@ -78,6 +79,15 @@ public class BigQueryImplTest { private static final String JOB = "job"; private static final String OTHER_TABLE = "otherTable"; private static final String OTHER_DATASET = "otherDataset"; + private static final String ROUTINE = "routine"; + private static final RoutineId ROUTINE_ID = RoutineId.of(DATASET, ROUTINE); + private static final String ETAG = "etag"; + private static final String ROUTINE_TYPE = "SCALAR_FUNCTION"; + private static final Long CREATION_TIME = 10L; + private static final Long LAST_MODIFIED_TIME = 20L; + private static final String LANGUAGE = "SQL"; + private static final String UPLOAD_ID = "uploadid"; + private static final int MIN_CHUNK_SIZE = 256 * 1024; private static final List ACCESS_RULES = ImmutableList.of( Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER), @@ -439,6 +449,43 @@ public class BigQueryImplTest { BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.MAX_RESULTS, 0L); + private static final RoutineArgument ARG_1 = + RoutineArgument.newBuilder() + .setDataType(StandardSQLDataType.newBuilder("STRING").build()) + .setName("arg1") + .build(); + + private static final List ARGUMENT_LIST = ImmutableList.of(ARG_1); + + private static final StandardSQLDataType RETURN_TYPE = + StandardSQLDataType.newBuilder("FLOAT64").build(); + + private static final List IMPORTED_LIBRARIES = + ImmutableList.of("gs://foo", "gs://bar", "gs://baz"); + + private static final String BODY = "body"; + + private static final RoutineInfo ROUTINE_INFO = + RoutineInfo.newBuilder(ROUTINE_ID) + .setEtag(ETAG) + .setRoutineType(ROUTINE_TYPE) + .setCreationTime(CREATION_TIME) + .setLastModifiedTime(LAST_MODIFIED_TIME) + .setLanguage(LANGUAGE) + .setArguments(ARGUMENT_LIST) + .setReturnType(RETURN_TYPE) + .setImportedLibraries(IMPORTED_LIBRARIES) + .setBody(BODY) + .build(); + private static final WriteChannelConfiguration LOAD_CONFIGURATION = + WriteChannelConfiguration.newBuilder(TABLE_ID) + .setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED) + .setWriteDisposition(JobInfo.WriteDisposition.WRITE_APPEND) + .setFormatOptions(FormatOptions.json()) + .setIgnoreUnknownValues(true) + .setMaxBadRecords(10) + .build(); + private BigQueryOptions options; private BigQueryRpcFactory rpcFactoryMock; private BigQueryRpc bigqueryRpcMock; @@ -446,6 +493,9 @@ public class BigQueryImplTest { @Captor private ArgumentCaptor> capturedOptions; @Captor private ArgumentCaptor jobCapture; + @Captor private ArgumentCaptor capturedBuffer; + + private TableDataWriteChannel writer; private BigQueryOptions createBigQueryOptionsForProject( String project, BigQueryRpcFactory rpcFactory) { @@ -535,6 +585,7 @@ public void testGetDatasetNotFoundWhenThrowIsDisabled() { @Test public void testGetDatasetNotFoundWhenThrowIsEnabled() { when(bigqueryRpcMock.getDataset(PROJECT, "dataset-not-found", EMPTY_RPC_OPTIONS)) + .thenReturn(null) .thenThrow(new BigQueryException(404, "Dataset not found")); options.setThrowNotFound(true); bigquery = options.getService(); @@ -782,6 +833,22 @@ public void testGetModel() { verify(bigqueryRpcMock).getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS); } + @Test + public void testGetModelNotFoundWhenThrowIsEnabled() { + String expected = "Model not found"; + when(bigqueryRpcMock.getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS)) + .thenReturn(null) + .thenThrow(new BigQueryException(404, expected)); + options.setThrowNotFound(true); + bigquery = options.getService(); + try { + bigquery.getModel(DATASET, MODEL); + } catch (BigQueryException ex) { + assertEquals(expected, ex.getMessage()); + } + verify(bigqueryRpcMock).getModel(PROJECT, DATASET, MODEL, EMPTY_RPC_OPTIONS); + } + @Test public void testListPartition() { when(bigqueryRpcMock.getTable( @@ -811,6 +878,7 @@ public void testGetTableNotFoundWhenThrowIsDisabled() { @Test public void testGetTableNotFoundWhenThrowIsEnabled() { when(bigqueryRpcMock.getTable(PROJECT, DATASET, "table-not-found", EMPTY_RPC_OPTIONS)) + .thenReturn(null) .thenThrow(new BigQueryException(404, "Table not found")); options.setThrowNotFound(true); bigquery = options.getService(); @@ -1001,6 +1069,22 @@ public void testListModels() { verify(bigqueryRpcMock).listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS); } + @Test + public void testListModelsWithModelId() { + bigquery = options.getService(); + ImmutableList modelList = + ImmutableList.of( + new Model(bigquery, new ModelInfo.BuilderImpl(MODEL_INFO_WITH_PROJECT)), + new Model(bigquery, new ModelInfo.BuilderImpl(OTHER_MODEL_INFO))); + Tuple> result = + Tuple.of(CURSOR, Iterables.transform(modelList, ModelInfo.TO_PB_FUNCTION)); + when(bigqueryRpcMock.listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + Page page = bigquery.listModels(DatasetId.of(DATASET)); + assertEquals(CURSOR, page.getNextPageToken()); + assertArrayEquals(modelList.toArray(), Iterables.toArray(page.getValues(), Model.class)); + verify(bigqueryRpcMock).listModels(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + } + @Test public void testDeleteTable() { when(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).thenReturn(true); @@ -1046,6 +1130,24 @@ public void testDeleteModel() { verify(bigqueryRpcMock).deleteModel(PROJECT, DATASET, MODEL); } + @Test + public void testUpdateModel() { + ModelInfo updateModelInfo = + MODEL_INFO_WITH_PROJECT + .setProjectId(OTHER_PROJECT) + .toBuilder() + .setDescription("newDescription") + .build(); + when(bigqueryRpcMock.patch(updateModelInfo.toPb(), EMPTY_RPC_OPTIONS)) + .thenReturn(updateModelInfo.toPb()); + BigQueryOptions bigQueryOptions = + createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); + bigquery = bigQueryOptions.getService(); + Model actualModel = bigquery.update(updateModelInfo); + assertEquals(new Model(bigquery, new ModelInfo.BuilderImpl(updateModelInfo)), actualModel); + verify(bigqueryRpcMock).patch(updateModelInfo.toPb(), EMPTY_RPC_OPTIONS); + } + @Test public void testUpdateTable() { TableInfo updatedTableInfo = @@ -1487,6 +1589,7 @@ public void testGetJobNotFoundWhenThrowIsDisabled() { @Test public void testGetJobNotFoundWhenThrowIsEnabled() { when(bigqueryRpcMock.getJob(PROJECT, "job-not-found", null, EMPTY_RPC_OPTIONS)) + .thenReturn(null) .thenThrow(new BigQueryException(404, "Job not found")); options.setThrowNotFound(true); bigquery = options.getService(); @@ -1900,7 +2003,7 @@ public void testGetQueryResultsWithOptions() { } @Test - public void testRetryableException() { + public void testGetDatasetRetryableException() { when(bigqueryRpcMock.getDataset(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) .thenThrow(new BigQueryException(500, "InternalError")) .thenReturn(DATASET_INFO_WITH_PROJECT.toPb()); @@ -1971,4 +2074,157 @@ public void testQueryDryRun() throws Exception { Assert.assertNotNull(ex.getMessage()); } } + + @Test + public void testCreateRoutine() { + RoutineInfo routineInfo = ROUTINE_INFO.setProjectId(OTHER_PROJECT); + when(bigqueryRpcMock.create(routineInfo.toPb(), EMPTY_RPC_OPTIONS)) + .thenReturn(routineInfo.toPb()); + BigQueryOptions bigQueryOptions = + createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); + bigquery = bigQueryOptions.getService(); + Routine actualRoutine = bigquery.create(routineInfo); + assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(routineInfo)), actualRoutine); + verify(bigqueryRpcMock).create(routineInfo.toPb(), EMPTY_RPC_OPTIONS); + } + + @Test + public void testGetRoutine() { + when(bigqueryRpcMock.getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) + .thenReturn(ROUTINE_INFO.toPb()); + bigquery = options.getService(); + Routine routine = bigquery.getRoutine(DATASET, ROUTINE); + assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)), routine); + verify(bigqueryRpcMock).getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); + } + + @Test + public void testGetRoutineWithRountineId() { + when(bigqueryRpcMock.getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) + .thenReturn(ROUTINE_INFO.toPb()); + bigquery = options.getService(); + Routine routine = bigquery.getRoutine(ROUTINE_ID); + assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO)), routine); + verify(bigqueryRpcMock).getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); + } + + @Test + public void testGetRoutineWithEnabledThrowNotFoundException() { + when(bigqueryRpcMock.getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS)) + .thenReturn(null) + .thenThrow(new BigQueryException(404, "Routine not found")); + options.setThrowNotFound(true); + bigquery = options.getService(); + try { + Routine routine = bigquery.getRoutine(ROUTINE_ID); + fail(); + } catch (BigQueryException ex) { + assertEquals("Routine not found", ex.getMessage()); + } + verify(bigqueryRpcMock).getRoutine(PROJECT, DATASET, ROUTINE, EMPTY_RPC_OPTIONS); + } + + @Test + public void testUpdateRoutine() { + RoutineInfo updatedRoutineInfo = + ROUTINE_INFO + .setProjectId(OTHER_PROJECT) + .toBuilder() + .setDescription("newDescription") + .build(); + when(bigqueryRpcMock.update(updatedRoutineInfo.toPb(), EMPTY_RPC_OPTIONS)) + .thenReturn(updatedRoutineInfo.toPb()); + BigQueryOptions bigQueryOptions = + createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); + bigquery = bigQueryOptions.getService(); + Routine routine = bigquery.update(updatedRoutineInfo); + assertEquals(new Routine(bigquery, new RoutineInfo.BuilderImpl(updatedRoutineInfo)), routine); + verify(bigqueryRpcMock).update(updatedRoutineInfo.toPb(), EMPTY_RPC_OPTIONS); + } + + @Test + public void testListRoutines() { + bigquery = options.getService(); + ImmutableList routineList = + ImmutableList.of(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO))); + Tuple> result = + Tuple.of(CURSOR, Iterables.transform(routineList, RoutineInfo.TO_PB_FUNCTION)); + when(bigqueryRpcMock.listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + Page page = bigquery.listRoutines(DATASET); + assertEquals(CURSOR, page.getNextPageToken()); + assertArrayEquals(routineList.toArray(), Iterables.toArray(page.getValues(), Routine.class)); + verify(bigqueryRpcMock).listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + } + + @Test + public void testListRoutinesWithDatasetId() { + bigquery = options.getService(); + ImmutableList routineList = + ImmutableList.of(new Routine(bigquery, new RoutineInfo.BuilderImpl(ROUTINE_INFO))); + Tuple> result = + Tuple.of(CURSOR, Iterables.transform(routineList, RoutineInfo.TO_PB_FUNCTION)); + when(bigqueryRpcMock.listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS)).thenReturn(result); + Page page = bigquery.listRoutines(DatasetId.of(PROJECT, DATASET)); + assertEquals(CURSOR, page.getNextPageToken()); + assertArrayEquals(routineList.toArray(), Iterables.toArray(page.getValues(), Routine.class)); + verify(bigqueryRpcMock).listRoutines(PROJECT, DATASET, EMPTY_RPC_OPTIONS); + } + + @Test + public void testDeleteRoutine() { + when(bigqueryRpcMock.deleteRoutine(PROJECT, DATASET, ROUTINE)).thenReturn(true); + bigquery = options.getService(); + assertTrue(bigquery.delete(ROUTINE_ID)); + verify(bigqueryRpcMock).deleteRoutine(PROJECT, DATASET, ROUTINE); + } + + @Test + public void testWriteWithJob() throws IOException { + bigquery = options.getService(); + Job job = new Job(bigquery, new JobInfo.BuilderImpl(JOB_INFO)); + when(bigqueryRpcMock.open( + new com.google.api.services.bigquery.model.Job() + .setJobReference(JOB_INFO.getJobId().toPb()) + .setConfiguration(LOAD_CONFIGURATION.toPb()))) + .thenReturn(UPLOAD_ID); + when(bigqueryRpcMock.write( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) + .thenReturn(job.toPb()); + writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); + writer.close(); + assertEquals(job, writer.getJob()); + bigquery.writer(JOB_INFO.getJobId(), LOAD_CONFIGURATION); + verify(bigqueryRpcMock) + .open( + new com.google.api.services.bigquery.model.Job() + .setJobReference(JOB_INFO.getJobId().toPb()) + .setConfiguration(LOAD_CONFIGURATION.toPb())); + verify(bigqueryRpcMock) + .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + } + + @Test + public void testWriteChannel() throws IOException { + bigquery = options.getService(); + Job job = new Job(bigquery, new JobInfo.BuilderImpl(JOB_INFO)); + when(bigqueryRpcMock.open( + new com.google.api.services.bigquery.model.Job() + .setJobReference(JOB_INFO.getJobId().toPb()) + .setConfiguration(LOAD_CONFIGURATION.toPb()))) + .thenReturn(UPLOAD_ID); + when(bigqueryRpcMock.write( + eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true))) + .thenReturn(job.toPb()); + writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION); + writer.close(); + assertEquals(job, writer.getJob()); + bigquery.writer(LOAD_CONFIGURATION); + verify(bigqueryRpcMock) + .open( + new com.google.api.services.bigquery.model.Job() + .setJobReference(JOB_INFO.getJobId().toPb()) + .setConfiguration(LOAD_CONFIGURATION.toPb())); + verify(bigqueryRpcMock) + .write(eq(UPLOAD_ID), capturedBuffer.capture(), eq(0), eq(0L), eq(0), eq(true)); + } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigtableOptionsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigtableOptionsTest.java index 51d27b793..88fa1595e 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigtableOptionsTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigtableOptionsTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableList; +import org.junit.Assert; import org.junit.Test; public class BigtableOptionsTest { @@ -61,6 +62,8 @@ public void testConstructors() { assertThat(COL1.getOnlyReadLatest()).isEqualTo(true); assertThat(COL1.getEncoding()).isEqualTo("BINARY"); assertThat(COL1.getType()).isEqualTo("BYTES"); + assertThat(COL1.equals(COL1)).isTrue(); + assertThat(COL1).isNotEqualTo(TESTFAMILY); // family assertThat(TESTFAMILY.getFamilyID()).isEqualTo("fooFamily"); @@ -73,6 +76,50 @@ public void testConstructors() { assertThat(OPTIONS.getIgnoreUnspecifiedColumnFamilies()).isEqualTo(true); assertThat(OPTIONS.getReadRowkeyAsString()).isEqualTo(true); assertThat(OPTIONS.getColumnFamilies()).isEqualTo(ImmutableList.of(TESTFAMILY)); + compareBigtableOptions(OPTIONS, OPTIONS.toBuilder().build()); + } + + @Test + public void testNullPointerException() { + try { + BigtableColumnFamily.newBuilder().setFamilyID(null).build(); + Assert.fail(); + } catch (NullPointerException ex) { + assertThat(ex.getMessage()).isNotNull(); + } + try { + BigtableColumnFamily.newBuilder().setColumns(null).build(); + Assert.fail(); + } catch (NullPointerException ex) { + assertThat(ex.getMessage()).isNotNull(); + } + try { + BigtableColumnFamily.newBuilder().setEncoding(null).build(); + Assert.fail(); + } catch (NullPointerException ex) { + assertThat(ex.getMessage()).isNotNull(); + } + try { + BigtableColumnFamily.newBuilder().setOnlyReadLatest(null).build(); + Assert.fail(); + } catch (NullPointerException ex) { + assertThat(ex.getMessage()).isNotNull(); + } + try { + BigtableColumnFamily.newBuilder().setType(null).build(); + Assert.fail(); + } catch (NullPointerException ex) { + assertThat(ex.getMessage()).isNotNull(); + } + } + + @Test + public void testIllegalStateException() { + try { + BigtableColumnFamily.newBuilder().build(); + } catch (IllegalStateException ex) { + assertThat(ex.getMessage()).isNotNull(); + } } @Test @@ -86,6 +133,10 @@ public void testToAndFromPb() { public void testEquals() { compareBigtableColumn(COL1, COL1); compareBigtableColumnFamily(TESTFAMILY, TESTFAMILY); + assertThat(TESTFAMILY.equals(TESTFAMILY)).isTrue(); + assertThat(TESTFAMILY).isNotEqualTo(COL1); + assertThat(OPTIONS.equals(OPTIONS)).isTrue(); + assertThat(OPTIONS).isNotEqualTo(TESTFAMILY); compareBigtableOptions(OPTIONS, OPTIONS); } @@ -96,6 +147,8 @@ private void compareBigtableColumn(BigtableColumn expected, BigtableColumn value assertThat(expected.getQualifierEncoded()).isEqualTo(value.getQualifierEncoded()); assertThat(expected.getOnlyReadLatest()).isEqualTo(value.getOnlyReadLatest()); assertThat(expected.getType()).isEqualTo(value.getType()); + assertThat(expected.toString()).isEqualTo(value.toString()); + assertThat(expected.hashCode()).isEqualTo(value.hashCode()); } private void compareBigtableColumnFamily( @@ -106,6 +159,8 @@ private void compareBigtableColumnFamily( assertThat(expected.getColumns()).isEqualTo(value.getColumns()); assertThat(expected.getEncoding()).isEqualTo(value.getEncoding()); assertThat(expected.getType()).isEqualTo(value.getType()); + assertThat(expected.toString()).isEqualTo(value.toString()); + assertThat(expected.hashCode()).isEqualTo(value.hashCode()); } private void compareBigtableOptions(BigtableOptions expected, BigtableOptions value) { @@ -114,5 +169,7 @@ private void compareBigtableOptions(BigtableOptions expected, BigtableOptions va .isEqualTo(value.getIgnoreUnspecifiedColumnFamilies()); assertThat(expected.getReadRowkeyAsString()).isEqualTo(value.getReadRowkeyAsString()); assertThat(expected.getColumnFamilies()).isEqualTo(value.getColumnFamilies()); + assertThat(expected.hashCode()).isEqualTo(value.hashCode()); + assertThat(expected.toString()).isEqualTo(value.toString()); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java index bd5a6cdfb..ea8ca1ada 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ExternalTableDefinitionTest.java @@ -17,6 +17,8 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import com.google.common.collect.ImmutableList; import java.util.List; @@ -79,6 +81,15 @@ public void testToBuilderIncomplete() { assertEquals(externalTableDefinition, externalTableDefinition.toBuilder().build()); } + @Test + public void testTypeNullPointerException() { + try { + EXTERNAL_TABLE_DEFINITION.toBuilder().setType(null).build(); + } catch (NullPointerException ex) { + assertNotNull(ex.getMessage()); + } + } + @Test public void testBuilder() { assertEquals(TableDefinition.Type.EXTERNAL, EXTERNAL_TABLE_DEFINITION.getType()); @@ -90,6 +101,7 @@ public void testBuilder() { assertEquals(SOURCE_URIS, EXTERNAL_TABLE_DEFINITION.getSourceUris()); assertEquals(AUTODETECT, EXTERNAL_TABLE_DEFINITION.getAutodetect()); assertEquals(HIVE_PARTITIONING_OPTIONS, EXTERNAL_TABLE_DEFINITION.getHivePartitioningOptions()); + assertNotEquals(EXTERNAL_TABLE_DEFINITION, TableDefinition.Type.EXTERNAL); } @Test diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java index 68ccfd627..1d34f4958 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; @@ -471,6 +472,7 @@ public void testToAndFromPb() { @Test public void testToAndFromPbWithoutConfiguration() { + assertNotEquals(expectedJob, bigquery); compareJob(expectedJob, Job.fromPb(bigquery, expectedJob.toPb())); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LoadJobConfigurationTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LoadJobConfigurationTest.java index b64e3c451..9f42d62b7 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LoadJobConfigurationTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/LoadJobConfigurationTest.java @@ -67,6 +67,13 @@ public class LoadJobConfigurationTest { RangePartitioning.Range.newBuilder().setStart(1L).setInterval(2L).setEnd(10L).build(); private static final RangePartitioning RANGE_PARTITIONING = RangePartitioning.newBuilder().setField("IntegerField").setRange(RANGE).build(); + private static final String MODE = "STRING"; + private static final String SOURCE_URI_PREFIX = "gs://bucket/path_to_table"; + private static final HivePartitioningOptions HIVE_PARTITIONING_OPTIONS = + HivePartitioningOptions.newBuilder() + .setMode(MODE) + .setSourceUriPrefix(SOURCE_URI_PREFIX) + .build(); private static final LoadJobConfiguration LOAD_CONFIGURATION_CSV = LoadJobConfiguration.newBuilder(TABLE_ID, SOURCE_URIS) .setCreateDisposition(CREATE_DISPOSITION) @@ -83,6 +90,8 @@ public class LoadJobConfigurationTest { .setLabels(LABELS) .setJobTimeoutMs(TIMEOUT) .setRangePartitioning(RANGE_PARTITIONING) + .setNullMarker("nullMarker") + .setHivePartitioningOptions(HIVE_PARTITIONING_OPTIONS) .build(); private static final DatastoreBackupOptions BACKUP_OPTIONS = @@ -238,5 +247,7 @@ private void compareLoadJobConfiguration( assertEquals(expected.getLabels(), value.getLabels()); assertEquals(expected.getJobTimeoutMs(), value.getJobTimeoutMs()); assertEquals(expected.getRangePartitioning(), value.getRangePartitioning()); + assertEquals(expected.getNullMarker(), value.getNullMarker()); + assertEquals(expected.getHivePartitioningOptions(), value.getHivePartitioningOptions()); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ModelTableDefinitionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ModelTableDefinitionTest.java new file mode 100644 index 000000000..62b2cfe7d --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ModelTableDefinitionTest.java @@ -0,0 +1,109 @@ +/* + * Copyright 2020 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 + * + * http://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.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; + +public class ModelTableDefinitionTest { + + private static final String LOCATION = "US"; + private static final Long NUM_BYTES = 14L; + private static final Field FIELD_SCHEMA1 = + Field.newBuilder("StringField", LegacySQLTypeName.STRING) + .setMode(Field.Mode.NULLABLE) + .setDescription("FieldDescription1") + .build(); + private static final Field FIELD_SCHEMA2 = + Field.newBuilder("IntegerField", LegacySQLTypeName.INTEGER) + .setMode(Field.Mode.REPEATED) + .setDescription("FieldDescription2") + .build(); + private static final Field FIELD_SCHEMA3 = + Field.newBuilder("RecordField", LegacySQLTypeName.RECORD, FIELD_SCHEMA1, FIELD_SCHEMA2) + .setMode(Field.Mode.REQUIRED) + .setDescription("FieldDescription3") + .build(); + private static final Schema TABLE_SCHEMA = Schema.of(FIELD_SCHEMA1, FIELD_SCHEMA2, FIELD_SCHEMA3); + private static final ModelTableDefinition MODEL_TABLE_DEFINITION = + ModelTableDefinition.newBuilder() + .setSchema(TABLE_SCHEMA) + .setType(TableDefinition.Type.MODEL) + .setLocation(LOCATION) + .setNumBytes(NUM_BYTES) + .build(); + + @Test + public void testToBuilder() { + compareModelTableDefinition(MODEL_TABLE_DEFINITION, MODEL_TABLE_DEFINITION.toBuilder().build()); + } + + @Test + public void testTypeNullPointerException() { + try { + MODEL_TABLE_DEFINITION.toBuilder().setType(null).build(); + fail(); + } catch (NullPointerException ex) { + assertNotNull(ex.getMessage()); + } + } + + @Test + public void testToBuilderIncomplete() { + ModelTableDefinition modelTableDefinition = ModelTableDefinition.newBuilder().build(); + assertEquals(modelTableDefinition, modelTableDefinition.toBuilder().build()); + } + + @Test + public void testToAndFromPb() { + assertEquals( + MODEL_TABLE_DEFINITION, ModelTableDefinition.fromPb(MODEL_TABLE_DEFINITION.toPb())); + } + + @Test + public void testBuilder() { + assertEquals(MODEL_TABLE_DEFINITION.getSchema(), TABLE_SCHEMA); + assertEquals(MODEL_TABLE_DEFINITION.getType(), TableDefinition.Type.MODEL); + assertEquals(MODEL_TABLE_DEFINITION.getLocation(), LOCATION); + assertEquals(MODEL_TABLE_DEFINITION.getNumBytes(), NUM_BYTES); + } + + @Test + public void testEquals() { + assertEquals(MODEL_TABLE_DEFINITION, MODEL_TABLE_DEFINITION); + } + + @Test + public void testNotEquals() { + assertNotEquals(MODEL_TABLE_DEFINITION, LOCATION); + } + + private void compareModelTableDefinition( + ModelTableDefinition expected, ModelTableDefinition value) { + assertEquals(expected, value); + assertEquals(expected.getSchema(), value.getSchema()); + assertEquals(expected.getType(), value.getType()); + assertEquals(expected.getLocation(), value.getLocation()); + assertEquals(expected.getNumBytes(), value.getNumBytes()); + assertEquals(expected.hashCode(), value.hashCode()); + assertEquals(expected.toString(), value.toString()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PolicyTagsTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PolicyTagsTest.java new file mode 100644 index 000000000..2136b2dab --- /dev/null +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/PolicyTagsTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2020 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 + * + * http://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.bigquery; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.junit.Test; + +public class PolicyTagsTest { + + private static final List POLICIES = ImmutableList.of("test/policy1", "test/policy2"); + private static final PolicyTags POLICY_TAGS = PolicyTags.newBuilder().setNames(POLICIES).build(); + + @Test + public void testToBuilder() { + comparePolicyTags(POLICY_TAGS, POLICY_TAGS.toBuilder().build()); + } + + @Test + public void testToBuilderIncomplete() { + PolicyTags policyTags = PolicyTags.newBuilder().build(); + assertEquals(policyTags, policyTags); + assertEquals(policyTags, policyTags.toBuilder().build()); + } + + @Test + public void testBuilder() { + assertEquals(POLICIES, POLICY_TAGS.getNames()); + assertNotEquals(POLICY_TAGS, POLICIES); + } + + @Test + public void testFromAndPb() { + assertEquals(POLICY_TAGS, PolicyTags.fromPb(POLICY_TAGS.toPb())); + } + + private void comparePolicyTags(PolicyTags expected, PolicyTags value) { + assertEquals(expected, value); + assertEquals(expected.getNames(), value.getNames()); + assertEquals(expected.hashCode(), value.hashCode()); + assertEquals(expected.toString(), value.toString()); + } +} diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryJobConfigurationTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryJobConfigurationTest.java index 406d7edfa..0e892b6e1 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryJobConfigurationTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryJobConfigurationTest.java @@ -80,6 +80,7 @@ public class QueryJobConfigurationTest { private static final boolean FLATTEN_RESULTS = true; private static final boolean USE_LEGACY_SQL = true; private static final Integer MAX_BILLING_TIER = 123; + private static final Long MAX_BYTES_BILL = 12345L; private static final List SCHEMA_UPDATE_OPTIONS = ImmutableList.of(SchemaUpdateOption.ALLOW_FIELD_RELAXATION); private static final List USER_DEFINED_FUNCTIONS = @@ -96,6 +97,14 @@ public class QueryJobConfigurationTest { RangePartitioning.Range.newBuilder().setStart(1L).setInterval(2L).setEnd(10L).build(); private static final RangePartitioning RANGE_PARTITIONING = RangePartitioning.newBuilder().setField("IntegerField").setRange(RANGE).build(); + private static final QueryParameterValue STRING_PARAMETER = + QueryParameterValue.string("stringValue"); + private static final QueryParameterValue TIMESTAMP_PARAMETER = + QueryParameterValue.timestamp("2014-01-01 07:00:00.000000+00:00"); + private static final List POSITIONAL_PARAMETER = + ImmutableList.of(STRING_PARAMETER, TIMESTAMP_PARAMETER); + private static final Map NAME_PARAMETER = + ImmutableMap.of("string", STRING_PARAMETER, "timestamp", TIMESTAMP_PARAMETER); private static final QueryJobConfiguration QUERY_JOB_CONFIGURATION = QueryJobConfiguration.newBuilder(QUERY) .setUseQueryCache(USE_QUERY_CACHE) @@ -111,6 +120,7 @@ public class QueryJobConfigurationTest { .setDryRun(true) .setUseLegacySql(USE_LEGACY_SQL) .setMaximumBillingTier(MAX_BILLING_TIER) + .setMaximumBytesBilled(MAX_BYTES_BILL) .setSchemaUpdateOptions(SCHEMA_UPDATE_OPTIONS) .setDestinationEncryptionConfiguration(JOB_ENCRYPTION_CONFIGURATION) .setTimePartitioning(TIME_PARTITIONING) @@ -119,6 +129,19 @@ public class QueryJobConfigurationTest { .setLabels(LABELS) .setRangePartitioning(RANGE_PARTITIONING) .setConnectionProperties(CONNECTION_PROPERTIES) + .setPositionalParameters(POSITIONAL_PARAMETER) + .build(); + private static final QueryJobConfiguration QUERY_JOB_CONFIGURATION_ADD_POSITIONAL_PARAMETER = + QUERY_JOB_CONFIGURATION + .toBuilder() + .setPositionalParameters(ImmutableList.of()) + .addPositionalParameter(STRING_PARAMETER) + .build(); + private static final QueryJobConfiguration QUERY_JOB_CONFIGURATION_SET_NAME_PARAMETER = + QUERY_JOB_CONFIGURATION + .toBuilder() + .setPositionalParameters(ImmutableList.of()) + .setNamedParameters(NAME_PARAMETER) .build(); @Test @@ -154,6 +177,8 @@ public void testToPbAndFromPb() { assertNotNull(QUERY_JOB_CONFIGURATION.getLabels()); assertNotNull(QUERY_JOB_CONFIGURATION.getRangePartitioning()); assertNotNull(QUERY_JOB_CONFIGURATION.getConnectionProperties()); + assertNotNull(QUERY_JOB_CONFIGURATION.getPositionalParameters()); + assertNotNull(QUERY_JOB_CONFIGURATION.getNamedParameters()); compareQueryJobConfiguration( QUERY_JOB_CONFIGURATION, QueryJobConfiguration.fromPb(QUERY_JOB_CONFIGURATION.toPb())); QueryJobConfiguration job = QueryJobConfiguration.of(QUERY); @@ -184,6 +209,20 @@ public void testGetType() { assertEquals(JobConfiguration.Type.QUERY, QUERY_JOB_CONFIGURATION.getType()); } + @Test + public void testPositionalParameter() { + compareQueryJobConfiguration( + QUERY_JOB_CONFIGURATION_ADD_POSITIONAL_PARAMETER, + QUERY_JOB_CONFIGURATION_ADD_POSITIONAL_PARAMETER.toBuilder().build()); + } + + @Test + public void testNamedParameter() { + compareQueryJobConfiguration( + QUERY_JOB_CONFIGURATION_SET_NAME_PARAMETER, + QUERY_JOB_CONFIGURATION_SET_NAME_PARAMETER.toBuilder().build()); + } + private void compareQueryJobConfiguration( QueryJobConfiguration expected, QueryJobConfiguration value) { assertEquals(expected, value); @@ -203,6 +242,7 @@ private void compareQueryJobConfiguration( assertEquals(expected.getWriteDisposition(), value.getWriteDisposition()); assertEquals(expected.useLegacySql(), value.useLegacySql()); assertEquals(expected.getMaximumBillingTier(), value.getMaximumBillingTier()); + assertEquals(expected.getMaximumBytesBilled(), value.getMaximumBytesBilled()); assertEquals(expected.getSchemaUpdateOptions(), value.getSchemaUpdateOptions()); assertEquals( expected.getDestinationEncryptionConfiguration(), @@ -213,5 +253,7 @@ private void compareQueryJobConfiguration( assertEquals(expected.getLabels(), value.getLabels()); assertEquals(expected.getRangePartitioning(), value.getRangePartitioning()); assertEquals(expected.getConnectionProperties(), value.getConnectionProperties()); + assertEquals(expected.getPositionalParameters(), value.getPositionalParameters()); + assertEquals(expected.getNamedParameters(), value.getNamedParameters()); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java index 930a29c40..09421565c 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryParameterValueTest.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Test; import org.threeten.bp.Instant; import org.threeten.bp.ZoneOffset; @@ -58,6 +59,34 @@ public class QueryParameterValueTest { .toFormatter() .withZone(ZoneOffset.UTC); + private static final QueryParameterValue QUERY_PARAMETER_VALUE = + QueryParameterValue.newBuilder() + .setType(StandardSQLTypeName.STRING) + .setValue("test-string") + .build(); + + @Test + public void testBuilder() { + QueryParameterValue value = QUERY_PARAMETER_VALUE.toBuilder().build(); + assertThat(value).isEqualTo(QUERY_PARAMETER_VALUE); + assertThat(value.getType()).isEqualTo(StandardSQLTypeName.STRING); + assertThat(value.getValue()).isEqualTo("test-string"); + assertThat(value.toString()).isEqualTo(QUERY_PARAMETER_VALUE.toString()); + assertThat(value.hashCode()).isEqualTo(QUERY_PARAMETER_VALUE.hashCode()); + assertThat(value.equals(value)).isTrue(); + assertThat(QUERY_PARAMETER_VALUE).isNotEqualTo(StandardSQLTypeName.STRING); + } + + @Test + public void testTypeNullPointerException() { + try { + QUERY_PARAMETER_VALUE.toBuilder().setType(null).build(); + Assert.fail(); + } catch (NullPointerException ex) { + assertThat(ex).isNotNull(); + } + } + @Test public void testBool() { QueryParameterValue value = QueryParameterValue.bool(true); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryStageTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryStageTest.java index 69edb3988..bc7d6083b 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryStageTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/QueryStageTest.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import com.google.api.services.bigquery.model.ExplainQueryStep; import com.google.cloud.bigquery.QueryStage.QueryStep; @@ -154,6 +155,12 @@ public void testEquals() { compareQueryStage(QUERY_STAGE, QUERY_STAGE); } + @Test + public void testNotEquals() { + assertNotEquals(QUERY_STAGE, QUERY_STEP1); + assertNotEquals(QUERY_STEP1, QUERY_STAGE); + } + private void compareQueryStage(QueryStage expected, QueryStage value) { assertEquals(expected, value); assertEquals(expected.getCompletedParallelInputs(), value.getCompletedParallelInputs()); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java index d167858fc..ce613193c 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/RoutineInfoTest.java @@ -128,5 +128,6 @@ public void compareRoutineInfo(RoutineInfo expected, RoutineInfo value) { assertEquals(expected.getImportedLibraries(), value.getImportedLibraries()); assertEquals(expected.getBody(), value.getBody()); assertEquals(expected.hashCode(), value.hashCode()); + assertEquals(expected.toString(), value.toString()); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java index 6742763cb..393b7fbc6 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/StandardTableDefinitionTest.java @@ -17,6 +17,8 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -95,6 +97,17 @@ public void testBuilder() { assertEquals(STREAMING_BUFFER, TABLE_DEFINITION.getStreamingBuffer()); assertEquals(TIME_PARTITIONING, TABLE_DEFINITION.getTimePartitioning()); assertEquals(CLUSTERING, TABLE_DEFINITION.getClustering()); + assertNotEquals(TABLE_DEFINITION, TableDefinition.Type.TABLE); + } + + @Test + public void testTypeNullPointerException() { + try { + TABLE_DEFINITION.toBuilder().setType(null).build(); + fail(); + } catch (NullPointerException ex) { + assertNotNull(ex.getMessage()); + } } @Test diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableTest.java index 860230f30..b93ed770b 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableTest.java @@ -32,6 +32,7 @@ import com.google.cloud.bigquery.InsertAllRequest.RowToInsert; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import java.math.BigInteger; import java.util.List; import org.junit.Before; import org.junit.Rule; @@ -51,8 +52,14 @@ public class TableTest { private static final Long CREATION_TIME = 10L; private static final Long EXPIRATION_TIME = 100L; private static final Long LAST_MODIFIED_TIME = 20L; + private static final Long NUM_BYTES = 42L; + private static final Long NUM_LONG_TERM_BYTES = 21L; + private static final Long NUM_ROWS = 43L; private static final TableId TABLE_ID1 = TableId.of("dataset", "table1"); private static final TableId TABLE_ID2 = TableId.of("dataset", "table2"); + private static final Boolean REQUIRE_PARTITION_FILTER = true; + private static final EncryptionConfiguration ENCRYPTION_CONFIGURATION = + EncryptionConfiguration.newBuilder().setKmsKeyName("KMS_KEY_1").build(); private static final CopyJobConfiguration COPY_JOB_CONFIGURATION = CopyJobConfiguration.of(TABLE_ID2, TABLE_ID1); private static final JobInfo COPY_JOB_INFO = JobInfo.of(COPY_JOB_CONFIGURATION); @@ -118,6 +125,10 @@ public void testBuilder() { .setGeneratedId(GENERATED_ID) .setLastModifiedTime(LAST_MODIFIED_TIME) .setSelfLink(SELF_LINK) + .setNumBytes(NUM_BYTES) + .setNumLongTermBytes(NUM_LONG_TERM_BYTES) + .setNumRows(BigInteger.valueOf(NUM_ROWS)) + .setRequirePartitionFilter(REQUIRE_PARTITION_FILTER) .build(); assertEquals(TABLE_ID1, builtTable.getTableId()); assertEquals(CREATION_TIME, builtTable.getCreationTime()); @@ -129,6 +140,10 @@ public void testBuilder() { assertEquals(LAST_MODIFIED_TIME, builtTable.getLastModifiedTime()); assertEquals(TABLE_DEFINITION, builtTable.getDefinition()); assertEquals(SELF_LINK, builtTable.getSelfLink()); + assertEquals(NUM_BYTES, builtTable.getNumBytes()); + assertEquals(NUM_LONG_TERM_BYTES, builtTable.getNumLongTermBytes()); + assertEquals(BigInteger.valueOf(NUM_ROWS), builtTable.getNumRows()); + assertEquals(REQUIRE_PARTITION_FILTER, builtTable.getRequirePartitionFilter()); assertSame(bigquery, builtTable.getBigQuery()); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimePartitioningTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimePartitioningTest.java index 10b303f21..ee7f5dde6 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimePartitioningTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimePartitioningTest.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -68,6 +69,8 @@ public void testBuilder() { assertEquals(100, (long) partitioning.getExpirationMs()); assertNull(partitioning.getRequirePartitionFilter()); assertNull(partitioning.getField()); + compareTimePartitioning(TIME_PARTITIONING_HOUR, TIME_PARTITIONING_HOUR.toBuilder().build()); + assertNotEquals(TIME_PARTITIONING_DAY, TYPE_DAY); } @Test diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java index 0d39fa25b..1d888f00d 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TimelineSampleTest.java @@ -16,6 +16,7 @@ package com.google.cloud.bigquery; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.junit.Test; @@ -36,6 +37,12 @@ public class TimelineSampleTest { .setSlotMillis(SLOT_MILLIS) .build(); + @Test + public void testToBuilder() { + TimelineSample sample = TIMELINE_SAMPLE.toBuilder().setPendingUnits(15L).build(); + assertEquals(15L, sample.getPendingUnits().longValue()); + } + @Test public void testTimelineSampleBuilder() { assertEquals(ELAPSED_MS, TIMELINE_SAMPLE.getElapsedMs()); @@ -48,5 +55,8 @@ public void testTimelineSampleBuilder() { @Test public void TestEquals() { assertEquals(TIMELINE_SAMPLE, TIMELINE_SAMPLE); + assertNotEquals(TIMELINE_SAMPLE, SLOT_MILLIS); + assertEquals(TIMELINE_SAMPLE.toString(), TIMELINE_SAMPLE.toString()); + assertEquals(TIMELINE_SAMPLE.hashCode(), TIMELINE_SAMPLE.hashCode()); } } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ViewDefinitionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ViewDefinitionTest.java index fa4f82cd0..d60c7be2b 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ViewDefinitionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/ViewDefinitionTest.java @@ -18,8 +18,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.google.common.collect.ImmutableList; import java.util.List; @@ -31,7 +34,7 @@ public class ViewDefinitionTest { private static final List USER_DEFINED_FUNCTIONS = ImmutableList.of(UserDefinedFunction.inline("Function"), UserDefinedFunction.fromUri("URI")); private static final ViewDefinition VIEW_DEFINITION = - ViewDefinition.newBuilder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).build(); + ViewDefinition.newBuilder(VIEW_QUERY, USER_DEFINED_FUNCTIONS).setSchema(Schema.of()).build(); @Test public void testToBuilder() { @@ -43,6 +46,17 @@ public void testToBuilder() { viewDefinition = viewDefinition.toBuilder().setUseLegacySql(true).build(); assertTrue(viewDefinition.useLegacySql()); + assertNotEquals(VIEW_DEFINITION, VIEW_QUERY); + } + + @Test + public void testTypeNullPointerException() { + try { + VIEW_DEFINITION.toBuilder().setType(null).build(); + fail(); + } catch (NullPointerException ex) { + assertNotNull(ex.getMessage()); + } } @Test