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
|
require_relative "spec_helper"
begin
require 'active_model'
rescue LoadError
warn "Skipping test of active_model plugin: can't load active_model"
else
describe "ActiveModel plugin" do
before do
class ::AMLintTest < Sequel::Model
set_primary_key :id
columns :id, :id2
def delete; end
end
module ::Blog
class Post < Sequel::Model
plugin :active_model
end
end
@c = AMLintTest
@c.plugin :active_model
@c.freeze if @freeze_class
@m = @model = @c.new
@o = @c.load({})
end
after do
Object.send(:remove_const, :AMLintTest)
Object.send(:remove_const, :Blog)
end
it ".to_model should return self, not a proxy object" do
@m.object_id.must_equal @m.to_model.object_id
end
it "#to_key should return a key array, or nil" do
@o.to_key.must_be_nil
@o.id = 1
@o.to_key.must_equal [1]
@o.id = nil
@o.to_key.must_be_nil
@c.set_primary_key [:id2, :id]
@c.freeze
@o.to_key.must_be_nil
@o.id = 1
@o.id2 = 2
@o.to_key.must_equal [2, 1]
@o.destroy
@o.to_key.must_equal [2, 1]
@o.id = nil
@o.to_key.must_be_nil
end
it "#to_param should return a param string or nil" do
@o.to_param.must_be_nil
@o.id = 1
@o.to_param.must_equal '1'
@c.set_primary_key [:id2, :id]
@c.freeze
@o.id2 = 2
@o.to_param.must_equal '2-1'
def @o.to_param_joiner; '|' end
@o.to_param.must_equal '2|1'
@o.destroy
@o.to_param.must_be_nil
end
it "#persisted? should return true if the object exists and has not been destroyed" do
@m.persisted?.must_equal false
@o.persisted?.must_equal true
@m.destroy
@o.destroy
@m.persisted?.must_equal false
@o.persisted?.must_equal false
end
it "#persisted? should return false if the object is created and the transaction is rolled back" do
DB.transaction(:rollback=>:always){@m.save}
@m.persisted?.must_equal false
end
it "#persisted? should return true if the object is created and the transaction is not rolled back" do
DB.transaction{@m.save}
@m.persisted?.must_equal true
end
it "#persisted? should return true if the object is created without a transaction" do
@m.save(:transaction=>false)
@m.persisted?.must_equal true
end
it "#to_partial_path should return a path string" do
@m.to_partial_path.must_equal 'am_lint_tests/am_lint_test'
Blog::Post.new.to_partial_path.must_equal 'blog/posts/post'
end
describe "with unfrozen model class" do
include ActiveModel::Lint::Tests
end
describe "with frozen model class" do
before do
@freeze_class = true
end
include ActiveModel::Lint::Tests
end
end
end
|