Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@
import io.unitycatalog.client.ApiException;
import io.unitycatalog.client.model.ColumnInfo;
import io.unitycatalog.client.model.ColumnTypeName;
import io.unitycatalog.client.model.CreateCatalog;
import io.unitycatalog.client.model.CreateSchema;
import io.unitycatalog.client.model.CreateTable;
import io.unitycatalog.client.model.DataSourceFormat;
import io.unitycatalog.client.model.SchemaInfo;
import io.unitycatalog.client.model.TableInfo;
import io.unitycatalog.client.model.TableType;
import io.unitycatalog.client.model.UpdateSchema;
import io.unitycatalog.server.base.BaseCRUDTest;
import io.unitycatalog.server.base.ServerConfig;
import io.unitycatalog.server.base.schema.SchemaOperations;
import io.unitycatalog.server.persist.dao.ColumnInfoDAO;
import io.unitycatalog.server.persist.dao.TableInfoDAO;
import io.unitycatalog.server.utils.TestUtils;
Expand All @@ -31,43 +24,22 @@
import java.util.UUID;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public abstract class BaseTableCRUDTest extends BaseCRUDTest {

protected SchemaOperations schemaOperations;
protected TableOperations tableOperations;
private String schemaId;

protected abstract SchemaOperations createSchemaOperations(ServerConfig serverConfig);

protected abstract TableOperations createTableOperations(ServerConfig serverConfig);

@BeforeEach
@Override
public void setUp() {
super.setUp();
schemaOperations = createSchemaOperations(serverConfig);
tableOperations = createTableOperations(serverConfig);
}

protected void createCommonResources() throws ApiException {
CreateCatalog createCatalog =
new CreateCatalog().name(TestUtils.CATALOG_NAME).comment(TestUtils.COMMENT);
catalogOperations.createCatalog(createCatalog);

SchemaInfo schemaInfo =
schemaOperations.createSchema(
new CreateSchema().name(TestUtils.SCHEMA_NAME).catalogName(TestUtils.CATALOG_NAME));
schemaId = schemaInfo.getSchemaId();
}
/**
* Abstract base class that provides some test cases for table CRUD operations in Unity Catalog.
*
* <p>This class extends {@link BaseTableCRUDTestEnv} and implements test suite that verifies the
* correct behavior of table operations including creation, retrieval, update, and deletion. It is
* derived by both CliTableCRUDTest and SdkTableCRUDTest which would exercise CLI and SDK with the
* test cases defined in this base class.
*/
public abstract class BaseTableCRUDTest extends BaseTableCRUDTestEnv {

@Test
public void testTableCRUD() throws IOException, ApiException {
assertThatThrownBy(() -> tableOperations.getTable(TestUtils.TABLE_FULL_NAME))
.isInstanceOf(Exception.class);
createCommonResources();

// Create and verify a table
TableInfo createdTable = createAndVerifyTable();
Expand All @@ -94,16 +66,6 @@ public void testTableCRUD() throws IOException, ApiException {
testTableAfterSchemaUpdateAndDeletion();
}

private TableInfo createAndVerifyTable() throws IOException, ApiException {
TableInfo tableInfo =
createTestingTable(TestUtils.TABLE_NAME, TestUtils.STORAGE_LOCATION, tableOperations);
assertThat(tableInfo.getName()).isEqualTo(TestUtils.TABLE_NAME);
assertThat(tableInfo.getCatalogName()).isEqualTo(TestUtils.CATALOG_NAME);
assertThat(tableInfo.getSchemaName()).isEqualTo(TestUtils.SCHEMA_NAME);
assertThat(tableInfo.getTableId()).isNotNull();
return tableInfo;
}

private void verifyTableInfo(TableInfo retrievedTable, TableInfo expectedTable) {
assertThat(retrievedTable).isEqualTo(expectedTable);
Collection<ColumnInfo> columns = retrievedTable.getColumns();
Expand Down Expand Up @@ -256,46 +218,6 @@ private void testTableAfterSchemaUpdateAndDeletion() throws ApiException {
.isInstanceOf(Exception.class);
}

public static TableInfo createTestingTable(
String tableName, String storageLocation, TableOperations tableOperations)
throws IOException, ApiException {
ColumnInfo columnInfo1 =
new ColumnInfo()
.name("as_int")
.typeText("INTEGER")
.typeJson("{\"type\": \"integer\"}")
.typeName(ColumnTypeName.INT)
.typePrecision(10)
.typeScale(0)
.position(0)
.comment("Integer column")
.nullable(true);

ColumnInfo columnInfo2 =
new ColumnInfo()
.name("as_string")
.typeText("VARCHAR(255)")
.typeJson("{\"type\": \"string\", \"length\": \"255\"}")
.typeName(ColumnTypeName.STRING)
.position(1)
.comment("String column")
.nullable(true);

CreateTable createTableRequest =
new CreateTable()
.name(tableName)
.catalogName(TestUtils.CATALOG_NAME)
.schemaName(TestUtils.SCHEMA_NAME)
.columns(List.of(columnInfo1, columnInfo2))
.properties(TestUtils.PROPERTIES)
.comment(TestUtils.COMMENT)
.storageLocation(storageLocation)
.tableType(TableType.EXTERNAL)
.dataSourceFormat(DataSourceFormat.DELTA);

return tableOperations.createTable(createTableRequest);
}

protected List<TableInfo> createMultipleTestingTables(int numberOfTables)
throws IOException, ApiException {
List<TableInfo> createdTables = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.unitycatalog.server.base.table;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is split from BaseTableCRUDTest.java


import static org.assertj.core.api.Assertions.assertThat;

import io.unitycatalog.client.ApiException;
import io.unitycatalog.client.model.ColumnInfo;
import io.unitycatalog.client.model.ColumnTypeName;
import io.unitycatalog.client.model.CreateCatalog;
import io.unitycatalog.client.model.CreateSchema;
import io.unitycatalog.client.model.CreateTable;
import io.unitycatalog.client.model.DataSourceFormat;
import io.unitycatalog.client.model.SchemaInfo;
import io.unitycatalog.client.model.TableInfo;
import io.unitycatalog.client.model.TableType;
import io.unitycatalog.server.base.BaseCRUDTest;
import io.unitycatalog.server.base.ServerConfig;
import io.unitycatalog.server.base.schema.SchemaOperations;
import io.unitycatalog.server.utils.TestUtils;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;

/**
* Abstract base class that provides the test environment setup for table CRUD operations.
*
* <p>This class extends {@link BaseCRUDTest} and serves as a foundation for testing table-related
* operations in Unity Catalog. It's useful for any CRUD test that needs to create test tables.
*/
public abstract class BaseTableCRUDTestEnv extends BaseCRUDTest {

protected SchemaOperations schemaOperations;
protected TableOperations tableOperations;
protected String schemaId;

protected abstract SchemaOperations createSchemaOperations(ServerConfig serverConfig);

protected abstract TableOperations createTableOperations(ServerConfig serverConfig);

@BeforeEach
@Override
public void setUp() {
super.setUp();
schemaOperations = createSchemaOperations(serverConfig);
tableOperations = createTableOperations(serverConfig);
createCommonResources();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to clean those created common resources after each test ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, I assume it will conflicts if we create those resources for the next test cases, right ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire catalog is deleted by BaseCRUDTest.cleanUp(), the lower level base class, despite that the catalog is created here.
It's a little weird but it's been like that. Maybe the reason is that not all tests based on BaseCRUDTest would need a catalog (or two).

}

private void createCommonResources() {
CreateCatalog createCatalog =
new CreateCatalog().name(TestUtils.CATALOG_NAME).comment(TestUtils.COMMENT);
try {
catalogOperations.createCatalog(createCatalog);
SchemaInfo schemaInfo =
schemaOperations.createSchema(
new CreateSchema().name(TestUtils.SCHEMA_NAME).catalogName(TestUtils.CATALOG_NAME));
schemaId = schemaInfo.getSchemaId();
} catch (ApiException e) {
throw new RuntimeException(e);
}
}

protected TableInfo createAndVerifyTable() throws IOException, ApiException {
TableInfo tableInfo =
createTestingTable(TestUtils.TABLE_NAME, TestUtils.STORAGE_LOCATION, tableOperations);
assertThat(tableInfo.getName()).isEqualTo(TestUtils.TABLE_NAME);
assertThat(tableInfo.getCatalogName()).isEqualTo(TestUtils.CATALOG_NAME);
assertThat(tableInfo.getSchemaName()).isEqualTo(TestUtils.SCHEMA_NAME);
assertThat(tableInfo.getTableId()).isNotNull();
return tableInfo;
}

public static TableInfo createTestingTable(
String tableName, String storageLocation, TableOperations tableOperations)
throws IOException, ApiException {
ColumnInfo columnInfo1 =
new ColumnInfo()
.name("as_int")
.typeText("INTEGER")
.typeJson("{\"type\": \"integer\"}")
.typeName(ColumnTypeName.INT)
.position(0)
.comment("Integer column")
.nullable(true);

ColumnInfo columnInfo2 =
new ColumnInfo()
.name("as_string")
.typeText("VARCHAR(255)")
.typeJson("{\"type\": \"string\", \"length\": \"255\"}")
.typeName(ColumnTypeName.STRING)
.position(1)
.comment("String column")
.nullable(true);

CreateTable createTableRequest =
new CreateTable()
.name(tableName)
.catalogName(TestUtils.CATALOG_NAME)
.schemaName(TestUtils.SCHEMA_NAME)
.columns(List.of(columnInfo1, columnInfo2))
.properties(TestUtils.PROPERTIES)
.comment(TestUtils.COMMENT)
.storageLocation(storageLocation)
.tableType(TableType.EXTERNAL)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't support managed table currently, so we have to use the EXTERNAL right ?

@yili-db yili-db Nov 5, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. This will soon be changed to a parameter by the following PR.

.dataSourceFormat(DataSourceFormat.DELTA);

return tableOperations.createTable(createTableRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private StagingTableDAO getStagingTable(String tableId) {

@Test
public void testListTablesWithNoNextPageTokenShouldReturnNull() throws Exception {
createCommonResources();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is now called inside setUp() for these tests.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

TableInfo testingTable =
createTestingTable(TestUtils.TABLE_NAME, TestUtils.STORAGE_LOCATION, tableOperations);
ListTablesResponse resp =
Expand All @@ -90,7 +89,6 @@ public void testListTablesWithNoNextPageTokenShouldReturnNull() throws Exception

@Test
public void testListTablesWithNextPageTokenShouldReturnNextPageToken() throws Exception {
createCommonResources();
List<TableInfo> testingTables = createMultipleTestingTables(11);
ListTablesResponse resp =
localTablesApi.listTables(
Expand All @@ -114,7 +112,6 @@ public void testListTablesWithNextPageTokenShouldReturnNextPageToken() throws Ex
*/
@Test
public void testStagingTableCreationAndManagedTableFromStagingLocation() throws Exception {
createCommonResources();
String stagingTableName = "staging_test_table";

// Step 1: Create a staging table
Expand Down Expand Up @@ -200,8 +197,6 @@ public void testStagingTableCreationAndManagedTableFromStagingLocation() throws
*/
@Test
public void testManagedTableCreationFromNonExistentStagingLocationShouldFail() throws Exception {
createCommonResources();

// Use a fake staging location that doesn't exist
String fakeLocationUuid = "00000000-0000-0000-0000-000000000000";
String fakeStagingLocation = "file:///tmp/ucroot/tables/" + fakeLocationUuid;
Expand All @@ -228,8 +223,6 @@ public void testManagedTableCreationFromNonExistentStagingLocationShouldFail() t
/** Test that attempting to create a duplicate staging table fails with ALREADY_EXISTS error. */
@Test
public void testDuplicateStagingTableCreationShouldFail() throws Exception {
createCommonResources();

// Create an external table
String externalTableName = "duplicate_table";
CreateTable createTableRequest =
Expand Down Expand Up @@ -286,8 +279,6 @@ public void testDuplicateStagingTableCreationShouldFail() throws Exception {
*/
@Test
public void testMultipleStagingTableCreation() throws Exception {
createCommonResources();

String stagingTable1 = "staging_table_1";
String stagingTable2 = "staging_table_2";

Expand Down Expand Up @@ -315,8 +306,6 @@ public void testMultipleStagingTableCreation() throws Exception {
/** Test that staging table creation fails when the schema doesn't exist. */
@Test
public void testStagingTableCreationWithNonExistentSchemaShouldFail() throws Exception {
createCommonResources();

CreateStagingTable createStagingTableRequest =
new CreateStagingTable()
.catalogName(TestUtils.CATALOG_NAME)
Expand Down