|
| 1 | +/* |
| 2 | + * Copyright © 2021 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.db; |
| 18 | + |
| 19 | +import io.cdap.cdap.api.data.schema.Schema; |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.MockitoJUnitRunner; |
| 26 | + |
| 27 | +import java.sql.ResultSetMetaData; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.sql.Types; |
| 30 | + |
| 31 | +import static org.mockito.ArgumentMatchers.eq; |
| 32 | +import static org.mockito.Mockito.when; |
| 33 | + |
| 34 | +@RunWith(MockitoJUnitRunner.class) |
| 35 | +public class CommonSchemaReaderTest { |
| 36 | + |
| 37 | + CommonSchemaReader reader; |
| 38 | + |
| 39 | + @Mock |
| 40 | + ResultSetMetaData metadata; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void before() { |
| 44 | + reader = new CommonSchemaReader(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testGetSchemaHandlesNull() throws SQLException { |
| 49 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.NULL); |
| 50 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.NULL)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testGetSchemaHandlesRowID() throws SQLException { |
| 55 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.ROWID); |
| 56 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.STRING)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testGetSchemaHandlesBoolean() throws SQLException { |
| 61 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.BOOLEAN); |
| 62 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.BOOLEAN)); |
| 63 | + |
| 64 | + when(metadata.getColumnType(eq(2))).thenReturn(Types.BIT); |
| 65 | + Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.BOOLEAN)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testGetSchemaHandlesInt() throws SQLException { |
| 70 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.TINYINT); |
| 71 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.INT)); |
| 72 | + |
| 73 | + when(metadata.getColumnType(eq(2))).thenReturn(Types.SMALLINT); |
| 74 | + Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.INT)); |
| 75 | + |
| 76 | + when(metadata.getColumnType(eq(3))).thenReturn(Types.INTEGER); |
| 77 | + Assert.assertEquals(reader.getSchema(metadata, 3), Schema.of(Schema.Type.INT)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testGetSchemaHandlesLong() throws SQLException { |
| 82 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.BIGINT); |
| 83 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.LONG)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testGetSchemaHandlesFloat() throws SQLException { |
| 88 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.REAL); |
| 89 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.FLOAT)); |
| 90 | + |
| 91 | + when(metadata.getColumnType(eq(2))).thenReturn(Types.FLOAT); |
| 92 | + Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.FLOAT)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testGetSchemaHandlesNumeric() throws SQLException { |
| 97 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.NUMERIC); |
| 98 | + when(metadata.getPrecision(eq(1))).thenReturn(10); |
| 99 | + when(metadata.getScale(eq(1))).thenReturn(0); |
| 100 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.decimalOf(10, 0)); |
| 101 | + |
| 102 | + when(metadata.getColumnType(eq(2))).thenReturn(Types.DECIMAL); |
| 103 | + when(metadata.getPrecision(eq(2))).thenReturn(10); |
| 104 | + when(metadata.getScale(eq(2))).thenReturn(1); |
| 105 | + Assert.assertEquals(reader.getSchema(metadata, 2), Schema.decimalOf(10, 1)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testGetSchemaHandlesDouble() throws SQLException { |
| 110 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.DOUBLE); |
| 111 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.DOUBLE)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetSchemaHandlesDate() throws SQLException { |
| 116 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.DATE); |
| 117 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.LogicalType.DATE)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetSchemaHandlesTime() throws SQLException { |
| 122 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.TIME); |
| 123 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.LogicalType.TIME_MICROS)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetSchemaHandlesTimestamp() throws SQLException { |
| 128 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.TIMESTAMP); |
| 129 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.LogicalType.TIMESTAMP_MICROS)); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testGetSchemaHandlesBytes() throws SQLException { |
| 134 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.BINARY); |
| 135 | + Assert.assertEquals(reader.getSchema(metadata, 1), Schema.of(Schema.Type.BYTES)); |
| 136 | + |
| 137 | + when(metadata.getColumnType(eq(2))).thenReturn(Types.VARBINARY); |
| 138 | + Assert.assertEquals(reader.getSchema(metadata, 2), Schema.of(Schema.Type.BYTES)); |
| 139 | + |
| 140 | + when(metadata.getColumnType(eq(3))).thenReturn(Types.LONGVARBINARY); |
| 141 | + Assert.assertEquals(reader.getSchema(metadata, 3), Schema.of(Schema.Type.BYTES)); |
| 142 | + |
| 143 | + when(metadata.getColumnType(eq(4))).thenReturn(Types.BLOB); |
| 144 | + Assert.assertEquals(reader.getSchema(metadata, 4), Schema.of(Schema.Type.BYTES)); |
| 145 | + } |
| 146 | + |
| 147 | + @Test(expected = SQLException.class) |
| 148 | + public void testGetSchemaThrowsExceptionOnNumericWithZeroPrecision() throws SQLException { |
| 149 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.NUMERIC); |
| 150 | + when(metadata.getPrecision(eq(1))).thenReturn(0); |
| 151 | + when(metadata.getScale(eq(1))).thenReturn(10); |
| 152 | + reader.getSchema(metadata, 1); |
| 153 | + } |
| 154 | + |
| 155 | + @Test(expected = SQLException.class) |
| 156 | + public void testGetSchemaThrowsExceptionOnArray() throws SQLException { |
| 157 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.ARRAY); |
| 158 | + reader.getSchema(metadata, 1); |
| 159 | + } |
| 160 | + |
| 161 | + @Test(expected = SQLException.class) |
| 162 | + public void testGetSchemaThrowsExceptionOnDatalink() throws SQLException { |
| 163 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.DATALINK); |
| 164 | + reader.getSchema(metadata, 1); |
| 165 | + } |
| 166 | + |
| 167 | + @Test(expected = SQLException.class) |
| 168 | + public void testGetSchemaThrowsExceptionOnDistinct() throws SQLException { |
| 169 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.DISTINCT); |
| 170 | + reader.getSchema(metadata, 1); |
| 171 | + } |
| 172 | + |
| 173 | + @Test(expected = SQLException.class) |
| 174 | + public void testGetSchemaThrowsExceptionOnJavaObject() throws SQLException { |
| 175 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.JAVA_OBJECT); |
| 176 | + reader.getSchema(metadata, 1); |
| 177 | + } |
| 178 | + |
| 179 | + @Test(expected = SQLException.class) |
| 180 | + public void testGetSchemaThrowsExceptionOnOther() throws SQLException { |
| 181 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.OTHER); |
| 182 | + reader.getSchema(metadata, 1); |
| 183 | + } |
| 184 | + |
| 185 | + @Test(expected = SQLException.class) |
| 186 | + public void testGetSchemaThrowsExceptionOnRef() throws SQLException { |
| 187 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.REF); |
| 188 | + reader.getSchema(metadata, 1); |
| 189 | + } |
| 190 | + |
| 191 | + @Test(expected = SQLException.class) |
| 192 | + public void testGetSchemaThrowsExceptionOnSQLXML() throws SQLException { |
| 193 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.SQLXML); |
| 194 | + reader.getSchema(metadata, 1); |
| 195 | + } |
| 196 | + |
| 197 | + @Test(expected = SQLException.class) |
| 198 | + public void testGetSchemaThrowsExceptionOnStruct() throws SQLException { |
| 199 | + when(metadata.getColumnType(eq(1))).thenReturn(Types.STRUCT); |
| 200 | + reader.getSchema(metadata, 1); |
| 201 | + } |
| 202 | +} |
0 commit comments