Skip to content

Commit

Permalink
test: add more java client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neverchanje committed Jul 17, 2024
1 parent 8b08c18 commit b89bb2f
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 17 deletions.
8 changes: 7 additions & 1 deletion integration_tests/client-library/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.5</version>
<version>42.7.3</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.8.1</version>
</dependency>

<!-- JUnit 5 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class TestCreateTable {

public void createSourceTable() throws SQLException {
public static void createSourceTable() throws SQLException {
String createTableQuery;
Statement statement;
try (Connection connection = TestUtils.establishConnection()) {
Expand Down Expand Up @@ -40,7 +40,7 @@ public void createSourceTable() throws SQLException {
}
}

public void dropSourceTable() throws SQLException {
public static void dropSourceTable() throws SQLException {
String dropSourceQuery = "DROP TABLE s1_java;";
try (Connection connection = TestUtils.establishConnection()) {
Statement statement = connection.createStatement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ static Connection establishConnection() throws SQLException {

@Test
public void testEstablishConnection() throws SQLException {
Connection conn = establishConnection();
assertNotNull(conn, "Connection should not be null");

String query = "SELECT 1";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(query);
assertTrue(resultSet.next(), "Expected a result");
int resultValue = resultSet.getInt(1);
assertEquals(1, resultValue, "Expected result value to be 1");

conn.close(); // Close the connection to release resources
try (Connection conn = TestUtils.establishConnection()) {
assertNotNull(conn, "Connection should not be null");

String query = "SELECT 1";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(query);
assertTrue(resultSet.next(), "Expected a result");
int resultValue = resultSet.getInt(1);
assertEquals(1, resultValue, "Expected result value to be 1");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.risingwave;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

public class TestMaterializedView {

Expand All @@ -17,11 +22,38 @@ public void clearDatabase() throws SQLException {
statement.executeUpdate(dropViewQuery);
System.out.println("Materialized view dropped successfully.");
}
String truncateTableQuery = "DROP TABLE my_table_java;";
String truncateTableQuery = "DROP TABLE IF EXISTS my_table_java;";
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(truncateTableQuery);
System.out.println("Table dropped successfully.");
System.out.println("Table my_table_java dropped successfully.");
}
truncateTableQuery = "DROP TABLE IF EXISTS test_struct;";
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(truncateTableQuery);
System.out.println("Table test_struct dropped successfully.");
}
}
}

@Test
public void testStruct() throws SQLException {
try (Connection conn = TestUtils.establishConnection()) {
Statement statement = conn.createStatement();
statement.executeUpdate("CREATE TABLE IF NOT EXISTS test_struct(" +
"i1 int [], v1 struct<v2 int, v3 double>, t1 timestamptz, c1 varchar" +
")");

String insertDataSQL = "INSERT INTO test_struct (i1, v1, t1, c1) VALUES ('{1}', (2, 3), '2020-01-01 01:02:03', 'abc')";
statement.execute(insertDataSQL);
statement.execute("FLUSH;");

QueryRunner runner = new QueryRunner();
String query = "SELECT * FROM test_struct";
List<Map<String, Object>> resultList = runner.query(conn, query, new MapListHandler());
Assertions.assertEquals(resultList.size(), 1);
Assertions.assertEquals(resultList.get(0).get("v1"), "(2,3)");
} finally {
clearDatabase();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package com.risingwave;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class TestMetadata {

public List<String> fetchAllStrings(ResultSet resultSet, String columnName) throws SQLException {
List<String> stringColumn = new ArrayList<>();

// Iterate through the ResultSet and collect schema names
while (resultSet.next()) {
String element = resultSet.getString(columnName);
stringColumn.add(element);
}

return stringColumn;
}

@Test
public void testGetSchemas() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet schemas = metaData.getSchemas()) {
// Check that the ResultSet is not null
assertNotNull(schemas);

List<String> schemaList = fetchAllStrings(schemas, "TABLE_SCHEM");

// Check that the schema list is not empty
assertFalse(schemaList.isEmpty());

// Check that "pg_catalog" schema exists in the list
assertTrue(schemaList.contains("pg_catalog"));
}
}
}

@Test
public void testGetCatalogs() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet catalogs = metaData.getCatalogs()) {
// Check that the ResultSet is not null
assertNotNull(catalogs);

List<String> catalogList = fetchAllStrings(catalogs, "TABLE_CAT");

// Check that the catalog list is not empty
assertFalse(catalogList.isEmpty());

// Check that a must-have database "dev" exists in the list
assertTrue(catalogList.contains("dev"));
}
}
}

/*
TODO: Support the parameter 'default_transaction_isolation'.
@Test
public void testGetDefaultTransactionIsolation() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
metaData.getDefaultTransactionIsolation();
}
}
*/

/*
TODO: Support the parameter 'max_index_keys'.
@Test
public void testGetMaxColumnsInIndex() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
metaData.getMaxColumnsInIndex();
metaData.getImportedKeys("dev", "information_schema", "tables");
metaData.getExportedKeys("dev", "information_schema", "tables");
metaData.getCrossReference("dev", "information_schema", "tables", "dev", "pg_catalog", "pg_tables");
}
}
*/

/*
TODO: Support the `name` type and give it a type length.
@Test
public void testGetMaxNameLength() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
metaData.getMaxCursorNameLength();
metaData.getMaxSchemaNameLength();
metaData.getMaxTableNameLength();
metaData.getMaxSchemaNameLength();
metaData.getMaxUserNameLength();
metaData.getMaxProcedureNameLength();
metaData.getMaxCatalogNameLength();
metaData.getClientInfoProperties();
}
}
*/

@Test
public void testGetProcedures() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet procedures = metaData.getProcedures("dev", "public", "")) {
assertNotNull(procedures);
List<String> procedureList = fetchAllStrings(procedures, "PROCEDURE_NAME");
assertTrue(procedureList.isEmpty());
}
}
}

@Test
public void testGetTables() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet tables = metaData.getTables("dev", "pg_catalog", "", null)) {
assertNotNull(tables);
List<String> tablesList = fetchAllStrings(tables, "TABLE_NAME");
assertFalse(tablesList.isEmpty());
}

/*
TODO: `relacl` is missing in `pg_class`.
try (ResultSet acl = metaData.getTablePrivileges("dev", "information_schema", "tables")) {
assertNotNull(acl);
}
*/
}
}

@Test
public void testGetColumns() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet columns = metaData.getColumns("dev", "information_schema", "tables", "")) {
assertNotNull(columns);
List<String> columnList = fetchAllStrings(columns, "COLUMN_NAME");
assertFalse(columnList.isEmpty());
}

/*
TODO: `relacl` is missing in `pg_class`.
try (ResultSet acl = metaData.getColumnPrivileges("dev", "information_schema", "tables", "")) {
assertNotNull(acl);
}
*/
}
}

/*
TODO: the query used in getPrimaryKeys retrieves nothing in RisingWave.
@Test
public void testGetPrimaryKeys() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
TestUtils.executeDdl(connection, "CREATE TABLE t_test_get_pks (id INT PRIMARY KEY, value TEXT);");
try (ResultSet pKeys = metaData.getPrimaryKeys("dev", "public", "t_test_get_pks")) {
assertNotNull(pKeys);
List<String> pKeyList = fetchAllStrings(pKeys, "PK_NAME");
assertFalse(pKeyList.isEmpty());
}
try (ResultSet pKeys = metaData.getPrimaryUniqueKeys("dev", "public", "t_test_get_pks")) {
assertNotNull(pKeys);
List<String> pKeyList = fetchAllStrings(pKeys, "PK_NAME");
assertFalse(pKeyList.isEmpty());
}
TestUtils.executeDdl(connection, "DROP TABLE t_test_get_pks;");
}
}
*/


/*
TODO: Support pg_get_function_result.
TODO: prokind is missing in pg_proc.
@Test
public void testGetFunctions() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
try (ResultSet functions = metaData.getFunctions("dev", "public", "")) {
assertNotNull(functions);
List<String> fList = fetchAllStrings(functions, "FUNCTION_NAME");
assertFalse(fList.isEmpty());
}
try (ResultSet cols = metaData.getFunctionColumns("dev", "public", "")) {
assertNotNull(cols);
}
}
}
*/

@Test
public void testGetUDTs() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();

try (ResultSet types = metaData.getUDTs("dev", "public", "", null)) {
assertNotNull(types);
}
}
}

/**
* Only checks the API callability, not results.
*/
@Test
public void testApiCallable() throws SQLException {
try (Connection connection = TestUtils.establishConnection()) {
DatabaseMetaData metaData = connection.getMetaData();

metaData.getTableTypes();
metaData.getTypeInfo();

/// metaData.getIndexInfo();

// TODO: Actually implement `pg_catalog.pg_get_keywords()`. Now it returns empty.
metaData.getSQLKeywords();

metaData.getBestRowIdentifier("dev", "information_schema", "tables", 1, true);
metaData.getVersionColumns("dev", "information_schema", "tables");
}
}
}
Loading

0 comments on commit b89bb2f

Please sign in to comment.