Skip to content

Instantly share code, notes, and snippets.

@jrmsamson
Created February 25, 2022 14:57
Show Gist options
  • Save jrmsamson/60f9f9d7ec360aab4a5b70ef4e7c83b0 to your computer and use it in GitHub Desktop.
Save jrmsamson/60f9f9d7ec360aab4a5b70ef4e7c83b0 to your computer and use it in GitHub Desktop.
How to test zio-lambda
package zio.lambda.example
import zio.Console._
import zio.Random
import zio._
import zio.json._
import zio.lambda._
import zio.test._
final case class CustomEvent(message: String)
object CustomEvent {
implicit val decoder: JsonDecoder[CustomEvent] = DeriveJsonDecoder.gen[CustomEvent]
}
object SimpleHandler extends ZLambda[CustomEvent, String] {
override def apply(event: CustomEvent, context: Context): RIO[ZEnv, String] =
for {
_ <- printLine(event.message)
} yield "Handler ran successfully"
}
object ContextGen {
val gen: Gen[Random with Sized, Context] =
for {
awsRequestId <- Gen.string
logGroupName <- Gen.string
logStreamName <- Gen.string
functionName <- Gen.string
functionVersion <- Gen.string
invokedFunctionArn <- Gen.string
remainingTimeInMillis <- Gen.long
memoryLimitInMB <- Gen.int
} yield Context(
awsRequestId,
logGroupName,
logStreamName,
functionName,
functionVersion,
invokedFunctionArn,
remainingTimeInMillis,
memoryLimitInMB,
None,
None
)
}
object SimpleHandlerSpec extends DefaultRunnableSpec {
override def spec: ZSpec[Environment, Failure] =
suite("Simple Handler spec") {
test("should run successfully") {
check(Gen.string, ContextGen.gen) { (message, context) =>
SimpleHandler
.apply(CustomEvent(message), context)
.map(_ => assertCompletes)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment