Skip to content

Instantly share code, notes, and snippets.

@fanf
Created June 18, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanf/464cc2ae21c2bd1176ef2db3c77ae36d to your computer and use it in GitHub Desktop.
Save fanf/464cc2ae21c2bd1176ef2db3c77ae36d to your computer and use it in GitHub Desktop.
import scalaz.zio._
import scalaz.zio.syntax._
import utest._
class TestService(data: Ref[Map[Int, String]]) extends Database.Service {
def lookup(id: Int) = for {
map <- data.get
// I assume the semantic is to fail if key is not defined
v <- map.get(id) match {
case Some(n) => n.succeed
case None => new IllegalArgumentException(s"key $id not found").fail
}
} yield {
v
}
def update(id: Int, name: String) = for {
map <- data.update(_ + (id -> name))
} yield ()
}
object AddNamesSpec extends TestSuite {
val tests = Tests {
"it adds names and gets the results" - {
val runnable =
for {
data <- Ref.make(Map(1 -> "1", 2 -> "2"))
testdb = new Database { def database = new TestService(data) }
result <- Repository.addAndGetNames(("foo", "bar")).provide(testdb)
} yield {
result
}
val actual = new DefaultRuntime {}.unsafeRun(runnable)
val expected = List("foo", "bar")
actual ==> expected
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment