File: db2_spec.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (170 lines) | stat: -rw-r--r-- 4,884 bytes parent folder | download | duplicates (3)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
SEQUEL_ADAPTER_TEST = :db2

require_relative 'spec_helper'

if DB.table_exists?(:test)
  DB.drop_table(:test)
end

describe Sequel::Database do
  before do
    @db = DB
    @db.create_table(:test){String :a}
    @ds = @db[:test]
  end

  after do
    @db.drop_table(:test)
  end
  
  it "should provide disconnect functionality after preparing a connection" do
    @ds.prepare(:first, :a).call
    @db.disconnect
    @db.pool.size.must_equal 0
  end

  it "should return version correctly" do
    @db.db2_version.must_match(/DB2 v/i)
  end
end

describe "Simple Dataset operations" do
  before(:all) do
    DB.use_clob_as_blob = true
    DB.create_table!(:items) do
      Integer :id, :primary_key => true
      Integer :number
      column  :bin_string, 'varchar(20) for bit data'
      column  :bin_clob, 'clob'
    end
    @ds = DB[:items]
  end
  after(:each) do
    @ds.delete
  end
  after(:all) do
    DB.use_clob_as_blob = false
    DB.drop_table(:items)
  end

  it "should insert with a primary key specified" do
    @ds.insert(:id => 1,   :number => 10)
    @ds.insert(:id => 100, :number => 20)
    @ds.select_hash(:id, :number).must_equal(1 => 10, 100 => 20)
  end

  it "should insert into binary columns" do
    @ds.insert(:id => 1, :bin_string => Sequel.blob("\1"), :bin_clob => Sequel.blob("\2"))
    @ds.select(:bin_string, :bin_clob).first.must_equal(:bin_string => "\1", :bin_clob => "\2")
  end
end

describe Sequel::Database do
  before do
    @db = DB
  end
  after do
    @db.drop_table(:items)
  end

  it "should parse primary keys from the schema properly" do
    @db.create_table!(:items){Integer :number}
    @db.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.must_equal []
    @db.create_table!(:items){primary_key :number}
    @db.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.must_equal [:number]
    @db.create_table!(:items){Integer :number1, :null => false; Integer :number2, :null => false; primary_key [:number1, :number2]}
    @db.schema(:items).collect{|k,v| k if v[:primary_key]}.compact.must_equal [:number1, :number2]
  end

  it "should not error on alter_table operations that need REORG" do
    @db.create_table!(:items) do
      varchar :a
    end
    @db.alter_table(:items) do
      add_column :b, :varchar, :null => true
      set_column_allow_null :a, false
      add_index :a, :unique => true
    end
  end
end

describe "Sequel::IBMDB::Database#convert_smallint_to_bool" do
  before do
    @db = DB
    @db.create_table!(:booltest){column :b, 'smallint'; column :i, 'integer'}
    @ds = @db[:booltest]
  end
  after do
    @db.convert_smallint_to_bool = true
    @db.drop_table(:booltest)
  end
  
  it "should consider smallint datatypes as boolean if set, but not larger smallints" do
    @db.schema(:booltest, :reload=>true).first.last[:type].must_equal :boolean
    @db.schema(:booltest, :reload=>true).first.last[:db_type].must_match(/smallint/i)
    @db.convert_smallint_to_bool = false
    @db.schema(:booltest, :reload=>true).first.last[:type].must_equal :integer
    @db.schema(:booltest, :reload=>true).first.last[:db_type].must_match(/smallint/i)
  end
  
  it "should return smallints as bools and integers as integers when set" do
    @db.convert_smallint_to_bool = true
    @ds.delete
    @ds.insert(:b=>true, :i=>10)
    @ds.all.must_equal [{:b=>true, :i=>10}]
    @ds.delete
    @ds.insert(:b=>false, :i=>0)
    @ds.all.must_equal [{:b=>false, :i=>0}]
    @ds.delete
    @ds.insert(:b=>true, :i=>1)
    @ds.all.must_equal [{:b=>true, :i=>1}]

    @ds = @ds.with_convert_smallint_to_bool(false)
    @ds.delete
    @ds.insert(:b=>true, :i=>10)
    @ds.all.must_equal [{:b=>1, :i=>10}]
  end

  it "should return all smallints as integers when unset" do
    @db.convert_smallint_to_bool = false
    @ds.delete
    @ds.insert(:b=>true, :i=>10)
    @ds.all.must_equal [{:b=>1, :i=>10}]
    @ds.delete
    @ds.insert(:b=>false, :i=>0)
    @ds.all.must_equal [{:b=>0, :i=>0}]
    
    @ds.delete
    @ds.insert(:b=>1, :i=>10)
    @ds.all.must_equal [{:b=>1, :i=>10}]
    @ds.delete
    @ds.insert(:b=>0, :i=>0)
    @ds.all.must_equal [{:b=>0, :i=>0}]

    @ds = @ds.with_convert_smallint_to_bool(true)
    @ds.delete
    @ds.insert(:b=>true, :i=>10)
    @ds.all.must_equal [{:b=>true, :i=>10}]
  end
end if DB.adapter_scheme == :ibmdb

describe "Simple Dataset operations in transactions" do
  before do
    DB.create_table!(:items_insert_in_transaction) do
      Integer :id, :primary_key => true
      integer :number
    end
    @ds = DB[:items_insert_in_transaction]
  end
  after do
    DB.drop_table(:items_insert_in_transaction)
  end

  it "should insert correctly with a primary key specified inside a transaction" do
    DB.transaction do
      @ds.insert(:id=>100, :number=>20)
      @ds.count.must_equal 1
      @ds.order(:id).all.must_equal [{:id=>100, :number=>20}]
    end
  end
end