samflores-couchrest 0.2.1 → 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -34
- data/Rakefile +5 -2
- data/bin/couchdir +20 -0
- data/examples/model/example.rb +13 -19
- data/examples/word_count/word_count.rb +24 -3
- data/examples/word_count/word_count_query.rb +6 -7
- data/lib/couchrest/core/database.rb +49 -126
- data/lib/couchrest/core/document.rb +25 -58
- data/lib/couchrest/core/model.rb +612 -0
- data/lib/couchrest/core/server.rb +10 -47
- data/lib/couchrest/core/validations.rb +328 -0
- data/lib/couchrest/monkeypatches.rb +0 -95
- data/lib/couchrest.rb +10 -57
- data/spec/couchrest/core/database_spec.rb +68 -183
- data/spec/couchrest/core/design_spec.rb +1 -1
- data/spec/couchrest/core/document_spec.rb +104 -285
- data/spec/couchrest/core/model_spec.rb +855 -0
- data/spec/couchrest/helpers/pager_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -13
- metadata +17 -83
- data/examples/word_count/word_count_views.rb +0 -26
- data/lib/couchrest/core/response.rb +0 -16
- data/lib/couchrest/mixins/attachments.rb +0 -31
- data/lib/couchrest/mixins/callbacks.rb +0 -483
- data/lib/couchrest/mixins/design_doc.rb +0 -64
- data/lib/couchrest/mixins/document_queries.rb +0 -48
- data/lib/couchrest/mixins/extended_attachments.rb +0 -68
- data/lib/couchrest/mixins/extended_document_mixins.rb +0 -6
- data/lib/couchrest/mixins/properties.rb +0 -125
- data/lib/couchrest/mixins/validation.rb +0 -234
- data/lib/couchrest/mixins/views.rb +0 -168
- data/lib/couchrest/mixins.rb +0 -4
- data/lib/couchrest/more/casted_model.rb +0 -28
- data/lib/couchrest/more/extended_document.rb +0 -217
- data/lib/couchrest/more/property.rb +0 -40
- data/lib/couchrest/support/blank.rb +0 -42
- data/lib/couchrest/support/class.rb +0 -191
- data/lib/couchrest/validation/auto_validate.rb +0 -163
- data/lib/couchrest/validation/contextual_validators.rb +0 -78
- data/lib/couchrest/validation/validation_errors.rb +0 -118
- data/lib/couchrest/validation/validators/absent_field_validator.rb +0 -74
- data/lib/couchrest/validation/validators/confirmation_validator.rb +0 -99
- data/lib/couchrest/validation/validators/format_validator.rb +0 -117
- data/lib/couchrest/validation/validators/formats/email.rb +0 -66
- data/lib/couchrest/validation/validators/formats/url.rb +0 -43
- data/lib/couchrest/validation/validators/generic_validator.rb +0 -120
- data/lib/couchrest/validation/validators/length_validator.rb +0 -134
- data/lib/couchrest/validation/validators/method_validator.rb +0 -89
- data/lib/couchrest/validation/validators/numeric_validator.rb +0 -104
- data/lib/couchrest/validation/validators/required_field_validator.rb +0 -109
- data/spec/couchrest/core/server_spec.rb +0 -35
- data/spec/couchrest/more/casted_extended_doc_spec.rb +0 -40
- data/spec/couchrest/more/casted_model_spec.rb +0 -98
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +0 -130
- data/spec/couchrest/more/extended_doc_spec.rb +0 -509
- data/spec/couchrest/more/extended_doc_view_spec.rb +0 -207
- data/spec/couchrest/more/property_spec.rb +0 -130
- data/spec/couchrest/support/class_spec.rb +0 -59
- data/spec/fixtures/more/article.rb +0 -34
- data/spec/fixtures/more/card.rb +0 -20
- data/spec/fixtures/more/course.rb +0 -14
- data/spec/fixtures/more/event.rb +0 -6
- data/spec/fixtures/more/invoice.rb +0 -17
- data/spec/fixtures/more/person.rb +0 -8
- data/spec/fixtures/more/question.rb +0 -6
- data/spec/fixtures/more/service.rb +0 -12
@@ -1,311 +1,130 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
before(:all) do
|
8
|
-
@couch = CouchRest.new
|
9
|
-
@db = @couch.database!(TESTDB)
|
3
|
+
describe CouchRest::Document, "[]=" do
|
4
|
+
before(:each) do
|
5
|
+
@doc = CouchRest::Document.new
|
10
6
|
end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
7
|
+
it "should work" do
|
8
|
+
@doc["enamel"].should == nil
|
9
|
+
@doc["enamel"] = "Strong"
|
10
|
+
@doc["enamel"].should == "Strong"
|
30
11
|
end
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
12
|
+
it "[]= should convert to string" do
|
13
|
+
@doc["enamel"].should == nil
|
14
|
+
@doc[:enamel] = "Strong"
|
15
|
+
@doc["enamel"].should == "Strong"
|
51
16
|
end
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
66
|
-
it "should freak out when saving without a database" do
|
67
|
-
lambda{@doc.save}.should raise_error(ArgumentError)
|
68
|
-
end
|
69
|
-
|
17
|
+
it "should read as a string" do
|
18
|
+
@doc[:enamel] = "Strong"
|
19
|
+
@doc[:enamel].should == "Strong"
|
70
20
|
end
|
21
|
+
end
|
71
22
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
76
|
-
@db = reset_test_db!
|
77
|
-
@resp = @db.save_doc(@doc)
|
78
|
-
end
|
79
|
-
it "should apply the database" do
|
80
|
-
@doc.database.should == @db
|
81
|
-
end
|
82
|
-
it "should get id and rev" do
|
83
|
-
@doc.id.should == @resp["id"]
|
84
|
-
@doc.rev.should == @resp["rev"]
|
85
|
-
end
|
23
|
+
describe CouchRest::Document, "new" do
|
24
|
+
before(:each) do
|
25
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
86
26
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
@db = reset_test_db!
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should use the document bulk save cache" do
|
94
|
-
doc = CouchRest::Document.new({"_id" => "bulkdoc", "val" => 3})
|
95
|
-
doc.database = @db
|
96
|
-
doc.save(true)
|
97
|
-
lambda { doc.database.get(doc["_id"]) }.should raise_error(RestClient::ResourceNotFound)
|
98
|
-
doc.database.bulk_save
|
99
|
-
doc.database.get(doc["_id"])["val"].should == doc["val"]
|
100
|
-
end
|
27
|
+
it "should create itself from a Hash" do
|
28
|
+
@doc["key"].should == [1,2,3]
|
29
|
+
@doc["more"].should == "values"
|
101
30
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
@db = reset_test_db!
|
106
|
-
@resp = @db.save_doc({
|
107
|
-
"key" => "value"
|
108
|
-
})
|
109
|
-
@doc = @db.get @resp['id']
|
110
|
-
end
|
111
|
-
it "should return a document" do
|
112
|
-
@doc.should be_an_instance_of(CouchRest::Document)
|
113
|
-
end
|
114
|
-
it "should have a database" do
|
115
|
-
@doc.database.should == @db
|
116
|
-
end
|
117
|
-
it "should be saveable and resavable" do
|
118
|
-
@doc["more"] = "keys"
|
119
|
-
@doc.save
|
120
|
-
@db.get(@resp['id'])["more"].should == "keys"
|
121
|
-
@doc["more"] = "these keys"
|
122
|
-
@doc.save
|
123
|
-
@db.get(@resp['id'])["more"].should == "these keys"
|
124
|
-
end
|
31
|
+
it "should not have rev and id" do
|
32
|
+
@doc.rev.should be_nil
|
33
|
+
@doc.id.should be_nil
|
125
34
|
end
|
126
|
-
|
127
|
-
|
128
|
-
before(:all) do
|
129
|
-
@db = reset_test_db!
|
130
|
-
@resp = @db.save_doc({
|
131
|
-
"key" => "value"
|
132
|
-
})
|
133
|
-
@doc = @db.get @resp['id']
|
134
|
-
end
|
135
|
-
it "should make it disappear" do
|
136
|
-
@doc.destroy
|
137
|
-
lambda{@db.get @resp['id']}.should raise_error
|
138
|
-
end
|
139
|
-
it "should error when there's no db" do
|
140
|
-
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
141
|
-
lambda{@doc.destroy}.should raise_error(ArgumentError)
|
142
|
-
end
|
35
|
+
it "should freak out when saving without a database" do
|
36
|
+
lambda{@doc.save}.should raise_error(ArgumentError)
|
143
37
|
end
|
38
|
+
end
|
144
39
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
it "should defer actual deletion" do
|
155
|
-
@doc.destroy(true)
|
156
|
-
@doc['_id'].should == nil
|
157
|
-
@doc['_rev'].should == nil
|
158
|
-
lambda{@db.get @resp['id']}.should_not raise_error
|
159
|
-
@db.bulk_save
|
160
|
-
lambda{@db.get @resp['id']}.should raise_error
|
161
|
-
end
|
40
|
+
# move to database spec
|
41
|
+
describe CouchRest::Document, "saving using a database" do
|
42
|
+
before(:all) do
|
43
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
44
|
+
@db = reset_test_db!
|
45
|
+
@resp = @db.save(@doc)
|
46
|
+
end
|
47
|
+
it "should apply the database" do
|
48
|
+
@doc.database.should == @db
|
162
49
|
end
|
50
|
+
it "should get id and rev" do
|
51
|
+
@doc.id.should == @resp["id"]
|
52
|
+
@doc.rev.should == @resp["rev"]
|
53
|
+
end
|
54
|
+
end
|
163
55
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
@resp = @db.save_doc({'key' => 'value'})
|
168
|
-
@docid = 'new-location'
|
169
|
-
@doc = @db.get(@resp['id'])
|
170
|
-
end
|
171
|
-
describe "to a new location" do
|
172
|
-
it "should work" do
|
173
|
-
@doc.copy @docid
|
174
|
-
newdoc = @db.get(@docid)
|
175
|
-
newdoc['key'].should == 'value'
|
176
|
-
end
|
177
|
-
it "should fail without a database" do
|
178
|
-
lambda{CouchRest::Document.new({"not"=>"a real doc"}).copy}.should raise_error(ArgumentError)
|
179
|
-
end
|
180
|
-
end
|
181
|
-
describe "to an existing location" do
|
182
|
-
before :each do
|
183
|
-
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
184
|
-
end
|
185
|
-
it "should fail without a rev" do
|
186
|
-
lambda{@doc.copy @docid}.should raise_error(RestClient::RequestFailed)
|
187
|
-
end
|
188
|
-
it "should succeed with a rev" do
|
189
|
-
@to_be_overwritten = @db.get(@docid)
|
190
|
-
@doc.copy "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
191
|
-
newdoc = @db.get(@docid)
|
192
|
-
newdoc['key'].should == 'value'
|
193
|
-
end
|
194
|
-
it "should succeed given the doc to overwrite" do
|
195
|
-
@to_be_overwritten = @db.get(@docid)
|
196
|
-
@doc.copy @to_be_overwritten
|
197
|
-
newdoc = @db.get(@docid)
|
198
|
-
newdoc['key'].should == 'value'
|
199
|
-
end
|
200
|
-
end
|
56
|
+
describe CouchRest::Document, "bulk saving" do
|
57
|
+
before :all do
|
58
|
+
@db = reset_test_db!
|
201
59
|
end
|
202
60
|
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
describe "to a new location" do
|
211
|
-
it "should work" do
|
212
|
-
@doc.move @docid
|
213
|
-
newdoc = @db.get(@docid)
|
214
|
-
newdoc['key'].should == 'value'
|
215
|
-
lambda {@db.get(@resp['id'])}.should raise_error(RestClient::ResourceNotFound)
|
216
|
-
end
|
217
|
-
it "should fail without a database" do
|
218
|
-
lambda{CouchRest::Document.new({"not"=>"a real doc"}).move}.should raise_error(ArgumentError)
|
219
|
-
lambda{CouchRest::Document.new({"_id"=>"not a real doc"}).move}.should raise_error(ArgumentError)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
describe "to an existing location" do
|
223
|
-
before :each do
|
224
|
-
@db.save_doc({'_id' => @docid, 'will-exist' => 'here'})
|
225
|
-
end
|
226
|
-
it "should fail without a rev" do
|
227
|
-
lambda{@doc.move @docid}.should raise_error(RestClient::RequestFailed)
|
228
|
-
lambda{@db.get(@resp['id'])}.should_not raise_error
|
229
|
-
end
|
230
|
-
it "should succeed with a rev" do
|
231
|
-
@to_be_overwritten = @db.get(@docid)
|
232
|
-
@doc.move "#{@docid}?rev=#{@to_be_overwritten['_rev']}"
|
233
|
-
newdoc = @db.get(@docid)
|
234
|
-
newdoc['key'].should == 'value'
|
235
|
-
lambda {@db.get(@resp['id'])}.should raise_error(RestClient::ResourceNotFound)
|
236
|
-
end
|
237
|
-
it "should succeed given the doc to overwrite" do
|
238
|
-
@to_be_overwritten = @db.get(@docid)
|
239
|
-
@doc.move @to_be_overwritten
|
240
|
-
newdoc = @db.get(@docid)
|
241
|
-
newdoc['key'].should == 'value'
|
242
|
-
lambda {@db.get(@resp['id'])}.should raise_error(RestClient::ResourceNotFound)
|
243
|
-
end
|
244
|
-
end
|
61
|
+
it "should use the document bulk save cache" do
|
62
|
+
doc = CouchRest::Document.new({"_id" => "bulkdoc", "val" => 3})
|
63
|
+
doc.database = @db
|
64
|
+
doc.save(true)
|
65
|
+
lambda { doc.database.get(doc["_id"]) }.should raise_error(RestClient::ResourceNotFound)
|
66
|
+
doc.database.bulk_save
|
67
|
+
doc.database.get(doc["_id"])["val"].should == doc["val"]
|
245
68
|
end
|
246
69
|
end
|
247
70
|
|
248
|
-
describe "
|
249
|
-
before do
|
71
|
+
describe "getting from a database" do
|
72
|
+
before(:all) do
|
250
73
|
@db = reset_test_db!
|
251
|
-
@
|
252
|
-
|
253
|
-
|
74
|
+
@resp = @db.save({
|
75
|
+
"key" => "value"
|
76
|
+
})
|
77
|
+
@doc = @db.get @resp['id']
|
78
|
+
end
|
79
|
+
it "should return a document" do
|
80
|
+
@doc.should be_an_instance_of(CouchRest::Document)
|
81
|
+
end
|
82
|
+
it "should have a database" do
|
83
|
+
@doc.database.should == @db
|
254
84
|
end
|
255
|
-
|
256
|
-
|
257
|
-
@doc['_attachments'] ||= {}
|
258
|
-
@doc['_attachments'][name] = {
|
259
|
-
'type' => 'text/html',
|
260
|
-
'data' => attach
|
261
|
-
}
|
85
|
+
it "should be saveable and resavable" do
|
86
|
+
@doc["more"] = "keys"
|
262
87
|
@doc.save
|
263
|
-
@
|
88
|
+
@db.get(@resp['id'])["more"].should == "keys"
|
89
|
+
@doc["more"] = "these keys"
|
90
|
+
@doc.save
|
91
|
+
@db.get(@resp['id'])["more"].should == "these keys"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "destroying a document from a db" do
|
96
|
+
before(:all) do
|
97
|
+
@db = reset_test_db!
|
98
|
+
@resp = @db.save({
|
99
|
+
"key" => "value"
|
100
|
+
})
|
101
|
+
@doc = @db.get @resp['id']
|
264
102
|
end
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
@doc.put_attachment('test.html', @attach)
|
269
|
-
end
|
270
|
-
|
271
|
-
it "is there" do
|
272
|
-
@db.fetch_attachment(@doc, 'test.html').should == @attach
|
273
|
-
end
|
274
|
-
|
275
|
-
it "updates the revision" do
|
276
|
-
@doc['_rev'].should_not == @rev
|
277
|
-
end
|
278
|
-
|
279
|
-
it "updates attachments" do
|
280
|
-
@attach2 = "<html><head><title>My Doc</title></head><body><p>Is Different.</p></body></html>"
|
281
|
-
@doc.put_attachment('test.html', @attach2)
|
282
|
-
@db.fetch_attachment(@doc, 'test.html').should == @attach2
|
283
|
-
end
|
103
|
+
it "should make it disappear" do
|
104
|
+
@doc.destroy
|
105
|
+
lambda{@db.get @resp['id']}.should raise_error
|
284
106
|
end
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
append_attachment
|
289
|
-
end
|
290
|
-
|
291
|
-
it "pulls the attachment" do
|
292
|
-
@doc.fetch_attachment('test.html').should == @attach
|
293
|
-
end
|
107
|
+
it "should error when there's no db" do
|
108
|
+
@doc = CouchRest::Document.new("key" => [1,2,3], :more => "values")
|
109
|
+
lambda{@doc.destroy}.should raise_error(ArgumentError)
|
294
110
|
end
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
describe "destroying a document from a db using bulk save" do
|
115
|
+
before(:all) do
|
116
|
+
@db = reset_test_db!
|
117
|
+
@resp = @db.save({
|
118
|
+
"key" => "value"
|
119
|
+
})
|
120
|
+
@doc = @db.get @resp['id']
|
121
|
+
end
|
122
|
+
it "should defer actual deletion" do
|
123
|
+
@doc.destroy(true)
|
124
|
+
@doc['_id'].should == nil
|
125
|
+
@doc['_rev'].should == nil
|
126
|
+
lambda{@db.get @resp['id']}.should_not raise_error
|
127
|
+
@db.bulk_save
|
128
|
+
lambda{@db.get @resp['id']}.should raise_error
|
309
129
|
end
|
310
|
-
|
311
|
-
end
|
130
|
+
end
|