sequel 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/hooks_spec.rb CHANGED
@@ -196,9 +196,16 @@ describe "Model#before_create && Model#after_create" do
196
196
  ]
197
197
  end
198
198
 
199
- specify "should cancel the save if before_create returns false" do
199
+ specify ".create should cancel the save and raise an error if before_create returns false and raise_on_save_failure is true" do
200
200
  @c.before_create{false}
201
- @c.create(:x => 2).should == false
201
+ proc{@c.create(:x => 2)}.should raise_error(Sequel::Error)
202
+ MODEL_DB.sqls.should == []
203
+ end
204
+
205
+ specify ".create should cancel the save and return nil if before_create returns false and raise_on_save_failure is false" do
206
+ @c.before_create{false}
207
+ @c.raise_on_save_failure = false
208
+ @c.create(:x => 2).should == nil
202
209
  MODEL_DB.sqls.should == []
203
210
  end
204
211
  end
@@ -223,9 +230,16 @@ describe "Model#before_update && Model#after_update" do
223
230
  ]
224
231
  end
225
232
 
226
- specify "should cancel the save if before_update returns false" do
233
+ specify "#save should cancel the save and raise an error if before_update returns false and raise_on_save_failure is true" do
227
234
  @c.before_update{false}
228
- @c.load(:id => 2233).save.should == false
235
+ proc{@c.load(:id => 2233).save}.should raise_error(Sequel::Error)
236
+ MODEL_DB.sqls.should == []
237
+ end
238
+
239
+ specify "#save should cancel the save and return nil if before_update returns false and raise_on_save_failure is false" do
240
+ @c.before_update{false}
241
+ @c.raise_on_save_failure = false
242
+ @c.load(:id => 2233).save.should == nil
229
243
  MODEL_DB.sqls.should == []
230
244
  end
231
245
  end
@@ -236,12 +250,12 @@ describe "Model#before_save && Model#after_save" do
236
250
 
237
251
  @c = Class.new(Sequel::Model(:items)) do
238
252
  columns :x
239
- before_save {MODEL_DB << "BLAH before"}
240
253
  after_save {MODEL_DB << "BLAH after"}
241
254
  end
242
255
  end
243
256
 
244
257
  specify "should be called around record update" do
258
+ @c.before_save {MODEL_DB << "BLAH before"}
245
259
  m = @c.load(:id => 2233)
246
260
  m.save
247
261
  MODEL_DB.sqls.should == [
@@ -252,6 +266,7 @@ describe "Model#before_save && Model#after_save" do
252
266
  end
253
267
 
254
268
  specify "should be called around record creation" do
269
+ @c.before_save {MODEL_DB << "BLAH before"}
255
270
  @c.no_primary_key
256
271
  @c.create(:x => 2)
257
272
  MODEL_DB.sqls.should == [
@@ -261,10 +276,17 @@ describe "Model#before_save && Model#after_save" do
261
276
  ]
262
277
  end
263
278
 
264
- specify "should cancel the save if before_save returns false" do
279
+ specify "#save should cancel the save and raise an error if before_save returns false and raise_on_save_failure is true" do
280
+ @c.before_save{false}
281
+ proc{@c.load(:id => 2233).save}.should raise_error(Sequel::Error)
282
+ MODEL_DB.sqls.should == []
283
+ end
284
+
285
+ specify "#save should cancel the save and return nil if before_save returns false and raise_on_save_failure is false" do
265
286
  @c.before_save{false}
266
- @c.load(:id => 2233).save.should == false
267
- MODEL_DB.sqls.should == ["BLAH before"]
287
+ @c.raise_on_save_failure = false
288
+ @c.load(:id => 2233).save.should == nil
289
+ MODEL_DB.sqls.should == []
268
290
  end
269
291
  end
270
292
 
@@ -292,9 +314,16 @@ describe "Model#before_destroy && Model#after_destroy" do
292
314
  ]
293
315
  end
294
316
 
295
- specify "should cancel the destroy if before_destroy returns false" do
317
+ specify "#destroy should cancel the destroy and raise an error if before_destroy returns false and raise_on_save_failure is true" do
318
+ @c.before_destroy{false}
319
+ proc{@c.load(:id => 2233).destroy}.should raise_error(Sequel::Error)
320
+ MODEL_DB.sqls.should == []
321
+ end
322
+
323
+ specify "#destroy should cancel the destroy and return nil if before_destroy returns false and raise_on_save_failure is false" do
296
324
  @c.before_destroy{false}
297
- @c.load(:id => 2233).destroy.should == false
325
+ @c.raise_on_save_failure = false
326
+ @c.load(:id => 2233).destroy.should == nil
298
327
  MODEL_DB.sqls.should == []
299
328
  end
300
329
  end
@@ -304,7 +333,6 @@ describe "Model#before_validation && Model#after_validation" do
304
333
  MODEL_DB.reset
305
334
 
306
335
  @c = Class.new(Sequel::Model(:items)) do
307
- before_validation{MODEL_DB << "BLAH before"}
308
336
  after_validation{MODEL_DB << "BLAH after"}
309
337
 
310
338
  def self.validate(o)
@@ -320,6 +348,7 @@ describe "Model#before_validation && Model#after_validation" do
320
348
  end
321
349
 
322
350
  specify "should be called around validation" do
351
+ @c.before_validation{MODEL_DB << "BLAH before"}
323
352
  m = @c.new(:id => 2233)
324
353
  m.should be_valid
325
354
  MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
@@ -331,20 +360,29 @@ describe "Model#before_validation && Model#after_validation" do
331
360
  end
332
361
 
333
362
  specify "should be called when calling save" do
363
+ @c.before_validation{MODEL_DB << "BLAH before"}
334
364
  m = @c.new(:id => 2233)
335
365
  m.save.should == m
336
366
  MODEL_DB.sqls.should == ['BLAH before', 'BLAH after', 'CREATE BLAH']
337
367
 
338
368
  MODEL_DB.sqls.clear
339
369
  m = @c.new(:id => 22)
340
- m.save.should == false
370
+ m.raise_on_save_failure = false
371
+ m.save.should == nil
341
372
  MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
342
373
  end
343
374
 
344
- specify "should cancel the save if before_validation returns false" do
375
+ specify "#save should cancel the save and raise an error if before_validation returns false and raise_on_save_failure is true" do
345
376
  @c.before_validation{false}
346
- @c.load(:id => 2233).save.should == false
347
- MODEL_DB.sqls.should == ["BLAH before"]
377
+ proc{@c.load(:id => 2233).save}.should raise_error(Sequel::Error)
378
+ MODEL_DB.sqls.should == []
379
+ end
380
+
381
+ specify "#save should cancel the save and return nil if before_validation returns false and raise_on_save_failure is false" do
382
+ @c.before_validation{false}
383
+ @c.raise_on_save_failure = false
384
+ @c.load(:id => 2233).save.should == nil
385
+ MODEL_DB.sqls.should == []
348
386
  end
349
387
  end
350
388
 
data/spec/model_spec.rb CHANGED
@@ -205,21 +205,21 @@ describe Sequel::Model, ".subset" do
205
205
  @c = Class.new(Sequel::Model(:items))
206
206
  end
207
207
 
208
- pt_specify "should create a filter on the underlying dataset" do
208
+ specify "should create a filter on the underlying dataset" do
209
209
  proc {@c.new_only}.should raise_error(NoMethodError)
210
210
 
211
- @c.subset(:new_only) {:age == 'new'}
211
+ @c.subset(:new_only) {:age < 'new'}
212
212
 
213
- @c.new_only.sql.should == "SELECT * FROM items WHERE (age = 'new')"
214
- @c.dataset.new_only.sql.should == "SELECT * FROM items WHERE (age = 'new')"
213
+ @c.new_only.sql.should == "SELECT * FROM items WHERE (age < 'new')"
214
+ @c.dataset.new_only.sql.should == "SELECT * FROM items WHERE (age < 'new')"
215
215
 
216
216
  @c.subset(:pricey) {:price > 100}
217
217
 
218
218
  @c.pricey.sql.should == "SELECT * FROM items WHERE (price > 100)"
219
219
  @c.dataset.pricey.sql.should == "SELECT * FROM items WHERE (price > 100)"
220
220
 
221
- @c.pricey.new_only.sql.should == "SELECT * FROM items WHERE ((price > 100) AND (age = 'new'))"
222
- @c.new_only.pricey.sql.should == "SELECT * FROM items WHERE ((age = 'new') AND (price > 100))"
221
+ @c.pricey.new_only.sql.should == "SELECT * FROM items WHERE ((price > 100) AND (age < 'new'))"
222
+ @c.new_only.pricey.sql.should == "SELECT * FROM items WHERE ((age < 'new') AND (price > 100))"
223
223
  end
224
224
 
225
225
  end
@@ -250,11 +250,11 @@ describe Sequel::Model, ".find" do
250
250
  $sqls.last.should == "SELECT * FROM items WHERE (name LIKE 'abc%') LIMIT 1"
251
251
  end
252
252
 
253
- pt_specify "should accept filter blocks" do
254
- @c.find {:id == 1}.should be_a_kind_of(@c)
255
- $sqls.last.should == "SELECT * FROM items WHERE (id = 1) LIMIT 1"
253
+ specify "should accept filter blocks" do
254
+ @c.find{:id > 1}.should be_a_kind_of(@c)
255
+ $sqls.last.should == "SELECT * FROM items WHERE (id > 1) LIMIT 1"
256
256
 
257
- @c.find {:x > 1 && :y < 2}.should be_a_kind_of(@c)
257
+ @c.find {(:x > 1) & (:y < 2)}.should be_a_kind_of(@c)
258
258
  $sqls.last.should == "SELECT * FROM items WHERE ((x > 1) AND (y < 2)) LIMIT 1"
259
259
  end
260
260
 
@@ -552,7 +552,7 @@ context "Model.db_schema" do
552
552
  def @dataset.def_mutation_method(*names); end
553
553
  end
554
554
 
555
- specify "should use the database's schema_for_table and set the columns" do
555
+ specify "should use the database's schema_for_table and set the columns and dataset columns" do
556
556
  d = @dataset.db
557
557
  def d.schema(table, opts = {})
558
558
  [[:x, {:type=>:integer}], [:y, {:type=>:string}]]
@@ -560,6 +560,7 @@ context "Model.db_schema" do
560
560
  @c.dataset = @dataset
561
561
  @c.db_schema.should == {:x=>{:type=>:integer}, :y=>{:type=>:string}}
562
562
  @c.columns.should == [:x, :y]
563
+ @c.dataset.instance_variable_get(:@columns).should == [:x, :y]
563
564
  end
564
565
 
565
566
  specify "should restrict the schema and columns for datasets with a :select option" do
data/spec/spec_helper.rb CHANGED
@@ -65,10 +65,3 @@ class << Sequel::Model
65
65
  end
66
66
 
67
67
  Sequel::Model.db = MODEL_DB = MockDatabase.new
68
-
69
- class Spec::Example::ExampleGroup
70
- def self.pt_specify(*args, &block)
71
- specify(*args, &block) if defined?(ParseTree)
72
- end
73
- end
74
-
@@ -66,15 +66,11 @@ describe Sequel::Model do
66
66
 
67
67
  specify "should acccept validation definitions using validates_each" do
68
68
  @c.validates_each(:xx, :yy) {|o, a, v| o.errors[a] << 'too low' if v < 50}
69
-
70
- @c.validations[:xx].size.should == 1
71
- @c.validations[:yy].size.should == 1
72
-
73
69
  o = @c.new
74
- @c.validations[:xx].first.call(o, :aa, 40)
75
- @c.validations[:yy].first.call(o, :bb, 60)
76
-
77
- o.errors.full_messages.should == ['aa too low']
70
+ o.should_receive(:xx).once.and_return(40)
71
+ o.should_receive(:yy).once.and_return(60)
72
+ o.valid?.should == false
73
+ o.errors.full_messages.should == ['xx too low']
78
74
  end
79
75
 
80
76
  specify "should return true/false for has_validations?" do
@@ -83,18 +79,33 @@ describe Sequel::Model do
83
79
  @c.has_validations?.should == true
84
80
  end
85
81
 
82
+ specify "should overwrite existing validation with the same tag and attribute" do
83
+ @c.validates_each(:xx, :xx, :tag=>:low) {|o, a, v| o.xxx; o.errors[a] << 'too low' if v < 50}
84
+ @c.validates_each(:yy, :yy) {|o, a, v| o.yyy; o.errors[a] << 'too low' if v < 50}
85
+ @c.validates_presence_of(:zz, :zz)
86
+ o = @c.new
87
+ def o.zz
88
+ @a ||= 0
89
+ @a += 1
90
+ end
91
+ o.should_receive(:xx).once.and_return(40)
92
+ o.should_receive(:yy).once.and_return(60)
93
+ o.should_receive(:xxx).once
94
+ o.should_receive(:yyy).twice
95
+ o.valid?.should == false
96
+ o.zz.should == 2
97
+ o.errors.full_messages.should == ['xx too low']
98
+ end
99
+
86
100
  specify "should provide a validates method that takes block with validation definitions" do
87
101
  @c.validates do
88
102
  coolness_of :blah
89
103
  end
90
104
  @c.validations[:blah].should_not be_empty
91
-
92
105
  o = @c.new
93
- @c.validations[:blah].first.call(o, :ttt, 40)
94
- o.errors.full_messages.should == ['ttt is not cool']
95
- o.errors.clear
96
- @c.validations[:blah].first.call(o, :ttt, :cool)
97
- o.errors.should be_empty
106
+ o.should_receive(:blah).once.and_return(nil)
107
+ o.valid?.should == false
108
+ o.errors.full_messages.should == ['blah is not cool']
98
109
  end
99
110
  end
100
111
 
@@ -385,6 +396,12 @@ describe Sequel::Model do
385
396
  @m.should_not be_valid
386
397
  @m.value = 1234
387
398
  @m.should be_valid
399
+ @m.value = nil
400
+ @m.should_not be_valid
401
+ @m.value = true
402
+ @m.should be_valid
403
+ @m.value = false
404
+ @m.should be_valid
388
405
  end
389
406
 
390
407
  specify "should validate presence_of with if => true" do
@@ -753,6 +770,7 @@ describe "Model#save" do
753
770
  end
754
771
 
755
772
  specify "should save only if validations pass" do
773
+ @m.raise_on_save_failure = false
756
774
  @m.should_not be_valid
757
775
  @m.save
758
776
  MODEL_DB.sqls.should be_empty
@@ -763,7 +781,12 @@ describe "Model#save" do
763
781
  MODEL_DB.sqls.should == ['UPDATE people SET id = 5 WHERE (id = 5)']
764
782
  end
765
783
 
766
- specify "should return false if validations fail" do
767
- @m.save.should == false
784
+ specify "should raise error if validations fail and raise_on_save_faiure is true" do
785
+ proc{@m.save}.should raise_error(Sequel::Error)
786
+ end
787
+
788
+ specify "should return nil if validations fail and raise_on_save_faiure is false" do
789
+ @m.raise_on_save_failure = false
790
+ @m.save.should == nil
768
791
  end
769
792
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-17 00:00:00 -07:00
12
+ date: 2008-07-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - "="
21
21
  - !ruby/object:Gem::Version
22
- version: 2.1.0
22
+ version: 2.2.0
23
23
  version:
24
24
  description: "The Database Toolkit for Ruby: Model Classes"
25
25
  email: code@jeremyevans.net
@@ -31,10 +31,12 @@ extra_rdoc_files:
31
31
  - README
32
32
  - CHANGELOG
33
33
  - COPYING
34
+ - doc/advanced_associations.rdoc
34
35
  files:
35
36
  - COPYING
36
37
  - README
37
38
  - Rakefile
39
+ - doc/advanced_associations.rdoc
38
40
  - spec/associations_spec.rb
39
41
  - spec/base_spec.rb
40
42
  - spec/caching_spec.rb
@@ -49,8 +51,8 @@ files:
49
51
  - spec/spec_helper.rb
50
52
  - spec/validations_spec.rb
51
53
  - spec/inflector_spec.rb
52
- - spec/dataset_methods_spec.rb
53
54
  - spec/association_reflection_spec.rb
55
+ - spec/dataset_methods_spec.rb
54
56
  - lib/sequel.rb
55
57
  - lib/sequel_model.rb
56
58
  - lib/sequel_model