59
75
it "should use value returned by insert as the primary key and refresh the object" do
60
76
o = @c.new(:x => 11)
62
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (11)",
78
DB.sqls.should == ["INSERT INTO items (x) VALUES (11)",
63
79
"SELECT * FROM items WHERE (id = 13) LIMIT 1"]
66
82
it "should allow you to skip refreshing by overridding _save_refresh" do
67
83
@c.send(:define_method, :_save_refresh){}
68
84
@c.create(:x => 11)
69
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (11)"]
85
DB.sqls.should == ["INSERT INTO items (x) VALUES (11)"]
72
88
it "should work correctly for inserting a record without a primary key" do
74
90
o = @c.new(:x => 11)
76
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (11)"]
92
DB.sqls.should == ["INSERT INTO items (x) VALUES (11)"]
79
95
it "should set the autoincrementing_primary_key value to the value returned by insert" do
80
96
@c.unrestrict_primary_key
81
97
@c.set_primary_key [:x, :y]
82
98
o = @c.new(:x => 11)
83
o.meta_def(:autoincrementing_primary_key){:y}
99
def o.autoincrementing_primary_key() :y end
86
102
sqls.length.should == 2
87
103
sqls.first.should == "INSERT INTO items (x) VALUES (11)"
88
104
sqls.last.should =~ %r{SELECT \* FROM items WHERE \(\([xy] = 1[13]\) AND \([xy] = 1[13]\)\) LIMIT 1}
111
127
proc{o.save}.should_not raise_error
114
it "should update only the given columns if given" do
130
it "should respect the :columns option to specify the columns to save" do
115
131
o = @c.load(:id => 3, :x => 1, :y => nil)
117
MODEL_DB.sqls.first.should == "UPDATE items SET y = NULL WHERE (id = 3)"
133
DB.sqls.first.should == "UPDATE items SET y = NULL WHERE (id = 3)"
120
136
it "should mark saved columns as not changed" do
121
137
o = @c.load(:id => 3, :x => 1, :y => nil)
123
139
o.changed_columns.should == [:y]
125
141
o.changed_columns.should == [:y]
127
143
o.changed_columns.should == []
175
191
it "should use Model's use_transactions setting by default" do
176
192
@c.use_transactions = true
177
@c.load(:id => 3, :x => 1, :y => nil).save(:y)
178
MODEL_DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
193
@c.load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
194
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
179
195
@c.use_transactions = false
180
@c.load(:id => 3, :x => 1, :y => nil).save(:y)
181
MODEL_DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
196
@c.load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
197
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
184
200
it "should inherit Model's use_transactions setting" do
185
201
@c.use_transactions = true
186
Class.new(@c).load(:id => 3, :x => 1, :y => nil).save(:y)
187
MODEL_DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
202
Class.new(@c).load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
203
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
188
204
@c.use_transactions = false
189
Class.new(@c).load(:id => 3, :x => 1, :y => nil).save(:y)
190
MODEL_DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
205
Class.new(@c).load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
206
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
193
209
it "should use object's use_transactions setting" do
194
210
o = @c.load(:id => 3, :x => 1, :y => nil)
195
211
o.use_transactions = false
196
212
@c.use_transactions = true
198
MODEL_DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
214
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
199
215
o = @c.load(:id => 3, :x => 1, :y => nil)
200
216
o.use_transactions = true
201
217
@c.use_transactions = false
203
MODEL_DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
219
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
206
222
it "should use :transaction option if given" do
207
223
o = @c.load(:id => 3, :x => 1, :y => nil)
208
224
o.use_transactions = true
209
o.save(:y, :transaction=>false)
210
MODEL_DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
225
o.save(:columns=>:y, :transaction=>false)
226
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
211
227
o = @c.load(:id => 3, :x => 1, :y => nil)
212
228
o.use_transactions = false
213
o.save(:y, :transaction=>true)
214
MODEL_DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
229
o.save(:columns=>:y, :transaction=>true)
230
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
217
233
it "should rollback if before_save returns false and raise_on_save_failure = true" do
468
490
o.modified?.should == true
493
it "should be true if given a column argument and the column has been changed" do
495
o.modified?(:id).should be_false
497
o.modified?(:id).should be_true
501
describe "Model#modified!" do
503
@c = Class.new(Sequel::Model(:items))
510
it "should mark the object as modified so that save_changes still runs the callbacks" do
511
o = @c.load(:id=>1, :x=>2)
521
o.db.sqls.should == []
524
it "should mark given column argument as modified" do
525
o = @c.load(:id=>1, :x=>2)
527
o.changed_columns.should == [:x]
529
o.db.sqls.should == ["UPDATE items SET x = 2 WHERE (id = 1)"]
472
533
describe "Model#save_changes" do
474
535
@c = Class.new(Sequel::Model(:items)) do
475
536
unrestrict_primary_key
476
537
columns :id, :x, :y
481
542
it "should always save if the object is new" do
482
543
o = @c.new(:x => 1)
484
MODEL_DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
545
DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
487
548
it "should take options passed to save" do
488
549
o = @c.new(:x => 1)
489
550
def o.before_validation; false; end
490
551
proc{o.save_changes}.should raise_error(Sequel::Error)
491
MODEL_DB.sqls.should == []
492
553
o.save_changes(:validate=>false)
493
MODEL_DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
554
DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
496
557
it "should do nothing if no changed columns" do
497
558
o = @c.load(:id => 3, :x => 1, :y => nil)
499
MODEL_DB.sqls.should == []
502
563
it "should do nothing if modified? is false" do
503
564
o = @c.load(:id => 3, :x => 1, :y => nil)
504
565
def o.modified?; false; end
506
MODEL_DB.sqls.should == []
509
570
it "should update only changed columns" do
554
615
o = @c.load(:id => 3, :x => 1, :y => nil)
555
616
@c.send(:define_method, :before_update){self.x += 1}
557
MODEL_DB.sqls.should == []
560
MODEL_DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
621
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
562
MODEL_DB.sqls.should == []
565
MODEL_DB.sqls.should == ["UPDATE items SET x = 5 WHERE (id = 3)"]
626
DB.sqls.should == ["UPDATE items SET x = 5 WHERE (id = 3)"]
568
629
it "should update columns changed in a before_save hook" do
569
630
o = @c.load(:id => 3, :x => 1, :y => nil)
570
631
@c.send(:define_method, :before_update){self.x += 1}
572
MODEL_DB.sqls.should == []
575
MODEL_DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
636
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
577
MODEL_DB.sqls.should == []
580
MODEL_DB.sqls.should == ["UPDATE items SET x = 5 WHERE (id = 3)"]
641
DB.sqls.should == ["UPDATE items SET x = 5 WHERE (id = 3)"]
613
674
model_a.primary_key.should == :a
616
it "should support multi argument composite keys" do
617
model_a = Class.new(Sequel::Model){ set_primary_key :a, :b }
618
model_a.primary_key.should == [:a, :b]
621
677
it "should accept single argument composite keys" do
622
678
model_a = Class.new(Sequel::Model){ set_primary_key [:a, :b] }
623
679
model_a.primary_key.should == [:a, :b]
627
describe Sequel::Model, "w/o primary key" do
683
describe Sequel::Model, "without a primary key" do
628
684
it "should return nil for primary key" do
629
685
Class.new(Sequel::Model){no_primary_key}.primary_key.should be_nil
666
722
@m.columns :id, :x, :y
669
it "should be default return the value of the :id column" do
725
it "should by default return the value of the :id column" do
670
726
m = @m.load(:id => 111, :x => 2, :y => 3)
671
727
m.pk.should == 111
674
it "should be return the primary key value for custom primary key" do
730
it "should return the primary key value for custom primary key" do
675
731
@m.set_primary_key :x
676
732
m = @m.load(:id => 111, :x => 2, :y => 3)
680
it "should be return the primary key value for composite primary key" do
736
it "should return the primary key value for composite primary key" do
681
737
@m.set_primary_key [:y, :x]
682
738
m = @m.load(:id => 111, :x => 2, :y => 3)
683
739
m.pk.should == [3, 2]
700
756
@m.columns :id, :x, :y
703
it "should be default return the value of the :id column" do
759
it "should by default return a hash with the value of the :id column" do
704
760
m = @m.load(:id => 111, :x => 2, :y => 3)
705
761
m.pk_hash.should == {:id => 111}
708
it "should be return the primary key value for custom primary key" do
764
it "should return a hash with the primary key value for custom primary key" do
709
765
@m.set_primary_key :x
710
766
m = @m.load(:id => 111, :x => 2, :y => 3)
711
767
m.pk_hash.should == {:x => 2}
714
it "should be return the primary key value for composite primary key" do
770
it "should return a hash with the primary key values for composite primary key" do
715
771
@m.set_primary_key [:y, :x]
716
772
m = @m.load(:id => 111, :x => 2, :y => 3)
717
773
m.pk_hash.should == {:y => 3, :x => 2}
737
793
@c.strict_param_setting = false
739
795
@o2 = @c.load(:id => 5)
743
799
it "should filter the given params using the model columns" do
744
800
@o1.set(:x => 1, :z => 2)
745
801
@o1.values.should == {:x => 1}
746
MODEL_DB.sqls.should == []
748
804
@o2.set(:y => 1, :abc => 2)
749
805
@o2.values.should == {:y => 1, :id=> 5}
750
MODEL_DB.sqls.should == []
753
809
it "should work with both strings and symbols" do
754
810
@o1.set('x'=> 1, 'z'=> 2)
755
811
@o1.values.should == {:x => 1}
756
MODEL_DB.sqls.should == []
758
814
@o2.set('y'=> 1, 'abc'=> 2)
759
815
@o2.values.should == {:y => 1, :id=> 5}
760
MODEL_DB.sqls.should == []
763
819
it "should support virtual attributes" do
764
820
@c.send(:define_method, :blah=){|v| self.x = v}
765
821
@o1.set(:blah => 333)
766
822
@o1.values.should == {:x => 333}
767
MODEL_DB.sqls.should == []
768
824
@o1.set('blah'=> 334)
769
825
@o1.values.should == {:x => 334}
770
MODEL_DB.sqls.should == []
773
829
it "should not modify the primary key" do
774
830
@o1.set(:x => 1, :id => 2)
775
831
@o1.values.should == {:x => 1}
776
MODEL_DB.sqls.should == []
777
833
@o2.set('y'=> 1, 'id'=> 2)
778
834
@o2.values.should == {:y => 1, :id=> 5}
779
MODEL_DB.sqls.should == []
782
838
it "should return self" do
783
839
returned_value = @o1.set(:x => 1, :z => 2)
784
840
returned_value.should == @o1
785
MODEL_DB.sqls.should == []
788
844
it "should raise error if strict_param_setting is true and method does not exist" do
868
924
@c.strict_param_setting = false
870
926
@o2 = @c.load(:id => 5)
874
930
it "should filter the given params using the model columns" do
875
931
@o1.update(:x => 1, :z => 2)
876
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
932
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
879
935
@o2.update(:y => 1, :abc => 2)
880
MODEL_DB.sqls.should == ["UPDATE items SET y = 1 WHERE (id = 5)"]
936
DB.sqls.should == ["UPDATE items SET y = 1 WHERE (id = 5)"]
883
939
it "should support virtual attributes" do
884
940
@c.send(:define_method, :blah=){|v| self.x = v}
885
941
@o1.update(:blah => 333)
886
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
942
DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
889
945
it "should not modify the primary key" do
890
946
@o1.update(:x => 1, :id => 2)
891
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
947
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
893
949
@o2.update('y'=> 1, 'id'=> 2)
894
950
@o2.values.should == {:y => 1, :id=> 5}
895
MODEL_DB.sqls.should == ["UPDATE items SET y = 1 WHERE (id = 5)"]
951
DB.sqls.should == ["UPDATE items SET y = 1 WHERE (id = 5)"]
948
1003
@o1.values.should == {:x => 1, :y => 2}
949
1004
@o1.set_fields({:x => 9, :y => 8, :z=>6, :id=>7}, [:x, :y, :id], :missing=>:foo)
950
1005
@o1.values.should == {:x => 9, :y => 8, :id=>7}
951
MODEL_DB.sqls.should == []
1006
DB.sqls.should == []
1009
it "should respect model's default_set_fields_options" do
1010
@c.default_set_fields_options = {:missing=>:skip}
1011
@o1.set_fields({:x => 3}, [:x, :y])
1012
@o1.values.should == {:x => 3}
1013
@o1.set_fields({:x => 4}, [:x, :y], {})
1014
@o1.values.should == {:x => 4}
1015
proc{@o1.set_fields({:x => 3}, [:x, :y], :missing=>:raise)}.should raise_error(Sequel::Error)
1016
@c.default_set_fields_options = {:missing=>:raise}
1017
proc{@o1.set_fields({:x => 3}, [:x, :y])}.should raise_error(Sequel::Error)
1018
proc{@o1.set_fields({:x => 3}, [:x, :y], {})}.should raise_error(Sequel::Error)
1019
@o1.set_fields({:x => 5}, [:x, :y], :missing=>:skip)
1020
@o1.values.should == {:x => 5}
1021
@o1.set_fields({:x => 5}, [:x, :y], :missing=>nil)
1022
@o1.values.should == {:x => 5, :y=>nil}
1023
DB.sqls.should == []
1026
it "should respect model's default_set_fields_options in a subclass" do
1027
@c.default_set_fields_options = {:missing=>:skip}
1028
o = Class.new(@c).new
1029
o.set_fields({:x => 3}, [:x, :y])
1030
o.values.should == {:x => 3}
956
1034
describe Sequel::Model, "#update_fields" do
962
1040
@c.strict_param_setting = true
963
1041
@o1 = @c.load(:id=>1)
967
1045
it "should set only the given fields, and then save the changes to the record" do
968
1046
@o1.update_fields({:x => 1, :y => 2, :z=>3, :id=>4}, [:x, :y])
969
1047
@o1.values.should == {:x => 1, :y => 2, :id=>1}
971
1049
sqls.pop.should =~ /UPDATE items SET [xy] = [12], [xy] = [12] WHERE \(id = 1\)/
972
1050
sqls.should == []
974
1052
@o1.update_fields({:x => 1, :y => 5, :z=>6, :id=>7}, [:x, :y])
975
1053
@o1.values.should == {:x => 1, :y => 5, :id=>1}
976
MODEL_DB.sqls.should == ["UPDATE items SET y = 5 WHERE (id = 1)"]
1054
DB.sqls.should == ["UPDATE items SET y = 5 WHERE (id = 1)"]
979
1057
it "should support :missing=>:skip option" do
980
1058
@o1.update_fields({:x => 1, :z=>3, :id=>4}, [:x, :y], :missing=>:skip)
981
1059
@o1.values.should == {:x => 1, :id=>1}
982
MODEL_DB.sqls.should == ["UPDATE items SET x = 1 WHERE (id = 1)"]
1060
DB.sqls.should == ["UPDATE items SET x = 1 WHERE (id = 1)"]
985
1063
it "should support :missing=>:raise option" do
986
1064
proc{@o1.update_fields({:x => 1}, [:x, :y], :missing=>:raise)}.should raise_error(Sequel::Error)
1067
it "should respect model's default_set_fields_options" do
1068
@c.default_set_fields_options = {:missing=>:skip}
1069
@o1.update_fields({:x => 3}, [:x, :y])
1070
@o1.values.should == {:x => 3, :id=>1}
1071
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 1)"]
1073
@c.default_set_fields_options = {:missing=>:raise}
1074
proc{@o1.update_fields({:x => 3}, [:x, :y])}.should raise_error(Sequel::Error)
1075
DB.sqls.should == []
990
describe Sequel::Model, "#(set|update)_(all|except|only)" do
1079
describe Sequel::Model, "#(set|update)_(all|only)" do
992
1081
@c = Class.new(Sequel::Model(:items)) do
993
1082
set_primary_key :id
994
1083
columns :x, :y, :z, :id
995
1084
set_allowed_columns :x
996
set_restricted_columns :y
998
1086
@c.strict_param_setting = false
1003
1091
it "should raise errors if not all hash fields can be set and strict_param_setting is true" do
1004
1092
@c.strict_param_setting = true
1006
proc{@c.new.set_all(:x => 1, :y => 2, :z=>3, :id=>4)}.should raise_error(Sequel::Error)
1094
proc{@c.new.set_all(:x => 1, :y => 2, :z=>3, :use_after_commit_rollback => false)}.should raise_error(Sequel::Error)
1007
1095
(o = @c.new).set_all(:x => 1, :y => 2, :z=>3)
1008
1096
o.values.should == {:x => 1, :y => 2, :z=>3}
1011
1099
proc{@c.new.set_only({:x => 1, :y => 2, :z=>3}, :x, :y)}.should raise_error(Sequel::Error)
1012
1100
(o = @c.new).set_only({:x => 1, :y => 2}, :x, :y)
1013
1101
o.values.should == {:x => 1, :y => 2}
1015
proc{@c.new.set_except({:x => 1, :y => 2, :z=>3, :id=>4}, :x, :y)}.should raise_error(Sequel::Error)
1016
proc{@c.new.set_except({:x => 1, :y => 2, :z=>3}, :x, :y)}.should raise_error(Sequel::Error)
1017
(o = @c.new).set_except({:z => 3}, :x, :y)
1018
o.values.should == {:z=>3}
1021
it "#set_all should set all attributes except the primary key" do
1104
it "#set_all should set all attributes including the primary key" do
1022
1105
@o1.set_all(:x => 1, :y => 2, :z=>3, :id=>4)
1023
@o1.values.should == {:x => 1, :y => 2, :z=>3}
1106
@o1.values.should == {:id =>4, :x => 1, :y => 2, :z=>3}
1109
it "#set_all should set not set restricted fields" do
1110
@o1.set_all(:x => 1, :use_after_commit_rollback => false)
1111
@o1.use_after_commit_rollback.should be_true
1112
@o1.values.should == {:x => 1}
1026
1115
it "#set_only should only set given attributes" do
1032
1121
@o1.values.should == {:x => 9, :y => 8, :id=>7}
1035
it "#set_except should not set given attributes or the primary key" do
1036
@o1.set_except({:x => 1, :y => 2, :z=>3, :id=>4}, [:y, :z])
1037
@o1.values.should == {:x => 1}
1038
@o1.set_except({:x => 4, :y => 2, :z=>3, :id=>4}, :y, :z)
1039
@o1.values.should == {:x => 4}
1042
1124
it "#update_all should update all attributes" do
1043
@c.new.update_all(:x => 1, :id=>4)
1044
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1045
@c.new.update_all(:y => 1, :id=>4)
1046
MODEL_DB.sqls.should == ["INSERT INTO items (y) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1047
@c.new.update_all(:z => 1, :id=>4)
1048
MODEL_DB.sqls.should == ["INSERT INTO items (z) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1125
@c.new.update_all(:x => 1)
1126
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1127
@c.new.update_all(:y => 1)
1128
DB.sqls.should == ["INSERT INTO items (y) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1129
@c.new.update_all(:z => 1)
1130
DB.sqls.should == ["INSERT INTO items (z) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1051
1133
it "#update_only should only update given attributes" do
1052
1134
@o1.update_only({:x => 1, :y => 2, :z=>3, :id=>4}, [:x])
1053
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1135
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1054
1136
@c.new.update_only({:x => 1, :y => 2, :z=>3, :id=>4}, :x)
1055
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1058
it "#update_except should not update given attributes" do
1059
@o1.update_except({:x => 1, :y => 2, :z=>3, :id=>4}, [:y, :z])
1060
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1061
@c.new.update_except({:x => 1, :y => 2, :z=>3, :id=>4}, :y, :z)
1062
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1137
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1066
1141
describe Sequel::Model, "#destroy with filtered dataset" do
1068
@model = Class.new(Sequel::Model(MODEL_DB[:items].where(:a=>1)))
1143
@model = Class.new(Sequel::Model(DB[:items].where(:a=>1)))
1069
1144
@model.columns :id, :a
1070
1145
@instance = @model.load(:id => 1234)
1074
1149
it "should raise a NoExistingObject exception if the dataset delete call doesn't return 1" do
1075
@instance.this.meta_def(:execute_dui){|*a| 0}
1076
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1077
@instance.this.meta_def(:execute_dui){|*a| 2}
1078
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1079
@instance.this.meta_def(:execute_dui){|*a| 1}
1150
def (@instance.this).execute_dui(*a) 0 end
1151
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1152
def (@instance.this).execute_dui(*a) 2 end
1153
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1154
def (@instance.this).execute_dui(*a) 1 end
1080
1155
proc{@instance.delete}.should_not raise_error
1082
1157
@instance.require_modification = false
1083
@instance.this.meta_def(:execute_dui){|*a| 0}
1158
def (@instance.this).execute_dui(*a) 0 end
1084
1159
proc{@instance.delete}.should_not raise_error
1085
@instance.this.meta_def(:execute_dui){|*a| 2}
1160
def (@instance.this).execute_dui(*a) 2 end
1086
1161
proc{@instance.delete}.should_not raise_error
1089
1164
it "should include WHERE clause when deleting" do
1090
1165
@instance.destroy
1091
MODEL_DB.sqls.should == ["DELETE FROM items WHERE ((a = 1) AND (id = 1234))"]
1166
DB.sqls.should == ["DELETE FROM items WHERE ((a = 1) AND (id = 1234))"]
1108
1183
it "should raise a NoExistingObject exception if the dataset delete call doesn't return 1" do
1109
@model.dataset.meta_def(:execute_dui){|*a| 0}
1110
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1111
@model.dataset.meta_def(:execute_dui){|*a| 2}
1112
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1113
@model.dataset.meta_def(:execute_dui){|*a| 1}
1184
def (@model.dataset).execute_dui(*a) 0 end
1185
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1186
def (@model.dataset).execute_dui(*a) 2 end
1187
proc{@instance.delete}.should raise_error(Sequel::NoExistingObject)
1188
def (@model.dataset).execute_dui(*a) 1 end
1114
1189
proc{@instance.delete}.should_not raise_error
1116
1191
@instance.require_modification = false
1117
@model.dataset.meta_def(:execute_dui){|*a| 0}
1192
def (@model.dataset).execute_dui(*a) 0 end
1118
1193
proc{@instance.delete}.should_not raise_error
1119
@model.dataset.meta_def(:execute_dui){|*a| 2}
1194
def (@model.dataset).execute_dui(*a) 2 end
1120
1195
proc{@instance.delete}.should_not raise_error
1123
1198
it "should run within a transaction if use_transactions is true" do
1124
1199
@instance.use_transactions = true
1125
1200
@instance.destroy
1126
MODEL_DB.sqls.should == ["BEGIN", "DELETE FROM items WHERE id = 1234", "COMMIT"]
1201
DB.sqls.should == ["BEGIN", "DELETE FROM items WHERE id = 1234", "COMMIT"]
1129
1204
it "should not run within a transaction if use_transactions is false" do
1130
1205
@instance.use_transactions = false
1131
1206
@instance.destroy
1132
MODEL_DB.sqls.should == ["DELETE FROM items WHERE id = 1234"]
1207
DB.sqls.should == ["DELETE FROM items WHERE id = 1234"]
1135
1210
it "should run within a transaction if :transaction option is true" do
1136
1211
@instance.use_transactions = false
1137
1212
@instance.destroy(:transaction => true)
1138
MODEL_DB.sqls.should == ["BEGIN", "DELETE FROM items WHERE id = 1234", "COMMIT"]
1213
DB.sqls.should == ["BEGIN", "DELETE FROM items WHERE id = 1234", "COMMIT"]
1141
1216
it "should not run within a transaction if :transaction option is false" do
1142
1217
@instance.use_transactions = true
1143
1218
@instance.destroy(:transaction => false)
1144
MODEL_DB.sqls.should == ["DELETE FROM items WHERE id = 1234"]
1219
DB.sqls.should == ["DELETE FROM items WHERE id = 1234"]
1147
1222
it "should run before_destroy and after_destroy hooks" do
1148
@model.send(:define_method, :before_destroy){MODEL_DB.execute('before blah')}
1149
@model.send(:define_method, :after_destroy){MODEL_DB.execute('after blah')}
1223
@model.send(:define_method, :before_destroy){DB.execute('before blah')}
1224
@model.send(:define_method, :after_destroy){DB.execute('after blah')}
1150
1225
@instance.destroy
1151
MODEL_DB.sqls.should == ["before blah", "DELETE FROM items WHERE id = 1234", "after blah"]
1226
DB.sqls.should == ["before blah", "DELETE FROM items WHERE id = 1234", "after blah"]
1157
1232
@model = Class.new(Sequel::Model(:items))
1158
1233
@model.instance_dataset._fetch = @model.dataset._fetch = proc{|sql| {:x=>1} if sql =~ /id = 1/}
1162
1237
it "should do a query to check if the record exists" do
1163
1238
@model.load(:id=>1).exists?.should be_true
1164
MODEL_DB.sqls.should == ['SELECT 1 FROM items WHERE (id = 1) LIMIT 1']
1239
DB.sqls.should == ['SELECT 1 AS one FROM items WHERE (id = 1) LIMIT 1']
1167
1242
it "should return false when #this.count == 0" do
1168
1243
@model.load(:id=>2).exists?.should be_false
1169
MODEL_DB.sqls.should == ['SELECT 1 FROM items WHERE (id = 2) LIMIT 1']
1244
DB.sqls.should == ['SELECT 1 AS one FROM items WHERE (id = 2) LIMIT 1']
1172
1247
it "should return false without issuing a query if the model object is new" do
1173
1248
@model.new.exists?.should be_false
1174
MODEL_DB.sqls.should == []
1249
DB.sqls.should == []
1424
1499
it "should be able to create rows in the associated table" do
1425
1500
o = @c.create(:x => 1)
1426
1501
o.class.should == @c
1427
MODEL_DB.sqls.should == ['INSERT INTO items (x) VALUES (1)', "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1502
DB.sqls.should == ['INSERT INTO items (x) VALUES (1)', "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1430
1505
it "should be able to create rows without any values specified" do
1432
1507
o.class.should == @c
1433
MODEL_DB.sqls.should == ["INSERT INTO items DEFAULT VALUES", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1508
DB.sqls.should == ["INSERT INTO items DEFAULT VALUES", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1436
it "should accept a block and run it" do
1511
it "should accept a block and call it" do
1437
1512
o1, o2, o3 = nil, nil, nil
1438
1513
o = @c.create {|o4| o1 = o4; o3 = o4; o2 = :blah; o3.x = 333}
1439
1514
o.class.should == @c
1440
1515
o1.should === o
1441
1516
o3.should === o
1442
1517
o2.should == :blah
1443
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1518
DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
1446
1521
it "should create a row for a model with custom primary key" do
1447
1522
@c.set_primary_key :x
1448
1523
o = @c.create(:x => 30)
1449
1524
o.class.should == @c
1450
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (30)", "SELECT * FROM items WHERE (x = 30) LIMIT 1"]
1525
DB.sqls.should == ["INSERT INTO items (x) VALUES (30)", "SELECT * FROM items WHERE (x = 30) LIMIT 1"]
1849
1928
@c.dataset._fetch = {:id=>1}
1853
1932
it "should do nothing if the record is a new record" do
1856
o.meta_def(:_refresh){|x| called = true; super(x)}
1934
def o._refresh(x) raise Sequel::Error; super(x) end
1859
called.should == false
1860
MODEL_DB.sqls.should == []
1937
DB.sqls.should == []
1863
1940
it "should refresh the record using for_update if it is not a new record" do
1864
1941
o = @c.load(:id => 1)
1866
o.meta_def(:_refresh){|x| called = true; super(x)}
1942
def o._refresh(x) instance_variable_set(:@a, 1); super(x) end
1869
called.should == true
1870
MODEL_DB.sqls.should == ["SELECT * FROM items WHERE (id = 1) LIMIT 1 FOR UPDATE"]
1945
o.instance_variable_get(:@a).should == 1
1946
DB.sqls.should == ["SELECT * FROM items WHERE (id = 1) LIMIT 1 FOR UPDATE"]
1950
describe "Model#schema_type_class" do
1951
specify "should return the class or array of classes for the given type symbol" do
1952
@c = Class.new(Sequel::Model(:items))
1953
@c.class_eval{@db_schema = {:id=>{:type=>:integer}}}
1954
@c.new.send(:schema_type_class, :id).should == Integer