Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: redis hget return option #143

Merged
merged 3 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions redis/src/it/scala/monix/connect/redis/RedisIntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import org.scalatest.flatspec.AnyFlatSpec

import scala.concurrent.duration._

class RedisIntegrationTest
extends AnyFlatSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll {
class RedisIntegrationTest extends AnyFlatSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll {
paualarco marked this conversation as resolved.
Show resolved Hide resolved

val redisUrl = "redis://localhost:6379"
type K = String
Expand All @@ -27,7 +26,20 @@ class RedisIntegrationTest

implicit val connection: StatefulRedisConnection[String, String] = RedisClient.create(redisUrl).connect()

s"${RedisString} " should "insert a string into the given key and get its size from redis" in {
s"${RedisHash}" should "access non existing key in redis and get None" in {
//given
val key: K = genRedisKey.sample.get
val field: K = genRedisKey.sample.get

//when
val t: Task[Option[String]] = RedisHash.hget(key, field)

//then
val r: Option[String] = t.runSyncUnsafe()
r shouldBe None
}

s"${RedisString} " should "insert a string into the given key and get its size from redis" in {
//given
val key: K = genRedisKey.sample.get
val value: String = genRedisValue.sample.get.toString
Expand Down Expand Up @@ -71,11 +83,11 @@ class RedisIntegrationTest
RedisHash.hset(key, field, value).runSyncUnsafe()

//and
val t: Task[String] = RedisHash.hget(key, field)
val t: Task[Option[String]] = RedisHash.hget(key, field)

//then
val r: String = t.runSyncUnsafe()
r shouldBe value
val r: Option[String] = t.runSyncUnsafe()
r shouldBe Some(value)
}

s"${RedisKey}" should "handles ttl correctly" in {
Expand Down Expand Up @@ -182,10 +194,10 @@ class RedisIntegrationTest
//when
RedisSortedSet.zadd(k, minScore, v0)
val t: Task[(ScoredValue[String], ScoredValue[String])] = for {
_ <- RedisSortedSet.zadd(k, minScore, v0)
_ <- RedisSortedSet.zadd(k, middleScore, v1)
_ <- RedisSortedSet.zadd(k, maxScore, v2)
_ <- RedisSortedSet.zincrby(k, incrby, v1)
_ <- RedisSortedSet.zadd(k, minScore, v0)
_ <- RedisSortedSet.zadd(k, middleScore, v1)
_ <- RedisSortedSet.zadd(k, maxScore, v2)
_ <- RedisSortedSet.zincrby(k, incrby, v1)
min <- RedisSortedSet.zpopmin(k)
max <- RedisSortedSet.zpopmax(k)
} yield (min, max)
Expand Down
4 changes: 2 additions & 2 deletions redis/src/main/scala/monix/connect/redis/RedisHash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ private[redis] trait RedisHash {
* Get the value of a hash field.
* @return The value associated with field, or null when field is not present in the hash or key does not exist.
*/
def hget[K, V](key: K, field: K)(implicit connection: StatefulRedisConnection[K, V]): Task[V] =
Task.from(connection.reactive().hget(key, field))
def hget[K, V](key: K, field: K)(implicit connection: StatefulRedisConnection[K, V]): Task[Option[V]] =
Task.fromReactivePublisher(connection.reactive().hget(key, field))

/**
* Increment the integer value of a hash field by the given number.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class RedisHashSpec
when(reactiveRedisCommands.hget(k, field)).thenReturn(mockMono[V])

//when
val _: Task[V] = RedisHash.hget(k, field)
val _: Task[Option[V]] = RedisHash.hget(k, field)

//then
verify(reactiveRedisCommands).hget(k, field)
Expand Down