Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
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)
override def openWebsocket[T, WS_RESULT](
request: Request[T, S],
handler: WS_HANDLER[WS_RESULT]): F[WebSocketResponse[WS_RESULT]] = {
responseMonad.map(responseMonad.handleError(delegate.openWebsocket(request, handler)) {
case e: Exception =>
logger.error(s"Exception when opening websocket: $request", e)
responseMonad.error(e)
}) { response =>
logger.debug(s"Websocket open: $request, with response headers: ${response.headers}")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
case class LetterAndCount(letter: String, count: Int)
// result: Response[GitHubResult] is what we've read from the API
val firstLetterToName = result.items.groupBy(_.name.toUpperCase.charAt(0))
val pairs = firstLetterToName.toList.map { case (letter, projects) =>
LetterAndCount(letter.toString, projects.size)
}.sortBy(_.letter)
pairs
@adamw
adamw / sttp4.scala
Last active October 15, 2019 16:25
package sttp.client.asynchttpclient.monix
import monix.eval.Task
import sttp.client._
import sttp.client.ws.{WebSocket, WebSocketResponse}
import monix.execution.Scheduler.Implicits.global
import sttp.model.ws.WebSocketFrame
import scala.concurrent.duration._