Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create SparkCatalogTestBase class for the migration to JUnit5 #9129

Merged
merged 5 commits into from
Nov 24, 2023

Conversation

tomtongue
Copy link
Contributor

@tomtongue tomtongue commented Nov 22, 2023

Base class creation for the migration to JUnit5 in regards to #9076 .

fixes #9074 #9075 #9076

@github-actions github-actions bot added the spark label Nov 22, 2023
@tomtongue
Copy link
Contributor Author

tomtongue commented Nov 22, 2023

Working on updating other tests for JUnit5 and AssertJ. After updating the tests the class is merged into the original class (if I should separate other test updates to other PRs or there are another way, please let me know).

@nastra
Copy link
Contributor

nastra commented Nov 22, 2023

@tomtongue I think we first need to have a JUnit5 equivalent of SparkTestBase and SparkTestBaseWithCatalog before we can migrate SparkCatalogTestBase. This could be done as part of this PR as the required changes should be fairly small.

@tomtongue
Copy link
Contributor Author

Thank you! I'm working on changes. Let me do this.

@tomtongue
Copy link
Contributor Author

tomtongue commented Nov 22, 2023

Add the following classes that basically cover JUnit5 and AssertJ styles. Each class has a lot of inheritants so that new classes are created for now. And, can I update existing Spark tests to use the new base classes? @nastra

import org.assertj.core.api.Assertions;
import org.junit.Assert;

public class TestHelperBase {
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 we need this class, we can just use SparkTestHelperBase because there's nothing JUnit4-specific in there

Copy link
Contributor Author

@tomtongue tomtongue Nov 23, 2023

Choose a reason for hiding this comment

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

Yes, that's correct, thank you! I remove TestHelperBase and add changes to SparkTestHelperBase with JUnit5 updates.

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

public abstract class TestBase extends TestHelperBase {
Copy link
Contributor

Choose a reason for hiding this comment

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

this should extend SparkTestHelperBase

}
}

@TempDir public File temp;
Copy link
Contributor

Choose a reason for hiding this comment

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

for JUnit5 this can be protected

SparkCatalogConfig.SPARK.properties()));
}

@TempDir public File temp;
Copy link
Contributor

Choose a reason for hiding this comment

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

can be protected

Copy link
Contributor

Choose a reason for hiding this comment

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

actually I don't think we need this here, because we already have it in the super class

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.

this looks almost ready, just a few small things. Also could you please convert TestSparkFileRewriter to use this new base class?

@nastra
Copy link
Contributor

nastra commented Nov 23, 2023

Add the following classes that basically cover JUnit5 and AssertJ styles. Each class has a lot of inheritants so that new classes are created for now. And, can I update existing Spark tests to use the new base classes

Let's first get the new base classes in and convert a single test to the new base class. Then we can handle other classes in a separate PR

@tomtongue
Copy link
Contributor Author

Thanks for the review, @nastra! I add the following changes based on the reviews:

  • Change SparkTestHelperBase with the AssertJ style (removed TestHelperBase)
  • Switch TestSparkFileRewriter's super class to TestBase (from SparkTestBase)
  • Change TestSparkFileRewriter with the AssertJ style
  • Remove temp from CatalogTestBase and change temp scope in its super class to protected

@@ -55,12 +55,15 @@ private Object[] toJava(Row row) {

protected void assertEquals(
String context, List<Object[]> expectedRows, List<Object[]> actualRows) {
Assert.assertEquals(
context + ": number of results should match", expectedRows.size(), actualRows.size());
Assertions.assertThat(actualRows.size())
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 better to have

Assertions.assertThat(actualRows)
        .as(context + ": number of results should match")
        .hasSameSizeAs(expectedRows);

because this will show the content when the assertion ever fails

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmed that on my end, thanks. Update the tests.

for (int row = 0; row < expectedRows.size(); row += 1) {
Object[] expected = expectedRows.get(row);
Object[] actual = actualRows.get(row);
Assert.assertEquals("Number of columns should match", expected.length, actual.length);
Assertions.assertThat(actual.length)
Copy link
Contributor

Choose a reason for hiding this comment

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

for the same reason that I mentioned above, it's better to have Assertions.assertThat(actual).as("Number of columns should match").hasSameSizeAs(expected);

@@ -69,19 +72,25 @@ protected void assertEquals(
}

protected void assertEquals(String context, Object[] expectedRow, Object[] actualRow) {
Assert.assertEquals("Number of columns should match", expectedRow.length, actualRow.length);
Assertions.assertThat(actualRow.length)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

Assert.assertArrayEquals(newContext, (byte[]) expectedValue, (byte[]) actualValue);
Assertions.assertThat((byte[]) actualValue)
.as(newContext)
.isEqualTo((byte[]) expectedValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

this cast shouldn't be required

@@ -129,9 +128,9 @@ private void checkDataFilesDeleteThreshold(SizeBasedDataRewriter rewriter) {
rewriter.init(options);

Iterable<List<FileScanTask>> groups = rewriter.planFileGroups(tasks);
Assert.assertEquals("Must have 1 group", 1, Iterables.size(groups));
Assertions.assertThat(Iterables.size(groups)).as("Must have 1 group").isEqualTo(1);
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
Assertions.assertThat(Iterables.size(groups)).as("Must have 1 group").isEqualTo(1);
Assertions.assertThat(groups).hasSize(1);

@@ -110,9 +109,9 @@ private void checkDataFileSizeFiltering(SizeBasedDataRewriter rewriter) {
rewriter.init(options);

Iterable<List<FileScanTask>> groups = rewriter.planFileGroups(tasks);
Assert.assertEquals("Must have 1 group", 1, Iterables.size(groups));
Assertions.assertThat(Iterables.size(groups)).as("Must have 1 group").isEqualTo(1);
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
Assertions.assertThat(Iterables.size(groups)).as("Must have 1 group").isEqualTo(1);
Assertions.assertThat(groups).hasSize(1);

List<FileScanTask> group = Iterables.getOnlyElement(groups);
Assert.assertEquals("Must rewrite 2 files", 2, group.size());
Assertions.assertThat(group.size()).as("Must rewrite 2 files").isEqualTo(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

List<FileScanTask> group = Iterables.getOnlyElement(groups);
Assert.assertEquals("Must rewrite big file", 1, group.size());
Assertions.assertThat(group.size()).as("Must rewrite big file").isEqualTo(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

please adjust all of these to use Assertions.assertThat(groups).as(..).hasSize(1);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, thanks for the review. I'm working on it.

@tomtongue
Copy link
Contributor Author

tomtongue commented Nov 24, 2023

Thanks for the review again. Change the tests that are commented here. I reviewed all my changes on my end again, but If there's any part that should be changed, please let me know @nastra

@nastra nastra merged commit b20d30c into apache:main Nov 24, 2023
devangjhabakh pushed a commit to cdouglas/iceberg that referenced this pull request Apr 22, 2024
@tomtongue tomtongue deleted the switch-junit5-for-sparkcatalogtestbase branch July 30, 2024 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create JUnit5-version of SparkTestBase
2 participants