Skip to content

Commit

Permalink
feat: [+] Custom matchers for DoricErrors testing (issue hablapps#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
eruizalo committed Jun 6, 2022
1 parent dc213ed commit 33dfa62
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
30 changes: 30 additions & 0 deletions core/src/test/scala/doric/matchers/CustomMatchers.scala
Original file line number Diff line number Diff line change
@@ -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: _*
)
}
28 changes: 18 additions & 10 deletions core/src/test/scala/doric/syntax/DStructOpsSpec.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 33dfa62

Please sign in to comment.