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 May 17, 2022
1 parent 274d789 commit 16d9c0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 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: _*
)
}
33 changes: 20 additions & 13 deletions core/src/test/scala/doric/syntax/DStructOpsSpec.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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 org.scalatest.EitherValues
import org.scalatest.matchers.should.Matchers

import org.apache.spark.sql.types.{IntegerType, StringType}

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))
.toDF("col", "delete")
.select("col")

describe("Dinamic struct column") {
it("can get values subcolumns") {
describe("Dynamic struct column") {
it("can get values sub-columns") {
df.validateColumnType(colStruct("col").getChild[String]("name"))
df.validateColumnType(colStruct("col").getChild[Int]("age"))
}
Expand Down Expand Up @@ -48,14 +48,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("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)
}
}

Expand Down

0 comments on commit 16d9c0f

Please sign in to comment.