Last active
January 8, 2020 14:38
-
-
Save adamw/dcb20d8cfbd77df28b40ad97d73ae3ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Example1Test extends AnyFlatSpec with Matchers with BeforeAndAfterAll { | |
private var postgres: EmbeddedPostgres = _ | |
private var transactor: Transactor[IO] = _ | |
implicit private val ioContextShift: ContextShift[IO] = | |
IO.contextShift(ExecutionContext.global) | |
override protected def beforeAll(): Unit = { | |
super.beforeAll() | |
postgres = EmbeddedPostgres.builder().start() | |
transactor = Transactor.fromDriverManager[IO]( | |
"org.postgresql.Driver", | |
postgres.getJdbcUrl("postgres", "postgres"), | |
"postgres", | |
"postgres", | |
Blocker.liftExecutionContext(ExecutionContext.global) | |
) | |
sql"CREATE TABLE user_points (user_id uuid PRIMARY KEY, points int NOT NULL)" | |
.update.run | |
.transact(transactor) | |
.unsafeRunSync() | |
} | |
import Example1._ | |
it should "add 1 point" in { | |
// given | |
val userId = UUID.randomUUID() | |
addUserWithPoints(userId, 15) | |
// when | |
new Points(DefaultDao).increase(userId).transact(transactor).unsafeRunSync() | |
// then | |
readUserPoints(userId) shouldBe 16 | |
} | |
private def addUserWithPoints(userId: UUID, points: Int): Unit = { | |
sql"INSERT INTO user_points(user_id, points) VALUES ($userId, $points)" | |
.update.run.transact(transactor).unsafeRunSync() | |
} | |
private def readUserPoints(userId: UUID): Int = { | |
sql"SELECT points FROM user_points WHERE user_id = $userId" | |
.query[Int].unique.transact(transactor).unsafeRunSync() | |
} | |
override protected def afterAll(): Unit = { | |
postgres.close() | |
super.afterAll() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment