Commit 7c44bbb1 authored by antirez's avatar antirez
Browse files

Scala client added thanks to Alejanro Crosa

parent 45636487
import org.specs._
import com.redis._
import org.specs.mock.Mockito
import org.mockito.Mock._
import org.mockito.Mockito._
import org.mockito.Mockito.doNothing
object KeySpaceOperationsSpec extends Specification with Mockito {
"Redis Client Key Operations" should {
var client: RedisTestClient = null
var connection: Connection = null
doBefore{
connection = mock[Connection]
client = new RedisTestClient(connection)
}
"return all keys matching" in {
connection.readResponse returns "akey anotherkey adiffkey"
client.keys("a*")
connection.write("KEYS a*\r\n") was called
}
"return a random key" in {
connection.readResponse returns "+somerandonkey"
client.randomKey mustEqual "somerandonkey"
connection.write("RANDOMKEY\r\n") was called
}
"remame a key" in {
connection.readBoolean returns true
client.rename("a", "b") must beTrue
connection.write("RENAME a b\r\n") was called
}
"rename a key only if destintation doesn't exist" in {
connection.readBoolean returns false
client.renamenx("a", "b") must beFalse
connection.write("RENAMENX a b\r\n") was called
}
"tell the size of the db, # of keys" in {
connection.readInt returns 4
client.dbSize mustEqual 4
connection.write("DBSIZE\r\n") was called
}
}
}
\ No newline at end of file
import org.specs._
import com.redis._
import org.specs.mock.Mockito
import org.mockito.Mock._
import org.mockito.Mockito._
import org.mockito.Mockito.doNothing
object ListOperationsSpec extends Specification with Mockito {
"Redis Client List Operations" should {
var client: RedisTestClient = null
var connection: Connection = null
doBefore{
connection = mock[Connection]
client = new RedisTestClient(connection)
}
"push to head" in {
connection.readBoolean returns true
client.pushHead("k", "v") must beTrue
connection.write("LPUSH k 1\r\nv\r\n") was called
}
"push to tail" in {
connection.readBoolean returns true
client.pushTail("k", "v") must beTrue
connection.write("RPUSH k 1\r\nv\r\n") was called
}
"pop from head" in {
connection.readString returns "value"
client.popHead("key") mustEqual "value"
connection.write("LPOP key\r\n") was called
}
"pop from tail" in {
connection.readString returns "value"
client.popTail("key") mustEqual "value"
connection.write("RPOP key\r\n") was called
}
"return list index" in {
connection.readString returns "value"
client.listIndex("k", 2) mustEqual "value"
connection.write("LINDEX k 2\r\n") was called
}
"return set element at index" in {
connection.readBoolean returns true
client.listSet("k", 1, "value") mustEqual true
connection.write("LSET k 1 5\r\nvalue\r\n") was called
}
"return list size" in {
connection.readInt returns 3
client.listLength("k") mustEqual 3
connection.write("LLEN k\r\n") was called
}
"return list range" in {
val listResult: List[String] = List("one", "two", "three", "four", "five")
connection.readList returns listResult
client.listRange("k", 2, 4) mustEqual listResult
connection.write("LRANGE k 2 4\r\n") was called
}
"trim a list" in {
connection.readBoolean returns true
client.listTrim("k", 2, 4) mustEqual true
connection.write("LTRIM k 2 4\r\n") was called
}
"remove occurrences of a value in the list" in {
connection.readBoolean returns true
client.listRem("k", 2, "value") mustEqual true
connection.write("LREM k 2 5\r\nvalue\r\n") was called
}
}
}
\ No newline at end of file
import org.specs._
import com.redis._
import org.specs.mock.Mockito
import org.mockito.Mock._
import org.mockito.Mockito._
import org.mockito.Mockito.doNothing
object NodeOperationsSpec extends Specification with Mockito {
"Redis Client Node Operations" should {
var client: RedisTestClient = null
var connection: Connection = null
doBefore{
connection = mock[Connection]
client = new RedisTestClient(connection)
}
"save the db to disk" in {
connection.readBoolean returns true
client.save must beTrue
connection.write("SAVE\r\n") was called
}
"return the last time saved data to the db" in {
connection.readInt returns 1250421891
client.lastSave mustEqual 1250421891
connection.write("LASTSAVE\r\n") was called
}
"return all specified keys" in {
connection.readList returns List[String]("hola", null, null)
client.mget("a", "b", "c") mustEqual List[String]("hola", null, null)
connection.write("MGET a b c\r\n") was called
}
"return server info" in {
val sampleInfo = "res0: Any = \nredis_version:0.091\nconnected_clients:2\nconnected_slaves:0\nused_memory:3036\nchanges_since_last_save:0\nlast_save_time:1250440893\ntotal_connections_received:2\ntotal_commands_processed:0\nuptime_in_seconds:7\nuptime_in_days:0\n"
connection.readResponse returns sampleInfo
client.info mustEqual sampleInfo
connection.write("INFO\r\n") was called
}
"start monitor debug on the server" in {
connection.readBoolean returns true
client.monitor mustEqual true
connection.write("MONITOR\r\n") was called
}
"set a server as slave of a remote master" in {
connection.readBoolean returns true
client.slaveOf("localhost", 9999) mustEqual true
connection.write("SLAVEOF localhost 9999\r\n") was called
}
"set a server as master if no params sent" in {
connection.readBoolean returns true
client.slaveOf() mustEqual true
connection.write("SLAVEOF NO ONE\r\n") was called
}
"set a server as master" in {
connection.readBoolean returns true
client.setAsMaster mustEqual true
connection.write("SLAVEOF NO ONE\r\n") was called
}
"select the current db" in {
connection.readBoolean returns true
client.selectDb(3) mustEqual true
client.db mustEqual 3
connection.write("SELECT 3\r\n") was called
}
"flush the db" in {
connection.readBoolean returns true
client.flushDb mustEqual true
connection.write("FLUSHDB\r\n") was called
}
"flush all the dbs" in {
connection.readBoolean returns true
client.flushAll mustEqual true
connection.write("FLUSHALL\r\n") was called
}
"shutdown the db" in {
connection.readBoolean returns true
client.shutdown mustEqual true
connection.write("SHUTDOWN\r\n") was called
}
"move keys from one db to another" in {
connection.readBoolean returns true
client.move("a", 2) mustEqual true
connection.write("MOVE a 2\r\n") was called
}
"quit" in {
connection.disconnect returns true
client.quit mustEqual true
connection.write("QUIT\r\n") was called
connection.disconnect was called
}
"auth with the server" in {
connection.readBoolean returns true
client.auth("secret") mustEqual true
connection.write("AUTH secret\r\n") was called
}
}
}
import org.specs._
import com.redis._
import org.specs.mock.Mockito
import org.mockito.Mock._
import org.mockito.Mockito._
object OperationsSpec extends Specification with Mockito {
"Redis Client Operations" should {
var client: RedisTestClient = null
var connection: Connection = null
doBefore{
connection = mock[Connection]
client = new RedisTestClient(connection)
}
"set a key" in {
connection.readBoolean returns true
client.set("a", "b") mustEqual true
connection.write("SET a 1\r\nb\r\n") was called
}
"set a key with setKey" in {
connection.readBoolean returns true
client.setKey("a", "b") mustEqual true
connection.write("SET a 1\r\nb\r\n") was called
}
"set a key with expiration" in {
connection.readBoolean returns true
client.set("a", "b", 4) mustEqual true
connection.write("SET a 1\r\nb\r\n") was called
connection.write("EXPIRE a 4\r\n") was called
}
"expire a key" in {
connection.readBoolean returns true
client.expire("a", 4) mustEqual true
connection.write("EXPIRE a 4\r\n") was called
}
"get a key" in {
connection.readResponse returns "b"
client.get("a") mustEqual "b"
connection.write("GET a\r\n") was called
}
"get and set a key" in {
connection.readResponse returns "old"
client.getSet("a", "new") mustEqual "old"
connection.write("GETSET a 3\r\nnew\r\n") was called
}
"delete a key" in {
connection.readBoolean returns true
client.delete("a") mustEqual true
connection.write("DEL a\r\n") was called
}
"tell if a key exists" in {
connection.readBoolean returns true
client.exists("a") mustEqual true
connection.write("EXISTS a\r\n") was called
}
"tell if a key exists" in {
connection.readBoolean returns true
client.exists("a") mustEqual true
connection.write("EXISTS a\r\n") was called
}
"increment a value" in {
connection.readInt returns 1
client.incr("a") mustEqual 1
connection.write("INCR a\r\n") was called
}
"increment a value by N" in {
connection.readInt returns 27
client.incr("a", 23) mustEqual 27
connection.write("INCRBY a 23\r\n") was called
}
"decrement a value" in {
connection.readInt returns 0
client.decr("a") mustEqual 0
connection.write("DECR a\r\n") was called
}
"decrement a value by N" in {
connection.readInt returns 25
client.decr("a", 2) mustEqual 25
connection.write("DECRBY a 2\r\n") was called
}
"return type of key" in {
connection.readResponse returns "String"
client.getType("a") mustEqual "String"
connection.write("TYPE a\r\n") was called
}
}
}
import org.specs._
import com.redis._
import org.specs.mock.Mockito
import org.mockito.Mock._
import org.mockito.Mockito._
import org.mockito.Mockito.doNothing
object SetOperationsSpec extends Specification with Mockito {
"Redis Client Set Operations" should {
var client: RedisTestClient = null
var connection: Connection = null
doBefore{
connection = mock[Connection]
client = new RedisTestClient(connection)
}
"add a member to a set" in {
connection.readBoolean returns true
client.setAdd("set", "value") must beTrue
connection.write("SADD set 5\r\nvalue\r\n") was called
}
"remove an member from a set" in {
connection.readBoolean returns true
client.setDelete("set", "value") must beTrue
connection.write("SREM set 5\r\nvalue\r\n") was called
}
"return the number of elements in the set" in {
connection.readInt returns 5
client.setCount("set") mustEqual 5
connection.write("SCARD set\r\n") was called
}
"return all the members from a set" in {
val setResult = Set("one", "two", "three")
connection.readSet returns setResult
client.setMembers("set") mustEqual setResult
connection.write("SMEMBERS set\r\n") was called
}
"pop an element from the set" in {
connection.readString returns "one"
client.setPop("set") mustEqual "one"
connection.write("SPOP set\r\n") was called
}
"move an element from one set to another" in {
connection.readBoolean returns true
client.setMove("set", "toset", "value") mustEqual true
connection.write("SMOVE set toset value\r\n") was called
}
"tell if member exists on the set" in {
connection.readBoolean returns true
client.setMemberExists("set", "value") mustEqual true
connection.write("SISMEMBER set 5\r\nvalue\r\n") was called
}
"return the intersection between N sets" in {
val setResult = Set("one", "two", "three")
connection.readSet returns setResult
client.setIntersect("set", "otherset", "another") mustEqual setResult
connection.write("SINTER set otherset another\r\n") was called
}
"return the intersection between N sets and store it a new one" in {
connection.readBoolean returns true
client.setInterStore("set", "oneset", "twoset") mustEqual true
connection.write("SINTERSTORE set oneset twoset\r\n") was called
}
"return the difference between N sets" in {
val setResult = Set("one", "two", "three")
connection.readSet returns setResult
client.setDiff("set", "oneset", "twoset") mustEqual setResult
connection.write("SDIFF set oneset twoset\r\n") was called
}
"return the difference between N sets and store it in a new one" in {
connection.readBoolean returns true
client.setDiffStore("newset", "oneset", "twoset") mustEqual true
connection.write("SDIFFSTORE newset oneset twoset\r\n") was called
}
"return the union between N sets" in {
val setResult = Set("one", "two", "three")
connection.readSet returns setResult
client.setUnion("set", "oneset", "twoset") mustEqual setResult
connection.write("SUNION set oneset twoset\r\n") was called
}
"return the union between N sets and store it in a new one" in {
connection.readBoolean returns true
client.setUnionStore("set", "oneset", "twoset") mustEqual true
connection.write("SUNIONSTORE set oneset twoset\r\n") was called
}
}
}
\ No newline at end of file
import org.specs._
import com.redis._
import org.specs.mock.Mockito
import org.mockito.Mock._
import org.mockito.Mockito._
object SortOperationsSpec extends Specification with Mockito {
"Redis Client Sort Operations" should {
var client: RedisTestClient = null
var connection: Connection = null
doBefore{
connection = mock[Connection]
client = new RedisTestClient(connection)
}
"sort the contents of the specified key" in {
val listResult: List[String] = List("one", "two", "three")
connection.readList returns listResult
client.sort("set", "ALPHA DESC") mustEqual listResult
connection.write("SORT set ALPHA DESC\r\n") was called
}
"sort the contents of the specified key with default" in {
val listResult: List[String] = List("one", "two", "three")
connection.readList returns listResult
client.sort("set") mustEqual listResult
connection.write("SORT set\r\n") was called
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment