Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingjian Wu committed Apr 26, 2024
1 parent 6fec6f2 commit 1451aba
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {PolarisPersistenceConfig.class})
@ActiveProfiles(profiles = {"polarisconnectortest"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@ActiveProfiles(profiles = {"polaris_functional_test"})
@AutoConfigureDataJpa
public class PolarisConnectorDatabaseServiceFunctionalTest extends PolarisConnectorDatabaseServiceTest {
private void simulateDelay() {
Expand All @@ -48,113 +47,119 @@ private void simulateDelay() {
public void testSimpleListDb() {
final DatabaseInfo db1 = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).uri("uri1").build();
final DatabaseInfo db2 = DatabaseInfo.builder().name(DB2_QUALIFIED_NAME).uri("uri2").build();
polarisDBService.create(requestContext, db1);
polarisDBService.create(requestContext, db2);
Assert.assertTrue(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME));
Assert.assertTrue(polarisDBService.exists(requestContext, DB2_QUALIFIED_NAME));
getPolarisDBService().create(getRequestContext(), db1);
getPolarisDBService().create(getRequestContext(), db2);
Assert.assertTrue(getPolarisDBService().exists(getRequestContext(), DB1_QUALIFIED_NAME));
Assert.assertTrue(getPolarisDBService().exists(getRequestContext(), DB2_QUALIFIED_NAME));

// Since now list dbs use follower_read_timestamp, we will not immediately get the newly created dbs
List<QualifiedName> dbNames =
polarisDBService.listNames(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
getPolarisDBService().listNames(
getRequestContext(), QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
Assert.assertEquals(Sets.newHashSet(dbNames), Sets.newHashSet(DB1_QUALIFIED_NAME, DB2_QUALIFIED_NAME));
List<DatabaseInfo> dbs =
polarisDBService.list(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
getPolarisDBService().list(
getRequestContext(), QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
Assert.assertTrue("Expected dbNames to be empty", dbNames.isEmpty());
Assert.assertTrue("Expected dbs to be empty", dbs.isEmpty());


// After sufficient time, the dbs should return using follower_read_timestamp
simulateDelay();
dbNames = polarisDBService.listNames(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
dbNames = getPolarisDBService().listNames(
getRequestContext(), QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
Assert.assertEquals(dbNames, Arrays.asList(DB1_QUALIFIED_NAME, DB2_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
dbs = getPolarisDBService().list(
getRequestContext(), QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
Assert.assertEquals(dbs, Arrays.asList(db1, db2));

// Test Prefix
dbNames = polarisDBService.listNames(
requestContext,
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), QualifiedName.ofDatabase(CATALOG_NAME, "db"),
null,
null);
Assert.assertEquals(dbNames, Arrays.asList(DB1_QUALIFIED_NAME, DB2_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext,
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
QualifiedName.ofDatabase(CATALOG_NAME, "db"),
null,
null);
Assert.assertEquals(dbs, Arrays.asList(db1, db2));

dbNames = polarisDBService.listNames(
requestContext,
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
QualifiedName.ofDatabase(CATALOG_NAME, "db1_"),
null,
null);
Assert.assertEquals(dbNames, Arrays.asList(DB1_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext,
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
QualifiedName.ofDatabase(CATALOG_NAME, "db1_"),
null,
null);
Assert.assertEquals(dbs, Arrays.asList(db1));

// Test Order desc
dbNames = polarisDBService.listNames(
requestContext,
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
null,
new Sort("name", SortOrder.DESC),
null);
Assert.assertEquals(dbNames, Arrays.asList(DB2_QUALIFIED_NAME, DB1_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext,
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
null,
new Sort("name", SortOrder.DESC),
null);
Assert.assertEquals(dbs, Arrays.asList(db2, db1));

// Test pageable
dbNames = polarisDBService.listNames(
requestContext,
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
null,
null,
new Pageable(5, 0));
Assert.assertEquals(dbNames, Arrays.asList(DB1_QUALIFIED_NAME, DB2_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext,
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME),
null,
null,
new Pageable(5, 0));
Assert.assertEquals(dbs, Arrays.asList(db1, db2));

dbNames = polarisDBService.listNames(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 0));
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 0));
Assert.assertEquals(dbNames, Arrays.asList(DB1_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 0));
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 0));
Assert.assertEquals(dbs, Arrays.asList(db1));

dbNames = polarisDBService.listNames(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 1));
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 1));
Assert.assertEquals(dbNames, Arrays.asList(DB2_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 1));
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(1, 1));
Assert.assertEquals(dbs, Arrays.asList(db2));

dbNames = polarisDBService.listNames(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(5, 1));
dbNames = getPolarisDBService().listNames(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(5, 1));
Assert.assertEquals(dbNames, Arrays.asList(DB2_QUALIFIED_NAME));
dbs = polarisDBService.list(
requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(5, 1));
dbs = getPolarisDBService().list(
getRequestContext(),
QualifiedName.ofCatalog(CATALOG_NAME), null, null, new Pageable(5, 1));
Assert.assertEquals(dbs, Arrays.asList(db2));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity;
import com.netflix.metacat.connector.polaris.store.entities.PolarisDatabaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.boot.model.relational.QualifiedName;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void testGetTableEntities() {
}

/**
* Test testListDbPage.
* test list database with different db page size config.
*/
@Test
public void testListDbPage() {
Expand All @@ -131,28 +132,28 @@ public void testListDbPage() {

simulateDelay();

List<String> dbNames = getPolarisConnector().getDatabaseNames(null, null, 1);
List<PolarisDatabaseEntity> dbs = getPolarisConnector().getDatabases(null, null, 1);
List<String> dbNames = getPolarisConnector().getDatabaseNames("db", null, 1);
List<PolarisDatabaseEntity> dbs = getPolarisConnector().getDatabases("db", null, 1);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"), dbNames);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"),
Assert.assertEquals("Expected dbs ", Arrays.asList("db1", "db2", "db3"),
dbs.stream().map(PolarisDatabaseEntity::getDbName).collect(Collectors.toList()));

dbNames = getPolarisConnector().getDatabaseNames(null, null, 2);
dbs = getPolarisConnector().getDatabases(null, null, 2);
dbNames = getPolarisConnector().getDatabaseNames("db", null, 2);
dbs = getPolarisConnector().getDatabases("db", null, 2);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"), dbNames);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"),
Assert.assertEquals("Expected dbs ", Arrays.asList("db1", "db2", "db3"),
dbs.stream().map(PolarisDatabaseEntity::getDbName).collect(Collectors.toList()));

dbNames = getPolarisConnector().getDatabaseNames(null, null, 3);
dbs = getPolarisConnector().getDatabases(null, null, 3);
dbNames = getPolarisConnector().getDatabaseNames("db", null, 3);
dbs = getPolarisConnector().getDatabases("db", null, 3);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"), dbNames);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"),
Assert.assertEquals("Expected dbs ", Arrays.asList("db1", "db2", "db3"),
dbs.stream().map(PolarisDatabaseEntity::getDbName).collect(Collectors.toList()));

dbNames = getPolarisConnector().getDatabaseNames(null, null, 4);
dbs = getPolarisConnector().getDatabases(null, null, 4);
dbNames = getPolarisConnector().getDatabaseNames("db", null, 4);
dbs = getPolarisConnector().getDatabases("db", null, 4);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"), dbNames);
Assert.assertEquals("Expected dbNames ", Arrays.asList("db1", "db2", "db3"),
Assert.assertEquals("Expected dbs ", Arrays.asList("db1", "db2", "db3"),
dbs.stream().map(PolarisDatabaseEntity::getDbName).collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ public void deleteDatabase(final String dbName) {
public List<PolarisDatabaseEntity> getDatabases(
@Nullable final String dbNamePrefix,
@Nullable final Sort sort,
int pageSize) {
final int pageSize) {
return (List<PolarisDatabaseEntity>) dbRepo.getAllDatabases(dbNamePrefix, sort, pageSize, true);
}

@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<String> getDatabaseNames(@Nullable final String dbNamePrefix, @Nullable final Sort sort, int pageSize) {
public List<String> getDatabaseNames(
@Nullable final String dbNamePrefix,
@Nullable final Sort sort,
final int pageSize) {
return (List<String>) dbRepo.getAllDatabases(dbNamePrefix, sort, pageSize, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package com.netflix.metacat.connector.polaris;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.netflix.metacat.common.QualifiedName;
import com.netflix.metacat.common.server.connectors.ConnectorContext;
import com.netflix.metacat.common.server.connectors.ConnectorRequestContext;
Expand All @@ -15,6 +14,7 @@
import com.netflix.metacat.connector.polaris.configs.PolarisPersistenceConfig;
import com.netflix.metacat.connector.polaris.store.PolarisStoreService;
import com.netflix.spectator.api.NoopRegistry;
import lombok.Getter;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -27,10 +27,8 @@
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import spock.lang.Shared;
import spock.lang.Unroll;

import java.util.Date;
import java.util.List;


/**
Expand All @@ -41,6 +39,7 @@
@ActiveProfiles(profiles = {"polarisconnectortest"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@AutoConfigureDataJpa
@Getter
public class PolarisConnectorDatabaseServiceTest {
public static final String CATALOG_NAME = "catalog_name";
public static final String DB1_NAME = "db1_name";
Expand All @@ -49,16 +48,16 @@ public class PolarisConnectorDatabaseServiceTest {
public static final QualifiedName DB2_QUALIFIED_NAME = QualifiedName.ofDatabase(CATALOG_NAME, DB2_NAME);

@Autowired
public PolarisStoreService polarisStoreService;
private PolarisStoreService polarisStoreService;

@Shared
public ConnectorContext connectorContext;
private ConnectorContext connectorContext;

@Shared
public ConnectorRequestContext requestContext = new ConnectorRequestContext();
private ConnectorRequestContext requestContext = new ConnectorRequestContext();

@Shared
public PolarisConnectorDatabaseService polarisDBService;
private PolarisConnectorDatabaseService polarisDBService;

/**
* Initialization.
Expand Down Expand Up @@ -167,24 +166,5 @@ public void testDeleteDb() {
polarisDBService.delete(requestContext, DB1_QUALIFIED_NAME);
Assert.assertFalse(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME));
}

/**
* Test list databases.
*/
@Test
public void testListDb() {
final DatabaseInfo db1 = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).uri("uri1").build();
final DatabaseInfo db2 = DatabaseInfo.builder().name(DB2_QUALIFIED_NAME).uri("uri2").build();
polarisDBService.create(requestContext, db1);
polarisDBService.create(requestContext, db2);
Assert.assertTrue(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME));
Assert.assertTrue(polarisDBService.exists(requestContext, DB2_QUALIFIED_NAME));
final List<QualifiedName> dbNames =
polarisDBService.listNames(requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
Assert.assertEquals(Sets.newHashSet(dbNames), Sets.newHashSet(DB1_QUALIFIED_NAME, DB2_QUALIFIED_NAME));
final List<DatabaseInfo> dbs =
polarisDBService.list(requestContext, QualifiedName.ofCatalog(CATALOG_NAME), null, null, null);
Assert.assertEquals(Sets.newHashSet(dbs), Sets.newHashSet(db1, db2));
}
}

0 comments on commit 1451aba

Please sign in to comment.