Skip to content

Create ExtensionTestBase for migration to JUnit5#9613

Merged
nastra merged 5 commits intoapache:mainfrom
tomtongue:mig-junit4-to-5-spark-extensions
Feb 2, 2024
Merged

Create ExtensionTestBase for migration to JUnit5#9613
nastra merged 5 commits intoapache:mainfrom
tomtongue:mig-junit4-to-5-spark-extensions

Conversation

@tomtongue
Copy link
Contributor

Create ExtensionTestBase to migrate current tests in SparkExtension to JUnit 5 in regards to #9086.

For now, only one test TestAddFilesProcedure is updated with JUnit 5. Once the current migration works well, I will migrate other classes in the extension path.

@github-actions github-actions bot added the spark label Feb 1, 2024
private static final Random RANDOM = ThreadLocalRandom.current();

@Parameters(name = "catalogName = {0}, implementation = {1}, config = {2}")
protected static Object[][] parameters() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is needed here because the default params are coming from CatalogTestBase

Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove the parameters part (keep the RANDOM because the parent class doesn't have it and it's required).

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(ParameterizedTestExtension.class)
Copy link
Contributor

Choose a reason for hiding this comment

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

I also don't think this is required, because CatalogTestBase already has this


private final int formatVersion;
@Parameter(index = 3)
protected int formatVersion;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
protected int formatVersion;
private int formatVersion;

}

@Rule public TemporaryFolder temp = new TemporaryFolder();
@TempDir protected Path temp;
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to make this protected, because this class doesn't have any subclasses

// Spark Session Catalog cannot load metadata tables
// with "The namespace in session catalog must have exactly one name part"
Assume.assumeFalse(catalogName.equals("spark_catalog"));
Assumptions.assumeFalse(catalogName.equals("spark_catalog"));
Copy link
Contributor

Choose a reason for hiding this comment

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

please use the one from AssertJ: assumeThat(catalog).isNotEqualTo("spark_catalog")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, sorry, let me fix this.

record2.put("id", 2L);
record2.put("data", "b");
File outputFile = temp.newFile("test.avro");
File outputFile = File.createTempFile("test", ".avro", temp.toFile());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
File outputFile = File.createTempFile("test", ".avro", temp.toFile());
File outputFile = temp.resolve("test.avro").toFile();

catalogName, tableName, fileTableDir.getAbsolutePath());

Assert.assertEquals(2L, result);
Assertions.assertThat(result).isEqualTo(2L);
Copy link
Contributor

Choose a reason for hiding this comment

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

it's ok to statically import assertThat() in places where it's newly introduced


Matcher matcher = uuidPattern.matcher(manifestPath);
Assert.assertTrue("verify manifest path has uuid", matcher.find());
Assertions.assertThat(matcher.find()).as("verify manifest path has uuid").isTrue();
Copy link
Contributor

@nastra nastra Feb 1, 2024

Choose a reason for hiding this comment

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

Suggested change
Assertions.assertThat(matcher.find()).as("verify manifest path has uuid").isTrue();
assertThat(matcher).as("verify manifest path has uuid").matches();

Copy link
Contributor Author

@tomtongue tomtongue Feb 2, 2024

Choose a reason for hiding this comment

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

matches is a full string match, so this couldn't pass the test if this test was changed. Or, should I change the regex pattern?

} catch (IOException e) {
throw new RuntimeException(e);
}
fileTableDir = temp.toFile();
Copy link
Contributor

Choose a reason for hiding this comment

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

could you please check if we actually need fileTableDir? I think the calls that use fileTableDir.getAbsolutePath() can be replaced with temp.toFile().getAbsolutePath().

Copy link
Contributor Author

@tomtongue tomtongue Feb 2, 2024

Choose a reason for hiding this comment

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

I believe temp.toFile().getAbsolutePath() is evaluated in the sql and I couldn't find any errors based on my several attempts. However, fileTableDir is called 33 times in this class, and keep the current one is less changes. What do you think? (committed without changes for now)

Copy link
Contributor

Choose a reason for hiding this comment

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

ok let's keep fileTableDir in this case

catalogName, tableName, fileTableDir.getAbsolutePath());

Assert.assertEquals(2L, result);
Assertions.assertThat(result).isEqualTo(2L);
Copy link
Contributor

Choose a reason for hiding this comment

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

please use the statically imported method

// Spark Session Catalog cannot load metadata tables
// with "The namespace in session catalog must have exactly one name part"
Assume.assumeFalse(catalogName.equals("spark_catalog"));
Assumptions.assumeThat(catalogName).isNotEqualTo("spark_catalog");
Copy link
Contributor

Choose a reason for hiding this comment

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

please use the statically imported method

// verify manifest file name has uuid pattern
String manifestPath = (String) sql("select path from %s.manifests", tableName).get(0)[0];

System.out.println(manifestPath);
Copy link
Contributor

Choose a reason for hiding this comment

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

should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot to remove...will do it.

@nastra
Copy link
Contributor

nastra commented Feb 2, 2024

please also add the below diff to iceberg-spark-extensions so that JUnit5 tests are properly executed

diff --git a/spark/v3.5/build.gradle b/spark/v3.5/build.gradle
index eeef75be6..5b07a3c38 100644
--- a/spark/v3.5/build.gradle
+++ b/spark/v3.5/build.gradle
@@ -172,6 +172,10 @@ project(":iceberg-spark:iceberg-spark-extensions-${sparkMajorVersion}_${scalaVer
     antlr libs.antlr.antlr4
   }
 
+  test {
+    useJUnitPlatform()
+  }
+

@github-actions github-actions bot added the build label Feb 2, 2024
Copy link
Contributor

@nastra nastra left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @tomtongue. It would be great if you could also move subclasses of SparkExtensionsTestBase to ExtensionsTestBase in a few follow-up PRs

@tomtongue
Copy link
Contributor Author

@nastra thanks a lot for the review. I will create a new PR to move other classes in the extension after this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants