Skip to content

Commit

Permalink
feat(tvf): support for testing table valued function
Browse files Browse the repository at this point in the history
Done the dame way as other fragments however we define the tvf parameters at the
top of the fragment.
  • Loading branch information
ingarabr committed Nov 8, 2023
1 parent aca4ebf commit 6ab8b55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions testing/src/main/scala/no/nrk/bigquery/testing/BQSmokeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import no.nrk.bigquery.internal.SchemaHelper
import no.nrk.bigquery.testing.BQSmokeTest.{CheckType, bqCheckFragment}
import org.typelevel.log4cats.slf4j._
import no.nrk.bigquery.syntax._
import no.nrk.bigquery.util.{Nat, Sized}
import org.typelevel.log4cats.SelfAwareStructuredLogger

import java.nio.charset.StandardCharsets
Expand Down Expand Up @@ -221,6 +222,23 @@ abstract class BQSmokeTest(testClient: Resource[IO, BigQueryClient[IO]]) extends
)
}
}

protected def bqCheckTableValueFunction[N <: Nat](testName: String, tvf: TVF[_, N])(
args: Sized[IndexedSeq[BQSqlFrag.Magnet], N]
)(implicit loc: Location): Unit = {
val values =
tvf.params.unsized
.zip(args.unsized)
.map { case (param, arg) => bqfr"declare ${param.name} default ${arg};" }
.mkFragment("\n")

bqCheckFragmentTest(testName)(
bqfr"""|$values
|${tvf.query}
""".stripMargin -> tvf.schema
)
}

}

object BQSmokeTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class BQUdfSmokeTest(testClient: Resource[IO, BigQueryClient[IO]]) exte
expected: Json
)(implicit loc: Location): Unit =
call.routine match {
case tvf: TVF[_, _] => fail(s"does not support TVF: ${tvf.name.asString}") // todo implement TVF check call!
case tvf: TVF[_, _] => fail(s"does not support TVF: ${tvf.name.asString}, Use BQSmokeTest")
case udf: UDF[_, _] =>
val longerTestName = show"${udf.name} - $testName"

Expand All @@ -65,7 +65,7 @@ object BQUdfSmokeTest {
): BigQueryClient[IO] => IO[Json] = { bqClient =>
call.routine match {
case _: TVF[_, _] =>
IO.raiseError[Json](new IllegalStateException("Does not support TVF")) // todo implement TVF call evaluation!
IO.raiseError[Json](new IllegalStateException("Does not support TVF, Use BQSmokeTest"))
case _: UDF.Temporary[_] => evalInlineableCall(testName, bqClient, call)
case _: UDF.Reference[_] => evalInlineableCall(testName, bqClient, call)
case udf: UDF.Persistent[_] => evalInlineableCall(testName, bqClient, call.copy(routine = udf.convertToTemporary))
Expand Down

0 comments on commit 6ab8b55

Please sign in to comment.