-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow altering prepared execution for Update (#2065)
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) 2013-2020 Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package doobie.util | ||
|
||
import cats.syntax.all.* | ||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import doobie.{Transactor, Update} | ||
import doobie.free.preparedstatement as IFPS | ||
|
||
class UpdateSuite extends munit.FunSuite { | ||
val xa: Transactor[IO] = Transactor.fromDriverManager[IO]( | ||
driver = "org.h2.Driver", | ||
url = "jdbc:h2:mem:;DB_CLOSE_DELAY=-1", | ||
user = "sa", | ||
password = "", | ||
logHandler = None | ||
) | ||
|
||
test("Update runAlteringExecution") { | ||
import doobie.implicits.* | ||
var didRun = false | ||
(for { | ||
_ <- sql"create temp table t1 (a int)".update.run | ||
res <- Update[Int]("insert into t1 (a) values (?)").runAlteringExecution( | ||
1, | ||
pe => pe.copy(exec = IFPS.delay { didRun = true } *> pe.exec)) | ||
} yield { | ||
assertEquals(res, 1) | ||
}) | ||
.transact(xa) | ||
.unsafeRunSync() | ||
|
||
assert(didRun) | ||
} | ||
|
||
test("Update updateManyAlteringExecution") { | ||
import doobie.implicits.* | ||
var didRun = false | ||
(for { | ||
_ <- sql"create temp table t1 (a int)".update.run | ||
res <- Update[Int]("insert into t1 (a) values (?)").updateManyAlteringExecution( | ||
List(2, 4, 6, 8), | ||
pe => pe.copy(exec = IFPS.delay { didRun = true } *> pe.exec)) | ||
} yield { | ||
assertEquals(res, 4) | ||
}) | ||
.transact(xa) | ||
.unsafeRunSync() | ||
|
||
assert(didRun) | ||
} | ||
|
||
test("Update withUniqueGeneratedKeysAlteringExecution") { | ||
import doobie.implicits.* | ||
var didRun = false | ||
(for { | ||
_ <- sql"create temp table t1 (a int, b int)".update.run | ||
res <- Update[(Int, Int)]("insert into t1 (a, b) values (?, ?)") | ||
.withUniqueGeneratedKeysAlteringExecution[(Int, Int)]("a", "b")( | ||
(5, 6), | ||
pe => pe.copy(exec = IFPS.delay { didRun = true } *> pe.exec) | ||
) | ||
} yield { | ||
assertEquals(res, (5, 6)) | ||
}) | ||
.transact(xa) | ||
.unsafeRunSync() | ||
|
||
assert(didRun) | ||
} | ||
|
||
} |