couchbase-jruby-client 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.jrubyrc +722 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +203 -0
  7. data/README.md +349 -0
  8. data/Rakefile +10 -0
  9. data/couchbase-jruby-client.gemspec +31 -0
  10. data/lib/couchbase/async/callback.rb +19 -0
  11. data/lib/couchbase/async/queue.rb +26 -0
  12. data/lib/couchbase/async.rb +140 -0
  13. data/lib/couchbase/bucket.rb +556 -0
  14. data/lib/couchbase/cluster.rb +105 -0
  15. data/lib/couchbase/constants.rb +12 -0
  16. data/lib/couchbase/design_doc.rb +61 -0
  17. data/lib/couchbase/error.rb +43 -0
  18. data/lib/couchbase/jruby/couchbase_client.rb +22 -0
  19. data/lib/couchbase/jruby/future.rb +8 -0
  20. data/lib/couchbase/operations/arithmetic.rb +301 -0
  21. data/lib/couchbase/operations/delete.rb +104 -0
  22. data/lib/couchbase/operations/design_docs.rb +99 -0
  23. data/lib/couchbase/operations/get.rb +282 -0
  24. data/lib/couchbase/operations/stats.rb +26 -0
  25. data/lib/couchbase/operations/store.rb +461 -0
  26. data/lib/couchbase/operations/touch.rb +136 -0
  27. data/lib/couchbase/operations/unlock.rb +192 -0
  28. data/lib/couchbase/operations/utils.rb +44 -0
  29. data/lib/couchbase/operations.rb +27 -0
  30. data/lib/couchbase/query.rb +73 -0
  31. data/lib/couchbase/result.rb +43 -0
  32. data/lib/couchbase/transcoder.rb +77 -0
  33. data/lib/couchbase/utils.rb +62 -0
  34. data/lib/couchbase/version.rb +3 -0
  35. data/lib/couchbase/view.rb +367 -0
  36. data/lib/couchbase/view_row.rb +193 -0
  37. data/lib/couchbase.rb +157 -0
  38. data/lib/jars/commons-codec-1.5.jar +0 -0
  39. data/lib/jars/couchbase-client-1.2.0-javadoc.jar +0 -0
  40. data/lib/jars/couchbase-client-1.2.0-sources.jar +0 -0
  41. data/lib/jars/couchbase-client-1.2.0.jar +0 -0
  42. data/lib/jars/httpcore-4.1.1.jar +0 -0
  43. data/lib/jars/httpcore-nio-4.1.1.jar +0 -0
  44. data/lib/jars/jettison-1.1.jar +0 -0
  45. data/lib/jars/netty-3.5.5.Final.jar +0 -0
  46. data/lib/jars/spymemcached-2.10.0-javadoc.jar +0 -0
  47. data/lib/jars/spymemcached-2.10.0-sources.jar +0 -0
  48. data/lib/jars/spymemcached-2.10.0.jar +0 -0
  49. data/test/profile/.gitignore +1 -0
  50. data/test/profile/.jrubyrc +722 -0
  51. data/test/profile/Gemfile +6 -0
  52. data/test/profile/benchmark.rb +168 -0
  53. data/test/profile/profile.rb +59 -0
  54. data/test/setup.rb +203 -0
  55. data/test/test_arithmetic.rb +177 -0
  56. data/test/test_async.rb +324 -0
  57. data/test/test_bucket.rb +213 -0
  58. data/test/test_cas.rb +79 -0
  59. data/test/test_couchbase.rb +29 -0
  60. data/test/test_couchbase_rails_cache_store.rb +341 -0
  61. data/test/test_delete.rb +125 -0
  62. data/test/test_design_docs.rb +72 -0
  63. data/test/test_errors.rb +82 -0
  64. data/test/test_format.rb +161 -0
  65. data/test/test_get.rb +417 -0
  66. data/test/test_query.rb +23 -0
  67. data/test/test_stats.rb +57 -0
  68. data/test/test_store.rb +213 -0
  69. data/test/test_timer.rb +43 -0
  70. data/test/test_touch.rb +97 -0
  71. data/test/test_unlock.rb +121 -0
  72. data/test/test_utils.rb +58 -0
  73. data/test/test_version.rb +53 -0
  74. data/test/test_view.rb +94 -0
  75. metadata +255 -0
@@ -0,0 +1,213 @@
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
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
127
+
128
+ assert_raises(Couchbase::Error::NotStored) do
129
+ connection.append(uniq_id(:missing), "foo")
130
+ end
131
+
132
+ assert_raises(Couchbase::Error::NotStored) do
133
+ connection.prepend(uniq_id(:missing), "foo")
134
+ end
135
+ end
136
+
137
+ def test_append
138
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
139
+
140
+ cas1 = connection.set(uniq_id, "foo")
141
+ assert cas1 > 0
142
+ cas2 = connection.append(uniq_id, "bar")
143
+ assert cas2 > 0
144
+ refute_equal cas2, cas1
145
+
146
+ val = connection.get(uniq_id)
147
+ assert_equal "foobar", val
148
+ end
149
+
150
+ def test_prepend
151
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
152
+
153
+ cas1 = connection.set(uniq_id, "foo")
154
+ assert cas1 > 0
155
+ cas2 = connection.prepend(uniq_id, "bar")
156
+ assert cas2 > 0
157
+ refute_equal cas2, cas1
158
+
159
+ val = connection.get(uniq_id)
160
+ assert_equal "barfoo", val
161
+ end
162
+
163
+ def test_set_with_prefix
164
+ skip
165
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :key_prefix => "prefix:")
166
+ connection.set(uniq_id(:foo), "bar")
167
+ assert_equal "bar", connection.get(uniq_id(:foo))
168
+ expected = {uniq_id(:foo) => "bar"}
169
+ assert_equal expected, connection.get(uniq_id(:foo), :assemble_hash => true)
170
+
171
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :key_prefix => nil)
172
+ expected = {"prefix:#{uniq_id(:foo)}" => "bar"}
173
+ assert_equal expected, connection.get("prefix:#{uniq_id(:foo)}", :assemble_hash => true)
174
+ end
175
+
176
+ ArbitraryData = Struct.new(:baz)
177
+
178
+ def test_set_using_brackets
179
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
180
+
181
+ connection[uniq_id(1)] = "foo"
182
+ val = connection.get(uniq_id(1))
183
+ assert_equal "foo", val
184
+
185
+ # if RUBY_VERSION =~ /^1\.9/
186
+ # eval <<-EOC
187
+ # connection[uniq_id(3), :format => :marshal] = ArbitraryData.new("thing")
188
+ # val = connection.get(uniq_id(3))
189
+ # assert val.is_a?(ArbitraryData)
190
+ # assert_equal "thing", val.baz
191
+ # EOC
192
+ # end
193
+ end
194
+
195
+ def test_multi_store
196
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_format => :plain)
197
+ connection.add(uniq_id(:a) => "bbb", uniq_id(:z) => "yyy")
198
+ assert_equal ["bbb", "yyy"], connection.get(uniq_id(:a), uniq_id(:z))
199
+
200
+ # connection.prepend(uniq_id(:a) => "aaa", uniq_id(:z) => "xxx")
201
+ # assert_equal ["aaabbb", "xxxyyy"], connection.get(uniq_id(:a), uniq_id(:z))
202
+
203
+ # connection.append(uniq_id(:a) => "ccc", uniq_id(:z) => "zzz")
204
+ # assert_equal ["aaabbbccc", "xxxyyyzzz"], connection.get(uniq_id(:a), uniq_id(:z))
205
+
206
+ # connection.replace(uniq_id(:a) => "foo", uniq_id(:z) => "bar")
207
+ # assert_equal ["foo", "bar"], connection.get(uniq_id(:a), uniq_id(:z))
208
+
209
+ res = connection.set(uniq_id(:a) => "bar", uniq_id(:z) => "foo")
210
+ assert_equal ["bar", "foo"], connection.get(uniq_id(:a), uniq_id(:z))
211
+ assert res.is_a?(Hash)
212
+ end
213
+ end
@@ -0,0 +1,43 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 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 TestTimer < 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_initialization
31
+ skip
32
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
33
+ num = 0
34
+ connection.run do
35
+ assert Couchbase::Timer.new(connection, 100) { num += 1}
36
+ assert connection.create_timer(100) { num += 1}
37
+
38
+ assert Couchbase::Timer.new(connection, 100, :periodic => true) {|t| num += 1; t.cancel}
39
+ assert connection.create_periodic_timer(100) {|t| num += 1; t.cancel}
40
+ end
41
+ assert_equal 4, num
42
+ end
43
+ end
@@ -0,0 +1,97 @@
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 TestTouch < 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_touch
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+ connection.set(uniq_id, "bar", :ttl => 1)
33
+ connection.touch(uniq_id, :ttl => 2)
34
+ sleep(1)
35
+ assert connection.get(uniq_id)
36
+ sleep(2)
37
+ assert_raises(Couchbase::Error::NotFound) do
38
+ connection.get(uniq_id)
39
+ end
40
+ end
41
+
42
+ def test_multi_touch
43
+ connection = Couchbase.new(:port => @mock.port)
44
+ connection.set(uniq_id(1), "bar")
45
+ connection.set(uniq_id(2), "baz")
46
+ ret = connection.touch(uniq_id(1) => 1, uniq_id(2) => 1)
47
+ assert ret[uniq_id(1)]
48
+ assert ret[uniq_id(2)]
49
+ sleep(2)
50
+ assert_raises(Couchbase::Error::NotFound) do
51
+ connection.get(uniq_id(1))
52
+ end
53
+ assert_raises(Couchbase::Error::NotFound) do
54
+ connection.get(uniq_id(2))
55
+ end
56
+ end
57
+
58
+ def test_it_uses_default_ttl_for_touch
59
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :default_ttl => 1)
60
+ connection.set(uniq_id, "bar", :ttl => 10)
61
+ connection.touch(uniq_id)
62
+ sleep(2)
63
+ assert_raises(Couchbase::Error::NotFound) do
64
+ connection.get(uniq_id)
65
+ end
66
+ end
67
+
68
+ def test_it_accepts_ttl_for_get_command
69
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
70
+ connection.set(uniq_id, "bar", :ttl => 10)
71
+ val = connection.get(uniq_id, :ttl => 1)
72
+ assert_equal "bar", val
73
+ sleep(2)
74
+ assert_raises(Couchbase::Error::NotFound) do
75
+ connection.get(uniq_id)
76
+ end
77
+ end
78
+
79
+ def test_missing_in_quiet_mode
80
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port, :quiet => true)
81
+ cas1 = connection.set(uniq_id(1), "foo1")
82
+ cas2 = connection.set(uniq_id(2), "foo2")
83
+
84
+ assert_raises(Couchbase::Error::NotFound) do
85
+ connection.touch(uniq_id(:missing), :quiet => false)
86
+ end
87
+
88
+ val = connection.touch(uniq_id(:missing))
89
+ refute(val)
90
+
91
+ ret = connection.touch(uniq_id(1), uniq_id(:missing), uniq_id(2))
92
+ assert_equal true, ret[uniq_id(1)]
93
+ assert_equal false, ret[uniq_id(:missing)]
94
+ assert_equal true, ret[uniq_id(2)]
95
+ end
96
+
97
+ end
@@ -0,0 +1,121 @@
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 TestUnlock < 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_unlock
31
+ if @mock.real?
32
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
33
+ connection.set(uniq_id, "foo")
34
+ _, _, cas = connection.get(uniq_id, :lock => true, :extended => true)
35
+ assert_raises Couchbase::Error::KeyExists do
36
+ connection.set(uniq_id, "bar")
37
+ end
38
+ assert connection.unlock(uniq_id, :cas => cas)
39
+ connection.set(uniq_id, "bar")
40
+ else
41
+ skip("GETL and UNL aren't implemented in CouchbaseMock.jar yet")
42
+ end
43
+ end
44
+
45
+ def test_alternative_syntax_for_single_key
46
+ if @mock.real?
47
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
48
+ connection.set(uniq_id, "foo")
49
+ _, _, cas = connection.get(uniq_id, :lock => true, :extended => true)
50
+ assert_raises Couchbase::Error::KeyExists do
51
+ connection.set(uniq_id, "bar")
52
+ end
53
+ assert connection.unlock(uniq_id, cas)
54
+ connection.set(uniq_id, "bar")
55
+ else
56
+ skip("GETL and UNL aren't implemented in CouchbaseMock.jar yet")
57
+ end
58
+ end
59
+
60
+ def test_multiple_unlock
61
+ skip
62
+ if @mock.real?
63
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
64
+ connection.set(uniq_id(1), "foo")
65
+ connection.set(uniq_id(2), "foo")
66
+ info = connection.get(uniq_id(1), uniq_id(2), :lock => true, :extended => true)
67
+ assert_raises Couchbase::Error::KeyExists do
68
+ connection.set(uniq_id(1), "bar")
69
+ end
70
+ assert_raises Couchbase::Error::KeyExists do
71
+ connection.set(uniq_id(2), "bar")
72
+ end
73
+ ret = connection.unlock(uniq_id(1) => info[uniq_id(1)][2],
74
+ uniq_id(2) => info[uniq_id(2)][2])
75
+ assert ret[uniq_id(1)]
76
+ assert ret[uniq_id(2)]
77
+ connection.set(uniq_id(1), "bar")
78
+ connection.set(uniq_id(2), "bar")
79
+ else
80
+ skip("GETL and UNL aren't implemented in CouchbaseMock.jar yet")
81
+ end
82
+ end
83
+
84
+ def test_quiet_mode
85
+ skip
86
+ if @mock.real?
87
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
88
+ connection.set(uniq_id, "foo")
89
+ _, _, cas = connection.get(uniq_id, :lock => true, :extended => true)
90
+ assert_raises Couchbase::Error::NotFound do
91
+ connection.unlock(uniq_id(:missing), :cas => 0xdeadbeef)
92
+ end
93
+ keys = {
94
+ uniq_id => cas,
95
+ uniq_id(:missing) => 0xdeadbeef
96
+ }
97
+ ret = connection.unlock(keys, :quiet => true)
98
+ assert ret[uniq_id]
99
+ refute ret[uniq_id(:missing)]
100
+ else
101
+ skip("GETL and UNL aren't implemented in CouchbaseMock.jar yet")
102
+ end
103
+ end
104
+
105
+ def test_tmp_failure
106
+ if @mock.real?
107
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
108
+ cas1 = connection.set(uniq_id(1), "foo")
109
+ cas2 = connection.set(uniq_id(2), "foo")
110
+ connection.get(uniq_id(1), :lock => true) # get with lock will update CAS
111
+ assert_raises Couchbase::Error::TemporaryFail do
112
+ connection.unlock(uniq_id(1), cas1)
113
+ end
114
+ assert_raises Couchbase::Error::TemporaryFail do
115
+ connection.unlock(uniq_id(2), cas2)
116
+ end
117
+ else
118
+ skip("GETL and UNL aren't implemented in CouchbaseMock.jar yet")
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,58 @@
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 TestUtils < 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_complex_startkey
31
+ assert_equal "all_docs?startkey=%5B%22Deadmau5%22%2C%22%22%5D", Couchbase::Utils.build_query("all_docs", :startkey => ["Deadmau5", ""])
32
+ end
33
+
34
+ def test_it_provides_enough_info_with_value_error
35
+ class << MultiJson
36
+ alias dump_good dump
37
+ def dump(obj)
38
+ raise ArgumentError, "cannot accept your object"
39
+ end
40
+ end
41
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
42
+ assert_raises(Couchbase::Error::ValueFormat) do
43
+ connection.set(uniq_id, "foo")
44
+ end
45
+ begin
46
+ connection.set(uniq_id, "foo")
47
+ rescue Couchbase::Error::ValueFormat => ex
48
+ assert_match /cannot accept your object/, ex.to_s
49
+ assert_instance_of ArgumentError, ex.inner_exception
50
+ end
51
+ ensure
52
+ class << MultiJson
53
+ undef dump
54
+ alias dump dump_good
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,53 @@
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 TestVersion < 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_sync_version
31
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
32
+ ver = connection.version
33
+ assert ver.is_a?(Hash)
34
+ assert_equal @mock.num_nodes, ver.size
35
+ end
36
+
37
+ def test_async_version
38
+ skip
39
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
40
+ ver = {}
41
+ connection.run do |conn|
42
+ conn.version do |ret|
43
+ assert ret.success?
44
+ ver[ret.node] = ret.value
45
+ end
46
+ end
47
+ assert_equal @mock.num_nodes, ver.size
48
+ ver.each do |node, v|
49
+ assert v.is_a?(String)
50
+ end
51
+ end
52
+
53
+ end
data/test/test_view.rb ADDED
@@ -0,0 +1,94 @@
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 TestView < MiniTest::Test
21
+
22
+ def setup
23
+ @mock = start_mock
24
+ @path = '_design/users/_view/by_age'
25
+ @cb = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
26
+ @cb.save_design_doc(design_doc)
27
+ { bob: 32, frank: 25, sam: 42, fred: 21 }.each_pair do |name, age|
28
+ @cb.set(name, { type: 'user', name: name, age: age })
29
+ end
30
+ @view = Couchbase::View.new(@cb, @path)
31
+ end
32
+
33
+ def teardown
34
+ stop_mock(@mock)
35
+ @cb.disconnect
36
+ end
37
+
38
+ def test_initialize
39
+ assert_equal 'users', @view.design_doc
40
+ assert_equal 'by_age', @view.name
41
+ end
42
+
43
+ def test_simple_fetch
44
+ assert results = @view.fetch
45
+ assert results.is_a?(Couchbase::View::ArrayWithTotalRows)
46
+ end
47
+
48
+ def test_fetch_without_stale
49
+ assert results = @view.fetch(stale: false)
50
+ assert results.first.is_a?(Couchbase::ViewRow)
51
+ assert results.first.doc.nil?
52
+ assert_equal 4, results.total_rows
53
+ results.each do |result|
54
+ %w(bob frank sam fred).include?(result.key)
55
+ end
56
+ end
57
+
58
+ def test_fetch_with_docs
59
+ assert results = @view.fetch(stale: false, include_docs: true)
60
+ assert results.is_a?(Array)
61
+ assert results.first.doc.is_a?(Hash)
62
+ end
63
+
64
+ def test_fetch_with_block
65
+ refute @view.fetch(stale: false, include_docs: true) { |row|
66
+ assert row.is_a?(Couchbase::ViewRow)
67
+ assert row.doc['name'].is_a?(String)
68
+ assert row.doc['age'].is_a?(Fixnum)
69
+ }
70
+ end
71
+
72
+ def test_design_doc_access
73
+ assert results = @cb.design_docs['users'].by_age.to_a
74
+ assert results.first.is_a?(Couchbase::ViewRow)
75
+ end
76
+
77
+ def design_doc
78
+ {
79
+ '_id' => '_design/users',
80
+ 'language' => 'javascript',
81
+ 'views' => {
82
+ 'by_age' => {
83
+ 'map' => <<-JS
84
+ function (doc, meta) {
85
+ if (doc.type && doc.type == 'user')
86
+ emit(meta.id, doc.age);
87
+ }
88
+ JS
89
+ }
90
+ }
91
+ }
92
+ end
93
+
94
+ end