Skip to content

Commit

Permalink
Merge pull request #25 from scoquelin/jl-test-re-org
Browse files Browse the repository at this point in the history
reorganize tests by command type grouping
  • Loading branch information
72squared authored Aug 8, 2024
2 parents a724fb6 + d978222 commit 208b3eb
Show file tree
Hide file tree
Showing 9 changed files with 1,445 additions and 1,312 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.scoquelin.arugula

import io.lettuce.core.RedisFuture
import org.mockito.Mockito.{verify, when}
import org.scalatest.matchers.must.Matchers
import org.scalatest.{FutureOutcome, wordspec}


class LettuceRedisBaseAsyncCommandsSpec extends wordspec.FixtureAsyncWordSpec with Matchers {

override type FixtureParam = LettuceRedisCommandsClientFixture.TestContext

override def withFixture(test: OneArgAsyncTest): FutureOutcome =
withFixture(test.toNoArgAsyncTest(new LettuceRedisCommandsClientFixture.TestContext))

"LettuceRedisBaseAsyncCommands" should {

"delegate PING command to Lettuce and lift result into a Future" in { testContext =>
import testContext._

val expectedValue = "PONG"
val mockRedisFuture: RedisFuture[String] = mockRedisFutureToReturn(expectedValue)
when(lettuceAsyncCommands.ping()).thenReturn(mockRedisFuture)

testClass.ping.map { result =>
result mustBe expectedValue
verify(lettuceAsyncCommands).ping()
succeed
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.scoquelin.arugula

import scala.concurrent.{ExecutionContext, Future}

import com.github.scoquelin.arugula.connection.RedisConnection
import io.lettuce.core.RedisFuture
import org.mockito.Mockito.when
import org.scalatestplus.mockito.MockitoSugar

import java.util.concurrent.CompletableFuture

object LettuceRedisCommandsClientFixture {
import io.lettuce.core.api.async.{RedisAsyncCommands => JRedisAsyncCommands}


class Mocks extends MockitoSugar {
val redisConnection: RedisConnection[String, String] = mock[RedisConnection[String, String]]
val lettuceAsyncCommands: JRedisAsyncCommands[String, String] = mock[JRedisAsyncCommands[String, String]]
def redisFuture[T]: RedisFuture[T] = mock[RedisFuture[T]]
}

class TestContext(implicit executionContext: ExecutionContext) {
val mocks = new Mocks
val redisConnection: RedisConnection[String, String] = mocks.redisConnection
val lettuceAsyncCommands: JRedisAsyncCommands[String, String] = mocks.lettuceAsyncCommands

def mockRedisFutureToReturn[T](value: T): RedisFuture[T] = {
val redisFuture = mocks.redisFuture[T]
val completableFuture = new CompletableFuture[T]()
completableFuture.complete(value)
when(redisFuture.toCompletableFuture).thenReturn(completableFuture)
redisFuture
}

when(redisConnection.async).thenReturn(Future.successful(lettuceAsyncCommands))

val testClass = new LettuceRedisCommandsClient[String, String](redisConnection, cluster = false)
}

}
Loading

0 comments on commit 208b3eb

Please sign in to comment.