From 754593feba8f80fd6b8001a1a4a32050e9feb1e3 Mon Sep 17 00:00:00 2001 From: sebastian-alfers Date: Mon, 22 Jan 2024 11:18:57 +0100 Subject: [PATCH] unit test for sqlserver query adapter --- .../persistence/r2dbc/internal/SqlSpec.scala | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/core/src/test/scala/akka/persistence/r2dbc/internal/SqlSpec.scala b/core/src/test/scala/akka/persistence/r2dbc/internal/SqlSpec.scala index 4154846a..425cbed6 100644 --- a/core/src/test/scala/akka/persistence/r2dbc/internal/SqlSpec.scala +++ b/core/src/test/scala/akka/persistence/r2dbc/internal/SqlSpec.scala @@ -6,14 +6,15 @@ package akka.persistence.r2dbc.internal import akka.persistence.r2dbc.internal.codec.IdentityAdapter import akka.persistence.r2dbc.internal.codec.QueryAdapter +import akka.persistence.r2dbc.internal.codec.SqlServerQueryAdapter import org.scalatest.TestSuite import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec class SqlSpec extends AnyWordSpec with TestSuite with Matchers { import Sql.Interpolation - implicit val queryAdapter: QueryAdapter = IdentityAdapter "SQL string interpolation" should { + implicit val queryAdapter: QueryAdapter = IdentityAdapter "replace ? bind parameters with numbered $ (avoiding escaped ones)" in { sql"select * from bar where a = ? and qa = 'Question?? Answer!'" shouldBe "select * from bar where a = $1 and qa = 'Question? Answer!'" sql"select * from bar where a = ? and b = ? and jsonb ?? 'status' and c = ?" shouldBe "select * from bar where a = $1 and b = $2 and jsonb ? 'status' and c = $3" @@ -40,4 +41,34 @@ class SqlSpec extends AnyWordSpec with TestSuite with Matchers { } } + "SQL string interpolation for sqlserver" should { + implicit val queryAdapter: QueryAdapter = SqlServerQueryAdapter + + "replace $ bind parameters with numbered @ (avoiding escaped ones)" in { + sql"select * from bar where a = ? and qa = 'Question?? Answer!'" shouldBe "select * from bar where a = @p1 and qa = 'Question? Answer!'" + sql"select * from bar where a = ? and b = ? and jsonb ?? 'status' and c = ?" shouldBe "select * from bar where a = @p1 and b = @p2 and jsonb ? 'status' and c = @p3" + sql"select * from bar" shouldBe "select * from bar" + } + + "work together with standard string interpolation" in { + val table = "foo" + sql"select * from $table where a = ?" shouldBe "select * from foo where a = @p1" + } + + "replace bind parameters after standard string interpolation" in { + val where = "where a = ? and b = ?" + sql"select * from foo $where" shouldBe "select * from foo where a = @p1 and b = @p2" + } + + "trim line breaks" in { + val table = "foo" + sql""" + select * from $table where + a = ? and + b = ? + """ shouldBe "select * from foo where a = @p1 and b = @p2" + } + + } + }