Sha256: e3257c89138652155fe7c8f10bfcd08572756ef364e9f29af313b4be5f8c99bb

Contents?: true

Size: 1 KB

Versions: 10

Compression:

Stored size: 1 KB

Contents

require File.join(File.dirname(__FILE__), "spec_helper")

context "LooserTypecasting Extension" do
  before do
    @db = Sequel::Database.new({})
    def @db.schema(*args)
      [[:id, {}], [:y, {:type=>:float}], [:b, {:type=>:integer}]]
    end 
    @c = Class.new(Sequel::Model(@db[:items]))
    @c.instance_eval do
      @columns = [:id, :b, :y] 
      def columns; @columns; end 
    end
  end

  specify "Should use to_i instead of Integer() for typecasting integers" do
    proc{@c.new(:b=>'a')}.should raise_error(Sequel::InvalidValue)
    @db.extend(Sequel::LooserTypecasting)
    @c.new(:b=>'a').b.should == 0

    o = Object.new
    def o.to_i
      1
    end
    @c.new(:b=>o).b.should == 1
  end

  specify "Should use to_f instead of Float() for typecasting floats" do
    proc{@c.new(:y=>'a')}.should raise_error(Sequel::InvalidValue)
    @db.extend(Sequel::LooserTypecasting)
    @c.new(:y=>'a').y.should == 0.0

    o = Object.new
    def o.to_f
      1.0
    end
    @c.new(:y=>o).y.should == 1.0
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
sequel-3.12.1 spec/extensions/looser_typecasting_spec.rb
sequel-3.12.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.11.0 spec/extensions/looser_typecasting_spec.rb
viking-sequel-3.10.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.10.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.9.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.8.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.7.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.6.0 spec/extensions/looser_typecasting_spec.rb
sequel-3.5.0 spec/extensions/looser_typecasting_spec.rb