ibruby 0.5.1-mswin32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/doc/README +463 -0
- data/doc/license.txt +411 -0
- data/examples/example01.rb +65 -0
- data/lib/SQLType.rb +230 -0
- data/lib/ib_lib.so +0 -0
- data/lib/ibruby.rb +22 -0
- data/lib/mkdoc +1 -0
- data/lib/src.rb +1794 -0
- data/test/AddRemoveUserTest.rb +55 -0
- data/test/BackupRestoreTest.rb +99 -0
- data/test/BooleanTest.rb +113 -0
- data/test/CharacterSetTest.rb +63 -0
- data/test/ConnectionTest.rb +111 -0
- data/test/DDLTest.rb +54 -0
- data/test/DatabaseTest.rb +83 -0
- data/test/GeneratorTest.rb +50 -0
- data/test/KeyTest.rb +140 -0
- data/test/ResultSetTest.rb +164 -0
- data/test/RoleTest.rb +104 -0
- data/test/RowCountTest.rb +65 -0
- data/test/RowTest.rb +205 -0
- data/test/SQLTest.rb +182 -0
- data/test/SQLTypeTest.rb +101 -0
- data/test/ServiceManagerTest.rb +29 -0
- data/test/StatementTest.rb +135 -0
- data/test/TestSetup.rb +11 -0
- data/test/TransactionTest.rb +112 -0
- data/test/TypeTest.rb +92 -0
- data/test/UnitTest.rb +64 -0
- metadata +77 -0
data/test/SQLTest.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'TestSetup'
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rubygems'
|
6
|
+
require 'ibruby'
|
7
|
+
|
8
|
+
include IBRuby
|
9
|
+
|
10
|
+
class SQLTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}sql_unit_test.ib"
|
13
|
+
ITERATIONS = 100
|
14
|
+
|
15
|
+
INSERT_SQL = "INSERT INTO TEST_TABLE VALUES(?, ?, ?, ?, ?, ?)"
|
16
|
+
|
17
|
+
def setup
|
18
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
19
|
+
if File::exist?(DB_FILE)
|
20
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
21
|
+
end
|
22
|
+
@database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
23
|
+
@connections = []
|
24
|
+
@transactions = []
|
25
|
+
|
26
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
27
|
+
cxn.execute_immediate('CREATE TABLE TEST_TABLE (TESTID INTEGER '\
|
28
|
+
'NOT NULL PRIMARY KEY, TESTTEXT VARCHAR(100), '\
|
29
|
+
'TESTFLOAT NUMERIC(8,2), TESTDATE DATE, TESTTIME '\
|
30
|
+
'TIME, TESTSTAMP TIMESTAMP)')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def teardown
|
35
|
+
@transactions.each do |tx|
|
36
|
+
tx.rollback if tx.active?
|
37
|
+
end
|
38
|
+
|
39
|
+
@connections.each do |cxn|
|
40
|
+
cxn.close if cxn.open?
|
41
|
+
end
|
42
|
+
|
43
|
+
if File::exist?(DB_FILE)
|
44
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
45
|
+
end
|
46
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
47
|
+
end
|
48
|
+
|
49
|
+
def test01
|
50
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
51
|
+
cxn.start_transaction do |tx|
|
52
|
+
s = Statement.new(cxn, tx, INSERT_SQL, 3)
|
53
|
+
f = 0.0
|
54
|
+
t = Time.new
|
55
|
+
|
56
|
+
1.upto(ITERATIONS) do |i|
|
57
|
+
f += 0.321
|
58
|
+
t = Time.at(t.to_i + 5)
|
59
|
+
s.execute_for([i, i.to_s, f, t, nil, t])
|
60
|
+
end
|
61
|
+
|
62
|
+
s.close
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
67
|
+
@connections[0].start_transaction do |tx|
|
68
|
+
r = tx.execute("SELECT COUNT(*) FROM TEST_TABLE")
|
69
|
+
assert(r.fetch[0] == ITERATIONS)
|
70
|
+
r.close
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test02
|
75
|
+
f = 0.0
|
76
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
77
|
+
1.upto(20) do |i|
|
78
|
+
f += i
|
79
|
+
sql = "INSERT INTO TEST_TABLE VALUES (#{i}, "\
|
80
|
+
"#{f.to_s}, #{f}, NULL, NULL, 'NOW')"
|
81
|
+
cxn.execute_immediate(sql)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
86
|
+
@transactions.push(@connections[0].start_transaction)
|
87
|
+
r = @transactions[0].execute("SELECT COUNT(*) FROM TEST_TABLE")
|
88
|
+
assert(r.fetch[0] == 20)
|
89
|
+
r.close
|
90
|
+
|
91
|
+
r = @transactions[0].execute("SELECT * FROM TEST_TABLE WHERE TESTID IN "\
|
92
|
+
"(2, 4, 6, 8, 10) ORDER BY TESTID ASCENDING")
|
93
|
+
a = r.fetch
|
94
|
+
assert(a[0] == 2)
|
95
|
+
assert(a[1] == '3.0')
|
96
|
+
assert(a[2] == 3.0)
|
97
|
+
assert(a[3] == nil)
|
98
|
+
assert(a[4] == nil)
|
99
|
+
|
100
|
+
a = r.fetch
|
101
|
+
assert(a[0] == 4)
|
102
|
+
assert(a[1] == '10.0')
|
103
|
+
assert(a[2] == 10.0)
|
104
|
+
assert(a[3] == nil)
|
105
|
+
assert(a[4] == nil)
|
106
|
+
|
107
|
+
a = r.fetch
|
108
|
+
assert(a[0] == 6)
|
109
|
+
assert(a[1] == '21.0')
|
110
|
+
assert(a[2] == 21.0)
|
111
|
+
assert(a[3] == nil)
|
112
|
+
assert(a[4] == nil)
|
113
|
+
|
114
|
+
a = r.fetch
|
115
|
+
assert(a[0] == 8)
|
116
|
+
assert(a[1] == '36.0')
|
117
|
+
assert(a[2] == 36.0)
|
118
|
+
assert(a[3] == nil)
|
119
|
+
assert(a[4] == nil)
|
120
|
+
|
121
|
+
a = r.fetch
|
122
|
+
assert(a[0] == 10)
|
123
|
+
assert(a[1] == '55.0')
|
124
|
+
assert(a[2] == 55.0)
|
125
|
+
assert(a[3] == nil)
|
126
|
+
assert(a[4] == nil)
|
127
|
+
|
128
|
+
r.close
|
129
|
+
|
130
|
+
@transactions[0].commit
|
131
|
+
|
132
|
+
@connections[0].start_transaction do |tx|
|
133
|
+
s = Statement.new(@connections[0], tx,
|
134
|
+
"UPDATE TEST_TABLE SET TESTSTAMP = NULL", 3)
|
135
|
+
s.execute()
|
136
|
+
s.close
|
137
|
+
|
138
|
+
r = tx.execute("SELECT TESTSTAMP FROM TEST_TABLE")
|
139
|
+
total = 0
|
140
|
+
r.each do |row|
|
141
|
+
assert(row[0] == nil)
|
142
|
+
total = total + 1
|
143
|
+
end
|
144
|
+
assert(total == 20)
|
145
|
+
r.close
|
146
|
+
end
|
147
|
+
|
148
|
+
a = []
|
149
|
+
t = nil
|
150
|
+
@connections[0].start_transaction do |tx|
|
151
|
+
# Perform an insert via a parameterized statement.
|
152
|
+
s = Statement.new(@connections[0], tx,
|
153
|
+
"INSERT INTO TEST_TABLE (TESTID, TESTTEXT, "\
|
154
|
+
"TESTFLOAT, TESTSTAMP) VALUES(?, ?, ?, ?)", 3)
|
155
|
+
t = Time.new
|
156
|
+
s.execute_for([25000, 'La la la', 3.14, t])
|
157
|
+
s.close
|
158
|
+
|
159
|
+
# Fetch the record and check the data.
|
160
|
+
r = tx.execute("SELECT TESTTEXT, TESTFLOAT, TESTSTAMP FROM "\
|
161
|
+
"TEST_TABLE WHERE TESTID = 25000")
|
162
|
+
a = r.fetch
|
163
|
+
r.close
|
164
|
+
end
|
165
|
+
assert(a[0] == 'La la la')
|
166
|
+
assert(a[1] == 3.14)
|
167
|
+
assert(a[2].to_i == t.to_i)
|
168
|
+
|
169
|
+
@connections[0].execute_immediate("DELETE FROM TEST_TABLE WHERE TESTID "\
|
170
|
+
"IN (1, 3, 5, 7, 9, 12, 14, 16, 18, 20)")
|
171
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
172
|
+
a = []
|
173
|
+
cxn.start_transaction do |tx|
|
174
|
+
r = tx.execute("SELECT COUNT(*) FROM TEST_TABLE")
|
175
|
+
a = r.fetch
|
176
|
+
r.close
|
177
|
+
end
|
178
|
+
assert(a[0] == 11)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
data/test/SQLTypeTest.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'TestSetup'
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rubygems'
|
6
|
+
require 'ibruby'
|
7
|
+
|
8
|
+
include IBRuby
|
9
|
+
|
10
|
+
class SQLTypeTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}sql_type_test.fdb"
|
13
|
+
|
14
|
+
def setup
|
15
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
16
|
+
if File::exist?(DB_FILE)
|
17
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
18
|
+
end
|
19
|
+
|
20
|
+
@database = Database.create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
21
|
+
@connection = @database.connect(DB_USER_NAME, DB_PASSWORD)
|
22
|
+
|
23
|
+
@connection.start_transaction do |tx|
|
24
|
+
tx.execute("create table all_types (col01 boolean, col02 blob, "\
|
25
|
+
"col03 char(100), col04 date, col05 decimal(5,2), "\
|
26
|
+
"col06 double precision, col07 float, col08 integer, "\
|
27
|
+
"col09 numeric(10,3), col10 smallint, col11 time, "\
|
28
|
+
"col12 timestamp, col13 varchar(23))")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def teardown
|
33
|
+
@connection.close if @connection != nil && @connection.open?
|
34
|
+
|
35
|
+
if File::exist?(DB_FILE)
|
36
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
37
|
+
end
|
38
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
39
|
+
end
|
40
|
+
|
41
|
+
def test01
|
42
|
+
types = []
|
43
|
+
types.push(SQLType.new(SQLType::BOOLEAN))
|
44
|
+
types.push(SQLType.new(SQLType::VARCHAR, 1000))
|
45
|
+
types.push(SQLType.new(SQLType::DECIMAL, nil, 10))
|
46
|
+
types.push(SQLType.new(SQLType::NUMERIC, nil, 5, 3))
|
47
|
+
types.push(SQLType.new(SQLType::BLOB, nil, nil, nil, 1))
|
48
|
+
|
49
|
+
assert(types[0] == types[0])
|
50
|
+
assert(!(types[0] == types[1]))
|
51
|
+
assert(types[1] == SQLType.new(SQLType::VARCHAR, 1000))
|
52
|
+
|
53
|
+
assert(types[0].type == SQLType::BOOLEAN)
|
54
|
+
assert(types[0].length == nil)
|
55
|
+
assert(types[0].precision == nil)
|
56
|
+
assert(types[0].scale == nil)
|
57
|
+
assert(types[0].subtype == nil)
|
58
|
+
|
59
|
+
assert(types[1].type == SQLType::VARCHAR)
|
60
|
+
assert(types[1].length == 1000)
|
61
|
+
assert(types[1].precision == nil)
|
62
|
+
assert(types[1].scale == nil)
|
63
|
+
assert(types[1].subtype == nil)
|
64
|
+
|
65
|
+
assert(types[2].type == SQLType::DECIMAL)
|
66
|
+
assert(types[2].length == nil)
|
67
|
+
assert(types[2].precision == 10)
|
68
|
+
assert(types[2].scale == nil)
|
69
|
+
assert(types[2].subtype == nil)
|
70
|
+
|
71
|
+
assert(types[3].type == SQLType::NUMERIC)
|
72
|
+
assert(types[3].length == nil)
|
73
|
+
assert(types[3].precision == 5)
|
74
|
+
assert(types[3].scale == 3)
|
75
|
+
assert(types[3].subtype == nil)
|
76
|
+
|
77
|
+
assert(types[4].type == SQLType::BLOB)
|
78
|
+
assert(types[4].length == nil)
|
79
|
+
assert(types[4].precision == nil)
|
80
|
+
assert(types[4].scale == nil)
|
81
|
+
assert(types[4].subtype == 1)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test02
|
85
|
+
types = SQLType.for_table("all_types", @connection)
|
86
|
+
|
87
|
+
assert(types['COL01'] == SQLType.new(SQLType::BOOLEAN))
|
88
|
+
assert(types['COL02'] == SQLType.new(SQLType::BLOB, nil, nil, nil, 0))
|
89
|
+
assert(types['COL03'] == SQLType.new(SQLType::CHAR, 100))
|
90
|
+
assert(types['COL04'] == SQLType.new(SQLType::DATE))
|
91
|
+
assert(types['COL05'] == SQLType.new(SQLType::DECIMAL, nil, 5, 2))
|
92
|
+
assert(types['COL06'] == SQLType.new(SQLType::DOUBLE))
|
93
|
+
assert(types['COL07'] == SQLType.new(SQLType::FLOAT))
|
94
|
+
assert(types['COL08'] == SQLType.new(SQLType::INTEGER))
|
95
|
+
assert(types['COL09'] == SQLType.new(SQLType::NUMERIC, nil, 10, 3))
|
96
|
+
assert(types['COL10'] == SQLType.new(SQLType::SMALLINT))
|
97
|
+
assert(types['COL11'] == SQLType.new(SQLType::TIME))
|
98
|
+
assert(types['COL12'] == SQLType.new(SQLType::TIMESTAMP))
|
99
|
+
assert(types['COL13'] == SQLType.new(SQLType::VARCHAR, 23))
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'TestSetup'
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rubygems'
|
6
|
+
require 'ibruby'
|
7
|
+
|
8
|
+
include IBRuby
|
9
|
+
|
10
|
+
class ServiceManagerTest < Test::Unit::TestCase
|
11
|
+
def setup
|
12
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
17
|
+
end
|
18
|
+
|
19
|
+
def test01
|
20
|
+
sm = ServiceManager.new('localhost')
|
21
|
+
assert(sm.connected? == false)
|
22
|
+
|
23
|
+
assert(sm.connect(DB_USER_NAME, DB_PASSWORD) == sm)
|
24
|
+
assert(sm.connected?)
|
25
|
+
|
26
|
+
assert(sm.disconnect == sm)
|
27
|
+
assert(sm.connected? == false)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'TestSetup'
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rubygems'
|
6
|
+
require 'ibruby'
|
7
|
+
|
8
|
+
include IBRuby
|
9
|
+
|
10
|
+
class StatementTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}stmt_unit_test.fdb"
|
13
|
+
|
14
|
+
def setup
|
15
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
16
|
+
if File::exist?(DB_FILE)
|
17
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
18
|
+
end
|
19
|
+
@database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
20
|
+
@connections = []
|
21
|
+
@transactions = []
|
22
|
+
end
|
23
|
+
|
24
|
+
def teardown
|
25
|
+
@transactions.each do |tx|
|
26
|
+
tx.rollback if tx.active?
|
27
|
+
end
|
28
|
+
@transactions.clear
|
29
|
+
@connections.each do |cxn|
|
30
|
+
cxn.close if cxn.open?
|
31
|
+
end
|
32
|
+
@connections.clear
|
33
|
+
if File::exist?(DB_FILE)
|
34
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
35
|
+
end
|
36
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
37
|
+
end
|
38
|
+
|
39
|
+
def test01
|
40
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
41
|
+
@transactions.push(@connections.last.start_transaction)
|
42
|
+
|
43
|
+
s1 = Statement.new(@connections[0],
|
44
|
+
@transactions[0],
|
45
|
+
"SELECT RDB$FIELD_NAME FROM RDB$FIELDS", 3)
|
46
|
+
assert(s1 != nil)
|
47
|
+
assert(s1.sql == "SELECT RDB$FIELD_NAME FROM RDB$FIELDS")
|
48
|
+
assert(s1.connection == @connections[0])
|
49
|
+
assert(s1.transaction == @transactions[0])
|
50
|
+
assert(s1.dialect == 3)
|
51
|
+
assert(s1.type == Statement::SELECT_STATEMENT)
|
52
|
+
s1.close
|
53
|
+
|
54
|
+
s2 = Statement.new(@connections[0],
|
55
|
+
@transactions[0],
|
56
|
+
"DELETE FROM RDB$EXCEPTIONS", 1)
|
57
|
+
assert(s2 != nil)
|
58
|
+
assert(s2.sql == "DELETE FROM RDB$EXCEPTIONS")
|
59
|
+
assert(s2.connection == @connections[0])
|
60
|
+
assert(s2.transaction == @transactions[0])
|
61
|
+
assert(s2.dialect == 1)
|
62
|
+
assert(s2.type == Statement::DELETE_STATEMENT)
|
63
|
+
s2.close
|
64
|
+
end
|
65
|
+
|
66
|
+
def test02
|
67
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
68
|
+
@transactions.push(@connections[0].start_transaction)
|
69
|
+
|
70
|
+
s = Statement.new(@connections[0], @transactions[0],
|
71
|
+
"SELECT RDB$FIELD_NAME FROM RDB$FIELDS "\
|
72
|
+
"WHERE RDB$FIELD_NAME LIKE ?", 3)
|
73
|
+
|
74
|
+
begin
|
75
|
+
r = s.execute
|
76
|
+
r.close
|
77
|
+
assert(false,
|
78
|
+
"Executed statement that required parameter without the "\
|
79
|
+
"parameter being specified.")
|
80
|
+
rescue Exception => error
|
81
|
+
end
|
82
|
+
|
83
|
+
begin
|
84
|
+
r = s.execute_for([])
|
85
|
+
r.close
|
86
|
+
assert(false,
|
87
|
+
"Executed statement that required a parameter with an empty "\
|
88
|
+
"parameter set.")
|
89
|
+
rescue Exception => error
|
90
|
+
end
|
91
|
+
|
92
|
+
begin
|
93
|
+
r = s.execute_for(['LALALA', 25])
|
94
|
+
r.close
|
95
|
+
assert(false,
|
96
|
+
"Executed statement that required a parameter with too many "\
|
97
|
+
"parameters.")
|
98
|
+
rescue Exception => error
|
99
|
+
end
|
100
|
+
|
101
|
+
r = s.execute_for(['LALALA'])
|
102
|
+
assert(r != nil)
|
103
|
+
assert(r.class == ResultSet)
|
104
|
+
r.close
|
105
|
+
|
106
|
+
total = 0
|
107
|
+
s.execute_for(["%"]) do |row|
|
108
|
+
total = total + 1
|
109
|
+
end
|
110
|
+
assert(total = 88)
|
111
|
+
s.close
|
112
|
+
end
|
113
|
+
|
114
|
+
def test03
|
115
|
+
d = nil
|
116
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
117
|
+
cxn.execute_immediate('CREATE TABLE STRING_TEST(TEXT VARCHAR(10))')
|
118
|
+
cxn.start_transaction do |tx|
|
119
|
+
# Perform an truncated insert.
|
120
|
+
s = Statement.new(cxn, tx, 'INSERT INTO STRING_TEST VALUES(?)', 3)
|
121
|
+
s.execute_for(['012345678901234'])
|
122
|
+
|
123
|
+
# Perform a select of the value inserted.
|
124
|
+
r = cxn.execute('SELECT * FROM STRING_TEST', tx)
|
125
|
+
d = r.fetch
|
126
|
+
|
127
|
+
# Clean up.
|
128
|
+
s.close
|
129
|
+
r.close
|
130
|
+
end
|
131
|
+
assert(d[0] == '0123456789')
|
132
|
+
cxn.execute_immediate('DROP TABLE STRING_TEST')
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
data/test/TestSetup.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Add extra search paths.
|
4
|
+
basedir = File::dirname(Dir.getwd)
|
5
|
+
$:.push(basedir)
|
6
|
+
$:.push("#{basedir}#{File::SEPARATOR}test")
|
7
|
+
$:.push("#{basedir}#{File::SEPARATOR}lib")
|
8
|
+
|
9
|
+
DB_USER_NAME = 'sysdba'
|
10
|
+
DB_PASSWORD = 'masterkey'
|
11
|
+
TEST_LOGGING = true
|
@@ -0,0 +1,112 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'TestSetup'
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rubygems'
|
6
|
+
require 'ibruby'
|
7
|
+
|
8
|
+
include IBRuby
|
9
|
+
|
10
|
+
class TransactionTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}tx_unit_test.ib"
|
13
|
+
|
14
|
+
def setup
|
15
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
16
|
+
if File::exist?(DB_FILE)
|
17
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
18
|
+
end
|
19
|
+
|
20
|
+
@database = Database.create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
21
|
+
@connections = []
|
22
|
+
@transactions = []
|
23
|
+
|
24
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
25
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
26
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
27
|
+
end
|
28
|
+
|
29
|
+
def teardown
|
30
|
+
@transactions.each do |tx|
|
31
|
+
tx.rollback if tx.active?
|
32
|
+
end
|
33
|
+
@transactions.clear
|
34
|
+
@connections.each do |cxn|
|
35
|
+
cxn.close if cxn.open?
|
36
|
+
end
|
37
|
+
if File::exist?(DB_FILE)
|
38
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
39
|
+
end
|
40
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
41
|
+
end
|
42
|
+
|
43
|
+
def test01
|
44
|
+
@transactions.push(@connections[0].start_transaction)
|
45
|
+
assert(@transactions[0].active?)
|
46
|
+
|
47
|
+
@transactions.push(Transaction.new(@connections[1]))
|
48
|
+
assert(@transactions[1].active?)
|
49
|
+
|
50
|
+
assert(@transactions[0].connections == [@connections[0]])
|
51
|
+
assert(@transactions[1].connections == [@connections[1]])
|
52
|
+
|
53
|
+
assert(@transactions[0].for_connection?(@connections[1]) == false)
|
54
|
+
assert(@transactions[1].for_connection?(@connections[0]) == false)
|
55
|
+
assert(@transactions[0].for_connection?(@connections[0]))
|
56
|
+
assert(@transactions[1].for_connection?(@connections[1]))
|
57
|
+
|
58
|
+
@transactions[0].commit
|
59
|
+
assert(@transactions[0].active? == false)
|
60
|
+
assert(@transactions[0].connections == [])
|
61
|
+
assert(@transactions[0].for_connection?(@connections[0]) == false)
|
62
|
+
assert(@transactions[0].for_connection?(@connections[1]) == false)
|
63
|
+
|
64
|
+
@transactions[1].rollback
|
65
|
+
assert(@transactions[1].active? == false)
|
66
|
+
assert(@transactions[1].connections == [])
|
67
|
+
assert(@transactions[1].for_connection?(@connections[0]) == false)
|
68
|
+
assert(@transactions[1].for_connection?(@connections[1]) == false)
|
69
|
+
|
70
|
+
@transactions[0] = Transaction.new([@connections[0], @connections[1]])
|
71
|
+
assert(@transactions[0].active? == true)
|
72
|
+
assert(@transactions[0].for_connection?(@connections[0]))
|
73
|
+
assert(@transactions[0].for_connection?(@connections[1]))
|
74
|
+
assert(@transactions[0].for_connection?(@connections[2]) == false)
|
75
|
+
|
76
|
+
@transactions[0].commit
|
77
|
+
assert(@transactions[0].active? == false)
|
78
|
+
assert(@transactions[0].connections == [])
|
79
|
+
assert(@transactions[0].for_connection?(@connections[0]) == false)
|
80
|
+
assert(@transactions[0].for_connection?(@connections[1]) == false)
|
81
|
+
assert(@transactions[0].for_connection?(@connections[2]) == false)
|
82
|
+
|
83
|
+
@transactions[0] = Transaction.new([@connections[0], @connections[2]])
|
84
|
+
assert(@transactions[0].active?)
|
85
|
+
@transactions[0].rollback
|
86
|
+
assert(@transactions[0].active? == false)
|
87
|
+
assert(@transactions[0].connections == [])
|
88
|
+
assert(@transactions[0].for_connection?(@connections[0]) == false)
|
89
|
+
assert(@transactions[0].for_connection?(@connections[1]) == false)
|
90
|
+
assert(@transactions[0].for_connection?(@connections[2]) == false)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test02
|
94
|
+
sql = []
|
95
|
+
sql.push("UPDATE RDB$EXCEPTIONS SET RDB$MESSAGE = 'WoooHooo'"\
|
96
|
+
"WHERE RDB$EXCEPTION_NAME = 'Lalala'")
|
97
|
+
sql.push("SELECT RDB$FIELD_NAME FROM RDB$FIELDS")
|
98
|
+
@transactions.push(Transaction.new(@connections[0]))
|
99
|
+
|
100
|
+
assert(@transactions[0].execute(sql[0]) == 0)
|
101
|
+
r = @transactions[0].execute(sql[1])
|
102
|
+
assert(r != nil)
|
103
|
+
assert(r.class == ResultSet)
|
104
|
+
r.close
|
105
|
+
|
106
|
+
total = 0
|
107
|
+
@transactions[0].execute(sql[1]) do |row|
|
108
|
+
total += 1
|
109
|
+
end
|
110
|
+
assert(total == 113)
|
111
|
+
end
|
112
|
+
end
|
data/test/TypeTest.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'TestSetup'
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rubygems'
|
6
|
+
require 'ibruby'
|
7
|
+
require 'date'
|
8
|
+
|
9
|
+
include IBRuby
|
10
|
+
|
11
|
+
class TypeTest < Test::Unit::TestCase
|
12
|
+
CURDIR = "#{Dir.getwd}"
|
13
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}types_unit_test.ib"
|
14
|
+
|
15
|
+
def setup
|
16
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
17
|
+
if File.exist?(DB_FILE)
|
18
|
+
db = Database.new(DB_FILE)
|
19
|
+
db.drop(DB_USER_NAME, DB_PASSWORD)
|
20
|
+
end
|
21
|
+
@db = Database.create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
22
|
+
cxn = @db.connect(DB_USER_NAME, DB_PASSWORD)
|
23
|
+
cxn.start_transaction do |tx|
|
24
|
+
tx.execute("create table types_table (COL01 integer, "\
|
25
|
+
"COL02 float, COL03 decimal(10,2), "\
|
26
|
+
"COL04 numeric(5,3), COL05 date, COL06 timestamp, "\
|
27
|
+
"COL07 char(10), COL08 time, COL09 varchar(30))")
|
28
|
+
end
|
29
|
+
|
30
|
+
cxn.start_transaction do |tx|
|
31
|
+
stmt = Statement.new(cxn, tx, "insert into types_table values "\
|
32
|
+
"(?, ?, ?, ?, ?, ?, ?, ?, ?)", 3)
|
33
|
+
stmt.execute_for([10, 100.2, 2378.65, 192.345,
|
34
|
+
Date.new(2005, 10, 21), Time.new, 'La la la',
|
35
|
+
Time.new, "Oobly Joobly"])
|
36
|
+
stmt.close
|
37
|
+
end
|
38
|
+
cxn.close
|
39
|
+
end
|
40
|
+
|
41
|
+
def teardown
|
42
|
+
if File::exist?(DB_FILE)
|
43
|
+
@db.drop(DB_USER_NAME, DB_PASSWORD)
|
44
|
+
end
|
45
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
46
|
+
end
|
47
|
+
|
48
|
+
def test01
|
49
|
+
rows = cxn = nil
|
50
|
+
begin
|
51
|
+
cxn = @db.connect(DB_USER_NAME, DB_PASSWORD)
|
52
|
+
rows = cxn.execute_immediate('select * from types_table')
|
53
|
+
row = rows.fetch
|
54
|
+
assert(row[0].kind_of?(Integer))
|
55
|
+
assert(row[1].instance_of?(Float))
|
56
|
+
assert(row[2].instance_of?(Float))
|
57
|
+
assert(row[3].instance_of?(Float))
|
58
|
+
assert(row[4].instance_of?(Date))
|
59
|
+
assert(row[5].instance_of?(Time))
|
60
|
+
assert(row[6].instance_of?(String))
|
61
|
+
assert(row[7].instance_of?(Time))
|
62
|
+
assert(row[8].instance_of?(String))
|
63
|
+
ensure
|
64
|
+
rows.close if rows != nil
|
65
|
+
cxn.close if cxn != nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def test02
|
70
|
+
$IBRubySettings[:DATE_AS_DATE] = false
|
71
|
+
rows = cxn = nil
|
72
|
+
begin
|
73
|
+
cxn = @db.connect(DB_USER_NAME, DB_PASSWORD)
|
74
|
+
rows = cxn.execute_immediate('select * from types_table')
|
75
|
+
row = rows.fetch
|
76
|
+
assert(row[0].kind_of?(Integer))
|
77
|
+
assert(row[1].instance_of?(Float))
|
78
|
+
assert(row[2].instance_of?(Float))
|
79
|
+
assert(row[3].instance_of?(Float))
|
80
|
+
assert(row[4].instance_of?(Time))
|
81
|
+
assert(row[5].instance_of?(Time))
|
82
|
+
assert(row[6].instance_of?(String))
|
83
|
+
assert(row[7].instance_of?(Time))
|
84
|
+
assert(row[8].instance_of?(String))
|
85
|
+
rows.close
|
86
|
+
ensure
|
87
|
+
rows.close if rows != nil
|
88
|
+
cxn.close if cxn != nil
|
89
|
+
end
|
90
|
+
$IBRubySettings[:DATE_AS_DATE] = true
|
91
|
+
end
|
92
|
+
end
|