Skip to content

Commit

Permalink
Add test for limiting query numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Wills committed May 16, 2016
1 parent 1e081b0 commit a2bd904
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ doctestDecodeHtmlEntities := true
doctestWithDependencies := false
doctestTestFramework := DoctestTestFramework.ScalaTest

parallelExecution in Test := false

homepage := Some(url("https://github.com/guardian/scanamo"))
licenses := Seq("Apache V2" -> url("http://www.apache.org/licenses/LICENSE-2.0.html"))
publishMavenStyle := true
Expand Down
64 changes: 64 additions & 0 deletions src/test/scala/com/gu/scanamo/ScanamoFreeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.gu.scanamo

import java.util

import cats._
import cats.data.State
import cats.implicits._
import com.amazonaws.services.dynamodbv2.model.{AttributeValue, QueryResult, ScanResult}
import com.gu.scanamo.ops.{BatchGet, BatchWrite, Query, _}
import org.scalatest.{FunSuite, Matchers}

import scala.collection.convert.decorateAll._

class ScanamoFreeTest extends FunSuite with Matchers {
test("only make one call for a limited result") {
val limitedScan = ScanamoFree.scanWithLimit[Int]("x", 1)

val countingInterpreter = new RequestCountingInterpreter()

val numOps = limitedScan.foldMap(countingInterpreter).runEmptyS.value

assert(numOps == 1)
}

test("unlimited scan, scans exhaustively") {
val limitedScan = ScanamoFree.scan[Int]("x")

val countingInterpreter = new RequestCountingInterpreter()

val numOps = limitedScan.foldMap(countingInterpreter).runEmptyS.value

assert(numOps == 42)
}
}

class RequestCountingInterpreter extends (ScanamoOpsA ~> RequestCountingInterpreter.CountingState) {
def apply[A](op: ScanamoOpsA[A]): RequestCountingInterpreter.CountingState[A] = op match {
case Put(req) => ???
case ConditionalPut(req) => ???
case Get(req) => ???
case Delete(req) => ???
case Scan(req) => State(counter =>
if (counter < 42)
counter + 1 -> new ScanResult().withLastEvaluatedKey(Map("x" -> DynamoFormat[Int].write(1)).asJava)
.withItems(List.fill(Option(req.getLimit).map(_.toInt).getOrElse(50))(
new util.HashMap[String, AttributeValue]()): _*)
else
counter -> new ScanResult().withItems(List.empty[java.util.Map[String, AttributeValue]].asJava)
)
case Query(req) => State(counter =>
if (counter < 42)
counter + 1 -> new QueryResult().withLastEvaluatedKey(Map("x" -> DynamoFormat[Int].write(1)).asJava)
.withItems(List.fill(req.getLimit)(new util.HashMap[String, AttributeValue]()): _*)
else
counter -> new QueryResult().withItems(List.empty[java.util.Map[String, AttributeValue]].asJava)
)
case BatchWrite(req) => ???
case BatchGet(req) => ???
}
}

object RequestCountingInterpreter {
type CountingState[V] = State[Int, V]
}

0 comments on commit a2bd904

Please sign in to comment.