sporkd-couchrest 0.30
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +176 -0
- data/README.md +176 -0
- data/Rakefile +74 -0
- data/THANKS.md +18 -0
- data/examples/model/example.rb +144 -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 +33 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/core/database.rb +317 -0
- data/lib/couchrest/core/design.rb +79 -0
- data/lib/couchrest/core/document.rb +84 -0
- data/lib/couchrest/core/response.rb +16 -0
- data/lib/couchrest/core/server.rb +88 -0
- data/lib/couchrest/core/view.rb +4 -0
- data/lib/couchrest/helper/pager.rb +103 -0
- data/lib/couchrest/helper/streamer.rb +44 -0
- data/lib/couchrest/helper/upgrade.rb +51 -0
- data/lib/couchrest/mixins/attachments.rb +31 -0
- data/lib/couchrest/mixins/callbacks.rb +532 -0
- data/lib/couchrest/mixins/class_proxy.rb +112 -0
- data/lib/couchrest/mixins/collection.rb +222 -0
- data/lib/couchrest/mixins/design_doc.rb +98 -0
- data/lib/couchrest/mixins/document_queries.rb +51 -0
- data/lib/couchrest/mixins/extended_attachments.rb +74 -0
- data/lib/couchrest/mixins/extended_document_mixins.rb +8 -0
- data/lib/couchrest/mixins/properties.rb +181 -0
- data/lib/couchrest/mixins/validation.rb +246 -0
- data/lib/couchrest/mixins/views.rb +173 -0
- data/lib/couchrest/mixins.rb +4 -0
- data/lib/couchrest/monkeypatches.rb +113 -0
- data/lib/couchrest/more/casted_model.rb +57 -0
- data/lib/couchrest/more/extended_document.rb +283 -0
- data/lib/couchrest/more/property.rb +59 -0
- data/lib/couchrest/support/blank.rb +42 -0
- data/lib/couchrest/support/class.rb +190 -0
- data/lib/couchrest/support/rails.rb +47 -0
- data/lib/couchrest/validation/auto_validate.rb +161 -0
- data/lib/couchrest/validation/contextual_validators.rb +78 -0
- data/lib/couchrest/validation/validation_errors.rb +118 -0
- data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
- data/lib/couchrest/validation/validators/confirmation_validator.rb +99 -0
- data/lib/couchrest/validation/validators/format_validator.rb +117 -0
- data/lib/couchrest/validation/validators/formats/email.rb +66 -0
- data/lib/couchrest/validation/validators/formats/url.rb +43 -0
- data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
- data/lib/couchrest/validation/validators/length_validator.rb +134 -0
- data/lib/couchrest/validation/validators/method_validator.rb +89 -0
- data/lib/couchrest/validation/validators/numeric_validator.rb +104 -0
- data/lib/couchrest/validation/validators/required_field_validator.rb +109 -0
- data/lib/couchrest.rb +200 -0
- data/spec/couchrest/core/couchrest_spec.rb +201 -0
- data/spec/couchrest/core/database_spec.rb +700 -0
- data/spec/couchrest/core/design_spec.rb +138 -0
- data/spec/couchrest/core/document_spec.rb +267 -0
- data/spec/couchrest/core/server_spec.rb +35 -0
- data/spec/couchrest/helpers/pager_spec.rb +122 -0
- data/spec/couchrest/helpers/streamer_spec.rb +23 -0
- data/spec/couchrest/more/casted_extended_doc_spec.rb +73 -0
- data/spec/couchrest/more/casted_model_spec.rb +406 -0
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +135 -0
- data/spec/couchrest/more/extended_doc_spec.rb +712 -0
- data/spec/couchrest/more/extended_doc_subclass_spec.rb +98 -0
- data/spec/couchrest/more/extended_doc_view_spec.rb +415 -0
- data/spec/couchrest/more/property_spec.rb +233 -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/more/article.rb +34 -0
- data/spec/fixtures/more/card.rb +22 -0
- data/spec/fixtures/more/cat.rb +19 -0
- data/spec/fixtures/more/course.rb +14 -0
- data/spec/fixtures/more/event.rb +6 -0
- data/spec/fixtures/more/invoice.rb +17 -0
- data/spec/fixtures/more/person.rb +9 -0
- data/spec/fixtures/more/question.rb +6 -0
- data/spec/fixtures/more/service.rb +12 -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 +6 -0
- data/spec/spec_helper.rb +37 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +194 -0
@@ -0,0 +1,712 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
require File.join(FIXTURE_PATH, 'more', 'article')
|
3
|
+
require File.join(FIXTURE_PATH, 'more', 'course')
|
4
|
+
require File.join(FIXTURE_PATH, 'more', 'cat')
|
5
|
+
|
6
|
+
|
7
|
+
describe "ExtendedDocument" do
|
8
|
+
|
9
|
+
class WithDefaultValues < CouchRest::ExtendedDocument
|
10
|
+
use_database TEST_SERVER.default_database
|
11
|
+
property :preset, :default => {:right => 10, :top_align => false}
|
12
|
+
property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
|
13
|
+
property :tags, :default => []
|
14
|
+
property :read_only_with_default, :default => 'generic', :read_only => true
|
15
|
+
property :default_false, :default => false
|
16
|
+
property :name
|
17
|
+
timestamps!
|
18
|
+
end
|
19
|
+
|
20
|
+
class WithCallBacks < CouchRest::ExtendedDocument
|
21
|
+
include ::CouchRest::Validation
|
22
|
+
use_database TEST_SERVER.default_database
|
23
|
+
property :name
|
24
|
+
property :run_before_validate
|
25
|
+
property :run_after_validate
|
26
|
+
property :run_before_save
|
27
|
+
property :run_after_save
|
28
|
+
property :run_before_create
|
29
|
+
property :run_after_create
|
30
|
+
property :run_before_update
|
31
|
+
property :run_after_update
|
32
|
+
|
33
|
+
before_validate do |object|
|
34
|
+
object.run_before_validate = true
|
35
|
+
end
|
36
|
+
after_validate do |object|
|
37
|
+
object.run_after_validate = true
|
38
|
+
end
|
39
|
+
before_save do |object|
|
40
|
+
object.run_before_save = true
|
41
|
+
end
|
42
|
+
after_save do |object|
|
43
|
+
object.run_after_save = true
|
44
|
+
end
|
45
|
+
before_create do |object|
|
46
|
+
object.run_before_create = true
|
47
|
+
end
|
48
|
+
after_create do |object|
|
49
|
+
object.run_after_create = true
|
50
|
+
end
|
51
|
+
before_update do |object|
|
52
|
+
object.run_before_update = true
|
53
|
+
end
|
54
|
+
after_update do |object|
|
55
|
+
object.run_after_update = true
|
56
|
+
end
|
57
|
+
|
58
|
+
property :run_one
|
59
|
+
property :run_two
|
60
|
+
property :run_three
|
61
|
+
|
62
|
+
before_save :run_one_method, :run_two_method do |object|
|
63
|
+
object.run_three = true
|
64
|
+
end
|
65
|
+
def run_one_method
|
66
|
+
self.run_one = true
|
67
|
+
end
|
68
|
+
def run_two_method
|
69
|
+
self.run_two = true
|
70
|
+
end
|
71
|
+
|
72
|
+
attr_accessor :run_it
|
73
|
+
property :conditional_one
|
74
|
+
property :conditional_two
|
75
|
+
|
76
|
+
before_save :conditional_one_method, :conditional_two_method, :if => proc { self.run_it }
|
77
|
+
def conditional_one_method
|
78
|
+
self.conditional_one = true
|
79
|
+
end
|
80
|
+
def conditional_two_method
|
81
|
+
self.conditional_two = true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class WithTemplateAndUniqueID < CouchRest::ExtendedDocument
|
86
|
+
use_database TEST_SERVER.default_database
|
87
|
+
unique_id do |model|
|
88
|
+
model['important-field']
|
89
|
+
end
|
90
|
+
property :preset, :default => 'value'
|
91
|
+
property :has_no_default
|
92
|
+
end
|
93
|
+
|
94
|
+
class WithGetterAndSetterMethods < CouchRest::ExtendedDocument
|
95
|
+
use_database TEST_SERVER.default_database
|
96
|
+
|
97
|
+
property :other_arg
|
98
|
+
def arg
|
99
|
+
other_arg
|
100
|
+
end
|
101
|
+
|
102
|
+
def arg=(value)
|
103
|
+
self.other_arg = "foo-#{value}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
before(:each) do
|
108
|
+
@obj = WithDefaultValues.new
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "instance database connection" do
|
112
|
+
it "should use the default database" do
|
113
|
+
@obj.database.name.should == 'couchrest-test'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should override the default db" do
|
117
|
+
@obj.database = TEST_SERVER.database!('couchrest-extendedmodel-test')
|
118
|
+
@obj.database.name.should == 'couchrest-extendedmodel-test'
|
119
|
+
@obj.database.delete!
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "a new model" do
|
124
|
+
it "should be a new document" do
|
125
|
+
@obj = Basic.new
|
126
|
+
@obj.rev.should be_nil
|
127
|
+
@obj.should be_new
|
128
|
+
@obj.should be_new_document
|
129
|
+
@obj.should be_new_record
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "creating a new document" do
|
134
|
+
it "should instantialize and save a document" do
|
135
|
+
article = Article.create(:title => 'my test')
|
136
|
+
article.title.should == 'my test'
|
137
|
+
article.should_not be_new
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should trigger the create callbacks" do
|
141
|
+
doc = WithCallBacks.create(:name => 'my other test')
|
142
|
+
doc.run_before_create.should be_true
|
143
|
+
doc.run_after_create.should be_true
|
144
|
+
doc.run_before_save.should be_true
|
145
|
+
doc.run_after_save.should be_true
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "update attributes without saving" do
|
150
|
+
before(:each) do
|
151
|
+
a = Article.get "big-bad-danger" rescue nil
|
152
|
+
a.destroy if a
|
153
|
+
@art = Article.new(:title => "big bad danger")
|
154
|
+
@art.save
|
155
|
+
end
|
156
|
+
it "should work for attribute= methods" do
|
157
|
+
@art['title'].should == "big bad danger"
|
158
|
+
@art.update_attributes_without_saving('date' => Time.now, :title => "super danger")
|
159
|
+
@art['title'].should == "super danger"
|
160
|
+
end
|
161
|
+
it "should silently ignore _id" do
|
162
|
+
@art.update_attributes_without_saving('_id' => 'foobar')
|
163
|
+
@art['_id'].should_not == 'foobar'
|
164
|
+
end
|
165
|
+
it "should silently ignore _rev" do
|
166
|
+
@art.update_attributes_without_saving('_rev' => 'foobar')
|
167
|
+
@art['_rev'].should_not == 'foobar'
|
168
|
+
end
|
169
|
+
it "should silently ignore created_at" do
|
170
|
+
@art.update_attributes_without_saving('created_at' => 'foobar')
|
171
|
+
@art['created_at'].should_not == 'foobar'
|
172
|
+
end
|
173
|
+
it "should silently ignore updated_at" do
|
174
|
+
@art.update_attributes_without_saving('updated_at' => 'foobar')
|
175
|
+
@art['updated_at'].should_not == 'foobar'
|
176
|
+
end
|
177
|
+
it "should also work using attributes= alias" do
|
178
|
+
@art.respond_to?(:attributes=).should be_true
|
179
|
+
@art.attributes = {'date' => Time.now, :title => "something else"}
|
180
|
+
@art['title'].should == "something else"
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should flip out if an attribute= method is missing" do
|
184
|
+
lambda {
|
185
|
+
@art.update_attributes_without_saving('slug' => "new-slug", :title => "super danger")
|
186
|
+
}.should raise_error
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should not change other attributes if there is an error" do
|
190
|
+
lambda {
|
191
|
+
@art.update_attributes_without_saving('slug' => "new-slug", :title => "super danger")
|
192
|
+
}.should raise_error
|
193
|
+
@art['title'].should == "big bad danger"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "update attributes" do
|
198
|
+
before(:each) do
|
199
|
+
a = Article.get "big-bad-danger" rescue nil
|
200
|
+
a.destroy if a
|
201
|
+
@art = Article.new(:title => "big bad danger")
|
202
|
+
@art.save
|
203
|
+
end
|
204
|
+
it "should save" do
|
205
|
+
@art['title'].should == "big bad danger"
|
206
|
+
@art.update_attributes('date' => Time.now, :title => "super danger")
|
207
|
+
loaded = Article.get(@art.id)
|
208
|
+
loaded['title'].should == "super danger"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "with default" do
|
213
|
+
it "should have the default value set at initalization" do
|
214
|
+
@obj.preset.should == {:right => 10, :top_align => false}
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should have the default false value explicitly assigned" do
|
218
|
+
@obj.default_false.should == false
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should automatically call a proc default at initialization" do
|
222
|
+
@obj.set_by_proc.should be_an_instance_of(Time)
|
223
|
+
@obj.set_by_proc.should == @obj.set_by_proc
|
224
|
+
@obj.set_by_proc.should < Time.now
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should let you overwrite the default values" do
|
228
|
+
obj = WithDefaultValues.new(:preset => 'test')
|
229
|
+
obj.preset = 'test'
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should work with a default empty array" do
|
233
|
+
obj = WithDefaultValues.new(:tags => ['spec'])
|
234
|
+
obj.tags.should == ['spec']
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should set default value of read-only property" do
|
238
|
+
obj = WithDefaultValues.new
|
239
|
+
obj.read_only_with_default.should == 'generic'
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
describe "a doc with template values (CR::Model spec)" do
|
244
|
+
before(:all) do
|
245
|
+
WithTemplateAndUniqueID.all.map{|o| o.destroy(true)}
|
246
|
+
WithTemplateAndUniqueID.database.bulk_delete
|
247
|
+
@tmpl = WithTemplateAndUniqueID.new
|
248
|
+
@tmpl2 = WithTemplateAndUniqueID.new(:preset => 'not_value', 'important-field' => '1')
|
249
|
+
end
|
250
|
+
it "should have fields set when new" do
|
251
|
+
@tmpl.preset.should == 'value'
|
252
|
+
end
|
253
|
+
it "shouldn't override explicitly set values" do
|
254
|
+
@tmpl2.preset.should == 'not_value'
|
255
|
+
end
|
256
|
+
it "shouldn't override existing documents" do
|
257
|
+
@tmpl2.save
|
258
|
+
tmpl2_reloaded = WithTemplateAndUniqueID.get(@tmpl2.id)
|
259
|
+
@tmpl2.preset.should == 'not_value'
|
260
|
+
tmpl2_reloaded.preset.should == 'not_value'
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
describe "getting a model" do
|
265
|
+
before(:all) do
|
266
|
+
@art = Article.new(:title => 'All About Getting')
|
267
|
+
@art.save
|
268
|
+
end
|
269
|
+
it "should load and instantiate it" do
|
270
|
+
foundart = Article.get @art.id
|
271
|
+
foundart.title.should == "All About Getting"
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "getting a model with a subobjects array" do
|
276
|
+
before(:all) do
|
277
|
+
course_doc = {
|
278
|
+
"title" => "Metaphysics 200",
|
279
|
+
"questions" => [
|
280
|
+
{
|
281
|
+
"q" => "Carve the ___ of reality at the ___.",
|
282
|
+
"a" => ["beast","joints"]
|
283
|
+
},{
|
284
|
+
"q" => "Who layed the smack down on Leibniz's Law?",
|
285
|
+
"a" => "Willard Van Orman Quine"
|
286
|
+
}
|
287
|
+
]
|
288
|
+
}
|
289
|
+
r = Course.database.save_doc course_doc
|
290
|
+
@course = Course.get r['id']
|
291
|
+
end
|
292
|
+
it "should load the course" do
|
293
|
+
@course.title.should == "Metaphysics 200"
|
294
|
+
end
|
295
|
+
it "should instantiate them as such" do
|
296
|
+
@course["questions"][0].a[0].should == "beast"
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
describe "finding all instances of a model" do
|
301
|
+
before(:all) do
|
302
|
+
WithTemplateAndUniqueID.design_doc_fresh = false
|
303
|
+
WithTemplateAndUniqueID.all.map{|o| o.destroy(true)}
|
304
|
+
WithTemplateAndUniqueID.database.bulk_delete
|
305
|
+
WithTemplateAndUniqueID.new('important-field' => '1').save
|
306
|
+
WithTemplateAndUniqueID.new('important-field' => '2').save
|
307
|
+
WithTemplateAndUniqueID.new('important-field' => '3').save
|
308
|
+
WithTemplateAndUniqueID.new('important-field' => '4').save
|
309
|
+
end
|
310
|
+
it "should make the design doc" do
|
311
|
+
WithTemplateAndUniqueID.all
|
312
|
+
d = WithTemplateAndUniqueID.design_doc
|
313
|
+
d['views']['all']['map'].should include('WithTemplateAndUniqueID')
|
314
|
+
end
|
315
|
+
it "should find all" do
|
316
|
+
rs = WithTemplateAndUniqueID.all
|
317
|
+
rs.length.should == 4
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
describe "counting all instances of a model" do
|
322
|
+
before(:each) do
|
323
|
+
@db = reset_test_db!
|
324
|
+
WithTemplateAndUniqueID.design_doc_fresh = false
|
325
|
+
end
|
326
|
+
|
327
|
+
it ".count should return 0 if there are no docuemtns" do
|
328
|
+
WithTemplateAndUniqueID.count.should == 0
|
329
|
+
end
|
330
|
+
|
331
|
+
it ".count should return the number of documents" do
|
332
|
+
WithTemplateAndUniqueID.new('important-field' => '1').save
|
333
|
+
WithTemplateAndUniqueID.new('important-field' => '2').save
|
334
|
+
WithTemplateAndUniqueID.new('important-field' => '3').save
|
335
|
+
|
336
|
+
WithTemplateAndUniqueID.count.should == 3
|
337
|
+
end
|
338
|
+
end
|
339
|
+
|
340
|
+
describe "finding the first instance of a model" do
|
341
|
+
before(:each) do
|
342
|
+
@db = reset_test_db!
|
343
|
+
WithTemplateAndUniqueID.design_doc_fresh = false
|
344
|
+
WithTemplateAndUniqueID.new('important-field' => '1').save
|
345
|
+
WithTemplateAndUniqueID.new('important-field' => '2').save
|
346
|
+
WithTemplateAndUniqueID.new('important-field' => '3').save
|
347
|
+
WithTemplateAndUniqueID.new('important-field' => '4').save
|
348
|
+
end
|
349
|
+
it "should make the design doc" do
|
350
|
+
WithTemplateAndUniqueID.all
|
351
|
+
d = WithTemplateAndUniqueID.design_doc
|
352
|
+
d['views']['all']['map'].should include('WithTemplateAndUniqueID')
|
353
|
+
end
|
354
|
+
it "should find first" do
|
355
|
+
rs = WithTemplateAndUniqueID.first
|
356
|
+
rs['important-field'].should == "1"
|
357
|
+
end
|
358
|
+
it "should return nil if no instances are found" do
|
359
|
+
WithTemplateAndUniqueID.all.each {|obj| obj.destroy }
|
360
|
+
WithTemplateAndUniqueID.first.should be_nil
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
describe "getting a model with a subobject field" do
|
365
|
+
before(:all) do
|
366
|
+
course_doc = {
|
367
|
+
"title" => "Metaphysics 410",
|
368
|
+
"professor" => {
|
369
|
+
"name" => ["Mark", "Hinchliff"]
|
370
|
+
},
|
371
|
+
"final_test_at" => "2008/12/19 13:00:00 +0800"
|
372
|
+
}
|
373
|
+
r = Course.database.save_doc course_doc
|
374
|
+
@course = Course.get r['id']
|
375
|
+
end
|
376
|
+
it "should load the course" do
|
377
|
+
@course["professor"]["name"][1].should == "Hinchliff"
|
378
|
+
end
|
379
|
+
it "should instantiate the professor as a person" do
|
380
|
+
@course['professor'].last_name.should == "Hinchliff"
|
381
|
+
end
|
382
|
+
it "should instantiate the final_test_at as a Time" do
|
383
|
+
@course['final_test_at'].should == Time.parse("2008/12/19 13:00:00 +0800")
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
describe "timestamping" do
|
388
|
+
before(:each) do
|
389
|
+
oldart = Article.get "saving-this" rescue nil
|
390
|
+
oldart.destroy if oldart
|
391
|
+
@art = Article.new(:title => "Saving this")
|
392
|
+
@art.save
|
393
|
+
end
|
394
|
+
|
395
|
+
it "should define the updated_at and created_at getters and set the values" do
|
396
|
+
@obj.save
|
397
|
+
obj = WithDefaultValues.get(@obj.id)
|
398
|
+
obj.should be_an_instance_of(WithDefaultValues)
|
399
|
+
obj.created_at.should be_an_instance_of(Time)
|
400
|
+
obj.updated_at.should be_an_instance_of(Time)
|
401
|
+
obj.created_at.to_s.should == @obj.updated_at.to_s
|
402
|
+
end
|
403
|
+
it "should set the time on create" do
|
404
|
+
(Time.now - @art.created_at).should < 2
|
405
|
+
foundart = Article.get @art.id
|
406
|
+
foundart.created_at.should == foundart.updated_at
|
407
|
+
end
|
408
|
+
it "should set the time on update" do
|
409
|
+
@art.save
|
410
|
+
@art.created_at.should < @art.updated_at
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
describe "basic saving and retrieving" do
|
415
|
+
it "should work fine" do
|
416
|
+
@obj.name = "should be easily saved and retrieved"
|
417
|
+
@obj.save
|
418
|
+
saved_obj = WithDefaultValues.get(@obj.id)
|
419
|
+
saved_obj.should_not be_nil
|
420
|
+
end
|
421
|
+
|
422
|
+
it "should parse the Time attributes automatically" do
|
423
|
+
@obj.name = "should parse the Time attributes automatically"
|
424
|
+
@obj.set_by_proc.should be_an_instance_of(Time)
|
425
|
+
@obj.save
|
426
|
+
@obj.set_by_proc.should be_an_instance_of(Time)
|
427
|
+
saved_obj = WithDefaultValues.get(@obj.id)
|
428
|
+
saved_obj.set_by_proc.should be_an_instance_of(Time)
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
describe "saving a model" do
|
433
|
+
before(:all) do
|
434
|
+
@sobj = Basic.new
|
435
|
+
@sobj.save.should == true
|
436
|
+
end
|
437
|
+
|
438
|
+
it "should save the doc" do
|
439
|
+
doc = Basic.get(@sobj.id)
|
440
|
+
doc['_id'].should == @sobj.id
|
441
|
+
end
|
442
|
+
|
443
|
+
it "should be set for resaving" do
|
444
|
+
rev = @obj.rev
|
445
|
+
@sobj['another-key'] = "some value"
|
446
|
+
@sobj.save
|
447
|
+
@sobj.rev.should_not == rev
|
448
|
+
end
|
449
|
+
|
450
|
+
it "should set the id" do
|
451
|
+
@sobj.id.should be_an_instance_of(String)
|
452
|
+
end
|
453
|
+
|
454
|
+
it "should set the type" do
|
455
|
+
@sobj['couchrest-type'].should == 'Basic'
|
456
|
+
end
|
457
|
+
end
|
458
|
+
|
459
|
+
describe "saving a model with a unique_id configured" do
|
460
|
+
before(:each) do
|
461
|
+
@art = Article.new
|
462
|
+
@old = Article.database.get('this-is-the-title') rescue nil
|
463
|
+
Article.database.delete_doc(@old) if @old
|
464
|
+
end
|
465
|
+
|
466
|
+
it "should be a new document" do
|
467
|
+
@art.should be_new
|
468
|
+
@art.title.should be_nil
|
469
|
+
end
|
470
|
+
|
471
|
+
it "should require the title" do
|
472
|
+
lambda{@art.save}.should raise_error
|
473
|
+
@art.title = 'This is the title'
|
474
|
+
@art.save.should == true
|
475
|
+
end
|
476
|
+
|
477
|
+
it "should not change the slug on update" do
|
478
|
+
@art.title = 'This is the title'
|
479
|
+
@art.save.should == true
|
480
|
+
@art.title = 'new title'
|
481
|
+
@art.save.should == true
|
482
|
+
@art.slug.should == 'this-is-the-title'
|
483
|
+
end
|
484
|
+
|
485
|
+
it "should raise an error when the slug is taken" do
|
486
|
+
@art.title = 'This is the title'
|
487
|
+
@art.save.should == true
|
488
|
+
@art2 = Article.new(:title => 'This is the title!')
|
489
|
+
lambda{@art2.save}.should raise_error
|
490
|
+
end
|
491
|
+
|
492
|
+
it "should set the slug" do
|
493
|
+
@art.title = 'This is the title'
|
494
|
+
@art.save.should == true
|
495
|
+
@art.slug.should == 'this-is-the-title'
|
496
|
+
end
|
497
|
+
|
498
|
+
it "should set the id" do
|
499
|
+
@art.title = 'This is the title'
|
500
|
+
@art.save.should == true
|
501
|
+
@art.id.should == 'this-is-the-title'
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
describe "saving a model with a unique_id lambda" do
|
506
|
+
before(:each) do
|
507
|
+
@templated = WithTemplateAndUniqueID.new
|
508
|
+
@old = WithTemplateAndUniqueID.get('very-important') rescue nil
|
509
|
+
@old.destroy if @old
|
510
|
+
end
|
511
|
+
|
512
|
+
it "should require the field" do
|
513
|
+
lambda{@templated.save}.should raise_error
|
514
|
+
@templated['important-field'] = 'very-important'
|
515
|
+
@templated.save.should == true
|
516
|
+
end
|
517
|
+
|
518
|
+
it "should save with the id" do
|
519
|
+
@templated['important-field'] = 'very-important'
|
520
|
+
@templated.save.should == true
|
521
|
+
t = WithTemplateAndUniqueID.get('very-important')
|
522
|
+
t.should == @templated
|
523
|
+
end
|
524
|
+
|
525
|
+
it "should not change the id on update" do
|
526
|
+
@templated['important-field'] = 'very-important'
|
527
|
+
@templated.save.should == true
|
528
|
+
@templated['important-field'] = 'not-important'
|
529
|
+
@templated.save.should == true
|
530
|
+
t = WithTemplateAndUniqueID.get('very-important')
|
531
|
+
t.should == @templated
|
532
|
+
end
|
533
|
+
|
534
|
+
it "should raise an error when the id is taken" do
|
535
|
+
@templated['important-field'] = 'very-important'
|
536
|
+
@templated.save.should == true
|
537
|
+
lambda{WithTemplateAndUniqueID.new('important-field' => 'very-important').save}.should raise_error
|
538
|
+
end
|
539
|
+
|
540
|
+
it "should set the id" do
|
541
|
+
@templated['important-field'] = 'very-important'
|
542
|
+
@templated.save.should == true
|
543
|
+
@templated.id.should == 'very-important'
|
544
|
+
end
|
545
|
+
end
|
546
|
+
|
547
|
+
describe "destroying an instance" do
|
548
|
+
before(:each) do
|
549
|
+
@dobj = Basic.new
|
550
|
+
@dobj.save.should == true
|
551
|
+
end
|
552
|
+
it "should return true" do
|
553
|
+
result = @dobj.destroy
|
554
|
+
result.should == true
|
555
|
+
end
|
556
|
+
it "should be resavable" do
|
557
|
+
@dobj.destroy
|
558
|
+
@dobj.rev.should be_nil
|
559
|
+
@dobj.id.should be_nil
|
560
|
+
@dobj.save.should == true
|
561
|
+
end
|
562
|
+
it "should make it go away" do
|
563
|
+
@dobj.destroy
|
564
|
+
lambda{Basic.get(@dobj.id)}.should raise_error
|
565
|
+
end
|
566
|
+
end
|
567
|
+
|
568
|
+
|
569
|
+
describe "callbacks" do
|
570
|
+
|
571
|
+
before(:each) do
|
572
|
+
@doc = WithCallBacks.new
|
573
|
+
end
|
574
|
+
|
575
|
+
|
576
|
+
describe "validate" do
|
577
|
+
it "should run before_validate before validating" do
|
578
|
+
@doc.run_before_validate.should be_nil
|
579
|
+
@doc.should be_valid
|
580
|
+
@doc.run_before_validate.should be_true
|
581
|
+
end
|
582
|
+
it "should run after_validate after validating" do
|
583
|
+
@doc.run_after_validate.should be_nil
|
584
|
+
@doc.should be_valid
|
585
|
+
@doc.run_after_validate.should be_true
|
586
|
+
end
|
587
|
+
end
|
588
|
+
describe "save" do
|
589
|
+
it "should run the after filter after saving" do
|
590
|
+
@doc.run_after_save.should be_nil
|
591
|
+
@doc.save.should be_true
|
592
|
+
@doc.run_after_save.should be_true
|
593
|
+
end
|
594
|
+
it "should run the grouped callbacks before saving" do
|
595
|
+
@doc.run_one.should be_nil
|
596
|
+
@doc.run_two.should be_nil
|
597
|
+
@doc.run_three.should be_nil
|
598
|
+
@doc.save.should be_true
|
599
|
+
@doc.run_one.should be_true
|
600
|
+
@doc.run_two.should be_true
|
601
|
+
@doc.run_three.should be_true
|
602
|
+
end
|
603
|
+
it "should not run conditional callbacks" do
|
604
|
+
@doc.run_it = false
|
605
|
+
@doc.save.should be_true
|
606
|
+
@doc.conditional_one.should be_nil
|
607
|
+
@doc.conditional_two.should be_nil
|
608
|
+
end
|
609
|
+
it "should run conditional callbacks" do
|
610
|
+
@doc.run_it = true
|
611
|
+
@doc.save.should be_true
|
612
|
+
@doc.conditional_one.should be_true
|
613
|
+
@doc.conditional_two.should be_true
|
614
|
+
end
|
615
|
+
end
|
616
|
+
describe "create" do
|
617
|
+
it "should run the before save filter when creating" do
|
618
|
+
@doc.run_before_save.should be_nil
|
619
|
+
@doc.create.should_not be_nil
|
620
|
+
@doc.run_before_save.should be_true
|
621
|
+
end
|
622
|
+
it "should run the before create filter" do
|
623
|
+
@doc.run_before_create.should be_nil
|
624
|
+
@doc.create.should_not be_nil
|
625
|
+
@doc.create
|
626
|
+
@doc.run_before_create.should be_true
|
627
|
+
end
|
628
|
+
it "should run the after create filter" do
|
629
|
+
@doc.run_after_create.should be_nil
|
630
|
+
@doc.create.should_not be_nil
|
631
|
+
@doc.create
|
632
|
+
@doc.run_after_create.should be_true
|
633
|
+
end
|
634
|
+
end
|
635
|
+
describe "update" do
|
636
|
+
|
637
|
+
before(:each) do
|
638
|
+
@doc.save
|
639
|
+
end
|
640
|
+
it "should run the before update filter when updating an existing document" do
|
641
|
+
@doc.run_before_update.should be_nil
|
642
|
+
@doc.update
|
643
|
+
@doc.run_before_update.should be_true
|
644
|
+
end
|
645
|
+
it "should run the after update filter when updating an existing document" do
|
646
|
+
@doc.run_after_update.should be_nil
|
647
|
+
@doc.update
|
648
|
+
@doc.run_after_update.should be_true
|
649
|
+
end
|
650
|
+
it "should run the before update filter when saving an existing document" do
|
651
|
+
@doc.run_before_update.should be_nil
|
652
|
+
@doc.save
|
653
|
+
@doc.run_before_update.should be_true
|
654
|
+
end
|
655
|
+
|
656
|
+
end
|
657
|
+
end
|
658
|
+
|
659
|
+
describe "getter and setter methods" do
|
660
|
+
it "should try to call the arg= method before setting :arg in the hash" do
|
661
|
+
@doc = WithGetterAndSetterMethods.new(:arg => "foo")
|
662
|
+
@doc['arg'].should be_nil
|
663
|
+
@doc[:arg].should be_nil
|
664
|
+
@doc.other_arg.should == "foo-foo"
|
665
|
+
end
|
666
|
+
end
|
667
|
+
|
668
|
+
describe "recursive validation on an extended document" do
|
669
|
+
before :each do
|
670
|
+
reset_test_db!
|
671
|
+
@cat = Cat.new(:name => 'Sockington')
|
672
|
+
end
|
673
|
+
|
674
|
+
it "should not save if a nested casted model is invalid" do
|
675
|
+
@cat.favorite_toy = CatToy.new
|
676
|
+
@cat.should_not be_valid
|
677
|
+
@cat.save.should be_false
|
678
|
+
lambda{@cat.save!}.should raise_error
|
679
|
+
end
|
680
|
+
|
681
|
+
it "should save when nested casted model is valid" do
|
682
|
+
@cat.favorite_toy = CatToy.new(:name => 'Squeaky')
|
683
|
+
@cat.should be_valid
|
684
|
+
@cat.save.should be_true
|
685
|
+
lambda{@cat.save!}.should_not raise_error
|
686
|
+
end
|
687
|
+
|
688
|
+
it "should not save when nested collection contains an invalid casted model" do
|
689
|
+
@cat.toys = [CatToy.new(:name => 'Feather'), CatToy.new]
|
690
|
+
@cat.should_not be_valid
|
691
|
+
@cat.save.should be_false
|
692
|
+
lambda{@cat.save!}.should raise_error
|
693
|
+
end
|
694
|
+
|
695
|
+
it "should save when nested collection contains valid casted models" do
|
696
|
+
@cat.toys = [CatToy.new(:name => 'feather'), CatToy.new(:name => 'ball-o-twine')]
|
697
|
+
@cat.should be_valid
|
698
|
+
@cat.save.should be_true
|
699
|
+
lambda{@cat.save!}.should_not raise_error
|
700
|
+
end
|
701
|
+
|
702
|
+
it "should not fail if the nested casted model doesn't have validation" do
|
703
|
+
Cat.property :trainer, :cast_as => 'Person'
|
704
|
+
Cat.validates_present :name
|
705
|
+
cat = Cat.new(:name => 'Mr Bigglesworth')
|
706
|
+
cat.trainer = Person.new
|
707
|
+
cat.trainer.validatable?.should be_false
|
708
|
+
cat.should be_valid
|
709
|
+
cat.save.should be_true
|
710
|
+
end
|
711
|
+
end
|
712
|
+
end
|