Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from tkrs/omit-compiler-warnings
Browse files Browse the repository at this point in the history
Omit the compiler warnings of test
  • Loading branch information
tkrs committed Dec 21, 2019
2 parents 1cbeb3c + d55641c commit 7822da7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
4 changes: 2 additions & 2 deletions core/src/test/scala/agni/BinderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class BinderSpec extends TypedSuite with MockitoSugar {
when(c.setBytesUnsafe(eqTo(2), eqTo(TypeCodecs.BIGINT.encode(2L, v)))).thenReturn(d)
when(d.setBytesUnsafe(eqTo(3), eqTo(TypeCodecs.ASCII.encode("z", v)))).thenReturn(e)

val Right(s) = Binder[(String, Int, Long, String)].apply(a, v, ("a", 1, 2L, "z"))
val s = Binder[(String, Int, Long, String)].apply(a, v, ("a", 1, 2L, "z"))

assert(e === s)
assert(s === Right(e))
}
}
10 changes: 8 additions & 2 deletions core/src/test/scala/agni/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ class CodecSpec extends FunSuite with Checkers with Matchers {

def roundTrip[A: Deserializer: Serializer: Arbitrary: Shrink]: Assertion =
check(Prop.forAll({ a: A =>
val Right(se) = Serializer[A].apply(a, ProtocolVersion.DEFAULT)
val Right(de) = Deserializer[A].apply(se, ProtocolVersion.DEFAULT)
val se = Serializer[A].apply(a, ProtocolVersion.DEFAULT) match {
case Right(v) => v
case Left(e) => fail(e)
}
val de = Deserializer[A].apply(se, ProtocolVersion.DEFAULT) match {
case Right(v) => v
case Left(e) => fail(e)
}
de === a
}))

Expand Down
88 changes: 88 additions & 0 deletions core/src/test/scala_2.13+/agni/CqlSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package agni

import java.util.concurrent.CompletableFuture

import com.datastax.oss.driver.api.core.CqlSession
import com.datastax.oss.driver.api.core.cql.{AsyncResultSet, BoundStatement, Row}
import org.scalatest._
import org.mockito.Mockito._
import org.scalatestplus.mockito.MockitoSugar

import scala.annotation.tailrec
import scala.concurrent.Future
import scala.jdk.CollectionConverters._

class CqlSpec extends AsyncFunSpec with MockitoSugar with Matchers {

import agni.std.async._
import cats.instances.future._

describe("getRows") {
it("should convert to LazyList[Row] the value got from AsyncResultSet#currentPage()") {
val session = mock[CqlSession]
val stmt = mock[BoundStatement]
val asyncResultSet0 = mock[AsyncResultSet]

when(session.executeAsync(stmt))
.thenReturn(CompletableFuture.completedFuture(asyncResultSet0))

val asyncResultSet1 = mock[AsyncResultSet]
val asyncResultSet2 = mock[AsyncResultSet]
val asyncResultSet3 = mock[AsyncResultSet]

when(asyncResultSet0.fetchNextPage()).thenReturn(CompletableFuture.completedFuture(asyncResultSet1))
when(asyncResultSet0.hasMorePages).thenReturn(true)

when(asyncResultSet1.fetchNextPage()).thenReturn(CompletableFuture.completedFuture(asyncResultSet2))
when(asyncResultSet1.hasMorePages).thenReturn(true)

when(asyncResultSet2.fetchNextPage()).thenReturn(CompletableFuture.completedFuture(asyncResultSet3))
when(asyncResultSet2.hasMorePages).thenReturn(true)

when(asyncResultSet3.hasMorePages).thenReturn(false)

val rows0 = Iterator.continually(mock[Row]).take(2).to(Iterable)
val rows1 = Iterator.continually(mock[Row]).take(2).to(Iterable)
val rows2 = Iterator.continually(mock[Row]).take(2).to(Iterable)
val rows3 = Iterator.continually(mock[Row]).take(1).to(Iterable)

when(asyncResultSet0.currentPage()).thenReturn(rows0.asJava)
when(asyncResultSet1.currentPage()).thenReturn(rows1.asJava)
when(asyncResultSet2.currentPage()).thenReturn(rows2.asJava)
when(asyncResultSet3.currentPage()).thenReturn(rows3.asJava)

val got = Cql.getRows[Future](session, stmt)

got.map { xs =>
xs shouldBe LazyList.concat(rows0 ++ rows1 ++ rows2 ++ rows3)
}
}

it("should be stack-safe") {
val session = mock[CqlSession]
val stmt = mock[BoundStatement]
val r = mock[AsyncResultSet]

when(session.executeAsync(stmt)).thenReturn(CompletableFuture.completedFuture(r))

@tailrec def createResultSet(current: AsyncResultSet, size: Int): Unit = {
val page = Iterator.continually(mock[Row]).take(1).to(Iterable)
when(current.currentPage()).thenReturn(page.asJava)
val next = mock[AsyncResultSet]
if (size > 0) {
when(current.hasMorePages).thenReturn(true)
when(current.fetchNextPage()).thenReturn(CompletableFuture.completedFuture(next))
createResultSet(next, size - 1)
} else {
when(current.hasMorePages).thenReturn(false)
}
}

createResultSet(r, 9999)

val got = Cql.getRows[Future](session, stmt)

got.map { _.toList.size shouldBe 10000 }
}
}
}

0 comments on commit 7822da7

Please sign in to comment.