From 6ab8b554a418d7a3742a8f982ae2571ed9b065a0 Mon Sep 17 00:00:00 2001 From: Ingar Abrahamsen Date: Wed, 8 Nov 2023 15:04:46 +0100 Subject: [PATCH] feat(tvf): support for testing table valued function Done the dame way as other fragments however we define the tvf parameters at the top of the fragment. --- .../no/nrk/bigquery/testing/BQSmokeTest.scala | 18 ++++++++++++++++++ .../nrk/bigquery/testing/BQUdfSmokeTest.scala | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/testing/src/main/scala/no/nrk/bigquery/testing/BQSmokeTest.scala b/testing/src/main/scala/no/nrk/bigquery/testing/BQSmokeTest.scala index 307ac3b9..11c67b19 100644 --- a/testing/src/main/scala/no/nrk/bigquery/testing/BQSmokeTest.scala +++ b/testing/src/main/scala/no/nrk/bigquery/testing/BQSmokeTest.scala @@ -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 @@ -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 { diff --git a/testing/src/main/scala/no/nrk/bigquery/testing/BQUdfSmokeTest.scala b/testing/src/main/scala/no/nrk/bigquery/testing/BQUdfSmokeTest.scala index f32ba8c1..afcb51de 100644 --- a/testing/src/main/scala/no/nrk/bigquery/testing/BQUdfSmokeTest.scala +++ b/testing/src/main/scala/no/nrk/bigquery/testing/BQUdfSmokeTest.scala @@ -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" @@ -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))