-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1567 from oyvindberg/fix-effectful-copy-in
Fix effectful `copyIn` (Fixes #1512)
- Loading branch information
Showing
2 changed files
with
106 additions
and
13 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
89 changes: 89 additions & 0 deletions
89
modules/postgres/src/test/scala/doobie/postgres/Issue1512.scala
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,89 @@ | ||
// 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.postgres | ||
|
||
import cats.effect.IO | ||
import cats.implicits.catsSyntaxApplicativeId | ||
import doobie.ConnectionIO | ||
import doobie.implicits._ | ||
import doobie.postgres.implicits._ | ||
import doobie.util.transactor.Transactor | ||
import munit.CatsEffectSuite | ||
import org.postgresql.ds.PGSimpleDataSource | ||
|
||
import javax.sql.DataSource | ||
|
||
class Issue1512 extends CatsEffectSuite { | ||
|
||
val minChunkSize = 200 | ||
|
||
val datasource: DataSource = { | ||
val ds = new PGSimpleDataSource | ||
ds.setUser("postgres") | ||
ds.setPassword("") | ||
ds | ||
} | ||
val xa: Transactor[IO] = | ||
Transactor.fromDataSource[IO](datasource, scala.concurrent.ExecutionContext.global) | ||
|
||
val setup: IO[Int] = | ||
sql""" | ||
DROP TABLE IF EXISTS demo; | ||
CREATE TABLE demo(id BIGSERIAL PRIMARY KEY NOT NULL, data BIGINT NOT NULL); | ||
""".update.run | ||
.transact(xa) | ||
|
||
test("A stream with a Pure effect inserts items properly") { | ||
|
||
setup.unsafeRunSync() | ||
|
||
// A pure stream is fine - can copy many items | ||
val count = 10000 | ||
val stream = fs2.Stream.emits(1 to count) | ||
|
||
sql"COPY demo(data) FROM STDIN".copyIn(stream, minChunkSize).transact(xa).unsafeRunSync() | ||
|
||
val queryCount = | ||
sql"SELECT count(*) from demo".query[Int].unique.transact(xa).unsafeRunSync() | ||
|
||
assertEquals(queryCount, count) | ||
} | ||
|
||
test("A stream with a ConnectionIO effect copies <= than minChunkSize items") { | ||
|
||
setup.unsafeRunSync() | ||
|
||
// Can copy up to minChunkSize just fine with ConnectionIO | ||
val inputs = 1 to minChunkSize | ||
val stream = fs2.Stream.emits[ConnectionIO, Int](inputs) | ||
.evalMap(i => (i + 2).pure[ConnectionIO]) | ||
|
||
val copiedRows = sql"COPY demo(data) FROM STDIN".copyIn(stream, minChunkSize).transact(xa).unsafeRunSync() | ||
|
||
assertEquals(copiedRows, inputs.size.toLong) | ||
|
||
val queryCount = | ||
sql"SELECT count(*) from demo".query[Int].unique.transact(xa).unsafeRunSync() | ||
|
||
assertEquals(queryCount, minChunkSize) | ||
} | ||
|
||
test("A stream with a ConnectionIO effect copies items with count > minChunkSize") { | ||
|
||
setup.unsafeRunSync() | ||
|
||
val inputs = 1 to minChunkSize + 1 | ||
val stream = fs2.Stream.emits[ConnectionIO, Int](inputs) | ||
.evalMap(i => (i + 2).pure[ConnectionIO]) | ||
|
||
val copiedRows = sql"COPY demo(data) FROM STDIN".copyIn(stream, minChunkSize).transact(xa).unsafeRunSync() | ||
assertEquals(copiedRows, inputs.size.toLong) | ||
|
||
val queryCount = | ||
sql"SELECT count(*) from demo".query[Int].unique.transact(xa).unsafeRunSync() | ||
|
||
assertEquals(queryCount, minChunkSize + 1) | ||
} | ||
} |