Skip to content

Commit f2bf731

Browse files
committed
test: add scalatest assertions and fix flakiness
1 parent 38cd74d commit f2bf731

File tree

6 files changed

+56
-20
lines changed

6 files changed

+56
-20
lines changed

common/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
<version>${project.version}</version>
4747
<scope>test</scope>
4848
</dependency>
49+
<dependency>
50+
<groupId>org.scalatest</groupId>
51+
<artifactId>scalatest_${scala.binary.version}</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.scalatestplus</groupId>
56+
<artifactId>junit-4-13_${scala.binary.version}</artifactId>
57+
<scope>test</scope>
58+
</dependency>
4959
</dependencies>
5060
<build>
5161
<plugins>

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@
113113
<artifactId>scala-reflect</artifactId>
114114
<version>${scala.version}</version>
115115
</dependency>
116+
<dependency>
117+
<groupId>org.scalatest</groupId>
118+
<artifactId>scalatest_${scala.binary.version}</artifactId>
119+
<version>3.2.19</version>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.scalatestplus</groupId>
123+
<artifactId>junit-4-13_${scala.binary.version}</artifactId>
124+
<version>3.2.19.0</version>
125+
</dependency>
116126
<dependency>
117127
<groupId>org.slf4j</groupId>
118128
<artifactId>slf4j-api</artifactId>

spark-3/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
<version>${project.version}</version>
5656
<scope>test</scope>
5757
</dependency>
58+
<dependency>
59+
<groupId>org.scalatest</groupId>
60+
<artifactId>scalatest_${scala.binary.version}</artifactId>
61+
<scope>test</scope>
62+
</dependency>
5863
</dependencies>
5964
<build>
6065
<plugins>

spark-3/src/test/scala/org/neo4j/spark/DataSourceWriterTSE.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ import org.neo4j.driver.summary.ResultSummary
3737
import org.neo4j.driver.types.IsoDuration
3838
import org.neo4j.driver.types.Type
3939
import org.neo4j.spark.util.Neo4jOptions
40+
import org.scalatest.matchers.must.Matchers.be
41+
import org.scalatest.matchers.must.Matchers.include
42+
import org.scalatest.matchers.must.Matchers.the
43+
import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper
4044

4145
import java.time.LocalTime
4246
import java.time.OffsetTime
43-
import java.time.ZoneOffset
4447

4548
import scala.collection.JavaConverters._
4649
import scala.collection.immutable.ListMap
@@ -572,7 +575,7 @@ class DataSourceWriterTSE extends SparkConnectorScalaBaseTSE {
572575
assertEquals(total, records.size)
573576
}
574577

575-
@Test(expected = classOf[SparkException])
578+
@Test
576579
def `should throw an error because the node already exists`(): Unit = {
577580
SparkConnectorScalaSuiteIT.session()
578581
.writeTransaction(new TransactionWork[Result] {
@@ -588,24 +591,23 @@ class DataSourceWriterTSE extends SparkConnectorScalaBaseTSE {
588591
val ds = Seq(SimplePerson("Andrea", "Santurbano")).toDS()
589592

590593
try {
591-
ds.write
592-
.format(classOf[DataSource].getName)
593-
.mode(SaveMode.Append)
594-
.option("url", SparkConnectorScalaSuiteIT.server.getBoltUrl)
595-
.option("labels", "Person")
596-
.save() // we need the action to be able to trigger the exception because of the changes in Spark 3
597-
} catch {
598-
case sparkException: SparkException => {
599-
val clientException = ExceptionUtils.getRootCause(sparkException)
600-
assertNotNull(clientException)
601-
assertTrue(clientException.isInstanceOf[ClientException])
602-
assertTrue(
603-
clientException.asInstanceOf[ClientException].code() == "Neo.ClientError.Schema.ConstraintValidationFailed"
604-
)
605-
throw sparkException
594+
val thrown = the[SparkException] thrownBy {
595+
ds.write
596+
.format(classOf[DataSource].getName)
597+
.mode(SaveMode.Append)
598+
.option("url", SparkConnectorScalaSuiteIT.server.getBoltUrl)
599+
.option("labels", "Person")
600+
.save() // we need the action to be able to trigger the exception because of the changes in Spark 3
601+
}
602+
603+
thrown.getMessage should include("org.neo4j.driver.exceptions.ClientException")
604+
val rootCause = ExceptionUtils.getRootCause(thrown)
605+
// root cause is not always returned as a ClientException so we pass it through pattern matching to remove flakiness
606+
rootCause match {
607+
case c: ClientException =>
608+
c.code() should be("Neo.ClientError.Schema.ConstraintValidationFailed")
609+
case _ =>
606610
}
607-
case e: Throwable =>
608-
fail(s"should be thrown a ${classOf[SparkException].getName} but is ${e.getClass.getSimpleName}")
609611
} finally {
610612
SparkConnectorScalaSuiteIT.session()
611613
.writeTransaction(new TransactionWork[Result] {

test-support/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
<artifactId>powermock-module-junit4</artifactId>
4848
<version>${powermock.version}</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.scalatest</groupId>
52+
<artifactId>scalatest_${scala.binary.version}</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.scalatestplus</groupId>
56+
<artifactId>junit-4-13_${scala.binary.version}</artifactId>
57+
</dependency>
5058
<dependency>
5159
<groupId>org.slf4j</groupId>
5260
<artifactId>slf4j-simple</artifactId>

test-support/src/main/scala/org/neo4j/spark/SparkConnectorScalaBaseTSE.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.neo4j.driver.Transaction
2525
import org.neo4j.driver.TransactionWork
2626
import org.neo4j.driver.summary.ResultSummary
2727
import org.neo4j.spark
28+
import org.scalatestplus.junit.JUnitSuite
2829

2930
import java.util.concurrent.TimeUnit
3031

@@ -52,7 +53,7 @@ object SparkConnectorScalaBaseTSE {
5253

5354
}
5455

55-
class SparkConnectorScalaBaseTSE {
56+
class SparkConnectorScalaBaseTSE extends JUnitSuite {
5657

5758
val conf: SparkConf = SparkConnectorScalaSuiteIT.conf
5859
val ss: SparkSession = SparkConnectorScalaSuiteIT.ss

0 commit comments

Comments
 (0)