Skip to content

Commit

Permalink
Fix test deps and Add assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
vinishjail97 committed Dec 30, 2024
1 parent 75f8a4f commit 89cf227
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package org.apache.xtable.testutil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -52,48 +54,74 @@ public static void validateTable(
}

public static class TestCatalogSyncImpl implements CatalogSyncClient {
private static final Map<String, Integer> FUNCTION_CALLS = new HashMap<>();

public TestCatalogSyncImpl(
ExternalCatalogConfig catalogConfig, String tableFormat, Configuration hadoopConf) {}

@Override
public String getCatalogId() {
trackFunctionCall();
return null;
}

@Override
public String getStorageLocation(Object o) {
trackFunctionCall();
return null;
}

@Override
public boolean hasDatabase(CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
return false;
}

@Override
public void createDatabase(CatalogTableIdentifier tableIdentifier) {}
public void createDatabase(CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
}

@Override
public Object getTable(CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
return null;
}

@Override
public void createTable(InternalTable table, CatalogTableIdentifier tableIdentifier) {}
public void createTable(InternalTable table, CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
}

@Override
public void refreshTable(
InternalTable table, Object catalogTable, CatalogTableIdentifier tableIdentifier) {}
InternalTable table, Object catalogTable, CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
}

@Override
public void createOrReplaceTable(InternalTable table, CatalogTableIdentifier tableIdentifier) {}
public void createOrReplaceTable(InternalTable table, CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
}

@Override
public void dropTable(InternalTable table, CatalogTableIdentifier tableIdentifier) {}
public void dropTable(InternalTable table, CatalogTableIdentifier tableIdentifier) {
trackFunctionCall();
}

@Override
public void close() throws Exception {}
public void close() throws Exception {
trackFunctionCall();
}

private void trackFunctionCall() {
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
FUNCTION_CALLS.put(methodName, FUNCTION_CALLS.getOrDefault(methodName, 0) + 1);
}

public static Map<String, Integer> getFunctionCalls() {
return FUNCTION_CALLS;
}
}

public static class TestCatalogConversionSourceImpl implements CatalogConversionSource {
Expand Down
14 changes: 14 additions & 0 deletions xtable-utilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xtable</groupId>
<artifactId>xtable-core_${scala.binary.version}</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.hudi</groupId>
<artifactId>hudi-spark${spark.version.prefix}-bundle_${scala.binary.version}</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Map;

import lombok.SneakyThrows;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand All @@ -46,6 +50,15 @@

public class ITRunCatalogSync {

private static final List<String> EXPECTED_FUNCTION_CALLS =
Arrays.asList(
"hasDatabase",
"createDatabase",
"getTable",
"getStorageLocation",
"createTable",
"getCatalogId");

@Test
void testCatalogSync(@TempDir Path tempDir) throws Exception {
String tableName = "test-table";
Expand All @@ -56,6 +69,10 @@ void testCatalogSync(@TempDir Path tempDir) throws Exception {
File configFile = writeConfigFile(tempDir, table, tableName);
String[] args = new String[] {"-catalogConfig", configFile.getPath()};
RunCatalogSync.main(args);
validateTargetMetadataIsPresent(table.getBasePath());
Map<String, Integer> functionCalls = ITTestUtils.TestCatalogSyncImpl.getFunctionCalls();
EXPECTED_FUNCTION_CALLS.forEach(
(function -> Assertions.assertEquals(2, functionCalls.get(function))));
}
}

Expand Down Expand Up @@ -112,22 +129,14 @@ private static File writeConfigFile(Path tempDir, GenericTable table, String tab
}

@SneakyThrows
private static void waitForNumIcebergCommits(Path metadataPath, int count) {
long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < TimeUnit.MINUTES.toMillis(5)) {
if (numIcebergMetadataJsonFiles(metadataPath) == count) {
break;
}
Thread.sleep(5000);
}
}

@SneakyThrows
private static long numIcebergMetadataJsonFiles(Path path) {
long count = 0;
if (Files.exists(path)) {
count = Files.list(path).filter(p -> p.toString().endsWith("metadata.json")).count();
}
return count;
private void validateTargetMetadataIsPresent(String basePath) {
Path icebergMetadataPath = Paths.get(URI.create(basePath + "/metadata"));
long icebergMetadataFiles =
Files.list(icebergMetadataPath).filter(p -> p.toString().endsWith("metadata.json")).count();
Assertions.assertEquals(2, icebergMetadataFiles);
Path deltaMetadataPath = Paths.get(URI.create(basePath + "/_delta_log"));
long deltaMetadataFiles =
Files.list(deltaMetadataPath).filter(p -> p.toString().endsWith(".json")).count();
Assertions.assertEquals(1, deltaMetadataFiles);
}
}

0 comments on commit 89cf227

Please sign in to comment.