From cf97997f007152fa073ee2b30ff1d9c3cf1a841b Mon Sep 17 00:00:00 2001 From: Eduardo Ruiz Date: Tue, 17 May 2022 18:03:40 +0200 Subject: [PATCH] feat: [+] Custom matchers for DoricErrors testing (issue #212) --- .../scala/doric/matchers/CustomMatchers.scala | 30 +++++++++++++++++++ .../scala/doric/syntax/DStructOpsSpec.scala | 28 ++++++++++------- 2 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 core/src/test/scala/doric/matchers/CustomMatchers.scala diff --git a/core/src/test/scala/doric/matchers/CustomMatchers.scala b/core/src/test/scala/doric/matchers/CustomMatchers.scala new file mode 100644 index 000000000..6e4a2a9cb --- /dev/null +++ b/core/src/test/scala/doric/matchers/CustomMatchers.scala @@ -0,0 +1,30 @@ +package doric.matchers + +import cats.data.NonEmptyChain +import doric.sem.{DoricMultiError, DoricSingleError} +import org.scalatest._ +import matchers._ + +/** + * Object to ease the import + */ +object CustomMatchers extends CustomMatchers + +/** + * Custom Doric errors matcher + */ +trait CustomMatchers { + class DoricErrorMatcher(exception: DoricSingleError*) + extends Matcher[DoricMultiError] { + override def apply(multiError: DoricMultiError): MatchResult = MatchResult( + matches = multiError.errors == NonEmptyChain(exception), + rawFailureMessage = + s"Expected\n${NonEmptyChain(exception)}\nbut found\n${multiError.errors}", + rawNegatedFailureMessage = s"Doric errors expected and found:\n${multiError.errors}" + ) + } + + def includeErrors(exception: DoricSingleError*) = new DoricErrorMatcher( + exception: _* + ) +} diff --git a/core/src/test/scala/doric/syntax/DStructOpsSpec.scala b/core/src/test/scala/doric/syntax/DStructOpsSpec.scala index f4418ab1d..b991829fc 100644 --- a/core/src/test/scala/doric/syntax/DStructOpsSpec.scala +++ b/core/src/test/scala/doric/syntax/DStructOpsSpec.scala @@ -1,7 +1,9 @@ package doric package syntax -import doric.sem.{ChildColumnNotFound, ColumnTypeError} +import cats.data.NonEmptyChain +import doric.matchers.CustomMatchers.includeErrors +import doric.sem.{ChildColumnNotFound, ColumnTypeError, DoricMultiError} import doric.types.SparkType import org.apache.spark.sql.Row import org.scalatest.EitherValues @@ -12,7 +14,6 @@ case class User(name: String, surname: String, age: Int) class DStructOpsSpec extends DoricTestElements with EitherValues with Matchers { - import doric.implicitConversions.stringCname import spark.implicits._ private val df = List((User("John", "doe", 34), 1)) @@ -49,14 +50,21 @@ class DStructOpsSpec extends DoricTestElements with EitherValues with Matchers { } it("throws an error if the sub column is not of the provided type") { - colStruct("col") - .getChild[String]("age") - .elem - .run(df) - .toEither - .left - .value - .head shouldBe ColumnTypeError("col.age", StringType, IntegerType) + intercept[DoricMultiError] { + df.select(colStruct("col").getChild[String]("age")) + } should includeErrors( + ColumnTypeError("age", StringType, IntegerType), + ColumnTypeError("agea", StringType, IntegerType), + ) +// err shouldBe ColumnTypeError("age", StringType, IntegerType) +// colStruct("col") +// .getChild[String]("age") +// .elem +// .run(df) +// .toEither +// .left +// .value +// .head shouldBe ColumnTypeError("age", StringType, IntegerType) } it(