Permalink
Browse files

Tests for all exercises. Cache related failing due to bug in Fetch

  • Loading branch information...
raulraja committed Jul 6, 2016
1 parent 307849a commit 48ecd953e279da87a8310efb8979d7238b9c9f30
@@ -51,7 +51,7 @@ object CachingSection extends FlatSpec with Matchers with Section {
*/
def cachePartialHits(res0: Int) = {
val env = List(1, 2, 3).traverse(getUser).runE[Id](cache)
env.rounds.size should be(res0)
env.rounds.count(_.cached) should be(res0)
}
/**
@@ -70,11 +70,11 @@ object CachingSection extends FlatSpec with Matchers with Section {
def fetchUsers = List(1, 2, 3).traverse(getUser)
val firstEnv = fetchUsers.runE[Id]
firstEnv.rounds.size should be(res0)
firstEnv.rounds.count(_.cached) should be(res0)
val secondEnv = fetchUsers.runA[Id](firstEnv.cache)
firstEnv.rounds.size should be(res1)
firstEnv.rounds.count(_.cached) should be(res1)
}
/**
@@ -118,7 +118,7 @@ object CachingSection extends FlatSpec with Matchers with Section {
val env = fetchSameTwice.runE[Id](ForgetfulCache())
env.rounds.size should be(res0)
env.rounds.count(_.cached) should be(res0)
}
}
@@ -76,8 +76,8 @@ object ConcurrencyMonadsSection extends FlatSpec with Matchers with Section {
}
val result = Await.result(op, 5 seconds)
result should be
result should be(res0)
}
/**
@@ -46,24 +46,24 @@ object ErrorHandlingSection extends FlatSpec with Matchers with Section {
* Now we can convert `Eval[User]` into `Eval[Throwable Xor User]` and capture exceptions as values
* in the left of the disjunction.
*/
def attemptFailedFetch(res0: Exception Xor User) = {
def attemptFailedFetch(res0: Boolean) = {
import fetch.unsafe.implicits._
import cats.Eval
import cats.data.Xor
val safeResult: Eval[Throwable Xor User] =
FetchMonadError[Eval].attempt(fetchError.runA[Eval])
safeResult.value should be(res0)
safeResult.value.isLeft should be(res0)
}
/**
* And more succintly with Cats' applicative error syntax.
*/
def attemptFailedFetchSyntax(res0: Exception Xor User) = {
def attemptFailedFetchSyntax(res0: Boolean) = {
import cats.syntax.applicativeError._
fetchError.runA[Eval].attempt.value should be(res0)
fetchError.runA[Eval].attempt.value.isLeft should be(res0)
}
}
@@ -355,13 +355,13 @@ object UsageSection extends FlatSpec with Matchers with Section {
* override def fetchOne(id: Post): Query[Option[PostTopic]] = {
* Query.sync({
* val topic = if (id.id % 2 == 0) "monad" else "applicative"
* latency(Option(topic), s"One Post Topic $id")
* Option(topic)
* })
* }
* override def fetchMany(ids: NonEmptyList[Post]): Query[Map[Post, PostTopic]] = {
* Query.sync({
* val result = ids.unwrap.map(id => (id, if (id.id % 2 == 0) "monad" else "applicative")).toMap
* latency(result, s"Many Post Topics $ids")
* result
* })
* }
* }
@@ -0,0 +1,28 @@
package exercises
import fetchlib._
import shapeless.HNil
import org.scalatest.Spec
import org.scalatest.prop.Checkers
import org.scalacheck.Shapeless._
class CachingSpec extends Spec with Checkers {
import FetchTutorialHelper._
import CachingSection._
import Test._
def `Cache Prepopulating` =
check(testSuccess(prepopulating _, 1 :: HNil))
def `Cache Partial Hits` =
check(testSuccess(cachePartialHits _, 1 :: HNil))
def `Cache Replay` =
check(testSuccess(replaying _, 0 :: 3 :: HNil))
def `Cache Custom` =
check(testSuccess(customCache _, 2 :: HNil))
}
@@ -0,0 +1,23 @@
package exercises
import fetchlib._
import shapeless.HNil
import org.scalatest.Spec
import org.scalatest.prop.Checkers
import org.scalacheck.Shapeless._
class CatsSpec extends Spec with Checkers {
import FetchTutorialHelper._
import CatsSection._
import Test._
def `Cartesian Builder` =
check(testSuccess(applicative _, 3 :: HNil))
def `Join ops` =
check(testSuccess(similarToJoin _, 3 :: HNil))
}
@@ -0,0 +1,26 @@
package exercises
import fetchlib._
import shapeless.HNil
import org.scalatest.Spec
import org.scalatest.prop.Checkers
import org.scalacheck.Shapeless._
class ConcurrencyMonadsSpec extends Spec with Checkers {
import FetchTutorialHelper._
import ConcurrencyMonadsSection._
import Test._
def `Std Future` =
check(testSuccess(stdFutures _, (2,2) :: HNil))
def `Monix Task` =
check(testSuccess(monixTask _, (2,2) :: HNil))
def `Custom Types via Monad Error` =
check(testSuccess(customTypes _, (2,2) :: HNil))
}
@@ -0,0 +1,26 @@
package exercises
import fetchlib._
import shapeless.HNil
import org.scalatest.Spec
import org.scalatest.prop.Checkers
import org.scalacheck.Shapeless._
class ErrorHandlingSpec extends Spec with Checkers {
import FetchTutorialHelper._
import ErrorHandlingSection._
import Test._
def `Failed Fetch` =
check(testSuccess(failedFetch _, true :: HNil))
def `Attempt Failed Fetch` =
check(testSuccess(attemptFailedFetch _, true :: HNil))
def `Attempt Failed Fetch Syntax` =
check(testSuccess(attemptFailedFetchSyntax _, true :: HNil))
}
@@ -0,0 +1,32 @@
package exercises
import fetchlib._
import shapeless.HNil
import org.scalatest.Spec
import org.scalatest.prop.Checkers
import org.scalacheck.Shapeless._
class SyntaxSpec extends Spec with Checkers {
import FetchTutorialHelper._
import SyntaxSection._
import Test._
def `Implicit Syntax` =
check(testSuccess(implicitSyntax _, 42 :: HNil))
def `Error Syntax` =
check(testSuccess(errorSyntax _, true :: HNil))
def `runA Syntax` =
check(testSuccess(runA _, 1 :: HNil))
def `runE Syntax` =
check(testSuccess(runE _, true :: HNil))
def `runF Syntax` =
check(testSuccess(runF _, 1 :: true :: HNil))
}
@@ -8,7 +8,7 @@ import org.scalatest.prop.Checkers
import org.scalacheck.Shapeless._
class QuickStartSpec extends Spec with Checkers {
class UsageSpec extends Spec with Checkers {
import FetchTutorialHelper._
import UsageSection._
@@ -29,5 +29,22 @@ class QuickStartSpec extends Spec with Checkers {
def `Caching Strategy` =
check(testSuccess(caching _, (userDatabase(1), userDatabase(1)) :: 1 :: HNil))
def `Sync Queries` =
check(testSuccess(synchronous _, true :: HNil))
def `Async Queries` =
check(testSuccess(asynchronous _, false :: HNil))
def `Combining Data` =
check(testSuccess(combiningData _, (postDatabase(1), "applicative") :: HNil))
def `Combining Concurrency` =
check(testSuccess(concurrency _, (postDatabase(1), userDatabase(2)) :: 1 :: HNil))
def `Combinators sequence` =
check(testSuccess(sequence _, List(userDatabase(1), userDatabase(2), userDatabase(3)) :: HNil))
def `Combinators traverse` =
check(testSuccess(traverse _, List(userDatabase(1), userDatabase(2), userDatabase(3)) :: HNil))
}

0 comments on commit 48ecd95

Please sign in to comment.