1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
require_relative "spec_helper"
describe "Sequel::Plugins::Timestamps" do
before do
dc = Object.new
dc.instance_eval do
def now
'2009-08-01'
end
singleton_class.send(:alias_method, :now, :now)
end
Sequel.datetime_class = dc
@c = Class.new(Sequel::Model(:t))
@c.class_eval do
columns :id, :created_at, :updated_at
plugin :timestamps
private
def _save_refresh(*) end
db.reset
end
end
after do
Sequel.datetime_class = Time
end
it "should handle validations on the timestamp fields for new objects" do
@c.plugin :timestamps, :update_on_create=>true
o = @c.new
def o.validate
errors.add(model.create_timestamp_field, 'not present') unless send(model.create_timestamp_field)
errors.add(model.update_timestamp_field, 'not present') unless send(model.update_timestamp_field)
end
o.valid?.must_equal true
end
it "should set timestamp fields when skipping validations" do
@c.plugin :timestamps
@c.new.save(:validate=>false)
@c.db.sqls.must_equal ["INSERT INTO t (created_at) VALUES ('2009-08-01')"]
end
it "should set the create timestamp field on creation" do
o = @c.create
@c.db.sqls.must_equal ["INSERT INTO t (created_at) VALUES ('2009-08-01')"]
o.created_at.must_equal '2009-08-01'
end
it "should set the update timestamp field on update" do
o = @c.load(:id=>1).save
@c.db.sqls.must_equal ["UPDATE t SET updated_at = '2009-08-01' WHERE (id = 1)"]
o.updated_at.must_equal '2009-08-01'
end
it "should leave manually set update timestamp, if :allow_manual_update was given" do
o = @c.load(:id=>1).update(:updated_at=>Date.new(2016))
@c.db.sqls.must_equal ["UPDATE t SET updated_at = '2009-08-01' WHERE (id = 1)"]
o.updated_at.must_equal '2009-08-01'
@c.plugin :timestamps, :allow_manual_update=>true
o = @c.load(:id=>1).update(:updated_at=>Date.new(2016))
@c.db.sqls.must_equal ["UPDATE t SET updated_at = '2016-01-01' WHERE (id = 1)"]
o.updated_at.must_equal Date.new(2016)
end
it "should work with current_datetime_timestamp extension" do
Sequel.datetime_class = Time
@c.dataset = @c.dataset.extension(:current_datetime_timestamp)
@c.create
@c.db.sqls.must_equal ["INSERT INTO t (created_at) VALUES (CURRENT_TIMESTAMP)"]
@c.load(:id=>1).save
@c.db.sqls.must_equal ["UPDATE t SET updated_at = CURRENT_TIMESTAMP WHERE (id = 1)"]
end
it "should not update the update timestamp on creation" do
@c.create.updated_at.must_be_nil
end
it "should use the same value for the creation and update timestamps when creating if the :update_on_create option is given" do
@c.plugin :timestamps, :update_on_create=>true
o = @c.create
@c.db.sqls.must_equal ["INSERT INTO t (created_at, updated_at) VALUES ('2009-08-01', '2009-08-01')"]
o.created_at.must_be :===, o.updated_at
end
it "should allow specifying the create timestamp field via the :create option" do
c = Class.new(Sequel::Model(:t))
c.class_eval do
columns :id, :c
plugin :timestamps, :create=>:c
private
def _save_refresh(*) end
end
o = c.create
c.db.sqls.must_equal ["INSERT INTO t (c) VALUES ('2009-08-01')"]
o.c.must_equal '2009-08-01'
end
it "should allow specifying the update timestamp field via the :update option" do
c = Class.new(Sequel::Model(:t))
c.class_eval do
columns :id, :u
plugin :timestamps, :update=>:u
db.reset
end
o = c.load(:id=>1).save
c.db.sqls.must_equal ["UPDATE t SET u = '2009-08-01' WHERE (id = 1)"]
o.u.must_equal '2009-08-01'
end
it "should not raise an error if the model doesn't have the timestamp columns" do
c = Class.new(Sequel::Model(:t))
c.class_eval do
columns :id, :x
plugin :timestamps
db.reset
private
def _save_refresh; self end
end
c.create(:x=>2)
c.load(:id=>1, :x=>2).save
c.db.sqls.must_equal ["INSERT INTO t (x) VALUES (2)", "UPDATE t SET x = 2 WHERE (id = 1)"]
end
it "should not overwrite an existing create timestamp" do
o = @c.create(:created_at=>'2009-08-03')
@c.db.sqls.must_equal ["INSERT INTO t (created_at) VALUES ('2009-08-03')"]
o.created_at.must_equal '2009-08-03'
end
it "should overwrite an existing create timestamp if the :force option is used" do
@c.plugin :timestamps, :force=>true
o = @c.create(:created_at=>'2009-08-03')
@c.db.sqls.must_equal ["INSERT INTO t (created_at) VALUES ('2009-08-01')"]
o.created_at.must_equal '2009-08-01'
end
it "should set update timestamp to same timestamp as create timestamp when setting creating timestamp" do
i = 1
Sequel.datetime_class.define_singleton_method(:now){"2009-08-0#{i+=1}"}
@c.plugin :timestamps, :update_on_create=>true
o = @c.create
sqls = @c.db.sqls
sqls.length.must_equal 1
["INSERT INTO t (created_at, updated_at) VALUES ('2009-08-02', '2009-08-02')",
"INSERT INTO t (updated_at, created_at) VALUES ('2009-08-02', '2009-08-02')"].must_include sqls.first
o.created_at.must_equal '2009-08-02'
o.updated_at.must_equal '2009-08-02'
end
it "should set update timestamp when using not overriding create timestamp" do
i = 1
Sequel.datetime_class.define_singleton_method(:now){"2009-08-0#{i+=1}"}
@c.plugin :timestamps, :update_on_create=>true
o = @c.create(:created_at=>'2009-08-10')
sqls = @c.db.sqls
sqls.length.must_equal 1
["INSERT INTO t (created_at, updated_at) VALUES ('2009-08-10', '2009-08-02')",
"INSERT INTO t (updated_at, created_at) VALUES ('2009-08-02', '2009-08-10')"].must_include sqls.first
o.created_at.must_equal '2009-08-10'
o.updated_at.must_equal '2009-08-02'
end
it "should have create_timestamp_field give the create timestamp field" do
@c.create_timestamp_field.must_equal :created_at
@c.plugin :timestamps, :create=>:c
@c.create_timestamp_field.must_equal :c
end
it "should have update_timestamp_field give the update timestamp field" do
@c.update_timestamp_field.must_equal :updated_at
@c.plugin :timestamps, :update=>:u
@c.update_timestamp_field.must_equal :u
end
it "should have create_timestamp_overwrite? give the whether to overwrite an existing create timestamp" do
@c.create_timestamp_overwrite?.must_equal false
@c.plugin :timestamps, :force=>true
@c.create_timestamp_overwrite?.must_equal true
end
it "should have set_update_timestamp_on_create? give whether to set the update timestamp on create" do
@c.set_update_timestamp_on_create?.must_equal false
@c.plugin :timestamps, :update_on_create=>true
@c.set_update_timestamp_on_create?.must_equal true
end
it "should work with subclasses" do
c = Class.new(@c)
o = c.create
o.created_at.must_equal '2009-08-01'
o.updated_at.must_be_nil
o = c.load(:id=>1).save
o.updated_at.must_equal '2009-08-01'
c.db.sqls.must_equal ["INSERT INTO t (created_at) VALUES ('2009-08-01')", "UPDATE t SET updated_at = '2009-08-01' WHERE (id = 1)"]
c.create(:created_at=>'2009-08-03').created_at.must_equal '2009-08-03'
c.class_eval do
columns :id, :c, :u
plugin :timestamps, :create=>:c, :update=>:u, :force=>true, :update_on_create=>true
end
c2 = Class.new(c)
c2.db.reset
o = c2.create
o.c.must_equal '2009-08-01'
o.u.must_be :===, o.c
c2.db.sqls.must_equal ["INSERT INTO t (c, u) VALUES ('2009-08-01', '2009-08-01')"]
c2.db.reset
o = c2.load(:id=>1).save
o.u.must_equal '2009-08-01'
c2.db.sqls.must_equal ["UPDATE t SET u = '2009-08-01' WHERE (id = 1)"]
c2.create(:c=>'2009-08-03').c.must_equal '2009-08-01'
end
end
|