Created
June 18, 2019 16:00
-
-
Save fanf/464cc2ae21c2bd1176ef2db3c77ae36d to your computer and use it in GitHub Desktop.
This file contains 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
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