-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Migrate Procedure sub-classes in spark-extensions to JUnit5 and AssertJ style #9760
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
Changes from 2 commits
2d41bef
5f20957
8f593a8
a375693
e94bd2f
ae3d3e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,9 @@ | |||||
| */ | ||||||
| package org.apache.iceberg.spark.extensions; | ||||||
|
|
||||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||||
| import static scala.collection.JavaConverters.seqAsJavaList; | ||||||
|
|
||||||
| import java.math.BigDecimal; | ||||||
| import java.sql.Timestamp; | ||||||
| import java.time.Instant; | ||||||
|
|
@@ -38,22 +41,16 @@ | |||||
| import org.apache.spark.sql.types.DataType; | ||||||
| import org.apache.spark.sql.types.DataTypes; | ||||||
| import org.assertj.core.api.Assertions; | ||||||
| import org.junit.AfterClass; | ||||||
| import org.junit.Assert; | ||||||
| import org.junit.BeforeClass; | ||||||
| import org.junit.Rule; | ||||||
| import org.junit.Test; | ||||||
| import org.junit.rules.TemporaryFolder; | ||||||
| import scala.collection.JavaConverters; | ||||||
| import org.junit.jupiter.api.AfterAll; | ||||||
| import org.junit.jupiter.api.BeforeAll; | ||||||
| import org.junit.jupiter.api.Test; | ||||||
|
|
||||||
| public class TestCallStatementParser { | ||||||
|
|
||||||
| @Rule public TemporaryFolder temp = new TemporaryFolder(); | ||||||
|
|
||||||
| private static SparkSession spark = null; | ||||||
| private static ParserInterface parser = null; | ||||||
|
|
||||||
| @BeforeClass | ||||||
| @BeforeAll | ||||||
| public static void startSpark() { | ||||||
| TestCallStatementParser.spark = | ||||||
| SparkSession.builder() | ||||||
|
|
@@ -64,7 +61,7 @@ public static void startSpark() { | |||||
| TestCallStatementParser.parser = spark.sessionState().sqlParser(); | ||||||
| } | ||||||
|
|
||||||
| @AfterClass | ||||||
| @AfterAll | ||||||
| public static void stopSpark() { | ||||||
| SparkSession currentSpark = TestCallStatementParser.spark; | ||||||
| TestCallStatementParser.spark = null; | ||||||
|
|
@@ -76,10 +73,9 @@ public static void stopSpark() { | |||||
| public void testCallWithPositionalArgs() throws ParseException { | ||||||
| CallStatement call = | ||||||
| (CallStatement) parser.parsePlan("CALL c.n.func(1, '2', 3L, true, 1.0D, 9.0e1, 900e-1BD)"); | ||||||
| Assert.assertEquals( | ||||||
| ImmutableList.of("c", "n", "func"), JavaConverters.seqAsJavaList(call.name())); | ||||||
| assertThat(seqAsJavaList(call.name())).hasSameElementsAs(ImmutableList.of("c", "n", "func")); | ||||||
|
|
||||||
| Assert.assertEquals(7, call.args().size()); | ||||||
| assertThat(seqAsJavaList(call.args())).hasSize(7); | ||||||
|
|
||||||
| checkArg(call, 0, 1, DataTypes.IntegerType); | ||||||
| checkArg(call, 1, "2", DataTypes.StringType); | ||||||
|
|
@@ -94,10 +90,10 @@ public void testCallWithPositionalArgs() throws ParseException { | |||||
| public void testCallWithNamedArgs() throws ParseException { | ||||||
| CallStatement call = | ||||||
| (CallStatement) parser.parsePlan("CALL cat.system.func(c1 => 1, c2 => '2', c3 => true)"); | ||||||
| Assert.assertEquals( | ||||||
| ImmutableList.of("cat", "system", "func"), JavaConverters.seqAsJavaList(call.name())); | ||||||
| assertThat(seqAsJavaList(call.name())) | ||||||
| .hasSameElementsAs(ImmutableList.of("cat", "system", "func")); | ||||||
|
|
||||||
| Assert.assertEquals(3, call.args().size()); | ||||||
| assertThat(seqAsJavaList(call.args())).hasSize(3); | ||||||
|
|
||||||
| checkArg(call, 0, "c1", 1, DataTypes.IntegerType); | ||||||
| checkArg(call, 1, "c2", "2", DataTypes.StringType); | ||||||
|
|
@@ -107,10 +103,10 @@ public void testCallWithNamedArgs() throws ParseException { | |||||
| @Test | ||||||
| public void testCallWithMixedArgs() throws ParseException { | ||||||
| CallStatement call = (CallStatement) parser.parsePlan("CALL cat.system.func(c1 => 1, '2')"); | ||||||
| Assert.assertEquals( | ||||||
| ImmutableList.of("cat", "system", "func"), JavaConverters.seqAsJavaList(call.name())); | ||||||
| assertThat(seqAsJavaList(call.name())) | ||||||
| .hasSameElementsAs(ImmutableList.of("cat", "system", "func")); | ||||||
|
|
||||||
| Assert.assertEquals(2, call.args().size()); | ||||||
| assertThat(seqAsJavaList(call.args())).hasSize(2); | ||||||
|
|
||||||
| checkArg(call, 0, "c1", 1, DataTypes.IntegerType); | ||||||
| checkArg(call, 1, "2", DataTypes.StringType); | ||||||
|
|
@@ -121,10 +117,10 @@ public void testCallWithTimestampArg() throws ParseException { | |||||
| CallStatement call = | ||||||
| (CallStatement) | ||||||
| parser.parsePlan("CALL cat.system.func(TIMESTAMP '2017-02-03T10:37:30.00Z')"); | ||||||
| Assert.assertEquals( | ||||||
| ImmutableList.of("cat", "system", "func"), JavaConverters.seqAsJavaList(call.name())); | ||||||
| assertThat(seqAsJavaList(call.name())) | ||||||
| .hasSameElementsAs(ImmutableList.of("cat", "system", "func")); | ||||||
|
|
||||||
| Assert.assertEquals(1, call.args().size()); | ||||||
| assertThat(seqAsJavaList(call.args())).hasSize(1); | ||||||
|
|
||||||
| checkArg( | ||||||
| call, 0, Timestamp.from(Instant.parse("2017-02-03T10:37:30.00Z")), DataTypes.TimestampType); | ||||||
|
|
@@ -134,10 +130,10 @@ public void testCallWithTimestampArg() throws ParseException { | |||||
| public void testCallWithVarSubstitution() throws ParseException { | ||||||
| CallStatement call = | ||||||
| (CallStatement) parser.parsePlan("CALL cat.system.func('${spark.extra.prop}')"); | ||||||
| Assert.assertEquals( | ||||||
| ImmutableList.of("cat", "system", "func"), JavaConverters.seqAsJavaList(call.name())); | ||||||
| assertThat(seqAsJavaList(call.name())) | ||||||
| .hasSameElementsAs(ImmutableList.of("cat", "system", "func")); | ||||||
|
|
||||||
| Assert.assertEquals(1, call.args().size()); | ||||||
| assertThat(seqAsJavaList(call.args())).hasSize(1); | ||||||
|
|
||||||
| checkArg(call, 0, "value", DataTypes.StringType); | ||||||
| } | ||||||
|
|
@@ -165,10 +161,10 @@ public void testCallStripsComments() throws ParseException { | |||||
| "CALL -- a line ending comment\n" + "cat.system.func('${spark.extra.prop}')"); | ||||||
| for (String sqlText : callStatementsWithComments) { | ||||||
| CallStatement call = (CallStatement) parser.parsePlan(sqlText); | ||||||
| Assert.assertEquals( | ||||||
| ImmutableList.of("cat", "system", "func"), JavaConverters.seqAsJavaList(call.name())); | ||||||
| assertThat(seqAsJavaList(call.name())) | ||||||
| .hasSameElementsAs(ImmutableList.of("cat", "system", "func")); | ||||||
|
|
||||||
| Assert.assertEquals(1, call.args().size()); | ||||||
| assertThat(seqAsJavaList(call.args())).hasSize(1); | ||||||
|
|
||||||
| checkArg(call, 0, "value", DataTypes.StringType); | ||||||
| } | ||||||
|
|
@@ -188,25 +184,26 @@ private void checkArg( | |||||
|
|
||||||
| if (expectedName != null) { | ||||||
| NamedArgument arg = checkCast(call.args().apply(index), NamedArgument.class); | ||||||
| Assert.assertEquals(expectedName, arg.name()); | ||||||
| assertThat(arg.name()).isEqualTo(expectedName); | ||||||
| } else { | ||||||
| CallArgument arg = call.args().apply(index); | ||||||
| checkCast(arg, PositionalArgument.class); | ||||||
| } | ||||||
|
|
||||||
| Expression expectedExpr = toSparkLiteral(expectedValue, expectedType); | ||||||
| Expression actualExpr = call.args().apply(index).expr(); | ||||||
| Assert.assertEquals("Arg types must match", expectedExpr.dataType(), actualExpr.dataType()); | ||||||
| Assert.assertEquals("Arg must match", expectedExpr, actualExpr); | ||||||
| assertThat(actualExpr.dataType()).as("Arg types must match").isEqualTo(expectedExpr.dataType()); | ||||||
| assertThat(actualExpr).as("Arg must match").isEqualTo(expectedExpr); | ||||||
| } | ||||||
|
|
||||||
| private Literal toSparkLiteral(Object value, DataType dataType) { | ||||||
| return Literal$.MODULE$.create(value, dataType); | ||||||
| } | ||||||
|
|
||||||
| private <T> T checkCast(Object value, Class<T> expectedClass) { | ||||||
| Assert.assertTrue( | ||||||
| "Expected instance of " + expectedClass.getName(), expectedClass.isInstance(value)); | ||||||
| assertThat(expectedClass.isInstance(value)) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this previously, but the tests related to this method failed with the following error:
This works well when using the current code like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the problem with the current assertion is that it only will print true/false if the assertion fails. What we rather want to achieve for assertions is to have enough context to know what actual/expected was exactly. That being said, we want to avoid the usage of What was the assertion that you previously used?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reply. Yes, I understand the situation and we should avoid using In the current PR, I change it to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry my bad, the check should be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, you're correct. sorry for that. Let me fix it. |
||||||
| .as("Expected instance of " + expectedClass.getName()) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| .isTrue(); | ||||||
| return expectedClass.cast(value); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also update the other places