namelessjon-couchrest 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +176 -0
- data/README.md +46 -0
- data/Rakefile +69 -0
- data/THANKS.md +21 -0
- data/couchrest.gemspec +111 -0
- data/examples/word_count/markov +38 -0
- data/examples/word_count/views/books/chunked-map.js +3 -0
- data/examples/word_count/views/books/united-map.js +1 -0
- data/examples/word_count/views/markov/chain-map.js +6 -0
- data/examples/word_count/views/markov/chain-reduce.js +7 -0
- data/examples/word_count/views/word_count/count-map.js +6 -0
- data/examples/word_count/views/word_count/count-reduce.js +3 -0
- data/examples/word_count/word_count.rb +46 -0
- data/examples/word_count/word_count_query.rb +40 -0
- data/examples/word_count/word_count_views.rb +26 -0
- data/history.txt +145 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/database.rb +373 -0
- data/lib/couchrest/design.rb +80 -0
- data/lib/couchrest/document.rb +89 -0
- data/lib/couchrest/helper/attachments.rb +29 -0
- data/lib/couchrest/helper/pager.rb +103 -0
- data/lib/couchrest/helper/streamer.rb +51 -0
- data/lib/couchrest/helper/upgrade.rb +52 -0
- data/lib/couchrest/json_response.rb +14 -0
- data/lib/couchrest/middlewares/logger.rb +263 -0
- data/lib/couchrest/monkeypatches.rb +42 -0
- data/lib/couchrest/response.rb +35 -0
- data/lib/couchrest/rest_api.rb +62 -0
- data/lib/couchrest/server.rb +90 -0
- data/lib/couchrest/support/inheritable_attributes.rb +107 -0
- data/lib/couchrest.rb +127 -0
- data/spec/couchrest/couchrest_spec.rb +202 -0
- data/spec/couchrest/database_spec.rb +870 -0
- data/spec/couchrest/design_spec.rb +158 -0
- data/spec/couchrest/document_spec.rb +279 -0
- data/spec/couchrest/helpers/pager_spec.rb +123 -0
- data/spec/couchrest/helpers/streamer_spec.rb +52 -0
- data/spec/couchrest/server_spec.rb +35 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +44 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +179 -0
@@ -0,0 +1,158 @@
|
|
1
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
+
|
3
|
+
describe CouchRest::Design do
|
4
|
+
|
5
|
+
describe "defining a view" do
|
6
|
+
it "should add a view to the design doc" do
|
7
|
+
@des = CouchRest::Design.new
|
8
|
+
method = @des.view_by :name
|
9
|
+
method.should == "by_name"
|
10
|
+
@des["views"]["by_name"].should_not be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "with an unsaved view" do
|
15
|
+
before(:each) do
|
16
|
+
@des = CouchRest::Design.new
|
17
|
+
@des.view_by :name
|
18
|
+
end
|
19
|
+
it "should accept a name" do
|
20
|
+
@des.name = "mytest"
|
21
|
+
@des.name.should == "mytest"
|
22
|
+
end
|
23
|
+
it "should not save on view definition" do
|
24
|
+
@des.rev.should be_nil
|
25
|
+
end
|
26
|
+
it "should freak out on view access" do
|
27
|
+
lambda{@des.view :by_name}.should raise_error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "saving" do
|
32
|
+
before(:each) do
|
33
|
+
@des = CouchRest::Design.new
|
34
|
+
@des.view_by :name
|
35
|
+
@des.database = reset_test_db!
|
36
|
+
end
|
37
|
+
it "should fail without a name" do
|
38
|
+
lambda{@des.save}.should raise_error(ArgumentError)
|
39
|
+
end
|
40
|
+
it "should work with a name" do
|
41
|
+
@des.name = "myview"
|
42
|
+
@des.save
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "when it's saved" do
|
47
|
+
before(:each) do
|
48
|
+
@db = reset_test_db!
|
49
|
+
@db.bulk_save([{"name" => "x"},{"name" => "y"}])
|
50
|
+
@des = CouchRest::Design.new
|
51
|
+
@des.database = @db
|
52
|
+
@des.view_by :name
|
53
|
+
end
|
54
|
+
it "should by queryable when it's saved" do
|
55
|
+
@des.name = "mydesign"
|
56
|
+
@des.save
|
57
|
+
res = @des.view :by_name
|
58
|
+
res["rows"][0]["key"].should == "x"
|
59
|
+
end
|
60
|
+
it "should be queryable on specified database" do
|
61
|
+
@des.name = "mydesign"
|
62
|
+
@des.save
|
63
|
+
@des.database = nil
|
64
|
+
res = @des.view_on @db, :by_name
|
65
|
+
res["rows"][0]["key"].should == "x"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "from a saved document" do
|
70
|
+
before(:each) do
|
71
|
+
@db = reset_test_db!
|
72
|
+
@db.save_doc({
|
73
|
+
"_id" => "_design/test",
|
74
|
+
"views" => {
|
75
|
+
"by_name" => {
|
76
|
+
"map" => "function(doc){if (doc.name) emit(doc.name, null)}"
|
77
|
+
}
|
78
|
+
}
|
79
|
+
})
|
80
|
+
@db.bulk_save([{"name" => "a"},{"name" => "b"}])
|
81
|
+
@des = @db.get "_design/test"
|
82
|
+
end
|
83
|
+
it "should be a Design" do
|
84
|
+
@des.should be_an_instance_of(CouchRest::Design)
|
85
|
+
end
|
86
|
+
it "should have a modifiable name" do
|
87
|
+
@des.name.should == "test"
|
88
|
+
@des.name = "supertest"
|
89
|
+
@des.id.should == "_design/supertest"
|
90
|
+
end
|
91
|
+
it "should by queryable" do
|
92
|
+
res = @des.view :by_name
|
93
|
+
res["rows"][0]["key"].should == "a"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "a view with default options" do
|
98
|
+
before(:all) do
|
99
|
+
@db = reset_test_db!
|
100
|
+
@des = CouchRest::Design.new
|
101
|
+
@des.name = "test"
|
102
|
+
@des.view_by :name, :descending => true
|
103
|
+
@des.database = @db
|
104
|
+
@des.save
|
105
|
+
@db.bulk_save([{"name" => "a"},{"name" => "z"}])
|
106
|
+
end
|
107
|
+
it "should save them" do
|
108
|
+
@d2 = @db.get(@des.id)
|
109
|
+
@d2["views"]["by_name"]["couchrest-defaults"].should == {"descending"=>true}
|
110
|
+
end
|
111
|
+
it "should use them" do
|
112
|
+
res = @des.view :by_name
|
113
|
+
res["rows"].first["key"].should == "z"
|
114
|
+
end
|
115
|
+
it "should override them" do
|
116
|
+
res = @des.view :by_name, :descending => false
|
117
|
+
res["rows"].first["key"].should == "a"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "a view with multiple keys" do
|
122
|
+
before(:all) do
|
123
|
+
@db = reset_test_db!
|
124
|
+
@des = CouchRest::Design.new
|
125
|
+
@des.name = "test"
|
126
|
+
@des.view_by :name, :age
|
127
|
+
@des.database = @db
|
128
|
+
@des.save
|
129
|
+
@db.bulk_save([{"name" => "a", "age" => 2},
|
130
|
+
{"name" => "a", "age" => 4},{"name" => "z", "age" => 9}])
|
131
|
+
end
|
132
|
+
it "should work" do
|
133
|
+
res = @des.view :by_name_and_age
|
134
|
+
res["rows"].first["key"].should == ["a",2]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "a view with nil and 0 values" do
|
139
|
+
before(:all) do
|
140
|
+
@db = reset_test_db!
|
141
|
+
@des = CouchRest::Design.new
|
142
|
+
@des.name = "test"
|
143
|
+
@des.view_by :code
|
144
|
+
@des.database = @db
|
145
|
+
@des.save
|
146
|
+
@db.bulk_save([{"code" => "a", "age" => 2},
|
147
|
+
{"code" => nil, "age" => 4},{"code" => 0, "age" => 9}])
|
148
|
+
end
|
149
|
+
it "should work" do
|
150
|
+
res = @des.view :by_code
|
151
|
+
res["rows"][0]["key"].should == 0
|
152
|
+
res["rows"][1]["key"].should == "a"
|
153
|
+
res["rows"][2].should be_nil
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,279 @@
|
|
1
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
+
|
3
|
+
class Video < CouchRest::Document; end
|
4
|
+
|
5
|
+
describe CouchRest::Document do
|
6
|
+
|
7
|
+
before(:all) do
|
8
|
+
@couch = CouchRest.new
|
9
|
+
@db = @couch.database!(TESTDB)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "[]=" do
|
13
|
+
before(:each) do
|
14
|
+
@doc = CouchRest::Document.new
|
15
|
+
end
|
16
|
+
it "should work" do
|
17
|
+
@doc["enamel"].should == nil
|
18
|
+
@doc["enamel"] = "Strong"
|
19
|
+
@doc["enamel"].should == "Strong"
|
20
|
+
end
|
21
|
+
it "[]= should convert to string" do
|
22
|
+
@doc["enamel"].should == nil
|
23
|
+
@doc[:enamel] = "Strong"
|
24
|
+
@doc["enamel"].should == "Strong"
|
25
|
+
end
|
26
|
+
it "should read as a string" do
|
27
|
+
@doc[:enamel] = "Strong"
|
28
|
+
@doc[:enamel].should == "Strong"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "default database" do
|
33
|
+
before(:each) do
|
34
|
+
Video.use_database nil
|
35
|
+
end
|
36
|
+
it "should be set using use_database on the model" do
|
37
|
+
Video.new.database.should be_nil
|
38
|
+
Video.use_database @db
|
39
|
+
Video.new.database.should == @db
|
40
|
+
Video.use_database nil
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be overwritten by instance" do
|
44
|
+
db = @couch.database('test')
|
45
|
+
article = Video.new
|
46
|
+
article.database.should be_nil
|
47
|
+
article.database = db
|
48
|
+
article.database.should_not be_nil
|
49
|
+
article.database.should == db
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "new" do
|
54
|
+
before(:each) do
|
55
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
56
|
+
end
|
57
|
+
it "should create itself from a Hash" do
|
58
|
+
@doc["key"].should == [1,2,3]
|
59
|
+
@doc["more"].should == "values"
|
60
|
+
end
|
61
|
+
it "should not have rev and id" do
|
62
|
+
@doc.rev.should be_nil
|
63
|
+
@doc.id.should be_nil
|
64
|
+
end
|
65
|
+
it "should be possible to set id" do
|
66
|
+
@doc.id = 1
|
67
|
+
@doc.id.should eql(1)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should freak out when saving without a database" do
|
71
|
+
lambda{@doc.save}.should raise_error(ArgumentError)
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
# move to database spec
|
77
|
+
describe "saving using a database" do
|
78
|
+
before(:all) do
|
79
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
80
|
+
@db = reset_test_db!
|
81
|
+
@resp = @db.save_doc(@doc)
|
82
|
+
end
|
83
|
+
it "should apply the database" do
|
84
|
+
@doc.database.should == @db
|
85
|
+
end
|
86
|
+
it "should get id and rev" do
|
87
|
+
@doc.id.should == @resp["id"]
|
88
|
+
@doc.rev.should == @resp["rev"]
|
89
|
+
end
|
90
|
+
it "should generate a correct URI" do
|
91
|
+
@doc.uri.should == "#{@db.root}/#{@doc.id}"
|
92
|
+
URI.parse(@doc.uri).to_s.should == @doc.uri
|
93
|
+
end
|
94
|
+
it "should generate a correct URI with revision" do
|
95
|
+
@doc.uri(true).should == "#{@db.root}/#{@doc.id}?rev=#{@doc.rev}"
|
96
|
+
URI.parse(@doc.uri(true)).to_s.should == @doc.uri(true)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "bulk saving" do
|
101
|
+
before :all do
|
102
|
+
@db = reset_test_db!
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should use the document bulk save cache" do
|
106
|
+
doc = CouchRest::Document.new({"_id" => "bulkdoc", "val" => 3})
|
107
|
+
doc.database = @db
|
108
|
+
doc.save(true)
|
109
|
+
lambda { doc.database.get(doc["_id"]) }.should raise_error(RestClient::ResourceNotFound)
|
110
|
+
doc.database.bulk_save
|
111
|
+
doc.database.get(doc["_id"])["val"].should == doc["val"]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe "getting from a database" do
|
116
|
+
before(:all) do
|
117
|
+
@db = reset_test_db!
|
118
|
+
@resp = @db.save_doc({
|
119
|
+
"key" => "value"
|
120
|
+
})
|
121
|
+
@doc = @db.get @resp['id']
|
122
|
+
end
|
123
|
+
it "should return a document" do
|
124
|
+
@doc.should be_an_instance_of(CouchRest::Document)
|
125
|
+
end
|
126
|
+
it "should have a database" do
|
127
|
+
@doc.database.should == @db
|
128
|
+
end
|
129
|
+
it "should be saveable and resavable" do
|
130
|
+
@doc["more"] = "keys"
|
131
|
+
@doc.save
|
132
|
+
@db.get(@resp['id'])["more"].should == "keys"
|
133
|
+
@doc["more"] = "these keys"
|
134
|
+
@doc.save
|
135
|
+
@db.get(@resp['id'])["more"].should == "these keys"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe "destroying a document from a db" do
|
140
|
+
before(:all) do
|
141
|
+
@db = reset_test_db!
|
142
|
+
@resp = @db.save_doc({
|
143
|
+
"key" => "value"
|
144
|
+
})
|
145
|
+
@doc = @db.get @resp['id']
|
146
|
+
end
|
147
|
+
it "should make it disappear" do
|
148
|
+
@doc.destroy
|
149
|
+
lambda{@db.get @resp['id']}.should raise_error
|
150
|
+
end
|
151
|
+
it "should error when there's no db" do
|
152
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
153
|
+
lambda{@doc.destroy}.should raise_error(ArgumentError)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
describe "destroying a document from a db using bulk save" do
|
159
|
+
before(:all) do
|
160
|
+
@db = reset_test_db!
|
161
|
+
@resp = @db.save_doc({
|
162
|
+
"key" => "value"
|
163
|
+
})
|
164
|
+
@doc = @db.get @resp['id']
|
165
|
+
end
|
166
|
+
it "should defer actual deletion" do
|
167
|
+
@doc.destroy(true)
|
168
|
+
@doc['_id'].should == nil
|
169
|
+
@doc['_rev'].should == nil
|
170
|
+
lambda{@db.get @resp['id']}.should_not raise_error
|
171
|
+
@db.bulk_save
|
172
|
+
lambda{@db.get @resp['id']}.should raise_error
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe "copying a document" do
|
177
|
+
before :each do
|
178
|
+
@db = reset_test_db!
|
179
|
+
@resp = @db.save_doc({'key' => 'value'})
|
180
|
+
@docid = 'new-location'
|
181
|
+
@doc = @db.get(@resp['id'])
|
182
|
+
end
|
183
|
+
describe "to a new location" do
|
184
|
+
it "should work" do
|
185
|
+
@doc.copy @docid
|
186
|
+
newdoc = @db.get(@docid)
|
187
|
+
newdoc['key'].should == 'value'
|
188
|
+
end
|
189
|
+
it "should fail without a database" do
|
190
|
+
lambda{CouchRest::Document.new({"not"=>"a real doc"}).copy}.should raise_error(ArgumentError)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
describe "to an existing location" do
|
194
|
+
before :each do
|
195
|
+
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
196
|
+
end
|
197
|
+
it "should fail without a rev" do
|
198
|
+
lambda{@doc.copy @docid}.should raise_error(RestClient::RequestFailed)
|
199
|
+
end
|
200
|
+
it "should succeed with a rev" do
|
201
|
+
@to_be_overwritten = @db.get(@docid)
|
202
|
+
@doc.copy "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
203
|
+
newdoc = @db.get(@docid)
|
204
|
+
newdoc['key'].should == 'value'
|
205
|
+
end
|
206
|
+
it "should succeed given the doc to overwrite" do
|
207
|
+
@to_be_overwritten = @db.get(@docid)
|
208
|
+
@doc.copy @to_be_overwritten
|
209
|
+
newdoc = @db.get(@docid)
|
210
|
+
newdoc['key'].should == 'value'
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "dealing with attachments" do
|
217
|
+
before do
|
218
|
+
@db = reset_test_db!
|
219
|
+
@attach = "<html><head><title>My Doc</title></head><body><p>Has words.</p></body></html>"
|
220
|
+
response = @db.save_doc({'key' => 'value'})
|
221
|
+
@doc = @db.get(response['id'])
|
222
|
+
end
|
223
|
+
|
224
|
+
def append_attachment(name='test.html', attach=@attach)
|
225
|
+
@doc['_attachments'] ||= {}
|
226
|
+
@doc['_attachments'][name] = {
|
227
|
+
'type' => 'text/html',
|
228
|
+
'data' => attach
|
229
|
+
}
|
230
|
+
@doc.save
|
231
|
+
@rev = @doc['_rev']
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "PUTing an attachment directly to the doc" do
|
235
|
+
before do
|
236
|
+
@doc.put_attachment('test.html', @attach)
|
237
|
+
end
|
238
|
+
|
239
|
+
it "is there" do
|
240
|
+
@db.fetch_attachment(@doc, 'test.html').should == @attach
|
241
|
+
end
|
242
|
+
|
243
|
+
it "updates the revision" do
|
244
|
+
@doc['_rev'].should_not == @rev
|
245
|
+
end
|
246
|
+
|
247
|
+
it "updates attachments" do
|
248
|
+
@attach2 = "<html><head><title>My Doc</title></head><body><p>Is Different.</p></body></html>"
|
249
|
+
@doc.put_attachment('test.html', @attach2)
|
250
|
+
@db.fetch_attachment(@doc, 'test.html').should == @attach2
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
describe "fetching an attachment from a doc directly" do
|
255
|
+
before do
|
256
|
+
append_attachment
|
257
|
+
end
|
258
|
+
|
259
|
+
it "pulls the attachment" do
|
260
|
+
@doc.fetch_attachment('test.html').should == @attach
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
describe "deleting an attachment from a doc directly" do
|
265
|
+
before do
|
266
|
+
append_attachment
|
267
|
+
@doc.delete_attachment('test.html')
|
268
|
+
end
|
269
|
+
|
270
|
+
it "removes it" do
|
271
|
+
lambda { @db.fetch_attachment(@doc, 'test.html').should }.should raise_error(RestClient::ResourceNotFound)
|
272
|
+
end
|
273
|
+
|
274
|
+
it "updates the revision" do
|
275
|
+
@doc['_rev'].should_not == @rev
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require File.expand_path("../../../spec_helper", __FILE__)
|
2
|
+
require 'couchrest/helper/pager'
|
3
|
+
|
4
|
+
describe CouchRest::Pager do
|
5
|
+
before(:all) do
|
6
|
+
@cr = CouchRest.new(COUCHHOST)
|
7
|
+
@db = @cr.database(TESTDB)
|
8
|
+
@db.delete! rescue nil
|
9
|
+
@db = @cr.create_db(TESTDB) rescue nil
|
10
|
+
@pager = CouchRest::Pager.new(@db)
|
11
|
+
end
|
12
|
+
|
13
|
+
after(:all) do
|
14
|
+
begin
|
15
|
+
@db.delete!
|
16
|
+
rescue RestClient::Request::RequestFailed
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should store the db" do
|
21
|
+
@pager.db.should == @db
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "paging all docs" do
|
25
|
+
before(:all) do
|
26
|
+
@docs = []
|
27
|
+
100.times do |i|
|
28
|
+
@docs << ({:number => (i % 10)})
|
29
|
+
end
|
30
|
+
@db.bulk_save(@docs)
|
31
|
+
end
|
32
|
+
it "should yield total_docs / limit times" do
|
33
|
+
n = 0
|
34
|
+
@pager.all_docs(10) do |doc|
|
35
|
+
n += 1
|
36
|
+
end
|
37
|
+
n.should == 10
|
38
|
+
end
|
39
|
+
it "should yield each docrow group without duplicate docs" do
|
40
|
+
docids = {}
|
41
|
+
@pager.all_docs(10) do |docrows|
|
42
|
+
docrows.each do |row|
|
43
|
+
docids[row['id']].should be_nil
|
44
|
+
docids[row['id']] = true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
docids.keys.length.should == 100
|
48
|
+
end
|
49
|
+
it "should yield each docrow group" do
|
50
|
+
@pager.all_docs(10) do |docrows|
|
51
|
+
doc = @db.get(docrows[0]['id'])
|
52
|
+
doc['number'].class.should == Fixnum
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "Pager with a view and docs" do
|
58
|
+
before(:all) do
|
59
|
+
@docs = []
|
60
|
+
100.times do |i|
|
61
|
+
@docs << ({:number => (i % 10)})
|
62
|
+
end
|
63
|
+
@db.bulk_save(@docs)
|
64
|
+
@db.save_doc({
|
65
|
+
'_id' => '_design/magic',
|
66
|
+
'views' => {
|
67
|
+
'number' => {
|
68
|
+
'map' => 'function(doc){emit(doc.number,null)}'
|
69
|
+
}
|
70
|
+
}
|
71
|
+
})
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should have docs" do
|
75
|
+
@docs.length.should == 100
|
76
|
+
@db.documents['rows'].length.should == 101
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should have a view" do
|
80
|
+
@db.view('magic/number', :limit => 10)['rows'][0]['key'].should == 0
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should yield once per key" do
|
84
|
+
results = {}
|
85
|
+
@pager.key_reduce('magic/number', 20) do |k,vs|
|
86
|
+
results[k] = vs.length
|
87
|
+
end
|
88
|
+
results[0].should == 10
|
89
|
+
results[3].should == 10
|
90
|
+
end
|
91
|
+
|
92
|
+
it "with a small step size should yield once per key" do
|
93
|
+
results = {}
|
94
|
+
@pager.key_reduce('magic/number', 7) do |k,vs|
|
95
|
+
results[k] = vs.length
|
96
|
+
end
|
97
|
+
results[0].should == 10
|
98
|
+
results[3].should == 10
|
99
|
+
results[9].should == 10
|
100
|
+
end
|
101
|
+
it "with a large step size should yield once per key" do
|
102
|
+
results = {}
|
103
|
+
@pager.key_reduce('magic/number', 1000) do |k,vs|
|
104
|
+
results[k] = vs.length
|
105
|
+
end
|
106
|
+
results[0].should == 10
|
107
|
+
results[3].should == 10
|
108
|
+
results[9].should == 10
|
109
|
+
end
|
110
|
+
it "with a begin and end should only yield in the range (and leave out the lastkey)" do
|
111
|
+
results = {}
|
112
|
+
@pager.key_reduce('magic/number', 1000, 4, 7) do |k,vs|
|
113
|
+
results[k] = vs.length
|
114
|
+
end
|
115
|
+
results[0].should be_nil
|
116
|
+
results[4].should == 10
|
117
|
+
results[6].should == 10
|
118
|
+
results[7].should be_nil
|
119
|
+
results[8].should be_nil
|
120
|
+
results[9].should be_nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path("../../../spec_helper", __FILE__)
|
2
|
+
|
3
|
+
describe CouchRest::Streamer do
|
4
|
+
before(:all) do
|
5
|
+
@cr = CouchRest.new(COUCHHOST)
|
6
|
+
@db = @cr.database(TESTDB)
|
7
|
+
@db.delete! rescue nil
|
8
|
+
@db = @cr.create_db(TESTDB) rescue nil
|
9
|
+
@streamer = CouchRest::Streamer.new(@db)
|
10
|
+
@docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
|
11
|
+
@db.bulk_save(@docs)
|
12
|
+
@db.save_doc({
|
13
|
+
"_id" => "_design/first",
|
14
|
+
:views => {
|
15
|
+
:test => {
|
16
|
+
:map => "function(doc){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
})
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should yield each row in a view" do
|
23
|
+
count = 0
|
24
|
+
sum = 0
|
25
|
+
@streamer.view("_all_docs") do |row|
|
26
|
+
count += 1
|
27
|
+
end
|
28
|
+
count.should == 1001
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should accept several params" do
|
32
|
+
count = 0
|
33
|
+
@streamer.view("_design/first/_view/test", :include_docs => true, :limit => 5) do |row|
|
34
|
+
count += 1
|
35
|
+
end
|
36
|
+
count.should == 5
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should accept both view formats" do
|
40
|
+
count = 0
|
41
|
+
@streamer.view("_design/first/_view/test") do |row|
|
42
|
+
count += 1
|
43
|
+
end
|
44
|
+
count.should == 2000
|
45
|
+
count = 0
|
46
|
+
@streamer.view("first/test") do |row|
|
47
|
+
count += 1
|
48
|
+
end
|
49
|
+
count.should == 2000
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
+
|
3
|
+
describe CouchRest::Server do
|
4
|
+
|
5
|
+
describe "available databases" do
|
6
|
+
before(:each) do
|
7
|
+
@couch = CouchRest::Server.new COUCHHOST
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:each) do
|
11
|
+
@couch.available_databases.each do |ref, db|
|
12
|
+
db.delete!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should let you add more databases" do
|
17
|
+
@couch.available_databases.should be_empty
|
18
|
+
@couch.define_available_database(:default, "cr-server-test-db")
|
19
|
+
@couch.available_databases.keys.should include(:default)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should verify that a database is available" do
|
23
|
+
@couch.define_available_database(:default, "cr-server-test-db")
|
24
|
+
@couch.available_database?(:default).should be_true
|
25
|
+
@couch.available_database?("cr-server-test-db").should be_true
|
26
|
+
@couch.available_database?(:matt).should be_false
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should let you set a default database" do
|
30
|
+
@couch.default_database = 'cr-server-test-default-db'
|
31
|
+
@couch.available_database?(:default).should be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
Binary file
|