Skip to content

Commit 58f4002

Browse files
committed
added unit tests for athena-vertica.
1 parent b8ceb73 commit 58f4002

File tree

7 files changed

+960
-75
lines changed

7 files changed

+960
-75
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*-
2+
* #%L
3+
* athena-vertica
4+
* %%
5+
* Copyright (C) 2019 - 2025 Amazon Web Services
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.amazonaws.athena.connectors.vertica;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.DATABASE;
29+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.DEFAULT;
30+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.HOST;
31+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.PORT;
32+
import static com.amazonaws.athena.connector.lambda.connection.EnvironmentConstants.SECRET_NAME;
33+
import static org.junit.Assert.assertEquals;
34+
35+
public class VerticaEnvironmentPropertiesTest {
36+
private Map<String, String> connectionProperties;
37+
private VerticaEnvironmentProperties verticaEnvironmentProperties;
38+
39+
@Before
40+
public void setUp() {
41+
connectionProperties = new HashMap<>();
42+
connectionProperties.put(HOST, "vertica-cluster-endpoint");
43+
connectionProperties.put(DATABASE, "verticadb");
44+
connectionProperties.put(SECRET_NAME, "vertica-secret");
45+
connectionProperties.put(PORT, "1234");
46+
verticaEnvironmentProperties = new VerticaEnvironmentProperties();
47+
}
48+
49+
@Test
50+
public void verticaConnectionPropertiesTest() {
51+
Map<String, String> verticaConnectionProperties = verticaEnvironmentProperties.connectionPropertiesToEnvironment(connectionProperties);
52+
53+
String expectedConnectionString = "vertica://jdbc:vertica://vertica-cluster-endpoint:1234/verticadb?${vertica-secret}";
54+
assertEquals(expectedConnectionString, verticaConnectionProperties.get(DEFAULT));
55+
}
56+
}

athena-vertica/src/test/java/com/amazonaws/athena/connectors/vertica/VerticaMetadataHandlerTest.java

Lines changed: 241 additions & 56 deletions
Large diffs are not rendered by default.

athena-vertica/src/test/java/com/amazonaws/athena/connectors/vertica/VerticaRecordHandlerTest.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.ByteArrayInputStream;
2323
import java.io.InputStream;
24+
import java.math.BigDecimal;
2425
import java.util.ArrayList;
2526
import java.util.Collections;
2627
import java.util.HashMap;
@@ -31,6 +32,12 @@
3132
import org.apache.arrow.memory.BufferAllocator;
3233
import org.apache.arrow.memory.RootAllocator;
3334
import org.apache.arrow.vector.BigIntVector;
35+
import org.apache.arrow.vector.BitVector;
36+
import org.apache.arrow.vector.TinyIntVector;
37+
import org.apache.arrow.vector.SmallIntVector;
38+
import org.apache.arrow.vector.Float8Vector;
39+
import org.apache.arrow.vector.Float4Vector;
40+
import org.apache.arrow.vector.DecimalVector;
3441
import org.apache.arrow.vector.VarCharVector;
3542
import org.apache.arrow.vector.VectorSchemaRoot;
3643
import org.apache.arrow.vector.ipc.ArrowReader;
@@ -164,6 +171,7 @@ public void after()
164171
logger.info("{}: exit ", testName.getMethodName());
165172
}
166173

174+
167175
@Test
168176
public void doReadRecordsNoSpill()
169177
throws Exception
@@ -305,45 +313,97 @@ public byte[] getBytes()
305313
private VectorSchemaRoot createRoot()
306314
{
307315
Schema schema = SchemaBuilder.newBuilder()
316+
.addBitField("bitField")
317+
.addTinyIntField("tinyIntField")
318+
.addSmallIntField("smallIntField")
308319
.addBigIntField("day")
309320
.addBigIntField("month")
310321
.addBigIntField("year")
322+
.addFloat4Field("float4Field")
323+
.addFloat8Field("float8Field")
324+
.addDecimalField("decimalField", 38, 10)
325+
311326
.addStringField("preparedStmt")
312327
.addStringField("queryId")
313328
.addStringField("awsRegionSql")
314329
.build();
330+
315331
VectorSchemaRoot schemaRoot = VectorSchemaRoot.create(schema, bufferAllocator);
332+
333+
BitVector bitVector = (BitVector) schemaRoot.getVector("bitField");
334+
bitVector.allocateNew(2);
335+
bitVector.set(0, 1);
336+
bitVector.set(1, 0);
337+
bitVector.setValueCount(2);
338+
339+
TinyIntVector tinyIntVector = (TinyIntVector) schemaRoot.getVector("tinyIntField");
340+
tinyIntVector.allocateNew(2);
341+
tinyIntVector.set(0, (byte) 10);
342+
tinyIntVector.set(1, (byte) 20);
343+
tinyIntVector.setValueCount(2);
344+
345+
SmallIntVector smallIntVector = (SmallIntVector) schemaRoot.getVector("smallIntField");
346+
smallIntVector.allocateNew(2);
347+
smallIntVector.set(0, (short) 100);
348+
smallIntVector.set(1, (short) 200);
349+
smallIntVector.setValueCount(2);
350+
316351
BigIntVector dayVector = (BigIntVector) schemaRoot.getVector("day");
317352
dayVector.allocateNew(2);
318353
dayVector.set(0, 0);
319354
dayVector.set(1, 1);
320355
dayVector.setValueCount(2);
356+
321357
BigIntVector monthVector = (BigIntVector) schemaRoot.getVector("month");
322358
monthVector.allocateNew(2);
323359
monthVector.set(0, 0);
324360
monthVector.set(1, 1);
325361
monthVector.setValueCount(2);
362+
326363
BigIntVector yearVector = (BigIntVector) schemaRoot.getVector("year");
327364
yearVector.allocateNew(2);
328365
yearVector.set(0, 2000);
329366
yearVector.set(1, 2001);
330367
yearVector.setValueCount(2);
368+
369+
Float4Vector float4Vector = (Float4Vector) schemaRoot.getVector("float4Field");
370+
float4Vector.allocateNew(2);
371+
float4Vector.set(0, 1.5f);
372+
float4Vector.set(1, 2.5f);
373+
float4Vector.setValueCount(2);
374+
375+
Float8Vector float8Vector = (Float8Vector) schemaRoot.getVector("float8Field");
376+
float8Vector.allocateNew(2);
377+
float8Vector.set(0, 3.141592653);
378+
float8Vector.set(1, 2.718281828);
379+
float8Vector.setValueCount(2);
380+
381+
DecimalVector decimalVector = (DecimalVector) schemaRoot.getVector("decimalField");
382+
decimalVector.allocateNew(2);
383+
decimalVector.set(0, new BigDecimal("123.4567890123"));
384+
decimalVector.set(1, new BigDecimal("987.6543210987"));
385+
decimalVector.setValueCount(2);
386+
331387
VarCharVector stmtVector = (VarCharVector) schemaRoot.getVector("preparedStmt");
332388
stmtVector.allocateNew(2);
333389
stmtVector.set(0, new Text("test1"));
334390
stmtVector.set(1, new Text("test2"));
335391
stmtVector.setValueCount(2);
392+
336393
VarCharVector idVector = (VarCharVector) schemaRoot.getVector("queryId");
337394
idVector.allocateNew(2);
338395
idVector.set(0, new Text("queryID1"));
339396
idVector.set(1, new Text("queryID2"));
340397
idVector.setValueCount(2);
398+
341399
VarCharVector regionVector = (VarCharVector) schemaRoot.getVector("awsRegionSql");
342400
regionVector.allocateNew(2);
343401
regionVector.set(0, new Text("region1"));
344402
regionVector.set(1, new Text("region2"));
345403
regionVector.setValueCount(2);
404+
346405
schemaRoot.setRowCount(2);
347406
return schemaRoot;
348407
}
349-
}
408+
409+
}

athena-vertica/src/test/java/com/amazonaws/athena/connectors/vertica/VerticaSchemaUtilsTest.java

Lines changed: 86 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,39 @@
2020
package com.amazonaws.athena.connectors.vertica;
2121

2222
import com.amazonaws.athena.connector.lambda.domain.TableName;
23-
import org.apache.arrow.vector.types.pojo.Field;
2423
import org.apache.arrow.vector.types.pojo.Schema;
25-
import org.junit.Assert;
2624
import org.junit.Before;
2725
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.Mock;
2828
import org.mockito.Mockito;
29-
import org.slf4j.Logger;
30-
import org.slf4j.LoggerFactory;
31-
32-
import java.sql.*;
29+
import org.mockito.junit.MockitoJUnitRunner;
30+
31+
import java.lang.reflect.Method;
32+
import java.security.cert.Certificate;
33+
import java.security.cert.X509Certificate;
34+
import java.sql.Types;
35+
import java.sql.ResultSet;
36+
import java.sql.Connection;
37+
import java.sql.DatabaseMetaData;
38+
import java.sql.SQLException;
39+
import java.util.Base64;
3340
import java.util.concurrent.atomic.AtomicInteger;
3441

35-
import static org.mockito.ArgumentMatchers.nullable;
42+
import static org.junit.Assert.assertEquals;
43+
import static org.junit.Assert.assertTrue;
44+
import static org.junit.Assert.fail;
45+
import static org.mockito.Mockito.when;
3646

47+
@RunWith(MockitoJUnitRunner.class)
3748
public class VerticaSchemaUtilsTest extends TestBase
3849
{
39-
private static final Logger logger = LoggerFactory.getLogger(VerticaSchemaUtils.class);
4050
private Connection connection;
4151
private DatabaseMetaData databaseMetaData;
4252
private TableName tableName;
4353

54+
@Mock
55+
private X509Certificate mockCertificate;
4456

4557
@Before
4658
public void setUp() throws SQLException
@@ -60,11 +72,27 @@ public void buildTableSchema() throws SQLException
6072
int [] types = {Types.INTEGER, Types.INTEGER};*/
6173

6274
String[] schema = {"TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "TYPE_NAME"};
63-
Object[][] values = {{"testSchema", "testTable1", "id", "bigint"}, {"testSchema", "testTable1", "date", "timestamp"},
64-
{"testSchema", "testTable1", "orders", "integer"}, {"testSchema", "testTable1", "price", "float4"},
65-
{"testSchema", "testTable1", "shop", "varchar"}
66-
};
67-
int[] types = {Types.BIGINT, Types.TIMESTAMP, Types.INTEGER,Types.FLOAT, Types.VARCHAR, Types.VARCHAR};
75+
Object[][] values = {
76+
{"testSchema", "testTable1", "bit_col", "BIT"},
77+
{"testSchema", "testTable1", "tinyint_col", "TINYINT"},
78+
{"testSchema", "testTable1", "smallint_col", "SMALLINT"},
79+
{"testSchema", "testTable1", "integer_col", "INTEGER"},
80+
{"testSchema", "testTable1", "bigint_col", "BIGINT"},
81+
{"testSchema", "testTable1", "float4_col", "FLOAT4"},
82+
{"testSchema", "testTable1", "float8_col", "FLOAT8"},
83+
{"testSchema", "testTable1", "numeric_col", "NUMERIC"},
84+
{"testSchema", "testTable1", "boolean_col", "BOOLEAN"},
85+
{"testSchema", "testTable1", "varchar_col", "VARCHAR"},
86+
{"testSchema", "testTable1", "timestamp_col", "TIMESTAMP"},
87+
{"testSchema", "testTable1", "timestamptz_col", "TIMESTAMPTZ"},
88+
{"testSchema", "testTable1", "datetime_col", "DATETIME"},
89+
{"testSchema", "testTable1", "unknown_col", "UNKNOWN"}
90+
};
91+
int[] types = {
92+
Types.BIT, Types.TINYINT, Types.SMALLINT, Types.INTEGER, Types.BIGINT,
93+
Types.FLOAT, Types.DOUBLE, Types.NUMERIC, Types.BOOLEAN, Types.VARCHAR,
94+
Types.TIMESTAMP, Types.TIMESTAMP, Types.DATE, Types.OTHER
95+
};
6896

6997
AtomicInteger rowNumber = new AtomicInteger(-1);
7098
ResultSet resultSet = mockResultSet(schema, types, values, rowNumber);
@@ -75,12 +103,52 @@ public void buildTableSchema() throws SQLException
75103
VerticaSchemaUtils verticaSchemaUtils = new VerticaSchemaUtils();
76104
Schema mockSchema = verticaSchemaUtils.buildTableSchema(this.connection, tableName);
77105

78-
Field testDateField = mockSchema.findField("date");
79-
Assert.assertEquals("Utf8", testDateField.getType().toString());
106+
assertEquals("Bool", mockSchema.findField("bit_col").getType().toString());
107+
assertEquals("Int(8, true)", mockSchema.findField("tinyint_col").getType().toString());
108+
assertEquals("Int(16, true)", mockSchema.findField("smallint_col").getType().toString());
109+
assertEquals("Int(64, true)", mockSchema.findField("integer_col").getType().toString());
110+
assertEquals("Int(64, true)", mockSchema.findField("bigint_col").getType().toString());
111+
assertEquals("FloatingPoint(SINGLE)", mockSchema.findField("float4_col").getType().toString());
112+
assertEquals("FloatingPoint(DOUBLE)", mockSchema.findField("float8_col").getType().toString());
113+
assertEquals("Decimal(10, 2, 128)", mockSchema.findField("numeric_col").getType().toString());
114+
assertEquals("Utf8", mockSchema.findField("boolean_col").getType().toString());
115+
assertEquals("Utf8", mockSchema.findField("varchar_col").getType().toString());
116+
assertEquals("Utf8", mockSchema.findField("timestamp_col").getType().toString());
117+
assertEquals("Utf8", mockSchema.findField("timestamptz_col").getType().toString());
118+
assertEquals("Date(DAY)", mockSchema.findField("datetime_col").getType().toString());
119+
assertEquals("Utf8", mockSchema.findField("unknown_col").getType().toString());
120+
}
80121

81-
Field testPriceField = mockSchema.findField("price");
82-
Assert.assertEquals("FloatingPoint(SINGLE)", testPriceField.getType().toString());
122+
@Test
123+
public void buildTableSchemaSQLException() throws SQLException
124+
{
125+
when(databaseMetaData.getColumns(null, tableName.getSchemaName(), tableName.getTableName(), null))
126+
.thenThrow(new SQLException("Database error"));
83127

128+
VerticaSchemaUtils verticaSchemaUtils = new VerticaSchemaUtils();
129+
try {
130+
verticaSchemaUtils.buildTableSchema(connection, tableName);
131+
fail("Expected RuntimeException");
132+
} catch (RuntimeException e) {
133+
assertTrue(e.getMessage().contains("Error in building the table schema"));
134+
assertTrue(e.getCause() instanceof SQLException);
135+
}
136+
}
84137

138+
@Test
139+
public void formatCrtFileContents() throws Exception
140+
{
141+
when(mockCertificate.getEncoded()).thenReturn("test-cert".getBytes());
142+
143+
Method formatMethod = VerticaSchemaUtils.class.getDeclaredMethod("formatCrtFileContents", Certificate.class);
144+
formatMethod.setAccessible(true);
145+
String result = (String) formatMethod.invoke(null, mockCertificate);
146+
147+
String expectedEncoded = Base64.getMimeEncoder(64, System.lineSeparator().getBytes())
148+
.encodeToString("test-cert".getBytes());
149+
String expected = "-----BEGIN CERTIFICATE-----" + System.lineSeparator() +
150+
expectedEncoded + System.lineSeparator() + "-----END CERTIFICATE-----";
151+
assertEquals(expected, result);
85152
}
86-
}
153+
154+
}

0 commit comments

Comments
 (0)