couchbase-jruby-client 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.ruby-version +1 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +203 -0
  6. data/README.md +347 -0
  7. data/Rakefile +10 -0
  8. data/couchbase-jruby-client.gemspec +30 -0
  9. data/lib/couchbase/async/callback.rb +19 -0
  10. data/lib/couchbase/async/queue.rb +26 -0
  11. data/lib/couchbase/async.rb +139 -0
  12. data/lib/couchbase/bucket.rb +663 -0
  13. data/lib/couchbase/cluster.rb +105 -0
  14. data/lib/couchbase/constants.rb +12 -0
  15. data/lib/couchbase/error.rb +28 -0
  16. data/lib/couchbase/jruby/couchbase_client.rb +22 -0
  17. data/lib/couchbase/jruby/future.rb +8 -0
  18. data/lib/couchbase/operations/arithmetic.rb +301 -0
  19. data/lib/couchbase/operations/delete.rb +104 -0
  20. data/lib/couchbase/operations/get.rb +298 -0
  21. data/lib/couchbase/operations/stats.rb +16 -0
  22. data/lib/couchbase/operations/store.rb +468 -0
  23. data/lib/couchbase/operations/touch.rb +123 -0
  24. data/lib/couchbase/operations/utils.rb +49 -0
  25. data/lib/couchbase/operations.rb +23 -0
  26. data/lib/couchbase/result.rb +43 -0
  27. data/lib/couchbase/transcoder.rb +83 -0
  28. data/lib/couchbase/utils.rb +62 -0
  29. data/lib/couchbase/version.rb +3 -0
  30. data/lib/couchbase/view.rb +506 -0
  31. data/lib/couchbase/view_row.rb +272 -0
  32. data/lib/couchbase.rb +177 -0
  33. data/lib/jars/commons-codec-1.5.jar +0 -0
  34. data/lib/jars/couchbase-client-1.2.0-javadoc.jar +0 -0
  35. data/lib/jars/couchbase-client-1.2.0-sources.jar +0 -0
  36. data/lib/jars/couchbase-client-1.2.0.jar +0 -0
  37. data/lib/jars/httpcore-4.1.1.jar +0 -0
  38. data/lib/jars/httpcore-nio-4.1.1.jar +0 -0
  39. data/lib/jars/jettison-1.1.jar +0 -0
  40. data/lib/jars/netty-3.5.5.Final.jar +0 -0
  41. data/lib/jars/spymemcached-2.10.0-javadoc.jar +0 -0
  42. data/lib/jars/spymemcached-2.10.0-sources.jar +0 -0
  43. data/lib/jars/spymemcached-2.10.0.jar +0 -0
  44. data/test/profile/.gitignore +1 -0
  45. data/test/profile/Gemfile +6 -0
  46. data/test/profile/benchmark.rb +195 -0
  47. data/test/setup.rb +201 -0
  48. data/test/test_arithmetic.rb +177 -0
  49. data/test/test_async.rb +324 -0
  50. data/test/test_bucket.rb +213 -0
  51. data/test/test_cas.rb +78 -0
  52. data/test/test_couchbase.rb +29 -0
  53. data/test/test_couchbase_rails_cache_store.rb +341 -0
  54. data/test/test_delete.rb +125 -0
  55. data/test/test_errors.rb +82 -0
  56. data/test/test_format.rb +161 -0
  57. data/test/test_get.rb +417 -0
  58. data/test/test_stats.rb +57 -0
  59. data/test/test_store.rb +216 -0
  60. data/test/test_timer.rb +42 -0
  61. data/test/test_touch.rb +97 -0
  62. data/test/test_unlock.rb +119 -0
  63. data/test/test_utils.rb +58 -0
  64. data/test/test_version.rb +52 -0
  65. metadata +226 -0
data/test/test_get.rb ADDED
@@ -0,0 +1,417 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class TestGet < MiniTest::Test
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ 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)
34
+ assert_equal "bar", val
35
+ end
36
+
37
+ 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)
42
+ assert_equal "bar", val
43
+ #assert_equal 0x0, flags
44
+ assert_equal orig_cas, cas
45
+
46
+ orig_cas = connection.set(uniq_id, "bar", :flags => 0x1000)
47
+ val, flags, cas = connection.get(uniq_id, :extended => true)
48
+ assert_equal "bar", val
49
+ #assert_equal 0x1000, flags
50
+ assert_equal orig_cas, cas
51
+ end
52
+
53
+ 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")
58
+
59
+ val1, val2 = connection.get(uniq_id(1), uniq_id(2))
60
+ assert_equal "foo1", val1
61
+ assert_equal "foo2", val2
62
+ end
63
+
64
+ def test_multi_get_extended
65
+ skip 'Looking like Java client doesnt support multi_get_extended'
66
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
67
+
68
+ cas1 = connection.set(uniq_id(1), "foo1")
69
+ cas2 = connection.set(uniq_id(2), "foo2")
70
+
71
+ results = connection.get(uniq_id(1), uniq_id(2), :extended => true)
72
+ assert_equal ["foo1", nil, cas1], results[uniq_id(1)]
73
+ assert_equal ["foo2", nil, cas2], results[uniq_id(2)]
74
+ end
75
+
76
+ 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")
80
+
81
+ results = connection.get(uniq_id(1) => 1, uniq_id(2) => 1)
82
+ assert results.is_a?(Hash)
83
+ assert_equal "foo1", results[uniq_id(1)]
84
+ assert_equal "foo2", results[uniq_id(2)]
85
+ sleep(2)
86
+ assert_raises(Couchbase::Error::NotFound) do
87
+ connection.get(uniq_id(1), uniq_id(2))
88
+ end
89
+ assert connection.get(uniq_id(1), uniq_id(2), :quiet => true).compact.empty?
90
+ end
91
+
92
+ def test_multi_get_and_touch_extended
93
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
94
+
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)
99
+ assert_equal ["foo1", nil, cas1], results[uniq_id(1)]
100
+ assert_equal ["foo2", nil, cas2], results[uniq_id(2)]
101
+ end
102
+
103
+ 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")
106
+
107
+ results = connection.get(uniq_id => 1)
108
+ assert results.is_a?(Hash)
109
+ assert_equal "foo1", results[uniq_id]
110
+ sleep(2)
111
+ assert_raises(Couchbase::Error::NotFound) do
112
+ connection.get(uniq_id)
113
+ end
114
+ end
115
+
116
+ def test_missing_in_quiet_mode
117
+ skip
118
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => true)
119
+ cas1 = connection.set(uniq_id(1), "foo1")
120
+ cas2 = connection.set(uniq_id(2), "foo2")
121
+
122
+ val = connection.get(uniq_id(:missing))
123
+ refute(val)
124
+ val = connection.get(uniq_id(:missing), :extended => true)
125
+ refute(val)
126
+
127
+ val1, missing, val2 = connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
128
+ assert_equal "foo1", val1
129
+ refute missing
130
+ assert_equal "foo2", val2
131
+
132
+ # TODO: multi get with cas
133
+ # results = connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
134
+ # assert_equal ["foo1", nil, cas1], results[uniq_id(1)]
135
+ # refute results[uniq_id(:missing)]
136
+ # assert_equal ["foo2", nil, cas2], results[uniq_id(2)]
137
+ end
138
+
139
+ def test_it_allows_temporary_quiet_flag
140
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => false)
141
+ assert_raises(Couchbase::Error::NotFound) do
142
+ connection.get(uniq_id(:missing))
143
+ end
144
+ refute connection.get(uniq_id(:missing), :quiet => true)
145
+ end
146
+
147
+ def test_missing_in_verbose_mode
148
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => false)
149
+ connection.set(uniq_id(1), "foo1")
150
+ connection.set(uniq_id(2), "foo2")
151
+
152
+ assert_raises(Couchbase::Error::NotFound) do
153
+ connection.get(uniq_id(:missing))
154
+ end
155
+
156
+ assert_raises(Couchbase::Error::NotFound) do
157
+ connection.get(uniq_id(:missing), :extended => true)
158
+ end
159
+
160
+ assert_raises(Couchbase::Error::NotFound) do
161
+ connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
162
+ end
163
+
164
+ assert_raises(Couchbase::Error::NotFound) do
165
+ connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
166
+ end
167
+ end
168
+
169
+ def test_asynchronous_get
170
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
171
+ cas = connection.set(uniq_id, "foo")
172
+ res = []
173
+
174
+ suite = lambda do |conn|
175
+ res.clear
176
+ conn.get(uniq_id) # ignore result
177
+ conn.get(uniq_id) {|ret| res << ret}
178
+ handler = lambda {|ret| res << ret}
179
+ conn.get(uniq_id, &handler)
180
+ end
181
+
182
+ checks = lambda do
183
+ res.each do |r|
184
+ assert r.is_a?(Couchbase::Result)
185
+ assert r.success?
186
+ assert_equal uniq_id, r.key
187
+ assert_equal "foo", r.value
188
+ # assert_equal cas, r.cas TODO: GetFuture does not hold CAS
189
+ end
190
+ end
191
+
192
+ connection.run(&suite)
193
+ checks.call
194
+
195
+ connection.run{ suite.call(connection) }
196
+ checks.call
197
+ end
198
+
199
+ def test_asynchronous_multi_get
200
+ skip
201
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
202
+ connection.set(uniq_id(1), "foo")
203
+ connection.set(uniq_id(2), "bar")
204
+
205
+ res = {}
206
+ connection.run do |conn|
207
+ conn.get(uniq_id(1), uniq_id(2)) {|ret| res[ret.key] = ret.value}
208
+ end
209
+
210
+ assert res[uniq_id(1)]
211
+ assert_equal "foo", res[uniq_id(1)]
212
+ assert res[uniq_id(2)]
213
+ assert_equal "bar", res[uniq_id(2)]
214
+ end
215
+
216
+ def test_asynchronous_get_missing
217
+ skip
218
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
219
+ connection.set(uniq_id, "foo")
220
+ res = {}
221
+ missing = []
222
+
223
+ get_handler = lambda do |ret|
224
+ assert_equal :get, ret.operation
225
+ if ret.success?
226
+ res[ret.key] = ret.value
227
+ else
228
+ if ret.error.is_a?(Couchbase::Error::NotFound)
229
+ missing << ret.key
230
+ else
231
+ raise ret.error
232
+ end
233
+ end
234
+ end
235
+
236
+ suite = lambda do |conn|
237
+ res.clear
238
+ missing.clear
239
+ conn.get(uniq_id(:missing1), &get_handler)
240
+ conn.get(uniq_id, uniq_id(:missing2), &get_handler)
241
+ end
242
+
243
+ connection.run(&suite)
244
+ refute res.has_key?(uniq_id(:missing1))
245
+ refute res.has_key?(uniq_id(:missing2))
246
+ assert_equal [uniq_id(:missing1), uniq_id(:missing2)], missing.sort
247
+ assert_equal "foo", res[uniq_id]
248
+
249
+ connection.quiet = true
250
+ connection.run(&suite)
251
+ assert_equal "foo", res[uniq_id]
252
+ assert res.has_key?(uniq_id(:missing1)) # handler was called with nil
253
+ refute res[uniq_id(:missing1)]
254
+ assert res.has_key?(uniq_id(:missing2))
255
+ refute res[uniq_id(:missing2)]
256
+ assert_empty missing
257
+ end
258
+
259
+ def test_get_using_brackets
260
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
261
+
262
+ orig_cas = connection.set(uniq_id, "foo", :flags => 0x1100)
263
+
264
+ val = connection[uniq_id]
265
+ assert_equal "foo", val
266
+
267
+ if RUBY_VERSION =~ /^1\.9/
268
+ eval <<-EOC
269
+ val, flags, cas = connection[uniq_id, :extended => true]
270
+ assert_equal "foo", val
271
+ #assert_equal 0x1100, flags
272
+ assert_equal orig_cas, cas
273
+ EOC
274
+ end
275
+ end
276
+
277
+ def test_it_allows_to_store_nil
278
+ skip "TODO: figure out nil storage"
279
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
280
+
281
+ orig_cas = connection.set(uniq_id, nil)
282
+ assert orig_cas.is_a?(Numeric)
283
+
284
+ refute connection.get(uniq_id)
285
+ # doesn't raise NotFound exception
286
+ refute connection.get(uniq_id, :quiet => false)
287
+ # returns CAS
288
+ value, flags, cas = connection.get(uniq_id, :extended => true)
289
+ refute value
290
+ #assert_equal 0x00, flags
291
+ assert_equal orig_cas, cas
292
+ end
293
+
294
+ def test_zero_length_string_is_not_nil
295
+ skip("zero length string")
296
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
297
+
298
+ connection.set(uniq_id, "", :format => :document)
299
+ assert_equal "", connection.get(uniq_id)
300
+
301
+ connection.set(uniq_id, "", :format => :plain)
302
+ assert_equal "", connection.get(uniq_id)
303
+
304
+ connection.set(uniq_id, "", :format => :marshal)
305
+ assert_equal "", connection.get(uniq_id)
306
+
307
+ connection.set(uniq_id, nil, :format => :document)
308
+ assert_equal nil, connection.get(uniq_id, :quiet => false)
309
+
310
+ assert_raises Couchbase::Error::ValueFormat do
311
+ connection.set(uniq_id, nil, :format => :plain)
312
+ end
313
+
314
+ connection.set(uniq_id, nil, :format => :marshal)
315
+ assert_equal nil, connection.get(uniq_id, :quiet => false)
316
+ end
317
+
318
+ def test_format_forcing
319
+ skip("format forcing")
320
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
321
+
322
+ connection.set(uniq_id, '{"foo":"bar"}', :format => :plain)
323
+ value, flags, _ = connection.get(uniq_id, :extended => true)
324
+ assert_equal '{"foo":"bar"}', value
325
+ assert_equal 0x02, flags
326
+
327
+ value, flags, _ = connection.get(uniq_id, :extended => true, :format => :document)
328
+ expected = {"foo" => "bar"}
329
+ assert_equal expected, value
330
+ assert_equal 0x02, flags
331
+
332
+ connection.prepend(uniq_id, "NOT-A-JSON")
333
+ assert_raises Couchbase::Error::ValueFormat do
334
+ connection.get(uniq_id, :format => :document)
335
+ end
336
+ end
337
+
338
+ # http://www.couchbase.com/issues/browse/RCBC-31
339
+ def test_consistent_behaviour_for_arrays
340
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
341
+
342
+ cas = connection.set(uniq_id("foo"), "foo")
343
+ connection.set(uniq_id("bar"), "bar")
344
+
345
+ assert_equal "foo", connection.get(uniq_id("foo"))
346
+ assert_equal ["foo"], connection.get([uniq_id("foo")])
347
+ assert_equal ["foo", "bar"], connection.get([uniq_id("foo"), uniq_id("bar")])
348
+ assert_equal ["foo", "bar"], connection.get(uniq_id("foo"), uniq_id("bar"))
349
+ # expected = {uniq_id("foo") => ["foo", nil, cas]}
350
+ # assert_equal expected, connection.get([uniq_id("foo")], :extended => true)
351
+ assert_raises TypeError do
352
+ connection.get([uniq_id("foo"), uniq_id("bar")], [uniq_id("foo")])
353
+ end
354
+ end
355
+
356
+ def test_get_with_lock_trivial
357
+ if @mock.real?
358
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
359
+ connection.set(uniq_id, "foo")
360
+
361
+ assert_equal "foo", connection.get(uniq_id, :lock => 1)
362
+ assert_raises Couchbase::Error::KeyExists do
363
+ connection.set(uniq_id, "bar")
364
+ end
365
+ sleep(2)
366
+ connection.set(uniq_id, "bar")
367
+ else
368
+ skip("implement GETL in CouchbaseMock.jar")
369
+ end
370
+ end
371
+
372
+ def test_multi_get_with_lock
373
+ skip("multi_get_with_lock")
374
+ if @mock.real?
375
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
376
+ connection.set(uniq_id(1), "foo1")
377
+ connection.set(uniq_id(2), "foo2")
378
+ assert_equal ["foo1", "foo2"], connection.get([uniq_id(1), uniq_id(2)], :lock => 1)
379
+ assert_raises Couchbase::Error::KeyExists do
380
+ connection.set(uniq_id(1), "bar")
381
+ end
382
+ assert_raises Couchbase::Error::KeyExists do
383
+ connection.set(uniq_id(2), "bar")
384
+ end
385
+ else
386
+ skip("implement GETL in CouchbaseMock.jar")
387
+ end
388
+ end
389
+
390
+ def test_multi_get_with_custom_locks
391
+ skip("multi_get_with_custom_locks")
392
+ if @mock.real?
393
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
394
+ connection.set(uniq_id(1), "foo1")
395
+ connection.set(uniq_id(2), "foo2")
396
+ expected = {uniq_id(1) => "foo1", uniq_id(2) => "foo2"}
397
+ assert_equal expected, connection.get({uniq_id(1) => 1, uniq_id(2) => 2}, :lock => true)
398
+ assert_raises Couchbase::Error::KeyExists do
399
+ connection.set(uniq_id(1), "foo")
400
+ end
401
+ assert_raises Couchbase::Error::KeyExists do
402
+ connection.set(uniq_id(2), "foo")
403
+ end
404
+ else
405
+ skip("implement GETL in CouchbaseMock.jar")
406
+ end
407
+ end
408
+
409
+ def test_multi_get_result_hash_assembling
410
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
411
+ connection.set(uniq_id(1), "foo")
412
+ connection.set(uniq_id(2), "bar")
413
+
414
+ expected = {uniq_id(1) => "foo", uniq_id(2) => "bar"}
415
+ assert_equal expected, connection.get(uniq_id(1), uniq_id(2), :assemble_hash => true)
416
+ end
417
+ end
@@ -0,0 +1,57 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class TestStats < MiniTest::Test
21
+
22
+ def setup
23
+ @mock = start_mock(:num_nodes => 4)
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_trivial_stats_without_argument
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+ stats = connection.stats
33
+ assert stats.is_a?(Hash)
34
+ assert stats.has_key?("pid")
35
+ key, info = stats.first
36
+ assert key.is_a?(String)
37
+ assert info.is_a?(Hash)
38
+ assert_equal @mock.num_nodes, info.size
39
+ end
40
+
41
+ def test_stats_with_argument
42
+ if @mock.real?
43
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
44
+ stats = connection.stats("memory")
45
+ assert stats.is_a?(Hash)
46
+ assert stats.has_key?("mem_used")
47
+ key, info = stats.first
48
+ assert key.is_a?(String)
49
+ assert info.is_a?(Hash)
50
+ assert_equal @mock.num_nodes, info.size
51
+ else
52
+ # FIXME
53
+ skip("make CouchbaseMock.jar STATS more real-life")
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,216 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 2012 Couchbase, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class TestStore < MiniTest::Test
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_trivial_set
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+ cas = connection.set(uniq_id, "bar")
33
+ assert(cas > 0)
34
+ end
35
+
36
+ def test_set_with_cas
37
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
38
+
39
+ cas1 = connection.set(uniq_id, "bar1")
40
+ assert cas1 > 0
41
+
42
+ assert_raises(Couchbase::Error::KeyExists) do
43
+ connection.set(uniq_id, "bar2", :cas => cas1+1)
44
+ end
45
+
46
+ cas2 = connection.set(uniq_id, "bar2", :cas => cas1)
47
+ assert cas2 > 0
48
+ refute_equal cas2, cas1
49
+
50
+ cas3 = connection.set(uniq_id, "bar3")
51
+ assert cas3 > 0
52
+ refute_equal cas3, cas2
53
+ refute_equal cas3, cas1
54
+ end
55
+
56
+ def test_add
57
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
58
+
59
+ cas1 = connection.add(uniq_id, "bar")
60
+ assert cas1 > 0
61
+
62
+ assert_raises(Couchbase::Error::KeyExists) do
63
+ connection.add(uniq_id, "bar")
64
+ end
65
+
66
+ assert_raises(Couchbase::Error::KeyExists) do
67
+ connection.add(uniq_id, "bar", :cas => cas1)
68
+ end
69
+ end
70
+
71
+ def test_replace
72
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
73
+
74
+ assert_raises(Couchbase::Error::NotFound) do
75
+ connection.replace(uniq_id, "bar")
76
+ end
77
+
78
+ cas1 = connection.set(uniq_id, "bar")
79
+ assert cas1 > 0
80
+
81
+ connection.replace(uniq_id, "bar")
82
+ end
83
+
84
+ def test_acceptable_keys
85
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
86
+
87
+ cas = connection.set(uniq_id.to_sym, "bar")
88
+ assert cas > 0
89
+
90
+ cas = connection.set(uniq_id.to_s, "bar")
91
+ assert cas > 0
92
+
93
+ assert_raises(TypeError) do
94
+ connection.set(nil, "bar")
95
+ end
96
+
97
+ obj = {:foo => "bar", :baz => 1}
98
+ assert_raises(TypeError) do
99
+ connection.set(obj, "bar")
100
+ end
101
+
102
+ class << obj
103
+ alias :to_str :to_s
104
+ end
105
+
106
+ connection.set(obj, "bar")
107
+ assert cas > 0
108
+ end
109
+
110
+ def test_asynchronous_set
111
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
112
+ ret = nil
113
+ connection.run do |conn|
114
+ conn.set(uniq_id("1"), "foo1") {|res| ret = res}
115
+ conn.set(uniq_id("2"), "foo2") # ignore result
116
+ end
117
+
118
+ assert ret.is_a?(Couchbase::Result)
119
+ assert ret.success?
120
+ assert_equal uniq_id("1"), ret.key
121
+ assert_equal :set, ret.operation
122
+ assert ret.cas.is_a?(Numeric)
123
+ end
124
+
125
+ def test_it_raises_error_when_appending_or_prepending_to_missing_key
126
+ skip
127
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
128
+
129
+ assert_raises(Couchbase::Error::NotStored) do
130
+ connection.append(uniq_id(:missing), "foo")
131
+ end
132
+
133
+ assert_raises(Couchbase::Error::NotStored) do
134
+ connection.prepend(uniq_id(:missing), "foo")
135
+ end
136
+ end
137
+
138
+ def test_append
139
+ skip
140
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
141
+
142
+ cas1 = connection.set(uniq_id, "foo")
143
+ assert cas1 > 0
144
+ cas2 = connection.append(uniq_id, "bar")
145
+ assert cas2 > 0
146
+ refute_equal cas2, cas1
147
+
148
+ val = connection.get(uniq_id)
149
+ assert_equal "foobar", val
150
+ end
151
+
152
+ def test_prepend
153
+ skip
154
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
155
+
156
+ cas1 = connection.set(uniq_id, "foo")
157
+ assert cas1 > 0
158
+ cas2 = connection.prepend(uniq_id, "bar")
159
+ assert cas2 > 0
160
+ refute_equal cas2, cas1
161
+
162
+ val = connection.get(uniq_id)
163
+ assert_equal "barfoo", val
164
+ end
165
+
166
+ def test_set_with_prefix
167
+ skip
168
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :key_prefix => "prefix:")
169
+ connection.set(uniq_id(:foo), "bar")
170
+ assert_equal "bar", connection.get(uniq_id(:foo))
171
+ expected = {uniq_id(:foo) => "bar"}
172
+ assert_equal expected, connection.get(uniq_id(:foo), :assemble_hash => true)
173
+
174
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :key_prefix => nil)
175
+ expected = {"prefix:#{uniq_id(:foo)}" => "bar"}
176
+ assert_equal expected, connection.get("prefix:#{uniq_id(:foo)}", :assemble_hash => true)
177
+ end
178
+
179
+ ArbitraryData = Struct.new(:baz)
180
+
181
+ def test_set_using_brackets
182
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
183
+
184
+ connection[uniq_id(1)] = "foo"
185
+ val = connection.get(uniq_id(1))
186
+ assert_equal "foo", val
187
+
188
+ # if RUBY_VERSION =~ /^1\.9/
189
+ # eval <<-EOC
190
+ # connection[uniq_id(3), :format => :marshal] = ArbitraryData.new("thing")
191
+ # val = connection.get(uniq_id(3))
192
+ # assert val.is_a?(ArbitraryData)
193
+ # assert_equal "thing", val.baz
194
+ # EOC
195
+ # end
196
+ end
197
+
198
+ def test_multi_store
199
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
200
+ connection.add(uniq_id(:a) => "bbb", uniq_id(:z) => "yyy")
201
+ assert_equal ["bbb", "yyy"], connection.get(uniq_id(:a), uniq_id(:z))
202
+
203
+ # connection.prepend(uniq_id(:a) => "aaa", uniq_id(:z) => "xxx")
204
+ # assert_equal ["aaabbb", "xxxyyy"], connection.get(uniq_id(:a), uniq_id(:z))
205
+
206
+ # connection.append(uniq_id(:a) => "ccc", uniq_id(:z) => "zzz")
207
+ # assert_equal ["aaabbbccc", "xxxyyyzzz"], connection.get(uniq_id(:a), uniq_id(:z))
208
+
209
+ # connection.replace(uniq_id(:a) => "foo", uniq_id(:z) => "bar")
210
+ # assert_equal ["foo", "bar"], connection.get(uniq_id(:a), uniq_id(:z))
211
+
212
+ res = connection.set(uniq_id(:a) => "bar", uniq_id(:z) => "foo")
213
+ assert_equal ["bar", "foo"], connection.get(uniq_id(:a), uniq_id(:z))
214
+ assert res.is_a?(Hash)
215
+ end
216
+ end