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
|
require_relative "spec_helper"
describe "Sequel::Plugins::StringStripper" do
before do
@db = Sequel.mock
@c = Class.new(Sequel::Model(@db[:test]))
@c.columns :name, :b
@c.db_schema[:b][:type] = :blob
@c.plugin :string_stripper
@o = @c.new
end
it "should strip all input strings" do
@o.name = ' name '
@o.name.must_equal 'name'
end
it "should not affect other types" do
@o.name = 1
@o.name.must_equal 1
@o.name = Date.today
@o.name.must_equal Date.today
end
it "should not strip strings for blob arguments" do
v = Sequel.blob(' name ')
@o.name = v
@o.name.must_be_same_as(v)
end
it "should not strip strings for blob columns" do
@o.b = ' name '
@o.b.must_be_kind_of(Sequel::SQL::Blob)
@o.b.must_equal Sequel.blob(' name ')
end
it "should allow skipping of columns using Model.skip_string_stripping" do
@c.skip_string_stripping?(:name).must_equal false
@c.skip_string_stripping :name
@c.skip_string_stripping?(:name).must_equal true
v = ' name '
@o.name = v
@o.name.must_be_same_as(v)
end
it "should work correctly in subclasses" do
o = Class.new(@c).new
o.name = ' name '
o.name.must_equal 'name'
o.b = ' name '
o.b.must_be_kind_of(Sequel::SQL::Blob)
o.b.must_equal Sequel.blob(' name ')
end
it "should work correctly for dataset changes" do
c = Class.new(Sequel::Model(@db[:test]))
c.plugin :string_stripper
def @db.supports_schema_parsing?() true end
def @db.schema(*) [[:name, {}], [:b, {:type=>:blob}]] end
c.set_dataset(@db[:test2])
o = c.new
o.name = ' name '
o.name.must_equal 'name'
o.b = ' name '
o.b.must_be_kind_of(Sequel::SQL::Blob)
o.b.must_equal Sequel.blob(' name ')
end
it "should handle classes without datasets" do
@db = Sequel.mock
@c = Class.new(Sequel::Model)
@c.plugin :string_stripper
@c.dataset = @db[:test]
@c.columns :name, :b
@o = @c.new
@o.name = ' name '
@o.name.must_equal 'name'
end
end
|