/
type_test.rb
127 lines (110 loc) · 4.52 KB
/
type_test.rb
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
require_relative "../adapters/spec_helper"
describe "Supported types" do
def create_items_table_with_column(name, type, opts={})
DB.create_table!(:items){column name, type, opts}
DB[:items]
end
after(:all) do
DB.drop_table?(:items)
end
it "should support casting correctly" do
ds = create_items_table_with_column(:number, Integer)
ds.insert(:number => 1)
ds.select(Sequel.cast(:number, String).as(:n)).map(:n).must_equal %w'1'
ds = create_items_table_with_column(:name, String)
ds.insert(:name=> '1')
ds.select(Sequel.cast(:name, Integer).as(:n)).map(:n).must_equal [1]
end
it "should support NULL correctly" do
ds = create_items_table_with_column(:number, Integer)
ds.insert(:number => nil)
ds.all.must_equal [{:number=>nil}]
end
it "should support generic integer type" do
ds = create_items_table_with_column(:number, Integer)
ds.insert(:number => 2)
ds.all.must_equal [{:number=>2}]
end
it "should support generic bignum type" do
ds = create_items_table_with_column(:number, :Bignum)
ds.insert(:number => 2**34)
ds.all.must_equal [{:number=>2**34}]
end
it "should support generic float type" do
ds = create_items_table_with_column(:number, Float)
ds.insert(:number => 2.1)
ds.all.must_equal [{:number=>2.1}]
end
cspecify "should support generic numeric type", [:odbc, :mssql] do
ds = create_items_table_with_column(:number, Numeric, :size=>[15, 10])
ds.insert(:number => BigDecimal('2.123456789'))
ds.all.must_equal [{:number=>BigDecimal('2.123456789')}]
ds = create_items_table_with_column(:number, BigDecimal, :size=>[15, 10])
ds.insert(:number => BigDecimal('2.123456789'))
ds.all.must_equal [{:number=>BigDecimal('2.123456789')}]
end
it "should support generic string type" do
ds = create_items_table_with_column(:name, String)
ds.insert(:name => 'Test User')
ds.all.must_equal [{:name=>'Test User'}]
end
it "should support generic text type" do
ds = create_items_table_with_column(:name, String, :text=>true)
ds.insert(:name => 'Test User'*100)
ds.all.must_equal [{:name=>'Test User'*100}]
ds.update(:name=>ds.get(:name))
ds.all.must_equal [{:name=>'Test User'*100}]
end
cspecify "should support generic date type", [:jdbc, :sqlite], [:tinytds], [:jdbc, :mssql], [:odbc, :mssql], :oracle do
ds = create_items_table_with_column(:dat, Date)
d = Date.today
ds.insert(:dat => d)
ds.first[:dat].must_be_kind_of(Date)
ds.first[:dat].to_s.must_equal d.to_s
end
cspecify "should support generic time type", [:odbc], [:jdbc, :mssql], [:jdbc, :sqlite], [:mysql2], [:tinytds], :oracle, [:ado], [:trilogy] do
ds = create_items_table_with_column(:tim, Time, :only_time=>true)
t = Sequel::SQLTime.now
ds.insert(:tim => t)
v = ds.first[:tim]
ds.literal(v).must_equal ds.literal(t)
v.must_be_kind_of(Sequel::SQLTime)
ds.delete
ds.insert(:tim => v)
v2 = ds.first[:tim]
ds.literal(v2).must_equal ds.literal(t)
v2.must_be_kind_of(Sequel::SQLTime)
end
cspecify "should support generic datetime type", [:jdbc, :sqlite] do
ds = create_items_table_with_column(:tim, DateTime)
t = DateTime.now
ds.insert(:tim => t)
ds.first[:tim].strftime('%Y%m%d%H%M%S').must_equal t.strftime('%Y%m%d%H%M%S')
ds = create_items_table_with_column(:tim, Time)
t = Time.now
ds.insert(:tim => t)
ds.first[:tim].strftime('%Y%m%d%H%M%S').must_equal t.strftime('%Y%m%d%H%M%S')
end
cspecify "should support generic file type", [:odbc, :mssql], [:mysql2], [:tinytds], [:trilogy] do
ds = create_items_table_with_column(:name, File)
ds.insert(:name =>Sequel.blob("a\0"*300))
ds.all.must_equal [{:name=>Sequel.blob("a\0"*300)}]
ds.first[:name].must_be_kind_of(::Sequel::SQL::Blob)
end
cspecify "should support generic boolean type", [:jdbc, :sqlite], [:jdbc, :db2], :oracle do
ds = create_items_table_with_column(:number, TrueClass)
ds.insert(:number => true)
ds.all.must_equal [{:number=>true}]
ds = create_items_table_with_column(:number, FalseClass)
ds.insert(:number => true)
ds.all.must_equal [{:number=>true}]
end
cspecify "should support generic boolean type with defaults", [:jdbc, :sqlite], [:jdbc, :db2], :oracle do
ds = create_items_table_with_column(:number, TrueClass, :default=>true)
ds.insert
ds.all.must_equal [{:number=>true}]
ds = create_items_table_with_column(:number, FalseClass, :default=>false)
ds.insert
ds.all.must_equal [{:number=>false}]
end
end