From 71aab624f0ff923d72bbe9ba132fafdf02170f39 Mon Sep 17 00:00:00 2001 From: Karel Cemus Date: Tue, 5 Mar 2024 00:20:16 +0100 Subject: [PATCH] Update to scalamock 6.0.0-M2 --- build.sbt | 2 +- .../redis/connector/RedisCommandsMock.scala | 118 ------------- .../api/cache/redis/impl/AsyncRedisMock.scala | 156 ------------------ .../redis/connector/RedisCommandsMock.scala | 0 .../api/cache/redis/impl/AsyncRedisMock.scala | 0 5 files changed, 1 insertion(+), 275 deletions(-) delete mode 100644 src/test/scala-3/play/api/cache/redis/connector/RedisCommandsMock.scala delete mode 100644 src/test/scala-3/play/api/cache/redis/impl/AsyncRedisMock.scala rename src/test/{scala-2.13 => scala}/play/api/cache/redis/connector/RedisCommandsMock.scala (100%) rename src/test/{scala-2.13 => scala}/play/api/cache/redis/impl/AsyncRedisMock.scala (100%) diff --git a/build.sbt b/build.sbt index d8e4e14..d18e087 100644 --- a/build.sbt +++ b/build.sbt @@ -24,7 +24,7 @@ libraryDependencies ++= Seq( "io.github.rediscala" %% "rediscala" % "1.14.0-pekko", // test framework with mockito extension "org.scalatest" %% "scalatest" % "3.2.18" % Test, - "org.scalamock" %% "scalamock" % "6.0.0-M1" % Test, + "org.scalamock" %% "scalamock" % "6.0.0-M2" % Test, // test module for play framework "org.playframework" %% "play-test" % playVersion.value % Test, // to run integration tests diff --git a/src/test/scala-3/play/api/cache/redis/connector/RedisCommandsMock.scala b/src/test/scala-3/play/api/cache/redis/connector/RedisCommandsMock.scala deleted file mode 100644 index acd8439..0000000 --- a/src/test/scala-3/play/api/cache/redis/connector/RedisCommandsMock.scala +++ /dev/null @@ -1,118 +0,0 @@ -package play.api.cache.redis.connector - -import org.scalamock.scalatest.AsyncMockFactory -import redis.* -import redis.api.ListPivot -import redis.protocol.RedisReply - -import scala.concurrent.{ExecutionContext, Future} - -// this is implemented due to a bug in ScalaMock 6.0.0-M1 and reported as https://github.com/paulbutcher/ScalaMock/issues/503 -private trait RedisCommandsMock { - - def get[R: ByteStringDeserializer](key: String): Future[Option[R]] - - def set[V: ByteStringSerializer](key: String, value: V, exSeconds: Option[Long], pxMilliseconds: Option[Long], NX: Boolean, XX: Boolean): Future[Boolean] - - def expire(key: String, seconds: Long): Future[Boolean] - - def mset[V: ByteStringSerializer](keysValues: Map[String, V]): Future[Boolean] - - def msetnx[V: ByteStringSerializer](keysValues: Map[String, V]): Future[Boolean] - - def incrby(key: String, increment: Long): Future[Long] - - def lrange[R: ByteStringDeserializer](key: String, start: Long, stop: Long): Future[Seq[R]] - - def lrem[V: ByteStringSerializer](key: String, count: Long, value: V): Future[Long] - - def ltrim(key: String, start: Long, stop: Long): Future[Boolean] - - def linsert[V: ByteStringSerializer](key: String, beforeAfter: ListPivot, pivot: String, value: V): Future[Long] - - def hincrby(key: String, field: String, increment: Long): Future[Long] - - def hset[V: ByteStringSerializer](key: String, field: String, value: V): Future[Boolean] - - def zcard(key: String): Future[Long] - - def zscore[V: ByteStringSerializer](key: String, member: V): Future[Option[Double]] - - def zrange[V: ByteStringDeserializer](key: String, start: Long, stop: Long): Future[Seq[V]] - - def zrevrange[V: ByteStringDeserializer](key: String, start: Long, stop: Long): Future[Seq[V]] - - def zaddMock[V: ByteStringSerializer](key: String, scoreMembers: Seq[(Double, V)]): Future[Long] - - def zremMock[V: ByteStringSerializer](key: String, members: Seq[V]): Future[Long] -} - -private object RedisCommandsMock { - - def mock(factory: AsyncMockFactory)(implicit ec: ExecutionContext): (RedisCommands, RedisCommandsMock) = { - val mock = factory.mock[RedisCommandsMock](factory) - (new RedisCommandsAdapter(mock), mock) - } - -} - -private class RedisCommandsAdapter(inner: RedisCommandsMock)(implicit override val executionContext: ExecutionContext) extends RedisCommands { - - override def send[T](redisCommand: RedisCommand[? <: RedisReply, T]): Future[T] = - throw new IllegalStateException(s"Uncaught call to mock: $redisCommand") - - final override def get[R: ByteStringDeserializer](key: String): Future[Option[R]] = - inner.get(key) - - final override def set[V: ByteStringSerializer](key: String, value: V, exSeconds: Option[Long], pxMilliseconds: Option[Long], NX: Boolean, XX: Boolean): Future[Boolean] = - inner.set(key, value, exSeconds, pxMilliseconds, NX, XX) - - final override def expire(key: String, seconds: Long): Future[Boolean] = - inner.expire(key, seconds) - - final override def mset[V: ByteStringSerializer](keysValues: Map[String, V]): Future[Boolean] = - inner.mset(keysValues) - - final override def msetnx[V: ByteStringSerializer](keysValues: Map[String, V]): Future[Boolean] = - inner.msetnx(keysValues) - - final override def incrby(key: String, increment: Long): Future[Long] = - inner.incrby(key, increment) - - final override def lrange[R: ByteStringDeserializer](key: String, start: Long, stop: Long): Future[Seq[R]] = - inner.lrange(key, start, stop) - - final override def lrem[V: ByteStringSerializer](key: String, count: Long, value: V): Future[Long] = - inner.lrem(key, count, value) - - final override def ltrim(key: String, start: Long, stop: Long): Future[Boolean] = - inner.ltrim(key, start, stop) - - final override def linsert[V: ByteStringSerializer](key: String, beforeAfter: ListPivot, pivot: String, value: V): Future[Long] = - inner.linsert(key, beforeAfter, pivot, value) - - final override def hincrby(key: String, field: String, increment: Long): Future[Long] = - inner.hincrby(key, field, increment) - - final override def hset[V: ByteStringSerializer](key: String, field: String, value: V): Future[Boolean] = - inner.hset(key, field, value) - - final override def zcard(key: String): Future[Long] = - inner.zcard(key) - - final override def zscore[V: ByteStringSerializer](key: String, member: V): Future[Option[Double]] = - inner.zscore(key, member) - - final override def zrange[V: ByteStringDeserializer](key: String, start: Long, stop: Long): Future[Seq[V]] = - inner.zrange(key, start, stop) - - final override def zrevrange[V: ByteStringDeserializer](key: String, start: Long, stop: Long): Future[Seq[V]] = - inner.zrevrange(key, start, stop) - - final override def zadd[V: ByteStringSerializer](key: String, scoreMembers: (Double, V)*): Future[Long] = - inner.zaddMock(key, scoreMembers) - - final override def zrem[V: ByteStringSerializer](key: String, members: V*): Future[Long] = - inner.zremMock(key, members) - -} diff --git a/src/test/scala-3/play/api/cache/redis/impl/AsyncRedisMock.scala b/src/test/scala-3/play/api/cache/redis/impl/AsyncRedisMock.scala deleted file mode 100644 index 0f4233b..0000000 --- a/src/test/scala-3/play/api/cache/redis/impl/AsyncRedisMock.scala +++ /dev/null @@ -1,156 +0,0 @@ -package play.api.cache.redis.impl - -import org.apache.pekko.Done -import org.scalamock.scalatest.AsyncMockFactory -import play.api.cache.redis.* - -import scala.concurrent.duration.Duration -import scala.concurrent.{ExecutionContext, Future} -import scala.reflect.ClassTag - -// this is implemented due to a bug in ScalaMock 6.0.0-M1 and reported as https://github.com/paulbutcher/ScalaMock/issues/503 -private trait AsyncRedisMock { - - def get[A: ClassTag](key: String): Future[Option[A]] - - def getOrElseUpdate[A: ClassTag](key: String, expiration: Duration)(orElse: => Future[A]): Future[A] - - def removeAll(): Future[Done] - - def getAllKeys[T](keys: Iterable[String]): Future[Seq[Option[T]]] - - def getOrElse[T: ClassTag](key: String, expiration: Duration)(orElse: => T): Future[T] - - def getOrFuture[T: ClassTag](key: String, expiration: Duration)(orElse: => Future[T]): Future[T] - - def exists(key: String): Future[Boolean] - - def matching(pattern: String): Future[Seq[String]] - - def set(key: String, value: Any, expiration: Duration): Future[Done] - - def setIfNotExists(key: String, value: Any, expiration: Duration): Future[Boolean] - - def setAll(keyValues: (String, Any)*): Future[Done] - - def setAllIfNotExist(keyValues: (String, Any)*): Future[Boolean] - - def append(key: String, value: String, expiration: Duration): Future[Done] - - def expire(key: String, expiration: Duration): Future[Done] - - def expiresIn(key: String): Future[Option[Duration]] - - def remove(key: String): Future[Done] - - def remove(key1: String, key2: String, keys: String*): Future[Done] - - def removeAllKeys(keys: Seq[String]): Future[Done] - - def removeMatching(pattern: String): Future[Done] - - def invalidate(): Future[Done] - - def increment(key: String, by: Long = 1): Future[Long] - - def decrement(key: String, by: Long = 1): Future[Long] - - def list[T: ClassTag](key: String): RedisList[T, Future] - - def set[T: ClassTag](key: String): RedisSet[T, Future] - - def map[T: ClassTag](key: String): RedisMap[T, Future] - - def zset[T: ClassTag](key: String): RedisSortedSet[T, Future] -} - -private object AsyncRedisMock { - - def mock(factory: AsyncMockFactory)(implicit ec: ExecutionContext): (AsyncRedis, AsyncRedisMock) = { - val mock = factory.mock[AsyncRedisMock](factory) - (new AsyncRedisAdapter(mock), mock) - } - -} - -private class AsyncRedisAdapter(inner: AsyncRedisMock) extends AsyncRedis { - - override def get[A: ClassTag](key: String): Future[Option[A]] = - inner.get(key) - - override def getOrElseUpdate[A: ClassTag](key: String, expiration: Duration)(orElse: => Future[A]): Future[A] = - inner.getOrElseUpdate(key, expiration)(orElse) - - override def removeAll(): Future[Done] = - inner.removeAll() - - override def getAll[T: ClassTag](keys: Iterable[String]): Future[Seq[Option[T]]] = - inner.getAllKeys(keys) - - override def getOrElse[T: ClassTag](key: String, expiration: Duration)(orElse: => T): Future[T] = - inner.getOrElse(key, expiration)(orElse) - - override def getOrFuture[T: ClassTag](key: String, expiration: Duration)(orElse: => Future[T]): Future[T] = - inner.getOrFuture(key, expiration)(orElse) - - override def exists(key: String): Future[Boolean] = - inner.exists(key) - - override def matching(pattern: String): Future[Seq[String]] = - inner.matching(pattern) - - override def set(key: String, value: Any, expiration: Duration): Future[Done] = - inner.set(key, value, expiration) - - override def setIfNotExists(key: String, value: Any, expiration: Duration): Future[Boolean] = - inner.setIfNotExists(key, value, expiration) - - override def setAll(keyValues: (String, Any)*): Future[Done] = - inner.setAll(keyValues: _*) - - override def setAllIfNotExist(keyValues: (String, Any)*): Future[Boolean] = - inner.setAllIfNotExist(keyValues: _*) - - override def append(key: String, value: String, expiration: Duration): Future[Done] = - inner.append(key, value, expiration) - - override def expire(key: String, expiration: Duration): Future[Done] = - inner.expire(key, expiration) - - override def expiresIn(key: String): Future[Option[Duration]] = - inner.expiresIn(key) - - override def remove(key: String): Future[Done] = - inner.remove(key) - - override def remove(key1: String, key2: String, keys: String*): Future[Done] = - inner.remove(key1, key2, keys: _*) - - override def removeAll(keys: String*): Future[Done] = - inner.removeAllKeys(keys) - - override def removeMatching(pattern: String): Future[Done] = - inner.removeMatching(pattern) - - override def invalidate(): Future[Done] = - inner.invalidate() - - override def increment(key: String, by: Long = 1): Future[Long] = - inner.increment(key, by) - - override def decrement(key: String, by: Long = 1): Future[Long] = - inner.decrement(key, by) - - override def list[T: ClassTag](key: String): RedisList[T, Future] = - inner.list(key) - - override def set[T: ClassTag](key: String): RedisSet[T, Future] = - inner.set(key) - - override def map[T: ClassTag](key: String): RedisMap[T, Future] = - inner.map(key) - - override def zset[T: ClassTag](key: String): RedisSortedSet[T, Future] = - inner.zset(key) - -} diff --git a/src/test/scala-2.13/play/api/cache/redis/connector/RedisCommandsMock.scala b/src/test/scala/play/api/cache/redis/connector/RedisCommandsMock.scala similarity index 100% rename from src/test/scala-2.13/play/api/cache/redis/connector/RedisCommandsMock.scala rename to src/test/scala/play/api/cache/redis/connector/RedisCommandsMock.scala diff --git a/src/test/scala-2.13/play/api/cache/redis/impl/AsyncRedisMock.scala b/src/test/scala/play/api/cache/redis/impl/AsyncRedisMock.scala similarity index 100% rename from src/test/scala-2.13/play/api/cache/redis/impl/AsyncRedisMock.scala rename to src/test/scala/play/api/cache/redis/impl/AsyncRedisMock.scala