Repository navigation
Flink 1.17: Create JUnit5 version of TestFlinkScan - #9185
Conversation
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| public class HadoopCatResource implements BeforeEachCallback, AfterEachCallback { | ||
| protected final @TempDir Path temporaryFolder; |
There was a problem hiding this comment.
Do we need the annotation here?
There was a problem hiding this comment.
Thanks for the catch! Will remove the annotation.
| protected String warehouse; | ||
| protected TableLoader tableLoader; | ||
|
|
||
| public HadoopCatResource(@TempDir Path temporaryFolder, String database, String tableName) { |
There was a problem hiding this comment.
Do we need the annotation here?
There was a problem hiding this comment.
Thanks for the catch! Will remove the annotation.
|
|
||
| Assert.assertTrue( | ||
| "expected and actual should be both null or not null", expected != null && actual != null); | ||
| Assertions.assertThat(expected != null && actual != null) |
There was a problem hiding this comment.
Maybe static import Assertions.assertThat?
| case FIXED: | ||
| Assertions.assertThat(expected).as("Should expect byte[]").isInstanceOf(byte[].class); | ||
| Assert.assertArrayEquals("binary should be equal", (byte[]) expected, (byte[]) actual); | ||
| Assertions.assertThat((byte[]) actual).as("binary should be equal").isEqualTo(expected); |
There was a problem hiding this comment.
Could we do chaining?
Assertions.assertThat(expected)
.as("Should expect byte[]")
.isInstanceOf(byte[].class)
.isEqualTo(actual);
| super(fileFormat); | ||
| } | ||
|
|
||
| public class TestFlinkInputFormat extends TestFlinkSrc { |
There was a problem hiding this comment.
How many tests inherit from the TestFlinkSource?
Would it be too much to review in one run? Or would it be easier to move all of them at once.
If we move all of them at once, we would not need TestFlinkSrc, and we can keep, and migrate TestFlinkSource instead
There was a problem hiding this comment.
I can see the following classes impacted by TestFlinkSource:
- TestFlinkScanSql
In TestFlinkScan, there's HadoopCatalogResource which is used by quite a number of unit tests. I had renamed HadoopCatalogResource to HadoopCatResource in this commit and migrate the impacted classes listed above.
| import org.apache.iceberg.flink.TestFixtures; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
|
|
||
| public abstract class TestFlinkSrc extends TestScan { |
There was a problem hiding this comment.
I can see the following classes impacted by TestFlinkScan:
- TestIcebergSourceBounded
- TestIcebergSourceBoundedSql
In TestFlinkScan, there's HadoopCatalogResource which is used by quite a number of unit tests. I had renamed HadoopCatalogResource to HadoopCatResource in this commit and migrate the impacted classes listed above.
| import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
|
||
| public class HadoopCatResource implements BeforeEachCallback, AfterEachCallback { |
There was a problem hiding this comment.
Isn't HadoopCatalogExtension a substitute for this?
There was a problem hiding this comment.
yes I think we can just use HadoopCatalogExtension, thus we can remove HadoopCatResource
There was a problem hiding this comment.
Thanks. I didn’t know about HadoopCatalogExtension, will use that instead
| this.fileFormat = FileFormat.fromString(fileFormat); | ||
| public static Stream<Arguments> parameters() { | ||
| return Stream.of( | ||
| Arguments.of(FileFormat.fromString("avro")), |
There was a problem hiding this comment.
Could this be FileFormat.PARQUET, FileFormat.ORC, FileFormat.AVRO?
There was a problem hiding this comment.
Also please join the discussion on #9161 to decide a general way to implement parametrized tests
| import org.assertj.core.api.Assertions; | ||
|
|
||
| /** Helper for appending {@link DataFile} to a table or appending {@link Record}s to a table. */ | ||
| public class GenAppenderHelper { |
There was a problem hiding this comment.
This is more like a question...
If I understand correctly GenericAppenderHelper is only used by Flink tests.
If we do not see it used in other places, we might want to consider it moving out from here, to the Flink specific code, and maybe with a package switch we can keep the class name
There was a problem hiding this comment.
Let me see whether we can do that.
There was a problem hiding this comment.
@pvary , GenericAppenderHelper is also used by iceberg-mr package. If I migrate GenericAppenderHelper, I have to migrate the test in iceberg-mr package too.
There was a problem hiding this comment.
If that is the case, then we should keep this here. Thanks
| Assert.assertTrue( | ||
| "expected Record and actual RowData should be both null or not null", | ||
| expectedRecord != null && actualRowData != null); | ||
| assertThat(expectedRecord != null && actualRowData != null) |
There was a problem hiding this comment.
this should be assertThat(expectedRecord).isNotNull() and then assertThat(actualRowData).isNotNull().
We want to avoid using isTrue/isFalse as much as possible, because it's more difficult to debug when the assertion fails
| "expected and actual should be both null or not null", expected != null && actual != null); | ||
| assertThat(expected != null && actual != null) | ||
| .as("expected and actual should be both null or not null") | ||
| .isTrue(); |
| List<Types.NestedField> fields = schema.asStruct().fields(); | ||
| Assert.assertEquals(fields.size(), record.getSchema().getFields().size()); | ||
| Assert.assertEquals(fields.size(), row.getArity()); | ||
| assertThat(fields.size()).isEqualTo(record.getSchema().getFields().size()); |
There was a problem hiding this comment.
assertThat(xyz).hasSameSizeAs(...)
| Assert.assertEquals(fields.size(), record.getSchema().getFields().size()); | ||
| Assert.assertEquals(fields.size(), row.getArity()); | ||
| assertThat(fields.size()).isEqualTo(record.getSchema().getFields().size()); | ||
| assertThat(fields.size()).isEqualTo(row.getArity()); |
There was a problem hiding this comment.
| assertThat(fields.size()).isEqualTo(row.getArity()); | |
| assertThat(fields).hasSize(row.getArity()); |
| "expected and actual should be both null or not null", expected != null && actual != null); | ||
| assertThat(expected != null && actual != null) | ||
| .as("expected and actual should be both null or not null") | ||
| .isTrue(); |
| "Deleted files count must match", expected.deletedFilesCount(), actual.deletedFilesCount()); | ||
| Assert.assertEquals( | ||
| "Deleted rows count must match", expected.deletedRowsCount(), actual.deletedRowsCount()); | ||
| assertThat(expected != null && actual != null).as("Should not be null.").isTrue(); |
| "PartitionFieldSummary size does not match", | ||
| expectedSummaries.size(), | ||
| actualSummaries.size()); | ||
| assertThat(actualSummaries.size()) |
There was a problem hiding this comment.
this should use hasSameSizeAs()
| Assert.assertEquals("Path", expected.path(), actual.path()); | ||
| Assert.assertEquals("Format", expected.format(), actual.format()); | ||
| Assert.assertEquals("Partition size", expected.partition().size(), actual.partition().size()); | ||
| assertThat(expected != null && actual != null).as("Shouldn't be null.").isTrue(); |
…use hasSameSizeAs to check collection size
|
|
||
| FileAppender<Record> appender = appenderFactory.newAppender(Files.localOutput(file), format); | ||
| try (FileAppender<Record> fileAppender = appender) { | ||
| fileAppender.addAll(records); |
There was a problem hiding this comment.
should we log if we have exceptions?
| actualSummaries.size()); | ||
| assertThat(actualSummaries) | ||
| .as("PartitionFieldSummary size does not match") | ||
| .hasSameSizeAs(expectedSummaries.size()); |
There was a problem hiding this comment.
I dont think this is the right way to use hasSameSizeAs assertions.
the parameter of
.hasSameSizeAsshouldn't be a number, but instead another collection.
Please make changes accordingly to the rest of the codebase
| break; | ||
| case BINARY: | ||
| Assertions.assertThat(expected) | ||
| assertThat(expected) |
There was a problem hiding this comment.
looks like the actual/expected is the wrong way here as well. Can you please check all the other places in this file to make sure that we're using `assertThat(actual)...isEqualTo(expected)
| import org.assertj.core.api.Assertions; | ||
|
|
||
| /** Helper for appending {@link DataFile} to a table or appending {@link Record}s to a table. */ | ||
| public class GenAppenderHelper { |
There was a problem hiding this comment.
I don't think we need this class. We can just update GenericAppenderHelper to work with JUnit4's TemporaryFolder and JUnit5's Path by applying this patch:
diff --git a/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java b/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java
index beccc8bb7..8521004ca 100644
--- a/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java
+++ b/data/src/test/java/org/apache/iceberg/data/GenericAppenderHelper.java
@@ -20,6 +20,7 @@ package org.apache.iceberg.data;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.iceberg.AppendFiles;
@@ -44,13 +45,24 @@ public class GenericAppenderHelper {
private final Table table;
private final FileFormat fileFormat;
private final TemporaryFolder tmp;
+ private final Path temp;
private final Configuration conf;
+ @Deprecated
public GenericAppenderHelper(
Table table, FileFormat fileFormat, TemporaryFolder tmp, Configuration conf) {
this.table = table;
this.fileFormat = fileFormat;
this.tmp = tmp;
+ this.temp = null;
+ this.conf = conf;
+ }
+
+ public GenericAppenderHelper(Table table, FileFormat fileFormat, Path temp, Configuration conf) {
+ this.table = table;
+ this.fileFormat = fileFormat;
+ this.tmp = null;
+ this.temp = temp;
this.conf = conf;
}
@@ -94,14 +106,14 @@ public class GenericAppenderHelper {
public DataFile writeFile(List<Record> records) throws IOException {
Preconditions.checkNotNull(table, "table not set");
- File file = tmp.newFile();
+ File file = null != tmp ? tmp.newFile() : File.createTempFile("junit", null, temp.toFile());
Assert.assertTrue(file.delete());
return appendToLocalFile(table, file, fileFormat, null, records, conf);
}
public DataFile writeFile(StructLike partition, List<Record> records) throws IOException {
Preconditions.checkNotNull(table, "table not set");
- File file = tmp.newFile();
+ File file = null != tmp ? tmp.newFile() : File.createTempFile("junit", null, temp.toFile());
Assert.assertTrue(file.delete());
return appendToLocalFile(table, file, fileFormat, partition, records, conf);
}
that way we don't have to maintain two files.
@cgpoh can you please remove GenAppenderHelper and apply the Assert -> AssertJ conversions to GenericAppenderHelper?
| @TestTemplate | ||
| public void testReadPartitionColumn() throws Exception { | ||
| Assume.assumeTrue("Temporary skip ORC", FileFormat.ORC != fileFormat); | ||
| Assumptions.assumeTrue(FileFormat.ORC != fileFormat, "Temporary skip ORC"); |
There was a problem hiding this comment.
please use Assumptions from AssertJ: `assumeThat(fileFormat).as("Temporary skip ORC").isNotEqualTo(FileFormat.ORC)
| public class TestFlinkInputFormat extends TestFlinkSource { | ||
|
|
||
| public TestFlinkInputFormat(String fileFormat) { | ||
| public TestFlinkInputFormat(FileFormat fileFormat) { |
There was a problem hiding this comment.
all of the constructors in the subclasses can be removed
| public final HadoopCatalogResource catalogResource = | ||
| new HadoopCatalogResource(TEMPORARY_FOLDER, TestFixtures.DATABASE, TestFixtures.TABLE); | ||
| @RegisterExtension | ||
| public static final HadoopCatalogExtension catalogExtension = |
There was a problem hiding this comment.
| public static final HadoopCatalogExtension catalogExtension = | |
| protected static final HadoopCatalogExtension catalogExtension = |
|
|
||
| TestFlinkScan(String fileFormat) { | ||
| this.fileFormat = FileFormat.fromString(fileFormat); | ||
| TestFlinkScan(FileFormat fileFormat) { |
There was a problem hiding this comment.
the constructor needs to be removed because the file format will be set by the parameterized environment
| new HadoopCatalogExtension(TestFixtures.DATABASE, TestFixtures.TABLE); | ||
|
|
||
| // parametrized variables | ||
| protected final FileFormat fileFormat; |
There was a problem hiding this comment.
| protected final FileFormat fileFormat; | |
| @Parameter protected FileFormat fileFormat; |
| private volatile TableEnvironment tEnv; | ||
|
|
||
| public TestIcebergSourceBoundedSql(String fileFormat) { | ||
| public TestIcebergSourceBoundedSql(FileFormat fileFormat) { |
ecd6a56 to
87c58bd
Compare
87c58bd to
e10733d
Compare
Thanks @nastra for addressing the remaining items. I don’t have access to my laptop for now until new year :) |
|
Do we have a way to check if the new tests were running on the CI? With the correct parameters? |
I don't think so, but I was verifying them all locally |
In this case, I am find with these changes. |
|
@cgpoh can you please port these changes to other Flink versions? |
Sure. Will try to port these changes next week. |
|
@nastra, thanks for the backport. Sorry that I'm not able to work on it earlier. |
Fixes #9077