couchbase 1.1.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_cas.rb ADDED
@@ -0,0 +1,59 @@
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 TestCas < MiniTest::Unit::TestCase
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_compare_and_swap
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port,
32
+ :default_format => :document)
33
+ connection.set(uniq_id, {"bar" => 1})
34
+ connection.cas(uniq_id) do |val|
35
+ val["baz"] = 2
36
+ val
37
+ end
38
+ val = connection.get(uniq_id)
39
+ expected = {"bar" => 1, "baz" => 2}
40
+ assert_equal expected, val
41
+ end
42
+
43
+ def test_compare_and_swap_async
44
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port,
45
+ :default_format => :document)
46
+ connection.set(uniq_id, {"bar" => 1})
47
+ connection.run do |conn|
48
+ conn.cas(uniq_id) do |ret|
49
+ new_val = ret.value
50
+ new_val["baz"] = 2
51
+ new_val
52
+ end
53
+ end
54
+ val = connection.get(uniq_id)
55
+ expected = {"bar" => 1, "baz" => 2}
56
+ assert_equal expected, val
57
+ end
58
+
59
+ end
@@ -0,0 +1,28 @@
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 TestCouchbase < MiniTest::Unit::TestCase
21
+
22
+ def test_that_it_create_instance_of_bucket
23
+ with_mock do |mock|
24
+ assert_instance_of Couchbase::Bucket, Couchbase.new("http://#{mock.host}:#{mock.port}/pools/default")
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,63 @@
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::Unit::TestCase
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_delete
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+ connection.set(uniq_id, "bar")
33
+ assert connection.delete(uniq_id)
34
+ refute connection.delete(uniq_id)
35
+ end
36
+
37
+ def test_delete_missing
38
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
39
+ refute connection.delete(uniq_id(:missing))
40
+ connection.quiet = false
41
+ assert_raises(Couchbase::Error::NotFound) do
42
+ connection.delete(uniq_id(:missing))
43
+ end
44
+ refute connection.delete(uniq_id(:missing), :quiet => true)
45
+ end
46
+
47
+ def test_delete_with_cas
48
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
49
+ cas = connection.set(uniq_id, "bar")
50
+ missing_cas = cas - 1
51
+ assert_raises(Couchbase::Error::KeyExists) do
52
+ connection.delete(uniq_id, :cas => missing_cas)
53
+ end
54
+ assert connection.delete(uniq_id, :cas => cas)
55
+ end
56
+
57
+ def test_allow_fixnum_as_cas_parameter
58
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
59
+ cas = connection.set(uniq_id, "bar")
60
+ assert connection.delete(uniq_id, cas)
61
+ end
62
+
63
+ end
@@ -0,0 +1,82 @@
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
+ require 'digest/md5'
20
+
21
+ class TestErrors < MiniTest::Unit::TestCase
22
+
23
+ def setup
24
+ @mock = start_mock
25
+ end
26
+
27
+ def teardown
28
+ stop_mock(@mock)
29
+ end
30
+
31
+ def genkey(item)
32
+ tuple = [item["author"], item["message"]]
33
+ Digest::MD5.hexdigest(tuple.join('-'))
34
+ end
35
+
36
+ def test_graceful_add_with_collision
37
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
38
+ msg1 = {"author" => "foo", "message" => "hi all", "time" => "2012-01-12 11:29:09"}
39
+ key1 = uniq_id(genkey(msg1))
40
+ msg2 = {"author" => "foo", "message" => "hi all", "time" => "2012-01-12 11:29:30"}
41
+ key2 = uniq_id(genkey(msg2))
42
+
43
+ connection.add(key1, msg1)
44
+ begin
45
+ connection.add(key2, msg2)
46
+ rescue Couchbase::Error::KeyExists => ex
47
+ # using info from exception
48
+ # it could be done with cas operation, but we can save one request
49
+ # here (in real world cas operation will be more consistent because it
50
+ # fetch fresh version from the cluster)
51
+ #
52
+ # connection.cas(key2) do |msg|
53
+ # msg.merge("time" => [msg["time"], msg2["time"]])
54
+ # end
55
+ msg2 = msg1.merge("time" => [msg1["time"], msg2["time"]])
56
+ connection.set(key2, msg2, :cas => ex.cas)
57
+ end
58
+
59
+ msg3 = {"author" => "foo", "message" => "hi all",
60
+ "time" => ["2012-01-12 11:29:09", "2012-01-12 11:29:30"]}
61
+ key3 = uniq_id(genkey(msg3))
62
+ assert_equal msg3, connection.get(key3)
63
+
64
+ connection.run do |conn|
65
+ msg4 = {"author" => "foo", "message" => "hi all", "time" => "2012-01-12 11:45:34"}
66
+ key4 = uniq_id(genkey(msg4))
67
+
68
+ connection.add(key4, msg4) do |ret|
69
+ assert_equal :add, ret.operation
70
+ assert_equal key4, ret.key
71
+ msg4 = msg3.merge("time" => msg3["time"] + [msg4["time"]])
72
+ connection.set(ret.key, msg4, :cas => ret.cas)
73
+ end
74
+ end
75
+
76
+ msg5 = {"author" => "foo", "message" => "hi all",
77
+ "time" => ["2012-01-12 11:29:09", "2012-01-12 11:29:30", "2012-01-12 11:45:34"]}
78
+ key5 = uniq_id(genkey(msg5))
79
+ assert_equal msg5, connection.get(key5)
80
+ end
81
+
82
+ end
@@ -0,0 +1,49 @@
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 TestFlush < MiniTest::Unit::TestCase
21
+
22
+ def setup
23
+ @mock = start_mock(:num_nodes => 7)
24
+ end
25
+
26
+ def teardown
27
+ stop_mock(@mock)
28
+ end
29
+
30
+ def test_trivial_flush
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+ assert connection.flush
33
+ end
34
+
35
+ def test_flush_with_block
36
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
37
+ flushed = {}
38
+ on_node_flush = lambda{|res| flushed[res.node] = res.success?}
39
+ connection.run do |conn|
40
+ conn.flush(&on_node_flush)
41
+ end
42
+ assert_equal @mock.num_nodes, flushed.size
43
+ flushed.each do |node, res|
44
+ assert node.is_a?(String)
45
+ assert res
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,98 @@
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 TestFormat < MiniTest::Unit::TestCase
21
+
22
+ ArbitraryClass = Struct.new(:name, :role)
23
+ class SkinyClass < Struct.new(:name, :role)
24
+ undef to_s rescue nil
25
+ undef to_json rescue nil
26
+ end
27
+
28
+ def setup
29
+ @mock = start_mock
30
+ end
31
+
32
+ def teardown
33
+ stop_mock(@mock)
34
+ end
35
+
36
+ def test_default_document_format
37
+ orig_doc = {'name' => 'Twoflower', 'role' => 'The tourist'}
38
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
39
+ assert_equal :document, connection.default_format
40
+ connection.set(uniq_id, orig_doc)
41
+ doc, flags, cas = connection.get(uniq_id, :extended => true)
42
+ assert_equal 0x00, flags & 0x11
43
+ assert doc.is_a?(Hash)
44
+ assert_equal 'Twoflower', doc['name']
45
+ assert_equal 'The tourist', doc['role']
46
+ end
47
+
48
+ def test_it_raises_error_for_document_format_when_neither_to_json_nor_to_s_defined
49
+ orig_doc = SkinyClass.new("Twoflower", "The tourist")
50
+ refute orig_doc.respond_to?(:to_s)
51
+ refute orig_doc.respond_to?(:to_json)
52
+
53
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :document)
54
+ assert_raises(Couchbase::Error::ValueFormat) do
55
+ connection.set(uniq_id, orig_doc)
56
+ end
57
+
58
+ class << orig_doc
59
+ def to_json
60
+ JSON.dump(:name => name, :role => role)
61
+ end
62
+ end
63
+ connection.set(uniq_id, orig_doc) # OK
64
+
65
+ class << orig_doc
66
+ undef to_json
67
+ def to_s
68
+ JSON.dump(:name => name, :role => role)
69
+ end
70
+ end
71
+ connection.set(uniq_id, orig_doc) # OK
72
+ end
73
+
74
+ def test_it_could_dump_arbitrary_class_using_marshal_format
75
+ orig_doc = ArbitraryClass.new("Twoflower", "The tourist")
76
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
77
+ connection.set(uniq_id, orig_doc, :format => :marshal)
78
+ doc, flags, cas = connection.get(uniq_id, :extended => true)
79
+ assert_equal 0x01, flags & 0x11
80
+ assert doc.is_a?(ArbitraryClass)
81
+ assert_equal 'Twoflower', doc.name
82
+ assert_equal 'The tourist', doc.role
83
+ end
84
+
85
+ def test_it_accepts_only_string_in_plain_mode
86
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
87
+ connection.set(uniq_id, "1")
88
+
89
+ assert_raises(Couchbase::Error::ValueFormat) do
90
+ connection.set(uniq_id, 1)
91
+ end
92
+
93
+ assert_raises(Couchbase::Error::ValueFormat) do
94
+ connection.set(uniq_id, {:foo => "bar"})
95
+ end
96
+ end
97
+
98
+ end
data/test/test_get.rb ADDED
@@ -0,0 +1,311 @@
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::Unit::TestCase
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
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
66
+
67
+ cas1 = connection.set(uniq_id(1), "foo1")
68
+ cas2 = connection.set(uniq_id(2), "foo2")
69
+
70
+ results = connection.get(uniq_id(1), uniq_id(2), :extended => true)
71
+ assert_equal ["foo1", 0x0, cas1], results[uniq_id(1)]
72
+ assert_equal ["foo2", 0x0, cas2], results[uniq_id(2)]
73
+ end
74
+
75
+ def test_multi_get_and_touch
76
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
77
+ connection.set(uniq_id(1), "foo1")
78
+ connection.set(uniq_id(2), "foo2")
79
+
80
+ results = connection.get(uniq_id(1) => 1, uniq_id(2) => 1)
81
+ assert results.is_a?(Hash)
82
+ assert_equal "foo1", results[uniq_id(1)]
83
+ assert_equal "foo2", results[uniq_id(2)]
84
+ sleep(2)
85
+ assert connection.get(uniq_id(1), uniq_id(2)).compact.empty?
86
+ end
87
+
88
+ def test_multi_get_and_touch_extended
89
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
90
+
91
+ cas1 = connection.set(uniq_id(1), "foo1")
92
+ cas2 = connection.set(uniq_id(2), "foo2")
93
+
94
+ results = connection.get({uniq_id(1) => 1, uniq_id(2) => 1}, :extended => true)
95
+ assert_equal ["foo1", 0x0, cas1], results[uniq_id(1)]
96
+ assert_equal ["foo2", 0x0, cas2], results[uniq_id(2)]
97
+ end
98
+
99
+ def test_multi_get_and_touch_with_single_key
100
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
101
+ connection.set(uniq_id, "foo1")
102
+
103
+ results = connection.get(uniq_id => 1)
104
+ assert results.is_a?(Hash)
105
+ assert_equal "foo1", results[uniq_id]
106
+ sleep(2)
107
+ refute = connection.get(uniq_id)
108
+ end
109
+
110
+ def test_missing_in_quiet_mode
111
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
112
+ cas1 = connection.set(uniq_id(1), "foo1")
113
+ cas2 = connection.set(uniq_id(2), "foo2")
114
+
115
+ val = connection.get(uniq_id(:missing))
116
+ refute(val)
117
+ val = connection.get(uniq_id(:missing), :extended => true)
118
+ refute(val)
119
+
120
+ val1, missing, val2 = connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
121
+ assert_equal "foo1", val1
122
+ refute missing
123
+ assert_equal "foo2", val2
124
+
125
+ results = connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
126
+ assert_equal ["foo1", 0x0, cas1], results[uniq_id(1)]
127
+ refute results[uniq_id(:missing)]
128
+ assert_equal ["foo2", 0x0, cas2], results[uniq_id(2)]
129
+ end
130
+
131
+ def test_it_allows_temporary_quiet_flag
132
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => false)
133
+ assert_raises(Couchbase::Error::NotFound) do
134
+ connection.get(uniq_id(:missing))
135
+ end
136
+ refute connection.get(uniq_id(:missing), :quiet => true)
137
+ end
138
+
139
+ def test_missing_in_verbose_mode
140
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => false)
141
+ connection.set(uniq_id(1), "foo1")
142
+ connection.set(uniq_id(2), "foo2")
143
+
144
+ assert_raises(Couchbase::Error::NotFound) do
145
+ connection.get(uniq_id(:missing))
146
+ end
147
+
148
+ assert_raises(Couchbase::Error::NotFound) do
149
+ connection.get(uniq_id(:missing), :extended => true)
150
+ end
151
+
152
+ assert_raises(Couchbase::Error::NotFound) do
153
+ connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2))
154
+ end
155
+
156
+ assert_raises(Couchbase::Error::NotFound) do
157
+ connection.get(uniq_id(1), uniq_id(:missing), uniq_id(2), :extended => true)
158
+ end
159
+ end
160
+
161
+ def test_asynchronous_get
162
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
163
+ cas = connection.set(uniq_id, "foo", :flags => 0x6660)
164
+ res = []
165
+
166
+ suite = lambda do |conn|
167
+ res.clear
168
+ conn.get(uniq_id) # ignore result
169
+ conn.get(uniq_id) {|ret| res << ret}
170
+ handler = lambda {|ret| res << ret}
171
+ conn.get(uniq_id, &handler)
172
+ assert_equal 3, conn.seqno
173
+ end
174
+
175
+ checks = lambda do
176
+ res.each do |r|
177
+ assert r.is_a?(Couchbase::Result)
178
+ assert r.success?
179
+ assert_equal uniq_id, r.key
180
+ assert_equal "foo", r.value
181
+ assert_equal 0x6660, r.flags
182
+ assert_equal cas, r.cas
183
+ end
184
+ end
185
+
186
+ connection.run(&suite)
187
+ checks.call
188
+
189
+ connection.run{ suite.call(connection) }
190
+ checks.call
191
+ end
192
+
193
+ def test_asynchronous_multi_get
194
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
195
+ connection.set(uniq_id(1), "foo")
196
+ connection.set(uniq_id(2), "bar")
197
+
198
+ res = {}
199
+ connection.run do |conn|
200
+ conn.get(uniq_id(1), uniq_id(2)) {|ret| res[ret.key] = ret.value}
201
+ assert_equal 2, conn.seqno
202
+ end
203
+
204
+ assert res[uniq_id(1)]
205
+ assert_equal "foo", res[uniq_id(1)]
206
+ assert res[uniq_id(2)]
207
+ assert_equal "bar", res[uniq_id(2)]
208
+ end
209
+
210
+ def test_asynchronous_get_missing
211
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
212
+ connection.set(uniq_id, "foo")
213
+ res = {}
214
+ missing = []
215
+
216
+ get_handler = lambda do |ret|
217
+ assert_equal :get, ret.operation
218
+ if ret.success?
219
+ res[ret.key] = ret.value
220
+ else
221
+ if ret.error.is_a?(Couchbase::Error::NotFound)
222
+ missing << ret.key
223
+ else
224
+ raise ret.error
225
+ end
226
+ end
227
+ end
228
+
229
+ suite = lambda do |conn|
230
+ res.clear
231
+ missing.clear
232
+ conn.get(uniq_id(:missing1), &get_handler)
233
+ conn.get(uniq_id, uniq_id(:missing2), &get_handler)
234
+ assert 3, conn.seqno
235
+ end
236
+
237
+ connection.run(&suite)
238
+ assert_equal "foo", res[uniq_id]
239
+ assert res.has_key?(uniq_id(:missing1)) # handler was called with nil
240
+ refute res[uniq_id(:missing1)]
241
+ assert res.has_key?(uniq_id(:missing2))
242
+ refute res[uniq_id(:missing2)]
243
+ assert_empty missing
244
+
245
+ connection.quiet = false
246
+
247
+ connection.run(&suite)
248
+ refute res.has_key?(uniq_id(:missing1))
249
+ refute res.has_key?(uniq_id(:missing2))
250
+ assert_equal [uniq_id(:missing1), uniq_id(:missing2)], missing.sort
251
+ assert_equal "foo", res[uniq_id]
252
+ end
253
+
254
+ def test_get_using_brackets
255
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
256
+
257
+ orig_cas = connection.set(uniq_id, "foo", :flags => 0x1100)
258
+
259
+ val = connection[uniq_id]
260
+ assert_equal "foo", val
261
+
262
+ if RUBY_VERSION =~ /^1\.9/
263
+ eval <<-EOC
264
+ val, flags, cas = connection[uniq_id, :extended => true]
265
+ assert_equal "foo", val
266
+ assert_equal 0x1100, flags
267
+ assert_equal orig_cas, cas
268
+ EOC
269
+ end
270
+ end
271
+
272
+ def test_it_allows_to_store_nil
273
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
274
+
275
+ orig_cas = connection.set(uniq_id, nil)
276
+ assert orig_cas.is_a?(Numeric)
277
+
278
+ refute connection.get(uniq_id)
279
+ # doesn't raise NotFound exception
280
+ refute connection.get(uniq_id, :quiet => false)
281
+ # returns CAS
282
+ value, flags, cas = connection.get(uniq_id, :extended => true)
283
+ refute value
284
+ assert_equal 0x00, flags
285
+ assert_equal orig_cas, cas
286
+ end
287
+
288
+ def test_zero_length_string_is_not_nil
289
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
290
+
291
+ connection.set(uniq_id, "", :format => :document)
292
+ assert_equal "", connection.get(uniq_id)
293
+
294
+ connection.set(uniq_id, "", :format => :plain)
295
+ assert_equal "", connection.get(uniq_id)
296
+
297
+ connection.set(uniq_id, "", :format => :marshal)
298
+ assert_equal "", connection.get(uniq_id)
299
+
300
+ connection.set(uniq_id, nil, :format => :document)
301
+ assert_equal nil, connection.get(uniq_id, :quiet => false)
302
+
303
+ assert_raises Couchbase::Error::ValueFormat do
304
+ connection.set(uniq_id, nil, :format => :plain)
305
+ end
306
+
307
+ connection.set(uniq_id, nil, :format => :marshal)
308
+ assert_equal nil, connection.get(uniq_id, :quiet => false)
309
+ end
310
+
311
+ end