File: looser_typecasting_spec.rb

package info (click to toggle)
libsequel-ruby 3.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,720 kB
  • ctags: 3,038
  • sloc: ruby: 43,327; makefile: 8
file content (39 lines) | stat: -rw-r--r-- 1,042 bytes parent folder | download
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
require File.join(File.dirname(File.expand_path(__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