couchbase-jruby-client 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_get.rb CHANGED
@@ -19,155 +19,136 @@ require File.join(File.dirname(__FILE__), 'setup')
19
19
 
20
20
  class TestGet < MiniTest::Test
21
21
 
22
- def setup
23
- @mock = start_mock
24
- end
25
-
26
- def teardown
27
- stop_mock(@mock)
28
- end
29
-
30
22
  def test_trivial_get
31
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
- connection.set(uniq_id, "bar")
33
- val = connection.get(uniq_id)
23
+ cb.set(uniq_id, "bar")
24
+ val = cb.get(uniq_id)
34
25
  assert_equal "bar", val
35
26
  end
36
27
 
37
28
  def test_extended_get
38
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
39
-
40
- orig_cas = connection.set(uniq_id, "bar")
41
- val, flags, cas = connection.get(uniq_id, :extended => true)
29
+ orig_cas = cb.set(uniq_id, "bar")
30
+ val, flags, cas = cb.get(uniq_id, :extended => true)
42
31
  assert_equal "bar", val
43
32
  #assert_equal 0x0, flags
44
33
  assert_equal orig_cas, cas
45
34
 
46
- orig_cas = connection.set(uniq_id, "bar", :flags => 0x1000)
47
- val, flags, cas = connection.get(uniq_id, :extended => true)
35
+ orig_cas = cb.set(uniq_id, "bar", :flags => 0x1000)
36
+ val, flags, cas = cb.get(uniq_id, :extended => true)
48
37
  assert_equal "bar", val
49
38
  #assert_equal 0x1000, flags
50
39
  assert_equal orig_cas, cas
51
40
  end
52
41
 
53
42
  def test_multi_get
54
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
55
-
56
- connection.set(uniq_id(1), "foo1")
57
- connection.set(uniq_id(2), "foo2")
43
+ cb.set(uniq_id(1), "foo1")
44
+ cb.set(uniq_id(2), "foo2")
58
45
 
59
- val1, val2 = connection.get(uniq_id(1), uniq_id(2))
46
+ val1, val2 = cb.get(uniq_id(1), uniq_id(2))
60
47
  assert_equal "foo1", val1
61
48
  assert_equal "foo2", val2
62
49
  end
63
50
 
64
51
  def test_multi_get_extended
65
52
  skip 'Looking like Java client doesnt support multi_get_extended'
66
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
67
53
 
68
- cas1 = connection.set(uniq_id(1), "foo1")
69
- cas2 = connection.set(uniq_id(2), "foo2")
54
+ cas1 = cb.set(uniq_id(1), "foo1")
55
+ cas2 = cb.set(uniq_id(2), "foo2")
70
56
 
71
- results = connection.get(uniq_id(1), uniq_id(2), :extended => true)
57
+ results = cb.get(uniq_id(1), uniq_id(2), :extended => true)
72
58
  assert_equal ["foo1", nil, cas1], results[uniq_id(1)]
73
59
  assert_equal ["foo2", nil, cas2], results[uniq_id(2)]
74
60
  end
75
61
 
76
62
  def test_multi_get_and_touch
77
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
78
- connection.set(uniq_id(1), "foo1")
79
- connection.set(uniq_id(2), "foo2")
63
+ cb.set(uniq_id(1), "foo1")
64
+ cb.set(uniq_id(2), "foo2")
80
65
 
81
- results = connection.get(uniq_id(1) => 1, uniq_id(2) => 1)
66
+ results = cb.get(uniq_id(1) => 1, uniq_id(2) => 1)
82
67
  assert results.is_a?(Hash)
83
68
  assert_equal "foo1", results[uniq_id(1)]
84
69
  assert_equal "foo2", results[uniq_id(2)]
85
70
  sleep(2)
86
71
  assert_raises(Couchbase::Error::NotFound) do
87
- connection.get(uniq_id(1), uniq_id(2))
72
+ cb.get(uniq_id(1), uniq_id(2))
88
73
  end
89
- assert connection.get(uniq_id(1), uniq_id(2), :quiet => true).compact.empty?
74
+ assert cb.get(uniq_id(1), uniq_id(2), :quiet => true).compact.empty?
90
75
  end
91
76
 
92
77
  def test_multi_get_and_touch_extended
93
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
78
+ cas1 = cb.set(uniq_id(1), "foo1")
79
+ cas2 = cb.set(uniq_id(2), "foo2")
94
80
 
95
- cas1 = connection.set(uniq_id(1), "foo1")
96
- cas2 = connection.set(uniq_id(2), "foo2")
97
-
98
- results = connection.get({uniq_id(1) => 1, uniq_id(2) => 1}, :extended => true)
81
+ results = cb.get({uniq_id(1) => 1, uniq_id(2) => 1}, :extended => true)
99
82
  assert_equal ["foo1", nil, cas1], results[uniq_id(1)]
100
83
  assert_equal ["foo2", nil, cas2], results[uniq_id(2)]
101
84
  end
102
85
 
103
86
  def test_multi_get_and_touch_with_single_key
104
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
105
- connection.set(uniq_id, "foo1")
87
+ cb.set(uniq_id, "foo1")
106
88
 
107
- results = connection.get(uniq_id => 1)
89
+ results = cb.get(uniq_id => 1)
108
90
  assert results.is_a?(Hash)
109
91
  assert_equal "foo1", results[uniq_id]
110
92
  sleep(2)
111
93
  assert_raises(Couchbase::Error::NotFound) do
112
- connection.get(uniq_id)
94
+ cb.get(uniq_id)
113
95
  end
114
96
  end
115
97
 
116
98
  def test_missing_in_quiet_mode
117
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => true)
118
- cas1 = connection.set(uniq_id(1), "foo1")
119
- cas2 = connection.set(uniq_id(2), "foo2")
99
+ cb.quiet = true
100
+ cas1 = cb.set(uniq_id(1), "foo1")
101
+ cas2 = cb.set(uniq_id(2), "foo2")
120
102
 
121
- val = connection.get(uniq_id(:missing))
103
+ val = cb.get(uniq_id(:missing))
122
104
  refute(val)
123
- val = connection.get(uniq_id(:missing), :extended => true)
105
+ val = cb.get(uniq_id(:missing), :extended => true)
124
106
  refute(val)
125
107
 
126
- val1, missing, val2 = connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
108
+ val1, missing, val2 = cb.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
127
109
  assert_equal "foo1", val1
128
110
  refute missing
129
111
  assert_equal "foo2", val2
130
112
 
131
113
  # TODO: multi get with cas
132
- # results = connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
114
+ # results = cb.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
133
115
  # assert_equal ["foo1", nil, cas1], results[uniq_id(1)]
134
116
  # refute results[uniq_id(:missing)]
135
117
  # assert_equal ["foo2", nil, cas2], results[uniq_id(2)]
118
+ ensure
119
+ cb.quiet = false
136
120
  end
137
121
 
138
122
  def test_it_allows_temporary_quiet_flag
139
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => false)
140
123
  assert_raises(Couchbase::Error::NotFound) do
141
- connection.get(uniq_id(:missing))
124
+ cb.get(uniq_id(:missing))
142
125
  end
143
- refute connection.get(uniq_id(:missing), :quiet => true)
126
+ refute cb.get(uniq_id(:missing), :quiet => true)
144
127
  end
145
128
 
146
129
  def test_missing_in_verbose_mode
147
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => false)
148
- connection.set(uniq_id(1), "foo1")
149
- connection.set(uniq_id(2), "foo2")
130
+ cb.set(uniq_id(1), "foo1")
131
+ cb.set(uniq_id(2), "foo2")
150
132
 
151
133
  assert_raises(Couchbase::Error::NotFound) do
152
- connection.get(uniq_id(:missing))
134
+ cb.get(uniq_id(:missing))
153
135
  end
154
136
 
155
137
  assert_raises(Couchbase::Error::NotFound) do
156
- connection.get(uniq_id(:missing), :extended => true)
138
+ cb.get(uniq_id(:missing), :extended => true)
157
139
  end
158
140
 
159
141
  assert_raises(Couchbase::Error::NotFound) do
160
- connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
142
+ cb.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
161
143
  end
162
144
 
163
145
  assert_raises(Couchbase::Error::NotFound) do
164
- connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
146
+ cb.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
165
147
  end
166
148
  end
167
149
 
168
150
  def test_asynchronous_get
169
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
170
- cas = connection.set(uniq_id, "foo")
151
+ cas = cb.set(uniq_id, "foo")
171
152
  res = []
172
153
 
173
154
  suite = lambda do |conn|
@@ -188,21 +169,20 @@ class TestGet < MiniTest::Test
188
169
  end
189
170
  end
190
171
 
191
- connection.run(&suite)
172
+ cb.run(&suite)
192
173
  checks.call
193
174
 
194
- connection.run{ suite.call(connection) }
175
+ cb.run{ suite.call(cb) }
195
176
  checks.call
196
177
  end
197
178
 
198
179
  def test_asynchronous_multi_get
199
180
  skip
200
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
201
- connection.set(uniq_id(1), "foo")
202
- connection.set(uniq_id(2), "bar")
181
+ cb.set(uniq_id(1), "foo")
182
+ cb.set(uniq_id(2), "bar")
203
183
 
204
184
  res = {}
205
- connection.run do |conn|
185
+ cb.run do |conn|
206
186
  conn.get(uniq_id(1), uniq_id(2)) {|ret| res[ret.key] = ret.value}
207
187
  end
208
188
 
@@ -214,8 +194,7 @@ class TestGet < MiniTest::Test
214
194
 
215
195
  def test_asynchronous_get_missing
216
196
  skip
217
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
218
- connection.set(uniq_id, "foo")
197
+ cb.set(uniq_id, "foo")
219
198
  res = {}
220
199
  missing = []
221
200
 
@@ -239,14 +218,14 @@ class TestGet < MiniTest::Test
239
218
  conn.get(uniq_id, uniq_id(:missing2), &get_handler)
240
219
  end
241
220
 
242
- connection.run(&suite)
221
+ cb.run(&suite)
243
222
  refute res.has_key?(uniq_id(:missing1))
244
223
  refute res.has_key?(uniq_id(:missing2))
245
224
  assert_equal [uniq_id(:missing1), uniq_id(:missing2)], missing.sort
246
225
  assert_equal "foo", res[uniq_id]
247
226
 
248
- connection.quiet = true
249
- connection.run(&suite)
227
+ cb.quiet = true
228
+ cb.run(&suite)
250
229
  assert_equal "foo", res[uniq_id]
251
230
  assert res.has_key?(uniq_id(:missing1)) # handler was called with nil
252
231
  refute res[uniq_id(:missing1)]
@@ -256,16 +235,14 @@ class TestGet < MiniTest::Test
256
235
  end
257
236
 
258
237
  def test_get_using_brackets
259
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
260
-
261
- orig_cas = connection.set(uniq_id, "foo", :flags => 0x1100)
238
+ orig_cas = cb.set(uniq_id, "foo", :flags => 0x1100)
262
239
 
263
- val = connection[uniq_id]
240
+ val = cb[uniq_id]
264
241
  assert_equal "foo", val
265
242
 
266
243
  if RUBY_VERSION =~ /^1\.9/
267
244
  eval <<-EOC
268
- val, flags, cas = connection[uniq_id, :extended => true]
245
+ val, flags, cas = cb[uniq_id, :extended => true]
269
246
  assert_equal "foo", val
270
247
  #assert_equal 0x1100, flags
271
248
  assert_equal orig_cas, cas
@@ -275,16 +252,15 @@ class TestGet < MiniTest::Test
275
252
 
276
253
  def test_it_allows_to_store_nil
277
254
  skip "TODO: figure out nil storage"
278
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
279
255
 
280
- orig_cas = connection.set(uniq_id, nil)
256
+ orig_cas = cb.set(uniq_id, nil)
281
257
  assert orig_cas.is_a?(Numeric)
282
258
 
283
- refute connection.get(uniq_id)
259
+ refute cb.get(uniq_id)
284
260
  # doesn't raise NotFound exception
285
- refute connection.get(uniq_id, :quiet => false)
261
+ refute cb.get(uniq_id, :quiet => false)
286
262
  # returns CAS
287
- value, flags, cas = connection.get(uniq_id, :extended => true)
263
+ value, flags, cas = cb.get(uniq_id, :extended => true)
288
264
  refute value
289
265
  #assert_equal 0x00, flags
290
266
  assert_equal orig_cas, cas
@@ -292,125 +268,106 @@ class TestGet < MiniTest::Test
292
268
 
293
269
  def test_zero_length_string_is_not_nil
294
270
  skip("zero length string")
295
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
296
271
 
297
- connection.set(uniq_id, "", :format => :document)
298
- assert_equal "", connection.get(uniq_id)
272
+ cb.set(uniq_id, "", :format => :document)
273
+ assert_equal "", cb.get(uniq_id)
299
274
 
300
- connection.set(uniq_id, "", :format => :plain)
301
- assert_equal "", connection.get(uniq_id)
275
+ cb.set(uniq_id, "", :format => :plain)
276
+ assert_equal "", cb.get(uniq_id)
302
277
 
303
- connection.set(uniq_id, "", :format => :marshal)
304
- assert_equal "", connection.get(uniq_id)
278
+ cb.set(uniq_id, "", :format => :marshal)
279
+ assert_equal "", cb.get(uniq_id)
305
280
 
306
- connection.set(uniq_id, nil, :format => :document)
307
- assert_equal nil, connection.get(uniq_id, :quiet => false)
281
+ cb.set(uniq_id, nil, :format => :document)
282
+ assert_equal nil, cb.get(uniq_id, :quiet => false)
308
283
 
309
284
  assert_raises Couchbase::Error::ValueFormat do
310
- connection.set(uniq_id, nil, :format => :plain)
285
+ cb.set(uniq_id, nil, :format => :plain)
311
286
  end
312
287
 
313
- connection.set(uniq_id, nil, :format => :marshal)
314
- assert_equal nil, connection.get(uniq_id, :quiet => false)
288
+ cb.set(uniq_id, nil, :format => :marshal)
289
+ assert_equal nil, cb.get(uniq_id, :quiet => false)
315
290
  end
316
291
 
317
292
  def test_format_forcing
318
293
  skip("format forcing")
319
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
320
294
 
321
- connection.set(uniq_id, '{"foo":"bar"}', :format => :plain)
322
- value, flags, _ = connection.get(uniq_id, :extended => true)
295
+ cb.set(uniq_id, '{"foo":"bar"}', :format => :plain)
296
+ value, flags, _ = cb.get(uniq_id, :extended => true)
323
297
  assert_equal '{"foo":"bar"}', value
324
298
  assert_equal 0x02, flags
325
299
 
326
- value, flags, _ = connection.get(uniq_id, :extended => true, :format => :document)
300
+ value, flags, _ = cb.get(uniq_id, :extended => true, :format => :document)
327
301
  expected = {"foo" => "bar"}
328
302
  assert_equal expected, value
329
303
  assert_equal 0x02, flags
330
304
 
331
- connection.prepend(uniq_id, "NOT-A-JSON")
305
+ cb.prepend(uniq_id, "NOT-A-JSON")
332
306
  assert_raises Couchbase::Error::ValueFormat do
333
- connection.get(uniq_id, :format => :document)
307
+ cb.get(uniq_id, :format => :document)
334
308
  end
335
309
  end
336
310
 
337
311
  # http://www.couchbase.com/issues/browse/RCBC-31
338
312
  def test_consistent_behaviour_for_arrays
339
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
340
313
 
341
- cas = connection.set(uniq_id("foo"), "foo")
342
- connection.set(uniq_id("bar"), "bar")
314
+ cas = cb.set(uniq_id("foo"), "foo")
315
+ cb.set(uniq_id("bar"), "bar")
343
316
 
344
- assert_equal "foo", connection.get(uniq_id("foo"))
345
- assert_equal ["foo"], connection.get([uniq_id("foo")])
346
- assert_equal ["foo", "bar"], connection.get([uniq_id("foo"), uniq_id("bar")])
347
- assert_equal ["foo", "bar"], connection.get(uniq_id("foo"), uniq_id("bar"))
317
+ assert_equal "foo", cb.get(uniq_id("foo"))
318
+ assert_equal ["foo"], cb.get([uniq_id("foo")])
319
+ assert_equal ["foo", "bar"], cb.get([uniq_id("foo"), uniq_id("bar")])
320
+ assert_equal ["foo", "bar"], cb.get(uniq_id("foo"), uniq_id("bar"))
348
321
  # expected = {uniq_id("foo") => ["foo", nil, cas]}
349
- # assert_equal expected, connection.get([uniq_id("foo")], :extended => true)
322
+ # assert_equal expected, cb.get([uniq_id("foo")], :extended => true)
350
323
  assert_raises TypeError do
351
- connection.get([uniq_id("foo"), uniq_id("bar")], [uniq_id("foo")])
324
+ cb.get([uniq_id("foo"), uniq_id("bar")], [uniq_id("foo")])
352
325
  end
353
326
  end
354
327
 
355
328
  def test_get_with_lock_trivial
356
- if @mock.real?
357
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
358
- connection.set(uniq_id, "foo")
329
+ cb.set(uniq_id, "foo")
359
330
 
360
- assert_equal "foo", connection.get(uniq_id, :lock => 1)
361
- assert_raises Couchbase::Error::KeyExists do
362
- connection.set(uniq_id, "bar")
363
- end
364
- sleep(2)
365
- connection.set(uniq_id, "bar")
366
- else
367
- skip("implement GETL in CouchbaseMock.jar")
331
+ assert_equal "foo", cb.get(uniq_id, :lock => 1)
332
+ assert_raises Couchbase::Error::KeyExists do
333
+ cb.set(uniq_id, "bar")
368
334
  end
335
+ sleep(2)
336
+ cb.set(uniq_id, "bar")
369
337
  end
370
338
 
371
339
  def test_multi_get_with_lock
372
340
  skip("multi_get_with_lock")
373
- if @mock.real?
374
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
375
- connection.set(uniq_id(1), "foo1")
376
- connection.set(uniq_id(2), "foo2")
377
- assert_equal ["foo1", "foo2"], connection.get([uniq_id(1), uniq_id(2)], :lock => 1)
378
- assert_raises Couchbase::Error::KeyExists do
379
- connection.set(uniq_id(1), "bar")
380
- end
381
- assert_raises Couchbase::Error::KeyExists do
382
- connection.set(uniq_id(2), "bar")
383
- end
384
- else
385
- skip("implement GETL in CouchbaseMock.jar")
341
+ cb.set(uniq_id(1), "foo1")
342
+ cb.set(uniq_id(2), "foo2")
343
+ assert_equal ["foo1", "foo2"], cb.get([uniq_id(1), uniq_id(2)], :lock => 1)
344
+ assert_raises Couchbase::Error::KeyExists do
345
+ cb.set(uniq_id(1), "bar")
346
+ end
347
+ assert_raises Couchbase::Error::KeyExists do
348
+ cb.set(uniq_id(2), "bar")
386
349
  end
387
350
  end
388
351
 
389
352
  def test_multi_get_with_custom_locks
390
353
  skip("multi_get_with_custom_locks")
391
- if @mock.real?
392
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
393
- connection.set(uniq_id(1), "foo1")
394
- connection.set(uniq_id(2), "foo2")
395
- expected = {uniq_id(1) => "foo1", uniq_id(2) => "foo2"}
396
- assert_equal expected, connection.get({uniq_id(1) => 1, uniq_id(2) => 2}, :lock => true)
397
- assert_raises Couchbase::Error::KeyExists do
398
- connection.set(uniq_id(1), "foo")
399
- end
400
- assert_raises Couchbase::Error::KeyExists do
401
- connection.set(uniq_id(2), "foo")
402
- end
403
- else
404
- skip("implement GETL in CouchbaseMock.jar")
354
+ cb.set(uniq_id(1), "foo1")
355
+ cb.set(uniq_id(2), "foo2")
356
+ expected = {uniq_id(1) => "foo1", uniq_id(2) => "foo2"}
357
+ assert_equal expected, cb.get({uniq_id(1) => 1, uniq_id(2) => 2}, :lock => true)
358
+ assert_raises Couchbase::Error::KeyExists do
359
+ cb.set(uniq_id(1), "foo")
360
+ end
361
+ assert_raises Couchbase::Error::KeyExists do
362
+ cb.set(uniq_id(2), "foo")
405
363
  end
406
364
  end
407
365
 
408
366
  def test_multi_get_result_hash_assembling
409
- connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
410
- connection.set(uniq_id(1), "foo")
411
- connection.set(uniq_id(2), "bar")
367
+ cb.set(uniq_id(1), "foo")
368
+ cb.set(uniq_id(2), "bar")
412
369
 
413
370
  expected = {uniq_id(1) => "foo", uniq_id(2) => "bar"}
414
- assert_equal expected, connection.get(uniq_id(1), uniq_id(2), :assemble_hash => true)
371
+ assert_equal expected, cb.get(uniq_id(1), uniq_id(2), :assemble_hash => true)
415
372
  end
416
373
  end