couchbase-jruby-client 0.1.0-java → 0.1.5-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.jrubyrc +722 -0
- data/.ruby-version +1 -1
- data/README.md +12 -90
- data/couchbase-jruby-client.gemspec +6 -6
- data/lib/couchbase/async.rb +18 -0
- data/lib/couchbase/bucket.rb +90 -180
- data/lib/couchbase/constants.rb +17 -0
- data/lib/couchbase/design_doc.rb +83 -0
- data/lib/couchbase/error.rb +31 -0
- data/lib/couchbase/operations/arithmetic.rb +17 -0
- data/lib/couchbase/operations/delete.rb +17 -0
- data/lib/couchbase/operations/design_docs.rb +99 -0
- data/lib/couchbase/operations/get.rb +73 -67
- data/lib/couchbase/operations/stats.rb +28 -1
- data/lib/couchbase/operations/store.rb +114 -97
- data/lib/couchbase/operations/touch.rb +49 -19
- data/lib/couchbase/operations/unlock.rb +209 -0
- data/lib/couchbase/operations/utils.rb +22 -10
- data/lib/couchbase/operations.rb +21 -0
- data/lib/couchbase/query.rb +92 -0
- data/lib/couchbase/result.rb +18 -1
- data/lib/couchbase/transcoder.rb +36 -42
- data/lib/couchbase/version.rb +18 -1
- data/lib/couchbase/view.rb +30 -172
- data/lib/couchbase/view_row.rb +38 -98
- data/lib/couchbase.rb +74 -72
- data/test/profile/.jrubyrc +722 -0
- data/test/profile/Gemfile +5 -5
- data/test/profile/benchmark.rb +106 -124
- data/test/profile/profile.rb +59 -0
- data/test/setup.rb +10 -145
- data/test/test_arithmetic.rb +54 -77
- data/test/test_async.rb +74 -102
- data/test/test_bucket.rb +74 -60
- data/test/test_cas.rb +10 -23
- data/test/test_couchbase.rb +11 -3
- data/test/test_delete.rb +41 -43
- data/test/test_design_docs.rb +62 -0
- data/test/test_errors.rb +9 -18
- data/test/test_format.rb +21 -31
- data/test/test_get.rb +107 -151
- data/test/test_query.rb +23 -0
- data/test/test_stats.rb +9 -24
- data/test/test_store.rb +52 -65
- data/test/test_timer.rb +4 -12
- data/test/test_touch.rb +26 -33
- data/test/test_unlock.rb +47 -78
- data/test/test_utils.rb +2 -11
- data/test/test_version.rb +5 -14
- data/test/test_view.rb +87 -0
- metadata +27 -14
- data/lib/couchbase/jruby/couchbase_client.rb +0 -22
- data/lib/couchbase/jruby/future.rb +0 -8
data/test/test_bucket.rb
CHANGED
@@ -20,31 +20,32 @@ require File.join(File.dirname(__FILE__), 'setup')
|
|
20
20
|
class TestBucket < MiniTest::Test
|
21
21
|
|
22
22
|
def test_it_substitute_default_parts_to_url
|
23
|
-
|
24
|
-
with_mock(:host => 'localhost') do |mock| # pick first free port
|
23
|
+
with_configs(:host => 'localhost') do |configs| # pick first free port
|
25
24
|
connections = [
|
26
|
-
Couchbase.new("http://#{
|
27
|
-
Couchbase.new(:port =>
|
28
|
-
Couchbase.new("http://#{
|
25
|
+
Couchbase.new("http://#{configs.host}:#{configs.port}"),
|
26
|
+
Couchbase.new(:port => configs.port),
|
27
|
+
Couchbase.new("http://#{configs.host}:8091", :port => configs.port)
|
29
28
|
]
|
30
29
|
connections.each do |connection|
|
31
|
-
assert_equal
|
32
|
-
assert_equal "#{
|
33
|
-
assert_equal "http://#{
|
30
|
+
assert_equal configs.port, connection.port
|
31
|
+
assert_equal "#{configs.host}:#{configs.port}", connection.authority
|
32
|
+
assert_equal "http://#{configs.host}:#{configs.port}/pools/default/buckets/default/", connection.url
|
34
33
|
end
|
34
|
+
connections.each(&:disconnect)
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
with_configs(:host => '127.0.0.1') do |configs|
|
38
38
|
connections = [
|
39
|
-
Couchbase.new("http://#{
|
40
|
-
Couchbase.new(:hostname =>
|
41
|
-
Couchbase.new('http://example.com:8091', :hostname =>
|
39
|
+
Couchbase.new("http://#{configs.host}:#{configs.port}"),
|
40
|
+
Couchbase.new(:hostname => configs.host, :port => configs.port),
|
41
|
+
Couchbase.new('http://example.com:8091', :hostname => configs.host, :port => configs.port)
|
42
42
|
]
|
43
43
|
connections.each do |connection|
|
44
|
-
assert_equal
|
45
|
-
assert_equal "#{
|
46
|
-
assert_equal "http://#{
|
44
|
+
assert_equal configs.host, connection.hostname
|
45
|
+
assert_equal "#{configs.host}:#{configs.port}", connection.authority
|
46
|
+
assert_equal "http://#{configs.host}:#{configs.port}/pools/default/buckets/default/", connection.url
|
47
47
|
end
|
48
|
+
connections.each(&:disconnect)
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
@@ -70,32 +71,36 @@ class TestBucket < MiniTest::Test
|
|
70
71
|
end
|
71
72
|
|
72
73
|
def test_it_able_to_connect_to_protected_buckets
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
skip
|
75
|
+
with_configs(:buckets_spec => 'protected:secret') do |configs|
|
76
|
+
connection = Couchbase.new(:hostname => configs.host,
|
77
|
+
:port => configs.port,
|
76
78
|
:bucket => 'protected',
|
77
79
|
:username => 'protected',
|
78
80
|
:password => 'secret')
|
79
81
|
assert_equal "protected", connection.bucket
|
80
82
|
assert_equal "protected", connection.username
|
81
83
|
assert_equal "secret", connection.password
|
84
|
+
connection.disconnect
|
82
85
|
end
|
83
86
|
end
|
84
87
|
|
85
88
|
def test_it_allows_to_specify_credentials_in_url
|
86
|
-
|
87
|
-
|
89
|
+
skip
|
90
|
+
with_configs(:buckets_spec => 'protected:secret') do |configs|
|
91
|
+
connection = Couchbase.new("http://protected:secret@#{configs.host}:#{configs.port}/pools/default/buckets/protected/")
|
88
92
|
assert_equal "protected", connection.bucket
|
89
93
|
assert_equal "protected", connection.username
|
90
94
|
assert_equal "secret", connection.password
|
95
|
+
connection.disconnect
|
91
96
|
end
|
92
97
|
end
|
93
98
|
|
94
99
|
def test_it_raises_error_with_wrong_credentials
|
95
|
-
|
100
|
+
with_configs do |configs|
|
96
101
|
assert_raises Couchbase::Error::Auth do
|
97
|
-
Couchbase.new(:hostname =>
|
98
|
-
:port =>
|
102
|
+
Couchbase.new(:hostname => configs.host,
|
103
|
+
:port => configs.port,
|
99
104
|
:bucket => 'default',
|
100
105
|
:username => 'wrong.username',
|
101
106
|
:password => 'wrong_password')
|
@@ -104,17 +109,18 @@ class TestBucket < MiniTest::Test
|
|
104
109
|
end
|
105
110
|
|
106
111
|
def test_it_unable_to_connect_to_protected_buckets_with_wrong_credentials
|
107
|
-
|
112
|
+
skip
|
113
|
+
with_configs(:buckets_spec => 'protected:secret') do |configs|
|
108
114
|
assert_raises Couchbase::Error::Auth do
|
109
|
-
Couchbase.new(:hostname =>
|
110
|
-
:port =>
|
115
|
+
Couchbase.new(:hostname => configs.host,
|
116
|
+
:port => configs.port,
|
111
117
|
:bucket => 'protected',
|
112
118
|
:username => 'wrong',
|
113
119
|
:password => 'secret')
|
114
120
|
end
|
115
121
|
assert_raises Couchbase::Error::Auth do
|
116
|
-
Couchbase.new(:hostname =>
|
117
|
-
:port =>
|
122
|
+
Couchbase.new(:hostname => configs.host,
|
123
|
+
:port => configs.port,
|
118
124
|
:bucket => 'protected',
|
119
125
|
:username => 'protected',
|
120
126
|
:password => 'wrong')
|
@@ -123,14 +129,15 @@ class TestBucket < MiniTest::Test
|
|
123
129
|
end
|
124
130
|
|
125
131
|
def test_it_allows_change_quiet_flag
|
126
|
-
|
127
|
-
connection = Couchbase.new(:hostname =>
|
128
|
-
:port =>
|
132
|
+
with_configs do |configs|
|
133
|
+
connection = Couchbase.new(:hostname => configs.host,
|
134
|
+
:port => configs.port)
|
129
135
|
|
130
136
|
refute connection.quiet?
|
131
137
|
|
132
|
-
connection
|
133
|
-
|
138
|
+
connection.disconnect
|
139
|
+
connection = Couchbase.new(:hostname => configs.host,
|
140
|
+
:port => configs.port,
|
134
141
|
:quiet => true)
|
135
142
|
assert connection.quiet?
|
136
143
|
|
@@ -139,30 +146,32 @@ class TestBucket < MiniTest::Test
|
|
139
146
|
|
140
147
|
connection.quiet = :foo
|
141
148
|
assert_equal true, connection.quiet?
|
149
|
+
connection.disconnect
|
142
150
|
end
|
143
151
|
end
|
144
152
|
|
145
153
|
def test_it_is_connected
|
146
|
-
|
147
|
-
connection = Couchbase.new(:hostname =>
|
148
|
-
:port =>
|
154
|
+
with_configs do |configs|
|
155
|
+
connection = Couchbase.new(:hostname => configs.host,
|
156
|
+
:port => configs.port)
|
149
157
|
assert connection.connected?
|
158
|
+
connection.disconnect
|
150
159
|
end
|
151
160
|
end
|
152
161
|
|
153
162
|
def test_it_is_possible_to_disconnect_instance
|
154
|
-
|
155
|
-
connection = Couchbase.new(:hostname =>
|
156
|
-
:port =>
|
163
|
+
with_configs do |configs|
|
164
|
+
connection = Couchbase.new(:hostname => configs.host,
|
165
|
+
:port => configs.port)
|
157
166
|
connection.disconnect
|
158
167
|
refute connection.connected?
|
159
168
|
end
|
160
169
|
end
|
161
170
|
|
162
171
|
def test_it_raises_error_on_double_disconnect
|
163
|
-
|
164
|
-
connection = Couchbase.new(:hostname =>
|
165
|
-
:port =>
|
172
|
+
with_configs do |configs|
|
173
|
+
connection = Couchbase.new(:hostname => configs.host,
|
174
|
+
:port => configs.port)
|
166
175
|
connection.disconnect
|
167
176
|
assert_raises Couchbase::Error::Connect do
|
168
177
|
connection.disconnect
|
@@ -171,43 +180,48 @@ class TestBucket < MiniTest::Test
|
|
171
180
|
end
|
172
181
|
|
173
182
|
def test_it_allows_to_reconnect_the_instance
|
174
|
-
|
175
|
-
connection = Couchbase.new(:hostname =>
|
176
|
-
:port =>
|
183
|
+
with_configs do |configs|
|
184
|
+
connection = Couchbase.new(:hostname => configs.host,
|
185
|
+
:port => configs.port)
|
177
186
|
connection.disconnect
|
178
187
|
refute connection.connected?
|
179
188
|
connection.reconnect
|
180
189
|
assert connection.connected?
|
181
190
|
assert connection.set(uniq_id, "foo")
|
191
|
+
connection.disconnect
|
182
192
|
end
|
183
193
|
end
|
184
194
|
|
185
195
|
def test_it_allows_to_change_configuration_during_reconnect
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
:username => 'protected',
|
191
|
-
:password => 'secret')
|
196
|
+
skip
|
197
|
+
with_configs do |configs|
|
198
|
+
connection = Couchbase.new(:quiet => true)
|
199
|
+
assert connection.quiet?
|
192
200
|
connection.disconnect
|
193
|
-
assert_raises Couchbase::Error::Auth do
|
194
|
-
connection.reconnect(:password => 'incorrect')
|
195
|
-
end
|
196
|
-
refute connection.connected?
|
197
201
|
|
198
|
-
connection.reconnect(:
|
199
|
-
|
202
|
+
connection.reconnect(:quiet => false)
|
203
|
+
refute connection.quiet?
|
204
|
+
connection.disconnect
|
200
205
|
end
|
201
206
|
end
|
202
207
|
|
203
208
|
def test_it_uses_bucket_name_as_username_if_username_is_empty
|
204
|
-
|
205
|
-
|
206
|
-
|
209
|
+
skip
|
210
|
+
with_configs(:buckets_spec => 'protected:secret') do |configs|
|
211
|
+
connection = Couchbase.new(:hostname => configs.host,
|
212
|
+
:port => configs.port,
|
207
213
|
:bucket => 'protected',
|
208
214
|
:password => 'secret')
|
209
215
|
assert connection.connected?
|
216
|
+
connection.disconnect
|
210
217
|
end
|
211
218
|
end
|
212
219
|
|
220
|
+
def test_it_converts_options_keys_to_symbols
|
221
|
+
bucket = Couchbase::Bucket.new('quiet' => true, 'default_ttl' => 10)
|
222
|
+
assert bucket.quiet?
|
223
|
+
assert_equal 10, bucket.default_ttl
|
224
|
+
bucket.disconnect
|
225
|
+
end
|
226
|
+
|
213
227
|
end
|
data/test/test_cas.rb
CHANGED
@@ -19,33 +19,22 @@ require File.join(File.dirname(__FILE__), 'setup')
|
|
19
19
|
|
20
20
|
class TestCas < 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_compare_and_swap
|
31
|
-
|
32
|
-
|
33
|
-
connection.set(uniq_id, {"bar" => 1})
|
34
|
-
connection.cas(uniq_id) do |val|
|
23
|
+
cb.set(uniq_id, {"bar" => 1})
|
24
|
+
cb.cas(uniq_id) do |val|
|
35
25
|
val["baz"] = 2
|
36
26
|
val
|
37
27
|
end
|
38
|
-
val =
|
28
|
+
val = cb.get(uniq_id)
|
39
29
|
expected = {"bar" => 1, "baz" => 2}
|
40
30
|
assert_equal expected, val
|
41
31
|
end
|
42
32
|
|
43
33
|
def test_compare_and_swap_async
|
44
|
-
|
45
|
-
|
46
|
-
connection.set(uniq_id, {"bar" => 1})
|
34
|
+
skip
|
35
|
+
cb.set(uniq_id, {"bar" => 1})
|
47
36
|
calls = 0
|
48
|
-
|
37
|
+
cb.run do |conn|
|
49
38
|
conn.cas(uniq_id) do |ret|
|
50
39
|
calls += 1
|
51
40
|
case ret.operation
|
@@ -61,18 +50,16 @@ class TestCas < MiniTest::Test
|
|
61
50
|
end
|
62
51
|
end
|
63
52
|
assert_equal 2, calls
|
64
|
-
val =
|
53
|
+
val = cb.get(uniq_id)
|
65
54
|
expected = {"bar" => 1, "baz" => 2}
|
66
55
|
assert_equal expected, val
|
67
56
|
end
|
68
57
|
|
69
58
|
def test_flags_replication
|
70
59
|
skip
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
connection.cas(uniq_id) { "baz" }
|
75
|
-
_, flags, _ = connection.get(uniq_id, :extended => true)
|
60
|
+
cb.set(uniq_id, "bar", :flags => 0x100)
|
61
|
+
cb.cas(uniq_id) { "baz" }
|
62
|
+
_, flags, _ = cb.get(uniq_id, :extended => true)
|
76
63
|
assert_equal 0x100, flags
|
77
64
|
end
|
78
65
|
end
|
data/test/test_couchbase.rb
CHANGED
@@ -16,13 +16,21 @@
|
|
16
16
|
#
|
17
17
|
|
18
18
|
require File.join(File.dirname(__FILE__), 'setup')
|
19
|
-
require 'minitest/mock'
|
20
19
|
|
21
20
|
class TestCouchbase < MiniTest::Test
|
22
21
|
|
23
22
|
def test_that_it_create_instance_of_bucket
|
24
|
-
|
25
|
-
|
23
|
+
with_configs do |configs|
|
24
|
+
connection = Couchbase.new("http://#{configs.host}:#{configs.port}")
|
25
|
+
assert_instance_of Couchbase::Bucket, connection
|
26
|
+
connection.disconnect
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_global_bucket_access
|
31
|
+
with_configs do |configs|
|
32
|
+
assert_instance_of Couchbase::Bucket, Couchbase.bucket
|
33
|
+
assert_equal Couchbase.bucket, Couchbase.bucket
|
26
34
|
end
|
27
35
|
end
|
28
36
|
|
data/test/test_delete.rb
CHANGED
@@ -19,107 +19,105 @@ require File.join(File.dirname(__FILE__), 'setup')
|
|
19
19
|
|
20
20
|
class TestStore < 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_delete
|
31
|
-
|
32
|
-
|
33
|
-
assert connection.delete(uniq_id)
|
23
|
+
cb.set(uniq_id, "bar")
|
24
|
+
assert cb.delete(uniq_id)
|
34
25
|
assert_raises(Couchbase::Error::NotFound) do
|
35
|
-
|
26
|
+
cb.delete(uniq_id)
|
36
27
|
end
|
37
28
|
end
|
38
29
|
|
39
30
|
def test_delete_missing
|
40
|
-
connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
|
41
31
|
assert_raises(Couchbase::Error::NotFound) do
|
42
|
-
|
32
|
+
cb.delete(uniq_id(:missing))
|
43
33
|
end
|
44
|
-
refute
|
45
|
-
refute
|
46
|
-
|
47
|
-
refute
|
34
|
+
refute cb.delete(uniq_id(:missing), :quiet => true)
|
35
|
+
refute cb.quiet?
|
36
|
+
cb.quiet = true
|
37
|
+
refute cb.delete(uniq_id(:missing))
|
48
38
|
end
|
49
39
|
|
50
40
|
def test_delete_with_cas
|
51
41
|
skip
|
52
|
-
|
53
|
-
cas = connection.set(uniq_id, "bar")
|
42
|
+
cas = cb.set(uniq_id, "bar")
|
54
43
|
missing_cas = cas - 1
|
55
44
|
assert_raises(Couchbase::Error::KeyExists) do
|
56
|
-
|
45
|
+
cb.delete(uniq_id, :cas => missing_cas)
|
57
46
|
end
|
58
|
-
assert
|
47
|
+
assert cb.delete(uniq_id, :cas => cas)
|
59
48
|
end
|
60
49
|
|
61
50
|
def test_allow_fixnum_as_cas_parameter
|
62
|
-
|
63
|
-
|
64
|
-
assert connection.delete(uniq_id, cas)
|
51
|
+
cas = cb.set(uniq_id, "bar")
|
52
|
+
assert cb.delete(uniq_id, cas)
|
65
53
|
end
|
66
54
|
|
67
55
|
def test_delete_with_prefix
|
68
56
|
skip
|
69
57
|
connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :key_prefix => "prefix:")
|
70
|
-
|
71
|
-
assert
|
58
|
+
cb.set(uniq_id(:foo), "bar")
|
59
|
+
assert cb.delete(uniq_id(:foo))
|
72
60
|
assert_raises(Couchbase::Error::NotFound) do
|
73
|
-
|
61
|
+
cb.get(uniq_id(:foo))
|
74
62
|
end
|
75
63
|
end
|
76
64
|
|
77
65
|
def test_simple_multi_delete
|
78
|
-
|
79
|
-
|
80
|
-
res =
|
66
|
+
cb.quiet = true
|
67
|
+
cb.set(uniq_id(1) => "bar", uniq_id(2) => "foo")
|
68
|
+
res = cb.delete(uniq_id(1), uniq_id(2))
|
81
69
|
assert res.is_a?(Hash)
|
82
70
|
assert res[uniq_id(1)]
|
83
71
|
assert res[uniq_id(2)]
|
72
|
+
ensure
|
73
|
+
cb.quiet = false
|
84
74
|
end
|
85
75
|
|
86
76
|
def test_simple_multi_delete_missing
|
87
|
-
|
88
|
-
|
89
|
-
res =
|
77
|
+
cb.quiet = true
|
78
|
+
cb.set(uniq_id(1) => "bar", uniq_id(2) => "foo")
|
79
|
+
res = cb.delete(uniq_id(1), uniq_id(:missing), :quiet => true)
|
90
80
|
assert res.is_a?(Hash)
|
91
81
|
assert res[uniq_id(1)]
|
92
82
|
refute res[uniq_id(:missing)]
|
83
|
+
ensure
|
84
|
+
cb.quiet = false
|
93
85
|
end
|
94
86
|
|
95
87
|
def test_multi_delete_with_cas_check
|
96
88
|
skip
|
97
|
-
|
98
|
-
cas =
|
99
|
-
res =
|
89
|
+
cb.quiet = true
|
90
|
+
cas = cb.set(uniq_id(1) => "bar", uniq_id(2) => "foo")
|
91
|
+
res = cb.delete(uniq_id(1) => cas[uniq_id(1)], uniq_id(2) => cas[uniq_id(2)])
|
100
92
|
assert res.is_a?(Hash)
|
101
93
|
assert res[uniq_id(1)]
|
102
94
|
assert res[uniq_id(2)]
|
95
|
+
ensure
|
96
|
+
cb.quiet = false
|
103
97
|
end
|
104
98
|
|
105
99
|
def test_multi_delete_missing_with_cas_check
|
106
100
|
skip
|
107
|
-
|
108
|
-
cas =
|
109
|
-
res =
|
101
|
+
cb.quiet = true
|
102
|
+
cas = cb.set(uniq_id(1) => "bar", uniq_id(2) => "foo")
|
103
|
+
res = cb.delete(uniq_id(1) => cas[uniq_id(1)], uniq_id(:missing) => cas[uniq_id(2)])
|
110
104
|
assert res.is_a?(Hash)
|
111
105
|
assert res[uniq_id(1)]
|
112
106
|
refute res[uniq_id(:missing)]
|
107
|
+
ensure
|
108
|
+
cb.quiet = false
|
113
109
|
end
|
114
110
|
|
115
111
|
def test_multi_delete_with_cas_check_mismatch
|
116
112
|
skip
|
117
|
-
|
118
|
-
cas =
|
113
|
+
cb.quiet = true
|
114
|
+
cas = cb.set(uniq_id(1) => "bar", uniq_id(2) => "foo")
|
119
115
|
|
120
116
|
assert_raises(Couchbase::Error::KeyExists) do
|
121
|
-
|
117
|
+
cb.delete(uniq_id(1) => cas[uniq_id(1)] + 1,
|
122
118
|
uniq_id(2) => cas[uniq_id(2)])
|
123
119
|
end
|
120
|
+
ensure
|
121
|
+
cb.quiet = false
|
124
122
|
end
|
125
123
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Author:: Mike Evans <mike@urlgonomics.com>
|
2
|
+
# Copyright:: 2013 Urlgonomics LLC.
|
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 TestDesignDocs < MiniTest::Test
|
21
|
+
|
22
|
+
def test_save_design_doc
|
23
|
+
assert cb.save_design_doc(design_doc)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_save_design_doc_with_bad_data
|
27
|
+
assert_raises ArgumentError do
|
28
|
+
cb.save_design_doc(123)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_delete_design_doc
|
33
|
+
cb.save_design_doc(design_doc)
|
34
|
+
assert cb.delete_design_doc('blog')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_design_doc_access
|
38
|
+
cb.save_design_doc(design_doc)
|
39
|
+
assert cb.design_docs['blog']
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_design_doc_missing_access
|
43
|
+
refute cb.design_docs['missing']
|
44
|
+
end
|
45
|
+
|
46
|
+
def design_doc
|
47
|
+
{
|
48
|
+
'_id' => '_design/blog',
|
49
|
+
'language' => 'javascript',
|
50
|
+
'views' => {
|
51
|
+
'recent_posts' => {
|
52
|
+
'map' => <<-JS
|
53
|
+
function (doc, meta) {
|
54
|
+
emit(doc.name);
|
55
|
+
}
|
56
|
+
JS
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/test/test_errors.rb
CHANGED
@@ -20,63 +20,54 @@ require 'digest/md5'
|
|
20
20
|
|
21
21
|
class TestErrors < MiniTest::Test
|
22
22
|
|
23
|
-
def setup
|
24
|
-
@mock = start_mock
|
25
|
-
end
|
26
|
-
|
27
|
-
def teardown
|
28
|
-
stop_mock(@mock)
|
29
|
-
end
|
30
|
-
|
31
23
|
def genkey(item)
|
32
24
|
tuple = [item["author"], item["message"]]
|
33
25
|
Digest::MD5.hexdigest(tuple.join('-'))
|
34
26
|
end
|
35
27
|
|
36
28
|
def test_graceful_add_with_collision
|
37
|
-
connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
|
38
29
|
msg1 = {"author" => "foo", "message" => "hi all", "time" => "2012-01-12 11:29:09"}
|
39
30
|
key1 = uniq_id(genkey(msg1))
|
40
31
|
msg2 = {"author" => "foo", "message" => "hi all", "time" => "2012-01-12 11:29:30"}
|
41
32
|
key2 = uniq_id(genkey(msg2))
|
42
33
|
|
43
|
-
|
34
|
+
cb.add(key1, msg1)
|
44
35
|
begin
|
45
|
-
|
36
|
+
cb.add(key2, msg2)
|
46
37
|
rescue Couchbase::Error::KeyExists => ex
|
47
38
|
# using info from exception
|
48
39
|
# it could be done with cas operation, but we can save one request
|
49
40
|
# here (in real world cas operation will be more consistent because it
|
50
41
|
# fetch fresh version from the cluster)
|
51
42
|
#
|
52
|
-
#
|
43
|
+
# cb.cas(key2) do |msg|
|
53
44
|
# msg.merge("time" => [msg["time"], msg2["time"]])
|
54
45
|
# end
|
55
46
|
msg2 = msg1.merge("time" => [msg1["time"], msg2["time"]])
|
56
|
-
|
47
|
+
cb.set(key2, msg2, :cas => ex.cas)
|
57
48
|
end
|
58
49
|
|
59
50
|
msg3 = {"author" => "foo", "message" => "hi all",
|
60
51
|
"time" => ["2012-01-12 11:29:09", "2012-01-12 11:29:30"]}
|
61
52
|
key3 = uniq_id(genkey(msg3))
|
62
|
-
assert_equal msg3,
|
53
|
+
assert_equal msg3, cb.get(key3)
|
63
54
|
|
64
|
-
|
55
|
+
cb.run do |conn|
|
65
56
|
msg4 = {"author" => "foo", "message" => "hi all", "time" => "2012-01-12 11:45:34"}
|
66
57
|
key4 = uniq_id(genkey(msg4))
|
67
58
|
|
68
|
-
|
59
|
+
cb.add(key4, msg4) do |ret|
|
69
60
|
assert_equal :add, ret.operation
|
70
61
|
assert_equal key4, ret.key
|
71
62
|
msg4 = msg3.merge("time" => msg3["time"] + [msg4["time"]])
|
72
|
-
|
63
|
+
cb.set(ret.key, msg4, :cas => ret.cas)
|
73
64
|
end
|
74
65
|
end
|
75
66
|
|
76
67
|
msg5 = {"author" => "foo", "message" => "hi all",
|
77
68
|
"time" => ["2012-01-12 11:29:09", "2012-01-12 11:29:30", "2012-01-12 11:45:34"]}
|
78
69
|
key5 = uniq_id(genkey(msg5))
|
79
|
-
assert_equal msg5,
|
70
|
+
assert_equal msg5, cb.get(key5)
|
80
71
|
end
|
81
72
|
|
82
73
|
end
|