This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
forked from yanana/agni
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
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
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
File renamed without changes.
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
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 } | ||
} | ||
} | ||
} |