我是Scala新手。我正试图使用ZIO库对Scala Spark数据框架的UT/DQ检查进行单元测试断言。如果有谁以前做过ZIO库,可以帮我一下吗?
我建议使用Spark -fast测试来对scala中的Spark dataframe进行断言。ZIO-test并不是spark-fast-tests文档支持的框架之一,但是你应该仍然可以使用它。
如果你在一个DataFrame上有一些需要测试的转换:
import org.apache.spark.sql.functions.lit
import org.apache.spark.sql.DataFrame
object Transformations {
def appendLiteral(incomingData: DataFrame): DataFrame =
incomingData.withColumn("foo", lit("bar"))
}
一个不利用更广泛的ZIO效应生态系统的幼稚测试可能是这样的:
import com.github.mrpowers.spark.fast.tests.DataFrameComparer
import org.apache.spark.sql.SparkSession
import zio.test._
import zio.test.Assertion._
object TransformationsSpec extends ZIOSpecDefault with DataFrameComparer {
val spark: SparkSession = SparkSession.builder().config("spark.master", "local").getOrCreate()
import spark.implicits._
def spec = suite("TransformationSpec")(
test("appendLiteral adds a column named 'foo' with value 'bar'") {
val testInput: DataFrame = Seq("Hello", "hi", "howdy").toDF("greeting")
val expected: DataFrame = Seq(("Hello", "bar"), ("hi", "bar"), ("howdy", "bar")).toDF("greeting", "foo")
val result = testInput.transform(Transformations.appendLiteral)
assert(assertSmallDataFrameEquality(expected, result, ignoreNullable = true))(isUnit)
}
)
}