/client-libraries/ruby/spec/redis_spec.rb

http://redis.googlecode.com/ · Ruby · 267 lines · 233 code · 34 blank · 0 comment · 65 complexity · 15495379f4dfde6e39d5b5aa8f75de5e MD5 · raw file

  1. require File.dirname(__FILE__) + '/spec_helper'
  2. class Foo
  3. attr_accessor :bar
  4. def initialize(bar)
  5. @bar = bar
  6. end
  7. def ==(other)
  8. @bar == other.bar
  9. end
  10. end
  11. describe "redis" do
  12. before do
  13. @r = Redis.new
  14. @r.select_db(15) # use database 15 for testing so we dont accidentally step on you real data
  15. @r['foo'] = 'bar'
  16. end
  17. after do
  18. @r.keys('*').each {|k| @r.delete k }
  19. end
  20. it "should properly marshall objects" do
  21. class MyFail; def fail; 'it will' end; end
  22. @r['fail'] = MyFail.new
  23. @r['fail'].fail.should == 'it will'
  24. end
  25. it "should be able to GET a key" do
  26. @r['foo'].should == 'bar'
  27. end
  28. it "should be able to SET a key" do
  29. @r['foo'] = 'nik'
  30. @r['foo'].should == 'nik'
  31. end
  32. it "should be able to SETNX(set_unless_exists)" do
  33. @r['foo'] = 'nik'
  34. @r['foo'].should == 'nik'
  35. @r.set_unless_exists 'foo', 'bar'
  36. @r['foo'].should == 'nik'
  37. end
  38. it "should be able to INCR(increment) a key" do
  39. @r.delete('counter')
  40. @r.incr('counter').should == 1
  41. @r.incr('counter').should == 2
  42. @r.incr('counter').should == 3
  43. end
  44. it "should be able to DECR(decrement) a key" do
  45. @r.delete('counter')
  46. @r.incr('counter').should == 1
  47. @r.incr('counter').should == 2
  48. @r.incr('counter').should == 3
  49. @r.decr('counter').should == 2
  50. @r.decr('counter').should == 1
  51. @r.decr('counter').should == 0
  52. end
  53. it "should be able to RANDKEY(return a random key)" do
  54. @r.randkey.should_not be_nil
  55. end
  56. it "should be able to RENAME a key" do
  57. @r.delete 'foo'
  58. @r.delete 'bar'
  59. @r['foo'] = 'hi'
  60. @r.rename! 'foo', 'bar'
  61. @r['bar'].should == 'hi'
  62. end
  63. it "should be able to RENAMENX(rename unless the new key already exists) a key" do
  64. @r.delete 'foo'
  65. @r.delete 'bar'
  66. @r['foo'] = 'hi'
  67. @r['bar'] = 'ohai'
  68. lambda {@r.rename 'foo', 'bar'}.should raise_error(RedisError)
  69. @r['bar'].should == 'ohai'
  70. end
  71. it "should be able to EXISTS(check if key exists)" do
  72. @r['foo'] = 'nik'
  73. @r.key?('foo').should be_true
  74. @r.delete 'foo'
  75. @r.key?('foo').should be_false
  76. end
  77. it "should be able to KEYS(glob for keys)" do
  78. @r.keys("f*").each do |key|
  79. @r.delete key
  80. end
  81. @r['f'] = 'nik'
  82. @r['fo'] = 'nak'
  83. @r['foo'] = 'qux'
  84. @r.keys("f*").sort.should == ['f','fo', 'foo'].sort
  85. end
  86. it "should be able to check the TYPE of a key" do
  87. @r['foo'] = 'nik'
  88. @r.type?('foo').should == "string"
  89. @r.delete 'foo'
  90. @r.type?('foo').should == "none"
  91. end
  92. it "should be able to push to the head of a list" do
  93. @r.push_head "list", 'hello'
  94. @r.push_head "list", 42
  95. @r.type?('list').should == "list"
  96. @r.list_length('list').should == 2
  97. @r.pop_head('list').should == '42'
  98. @r.delete('list')
  99. end
  100. it "should be able to push to the tail of a list" do
  101. @r.push_tail "list", 'hello'
  102. @r.type?('list').should == "list"
  103. @r.list_length('list').should == 1
  104. @r.delete('list')
  105. end
  106. it "should be able to pop the tail of a list" do
  107. @r.push_tail "list", 'hello'
  108. @r.push_tail "list", 'goodbye'
  109. @r.type?('list').should == "list"
  110. @r.list_length('list').should == 2
  111. @r.pop_tail('list').should == 'goodbye'
  112. @r.delete('list')
  113. end
  114. it "should be able to pop the head of a list" do
  115. @r.push_tail "list", 'hello'
  116. @r.push_tail "list", 'goodbye'
  117. @r.type?('list').should == "list"
  118. @r.list_length('list').should == 2
  119. @r.pop_head('list').should == 'hello'
  120. @r.delete('list')
  121. end
  122. it "should be able to get the length of a list" do
  123. @r.push_tail "list", 'hello'
  124. @r.push_tail "list", 'goodbye'
  125. @r.type?('list').should == "list"
  126. @r.list_length('list').should == 2
  127. @r.delete('list')
  128. end
  129. it "should be able to get a range of values from a list" do
  130. @r.push_tail "list", 'hello'
  131. @r.push_tail "list", 'goodbye'
  132. @r.push_tail "list", '1'
  133. @r.push_tail "list", '2'
  134. @r.push_tail "list", '3'
  135. @r.type?('list').should == "list"
  136. @r.list_length('list').should == 5
  137. @r.list_range('list', 2, -1).should == ['1', '2', '3']
  138. @r.delete('list')
  139. end
  140. it "should be able to trim a list" do
  141. @r.push_tail "list", 'hello'
  142. @r.push_tail "list", 'goodbye'
  143. @r.push_tail "list", '1'
  144. @r.push_tail "list", '2'
  145. @r.push_tail "list", '3'
  146. @r.type?('list').should == "list"
  147. @r.list_length('list').should == 5
  148. @r.list_trim 'list', 0, 1
  149. @r.list_length('list').should == 2
  150. @r.list_range('list', 0, -1).should == ['hello', 'goodbye']
  151. @r.delete('list')
  152. end
  153. it "should be able to get a value by indexing into a list" do
  154. @r.push_tail "list", 'hello'
  155. @r.push_tail "list", 'goodbye'
  156. @r.type?('list').should == "list"
  157. @r.list_length('list').should == 2
  158. @r.list_index('list', 1).should == 'goodbye'
  159. @r.delete('list')
  160. end
  161. it "should be able to set a value by indexing into a list" do
  162. @r.push_tail "list", 'hello'
  163. @r.push_tail "list", 'hello'
  164. @r.type?('list').should == "list"
  165. @r.list_length('list').should == 2
  166. @r.list_set('list', 1, 'goodbye').should be_true
  167. @r.list_index('list', 1).should == 'goodbye'
  168. @r.delete('list')
  169. end
  170. it "should be able add members to a set" do
  171. @r.set_add "set", 'key1'
  172. @r.set_add "set", 'key2'
  173. @r.type?('set').should == "set"
  174. @r.set_count('set').should == 2
  175. @r.set_members('set').sort.should == ['key1', 'key2'].sort
  176. @r.delete('set')
  177. end
  178. it "should be able delete members to a set" do
  179. @r.set_add "set", 'key1'
  180. @r.set_add "set", 'key2'
  181. @r.type?('set').should == "set"
  182. @r.set_count('set').should == 2
  183. @r.set_members('set').should == Set.new(['key1', 'key2'])
  184. @r.set_delete('set', 'key1')
  185. @r.set_count('set').should == 1
  186. @r.set_members('set').should == Set.new(['key2'])
  187. @r.delete('set')
  188. end
  189. it "should be able count the members of a set" do
  190. @r.set_add "set", 'key1'
  191. @r.set_add "set", 'key2'
  192. @r.type?('set').should == "set"
  193. @r.set_count('set').should == 2
  194. @r.delete('set')
  195. end
  196. it "should be able test for set membership" do
  197. @r.set_add "set", 'key1'
  198. @r.set_add "set", 'key2'
  199. @r.type?('set').should == "set"
  200. @r.set_count('set').should == 2
  201. @r.set_member?('set', 'key1').should be_true
  202. @r.set_member?('set', 'key2').should be_true
  203. @r.set_member?('set', 'notthere').should be_false
  204. @r.delete('set')
  205. end
  206. it "should be able to do set intersection" do
  207. @r.set_add "set", 'key1'
  208. @r.set_add "set", 'key2'
  209. @r.set_add "set2", 'key2'
  210. @r.set_intersect('set', 'set2').should == Set.new(['key2'])
  211. @r.delete('set')
  212. end
  213. it "should be able to do set intersection and store the results in a key" do
  214. @r.set_add "set", 'key1'
  215. @r.set_add "set", 'key2'
  216. @r.set_add "set2", 'key2'
  217. @r.set_inter_store('newone', 'set', 'set2')
  218. @r.set_members('newone').should == Set.new(['key2'])
  219. @r.delete('set')
  220. end
  221. it "should be able to do crazy SORT queries" do
  222. @r['dog_1'] = 'louie'
  223. @r.push_tail 'dogs', 1
  224. @r['dog_2'] = 'lucy'
  225. @r.push_tail 'dogs', 2
  226. @r['dog_3'] = 'max'
  227. @r.push_tail 'dogs', 3
  228. @r['dog_4'] = 'taj'
  229. @r.push_tail 'dogs', 4
  230. @r.sort('dogs', :get => 'dog_*', :limit => [0,1]).should == ['louie']
  231. @r.sort('dogs', :get => 'dog_*', :limit => [0,1], :order => 'desc alpha').should == ['taj']
  232. end
  233. end