|
| 1 | +/* |
| 2 | + * Copyright © 2023 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.plugin.gcp.bigquery.sink; |
| 18 | + |
| 19 | +import com.google.cloud.bigquery.TimePartitioning; |
| 20 | +import io.cdap.cdap.api.data.schema.Schema; |
| 21 | +import io.cdap.cdap.etl.api.FailureCollector; |
| 22 | +import io.cdap.cdap.etl.mock.validation.MockFailureCollector; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import java.lang.reflect.InvocationTargetException; |
| 28 | +import java.lang.reflect.Method; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for {@link BigQuerySinkConfig}. |
| 32 | + */ |
| 33 | + |
| 34 | +public class BigQuerySinkConfigTest { |
| 35 | + MockFailureCollector collector; |
| 36 | + BigQuerySinkConfig config; |
| 37 | + Method validateTimePartitioningColumnMethod; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setup() throws NoSuchMethodException { |
| 41 | + collector = new MockFailureCollector(); |
| 42 | + config = BigQuerySinkConfig.builder().build(); |
| 43 | + validateTimePartitioningColumnMethod = config.getClass() |
| 44 | + .getDeclaredMethod("validateTimePartitioningColumn", String.class, FailureCollector.class, |
| 45 | + Schema.class, TimePartitioning.Type.class); |
| 46 | + validateTimePartitioningColumnMethod.setAccessible(true); |
| 47 | + } |
| 48 | + @Test |
| 49 | + public void testValidateTimePartitioningColumnWithHourAndDate() throws |
| 50 | + InvocationTargetException, IllegalAccessException { |
| 51 | + String columnName = "partitionFrom"; |
| 52 | + Schema fieldSchema = Schema.recordOf("test", Schema.Field.of("partitionFrom", |
| 53 | + Schema.of(Schema.LogicalType.DATE))); |
| 54 | + TimePartitioning.Type timePartitioningType = TimePartitioning.Type.HOUR; |
| 55 | + |
| 56 | + validateTimePartitioningColumnMethod.invoke(config, columnName, collector, fieldSchema, timePartitioningType); |
| 57 | + Assert.assertEquals(String.format("Partition column '%s' is of invalid type '%s'.", |
| 58 | + columnName, fieldSchema.getDisplayName()), |
| 59 | + collector.getValidationFailures().stream().findFirst().get().getMessage()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testValidateTimePartitioningColumnWithHourAndTimestamp() throws |
| 64 | + InvocationTargetException, IllegalAccessException { |
| 65 | + |
| 66 | + String columnName = "partitionFrom"; |
| 67 | + Schema schema = Schema.of(Schema.LogicalType.TIMESTAMP_MICROS); |
| 68 | + |
| 69 | + Schema fieldSchema = schema.isNullable() ? schema.getNonNullable() : schema; |
| 70 | + TimePartitioning.Type timePartitioningType = TimePartitioning.Type.HOUR; |
| 71 | + |
| 72 | + validateTimePartitioningColumnMethod.invoke(config, columnName, collector, fieldSchema, timePartitioningType); |
| 73 | + Assert.assertEquals(0, collector.getValidationFailures().size()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testValidateTimePartitioningColumnWithDayAndString() throws |
| 78 | + InvocationTargetException, IllegalAccessException { |
| 79 | + |
| 80 | + String columnName = "partitionFrom"; |
| 81 | + Schema schema = Schema.of(Schema.Type.STRING); |
| 82 | + |
| 83 | + Schema fieldSchema = schema.isNullable() ? schema.getNonNullable() : schema; |
| 84 | + TimePartitioning.Type timePartitioningType = TimePartitioning.Type.DAY; |
| 85 | + |
| 86 | + validateTimePartitioningColumnMethod.invoke(config, columnName, collector, fieldSchema, timePartitioningType); |
| 87 | + Assert.assertEquals(String.format("Partition column '%s' is of invalid type '%s'.", |
| 88 | + columnName, fieldSchema.getDisplayName()), |
| 89 | + collector.getValidationFailures().stream().findFirst().get().getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testValidateTimePartitioningColumnWithDayAndDate() throws |
| 94 | + InvocationTargetException, IllegalAccessException { |
| 95 | + |
| 96 | + String columnName = "partitionFrom"; |
| 97 | + Schema schema = Schema.of(Schema.LogicalType.DATE); |
| 98 | + |
| 99 | + Schema fieldSchema = schema.isNullable() ? schema.getNonNullable() : schema; |
| 100 | + TimePartitioning.Type timePartitioningType = TimePartitioning.Type.DAY; |
| 101 | + |
| 102 | + validateTimePartitioningColumnMethod.invoke(config, columnName, collector, fieldSchema, timePartitioningType); |
| 103 | + Assert.assertEquals(0, collector.getValidationFailures().size()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testValidateTimePartitioningColumnWithNullAndDate() throws |
| 108 | + InvocationTargetException, IllegalAccessException { |
| 109 | + |
| 110 | + String columnName = "partitionFrom"; |
| 111 | + Schema schema = Schema.of(Schema.LogicalType.DATE); |
| 112 | + |
| 113 | + Schema fieldSchema = schema.isNullable() ? schema.getNonNullable() : schema; |
| 114 | + TimePartitioning.Type timePartitioningType = null; |
| 115 | + |
| 116 | + validateTimePartitioningColumnMethod.invoke(config, columnName, collector, fieldSchema, timePartitioningType); |
| 117 | + // No error as null time timePartitioningType will default to DAY |
| 118 | + Assert.assertEquals(0, collector.getValidationFailures().size()); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments