Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
package sttp.tapir.example
import io.circe.generic.JsonCodec
import sttp.tapir._
import sttp.model.StatusCode
import sttp.tapir.docs.openapi._
import sttp.tapir.openapi.circe.yaml._
import sttp.tapir.json.circe._
object Example extends App {
object CirceAnnotationCodecs {
import io.circe.{Decoder, Encoder}
import io.circe.generic.JsonCodec
import sttp.tapir._
import sttp.tapir.json.circe._
@JsonCodec
case class Foo(x: String)
it should "add 1 point" in {
// given
val updatedPoints = new AtomicInteger(0)
val userId = UUID.randomUUID()
val stubDao = newStubDaoConstantPoints(15, updatedPoints)
// when
new Points(stubDao).increase(userId).transact(transactor).unsafeRunSync()
// then
override protected def beforeAll(): Unit = {
super.beforeAll()
transactor = Transactor(
(),
(_: Unit) => Resource.pure(null),
KleisliInterpreter[IO](
Blocker.liftExecutionContext(ExecutionContext.global))
.ConnectionInterpreter,
Strategy.void
)
class Example3Test extends AnyFlatSpec with Matchers {
import Example3._
it should "add 1 point" in {
// given
val updatedPoints = new AtomicInteger(0)
val userId = UUID.randomUUID()
val stubDao = newStubDaoConstantPoints(15, updatedPoints)
// when
import java.util.UUID
import cats._
import cats.implicits._
class Points[F[_]: Monad](dao: Dao[F]) {
def increase(userId: UUID): F[Unit] =
for {
current <- dao.currentPoints(userId)
updated = calculatePointsIncrease(current)
trait Dao[F[_]] {
def currentPoints(userId: UUID): F[Int]
def updatePoints(userId: UUID, value: Int): F[Unit]
}
import doobie._
import doobie.implicits._
import doobie.postgres.implicits._
object DefaultDao extends Dao[ConnectionIO] {
it should "add 1 point" in {
import Example2._
PointsLogic.calculatePointsIncrease(15) shouldBe 16
}
it should "add 3 points" in {
import Example2._
PointsLogic.calculatePointsIncrease(16) shouldBe 19
}
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](
import java.util.UUID
import doobie._
import doobie.implicits._
import doobie.postgres.implicits._
class Points(dao: Dao) {
def increase(userId: UUID): ConnectionIO[Unit] =
for {
current <- dao.currentPoints(userId)