Skip to content

Commit

Permalink
unit test for sqlserver query adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-alfers committed Jan 22, 2024
1 parent ffe608d commit 754593f
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}

}

}

0 comments on commit 754593f

Please sign in to comment.