ibruby 0.5.5-i686-darwin8.9.1
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/ib_lib.bundle +0 -0
- data/lib/ibmeta.rb +823 -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 +59 -0
- data/test/GeneratorTest.rb.rej +24 -0
- data/test/KeyTest.rb +140 -0
- data/test/KeyTest.rb.rej +17 -0
- data/test/MetaTest.rb +99 -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/SQLTypeTest.rb.rej +17 -0
- data/test/ServiceManagerTest.rb +29 -0
- data/test/StatementTest.rb +135 -0
- data/test/StatementTest.rb.rej +17 -0
- data/test/TestSetup.rb +11 -0
- data/test/TransactionTest.rb +112 -0
- data/test/TypeTest.rb +92 -0
- data/test/TypeTest.rb.rej +38 -0
- data/test/UnitTest.rb +64 -0
- data/test/UnitTest.rb.rej +19 -0
- metadata +84 -0
data/test/KeyTest.rb
ADDED
@@ -0,0 +1,140 @@
|
|
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 KeyTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}key_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
|
+
# Switch to the old way of keying.
|
21
|
+
$IBRubySettings[:ALIAS_KEYS] = false
|
22
|
+
|
23
|
+
database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
24
|
+
@connection = database.connect(DB_USER_NAME, DB_PASSWORD)
|
25
|
+
@transaction = @connection.start_transaction
|
26
|
+
@results = ResultSet.new(@connection, @transaction,
|
27
|
+
'SELECT * FROM RDB$FIELDS', 3, nil)
|
28
|
+
@empty = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
29
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0]
|
30
|
+
|
31
|
+
@connection.execute_immediate('create table keytest (COL01 integer, '\
|
32
|
+
'COL02 varchar(10), COL03 integer)')
|
33
|
+
@connection.execute_immediate("insert into keytest values (1, 'Two', 3)")
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
@results.close
|
38
|
+
@transaction.rollback
|
39
|
+
@connection.close
|
40
|
+
if File::exist?(DB_FILE)
|
41
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
42
|
+
end
|
43
|
+
$IBRubySettings[:ALIAS_KEYS] = true
|
44
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_01
|
48
|
+
sql = 'select COL01 one, COL02 two, COL03 three from keytest'
|
49
|
+
rows = @connection.execute_immediate(sql)
|
50
|
+
data = rows.fetch
|
51
|
+
|
52
|
+
count = 0
|
53
|
+
data.each do |name, value|
|
54
|
+
assert(['COL01', 'COL02', 'COL03'].include?(name))
|
55
|
+
assert([1, 'Two', 3].include?(value))
|
56
|
+
count += 1
|
57
|
+
end
|
58
|
+
assert(count == 3)
|
59
|
+
|
60
|
+
count = 0
|
61
|
+
data.each_key do |name|
|
62
|
+
assert(['COL01', 'COL02', 'COL03'].include?(name))
|
63
|
+
count += 1
|
64
|
+
end
|
65
|
+
assert(count == 3)
|
66
|
+
|
67
|
+
count = 0
|
68
|
+
data.each_value do |value|
|
69
|
+
assert([1, 'Two', 3].include?(value))
|
70
|
+
count += 1
|
71
|
+
end
|
72
|
+
assert(count == 3)
|
73
|
+
|
74
|
+
assert(data.fetch('COL02') == 'Two')
|
75
|
+
assert(data.fetch('COL04', 'LALALA') == 'LALALA')
|
76
|
+
assert(data.fetch('COL00') {'NOT FOUND'} == 'NOT FOUND')
|
77
|
+
begin
|
78
|
+
data.fetch('COL05')
|
79
|
+
assert(false, 'Row#fetch succeeded for non-existent column name.')
|
80
|
+
rescue IndexError
|
81
|
+
end
|
82
|
+
|
83
|
+
assert(data.has_key?('COL01'))
|
84
|
+
assert(data.has_key?('COL10') == false)
|
85
|
+
|
86
|
+
assert(data.has_column?('COL02'))
|
87
|
+
assert(data.has_column?('COL22') == false)
|
88
|
+
|
89
|
+
assert(data.has_alias?('TWO'))
|
90
|
+
assert(data.has_alias?('FOUR') == false)
|
91
|
+
|
92
|
+
assert(data.has_value?(3))
|
93
|
+
assert(data.has_value?('LALALA') == false)
|
94
|
+
|
95
|
+
assert(data.keys.size == 3)
|
96
|
+
data.keys.each do |name|
|
97
|
+
assert(['COL01', 'COL02', 'COL03'].include?(name))
|
98
|
+
end
|
99
|
+
|
100
|
+
assert(data.names.size == 3)
|
101
|
+
data.names.each do |name|
|
102
|
+
assert(['COL01', 'COL02', 'COL03'].include?(name))
|
103
|
+
end
|
104
|
+
|
105
|
+
assert(data.aliases.size == 3)
|
106
|
+
data.aliases.each do |name|
|
107
|
+
assert(['ONE', 'TWO', 'THREE'].include?(name))
|
108
|
+
end
|
109
|
+
|
110
|
+
assert(data.values.size == 3)
|
111
|
+
data.values.each do |value|
|
112
|
+
assert([1, 'Two', 3].include?(value))
|
113
|
+
end
|
114
|
+
|
115
|
+
array = data.select {|name, value| name == 'COL02'}
|
116
|
+
assert(array.size == 1)
|
117
|
+
assert(array[0][0] == 'COL02')
|
118
|
+
assert(array[0][1] == 'Two')
|
119
|
+
|
120
|
+
array = data.to_a
|
121
|
+
assert(array.size == 3)
|
122
|
+
assert(array.include?(['COL01', 1]))
|
123
|
+
assert(array.include?(['COL02', 'Two']))
|
124
|
+
assert(array.include?(['COL03', 3]))
|
125
|
+
|
126
|
+
hash = data.to_hash
|
127
|
+
assert(hash.length == 3)
|
128
|
+
assert(hash['COL01'] == 1)
|
129
|
+
assert(hash['COL02'] == 'Two')
|
130
|
+
assert(hash['COL03'] == 3)
|
131
|
+
|
132
|
+
array = data.values_at('COL10', 'COL02', 'COL03')
|
133
|
+
assert(array.size == 3)
|
134
|
+
assert(array.include?('Two'))
|
135
|
+
assert(array.include?(3))
|
136
|
+
assert(array.include?(nil))
|
137
|
+
|
138
|
+
rows.close
|
139
|
+
end
|
140
|
+
end
|
data/test/KeyTest.rb.rej
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
***************
|
2
|
+
*** 9,15 ****
|
3
|
+
|
4
|
+
class KeyTest < Test::Unit::TestCase
|
5
|
+
CURDIR = "#{Dir.getwd}"
|
6
|
+
- DB_FILE = "#{CURDIR}#{File::SEPARATOR}key_unit_test.fdb"
|
7
|
+
|
8
|
+
def setup
|
9
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
10
|
+
--- 9,15 ----
|
11
|
+
|
12
|
+
class KeyTest < Test::Unit::TestCase
|
13
|
+
CURDIR = "#{Dir.getwd}"
|
14
|
+
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}key_unit_test.ib"
|
15
|
+
|
16
|
+
def setup
|
17
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
data/test/MetaTest.rb
ADDED
@@ -0,0 +1,99 @@
|
|
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 MetaTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}meta_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
|
+
@connection = @database.connect(DB_USER_NAME, DB_PASSWORD)
|
22
|
+
|
23
|
+
@creation_sql = "create table mtest (id integer not null, "\
|
24
|
+
"bool1 boolean default false, bool2 boolean, bool3 boolean default true, bool4 boolean default true not null,"\
|
25
|
+
" blob1 blob sub_type 1 not null, blob2 blob sub_type 1, blob3 blob sub_type 0,"\
|
26
|
+
" char1 char(10) default 'fred', char2 char(10) default 'fred' not null, char3 char(10), char4 char(20) default 'wil''ma',"\
|
27
|
+
" date1 date default current_date, date2 date not null, date3 date default current_date not null,"\
|
28
|
+
" decimal1 decimal(18,5) default 1.345, decimal2 decimal(15,5) default 20.22 not null, decimal3 decimal(12,6) not null"\
|
29
|
+
")"
|
30
|
+
#puts sql
|
31
|
+
@connection.execute_immediate( @creation_sql );
|
32
|
+
@connection.execute_immediate( "create table pk_table(id integer not null primary key)" )
|
33
|
+
@connection.execute_immediate( "create table fk_table(id integer not null primary key, "\
|
34
|
+
"fk_id integer references pk_table(id))" )
|
35
|
+
@pk_sql = "alter table mtest "
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
def teardown
|
41
|
+
@connection.close
|
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
|
+
table = InterBaseTable.new('mtest')
|
51
|
+
assert_nothing_thrown( "failed to load table mtest" ) { table.load(@connection) }
|
52
|
+
sql = []
|
53
|
+
assert_nothing_thrown( "unable to build sql!" ) { sql = table.to_sql }
|
54
|
+
assert_not_nil sql, "sql returned nil for create table"
|
55
|
+
assert_equal sql.size > 0, true, "sql returned has nothing in it for table creation!"
|
56
|
+
#assert_equal @creation_sql.upcase, sql[0].upcase
|
57
|
+
#they ARE the same, but shows false and don't know why
|
58
|
+
puts sql
|
59
|
+
end
|
60
|
+
|
61
|
+
def test02
|
62
|
+
col = InterBaseMetaFunctions.table_fields( @connection, "mtest", true, "decimal2" )
|
63
|
+
new_col = col.dup
|
64
|
+
new_col.precision = 16
|
65
|
+
new_col.scale = 2
|
66
|
+
col.change_column(@connection,new_col)
|
67
|
+
new_col = InterBaseMetaFunctions.table_fields( @connection, "mtest", true, "decimal2" )
|
68
|
+
|
69
|
+
assert_equal false, new_col == col, "column decimal2 not changed"
|
70
|
+
assert_equal new_col.precision, 16, "column decimal2 precision not 16!"
|
71
|
+
assert_equal new_col.scale, 2, "column scale should be 2!"
|
72
|
+
end
|
73
|
+
|
74
|
+
def test03
|
75
|
+
col = InterBaseMetaFunctions.table_fields( @connection, "mtest", true, "decimal2" )
|
76
|
+
col.rename_column( @connection, "decimal_rename" )
|
77
|
+
ren_col = InterBaseMetaFunctions.table_fields( @connection, "mtest", true, "decimal2" )
|
78
|
+
assert_equal nil, ren_col, "column not renamed!"
|
79
|
+
new_col = InterBaseMetaFunctions.table_fields( @connection, "mtest", true, "decimal_rename" )
|
80
|
+
assert_equal new_col, col, "column types not identical after rename"
|
81
|
+
new_col.rename_column( @connection, "decimal2" )
|
82
|
+
end
|
83
|
+
|
84
|
+
def test04
|
85
|
+
table = InterBaseTable.new('fk_table')
|
86
|
+
table.load(@connection)
|
87
|
+
assert_nothing_thrown( "unable build sql for table fk_table! " ) do
|
88
|
+
table.to_sql.each() {|sql| puts "table creation sql: #{sql}" }
|
89
|
+
end
|
90
|
+
assert_nothing_thrown( "unable to rename table" ) do
|
91
|
+
table.rename_table( @connection, 'new_fk_table' )
|
92
|
+
end
|
93
|
+
table = InterBaseTable.new('new_fk_table')
|
94
|
+
table.load(@connection)
|
95
|
+
assert_nothing_thrown( "unable build sql for table fk_table! " ) do
|
96
|
+
table.to_sql.each() {|sql| puts "table creation sql: #{sql}" }
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,164 @@
|
|
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 ResultSetTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}result_set_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
|
+
@database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
20
|
+
@connections = []
|
21
|
+
@transactions = []
|
22
|
+
|
23
|
+
@connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
|
24
|
+
|
25
|
+
@connections[0].start_transaction do |tx|
|
26
|
+
tx.execute("CREATE TABLE TEST_TABLE (TESTID INTEGER NOT NULL "\
|
27
|
+
"primary KEY, TESTINFO VARCHAR(100))")
|
28
|
+
tx.execute('create table all_types (col01 boolean, col02 blob, '\
|
29
|
+
'col03 char(100), col04 date, col05 decimal(5,2), '\
|
30
|
+
'col06 double precision, col07 float, col08 integer, '\
|
31
|
+
'col09 numeric(10,3), col10 smallint, col11 time, '\
|
32
|
+
'col12 timestamp, col13 varchar(23))')
|
33
|
+
end
|
34
|
+
|
35
|
+
@connections[0].start_transaction do |tx|
|
36
|
+
begin
|
37
|
+
tx.execute("INSERT INTO TEST_TABLE VALUES (10, 'Record One.')")
|
38
|
+
tx.execute("INSERT INTO TEST_TABLE VALUES (20, 'Record Two.')")
|
39
|
+
tx.execute("INSERT INTO TEST_TABLE VALUES (30, 'Record Three.')")
|
40
|
+
tx.execute("INSERT INTO TEST_TABLE VALUES (40, 'Record Four.')")
|
41
|
+
tx.execute("INSERT INTO TEST_TABLE VALUES (50, 'Record Five.')")
|
42
|
+
rescue Exception => error
|
43
|
+
puts error.message
|
44
|
+
error.backtrace.each {|step| puts " #{step}"}
|
45
|
+
raise
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
@transactions.push(@connections[0].start_transaction)
|
50
|
+
end
|
51
|
+
|
52
|
+
def teardown
|
53
|
+
@transactions.each do |tx|
|
54
|
+
tx.rollback if tx.active?
|
55
|
+
end
|
56
|
+
@transactions.clear
|
57
|
+
@connections.each do |cxn|
|
58
|
+
cxn.close if cxn.open?
|
59
|
+
end
|
60
|
+
@connections.clear
|
61
|
+
if File::exist?(DB_FILE)
|
62
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
63
|
+
end
|
64
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
65
|
+
end
|
66
|
+
|
67
|
+
def test01
|
68
|
+
r = ResultSet.new(@connections[0], @transactions[0],
|
69
|
+
"SELECT * FROM TEST_TABLE ORDER BY TESTID", 3, nil)
|
70
|
+
|
71
|
+
assert(r.connection == @connections[0])
|
72
|
+
assert(r.transaction == @transactions[0])
|
73
|
+
assert(r.sql == "SELECT * FROM TEST_TABLE ORDER BY TESTID")
|
74
|
+
assert(r.dialect == 3)
|
75
|
+
|
76
|
+
assert(r.fetch != nil)
|
77
|
+
assert(r.fetch.class == Row)
|
78
|
+
assert(r.fetch[0] == 30)
|
79
|
+
assert(r.fetch[1] == 'Record Four.')
|
80
|
+
r.fetch
|
81
|
+
assert(r.fetch == nil)
|
82
|
+
r.close
|
83
|
+
|
84
|
+
r = ResultSet.new(@connections[0], @transactions[0],
|
85
|
+
"SELECT * FROM TEST_TABLE ORDER BY TESTID", 3, nil)
|
86
|
+
assert(r.column_count == 2)
|
87
|
+
assert(r.column_name(0) == 'TESTID')
|
88
|
+
assert(r.column_name(1) == 'TESTINFO')
|
89
|
+
assert(r.column_name(3) == nil)
|
90
|
+
assert(r.column_name(-1) == nil)
|
91
|
+
assert(r.column_table(0) == 'TEST_TABLE')
|
92
|
+
assert(r.column_table(1) == 'TEST_TABLE')
|
93
|
+
assert(r.column_table(2) == nil)
|
94
|
+
assert(r.column_table(-1) == nil)
|
95
|
+
assert(r.column_alias(0) == 'TESTID')
|
96
|
+
assert(r.column_alias(1) == 'TESTINFO')
|
97
|
+
assert(r.column_alias(3) == nil)
|
98
|
+
assert(r.column_alias(-1) == nil)
|
99
|
+
r.close
|
100
|
+
|
101
|
+
r = ResultSet.new(@connections[0], @transactions[0],
|
102
|
+
"SELECT * FROM TEST_TABLE ORDER BY TESTID", 3, nil)
|
103
|
+
total = 0
|
104
|
+
r.each do |row|
|
105
|
+
total += 1
|
106
|
+
end
|
107
|
+
assert(total == 5)
|
108
|
+
assert(r.exhausted?)
|
109
|
+
end
|
110
|
+
|
111
|
+
def test02
|
112
|
+
r = ResultSet.new(@connections[0], @transactions[0],
|
113
|
+
'select * from test_table where testid between ? and ?',
|
114
|
+
3, [20, 40])
|
115
|
+
assert(r.exhausted? == false)
|
116
|
+
total = 0
|
117
|
+
r.each {|row| total += 1}
|
118
|
+
assert(total == 3)
|
119
|
+
assert(r.exhausted?)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test03
|
123
|
+
begin
|
124
|
+
ResultSet.new(@connections[0], @transactions[0],
|
125
|
+
"insert into test_table values(?, ?)", 3,
|
126
|
+
[100, 'Should fail.'])
|
127
|
+
assert(false, "Created result set with non-query SQL statement.")
|
128
|
+
rescue IBRubyException
|
129
|
+
end
|
130
|
+
|
131
|
+
begin
|
132
|
+
ResultSet.new(@connections[0], @transactions[0],
|
133
|
+
"select * from test_table where testid = ?", 3,
|
134
|
+
[])
|
135
|
+
assert(false, 'Created result set with insufficient parameters.')
|
136
|
+
rescue IBRubyException
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def test04
|
141
|
+
results = nil
|
142
|
+
begin
|
143
|
+
results = @transactions[0].execute('select * from all_types')
|
144
|
+
|
145
|
+
#assert(results.get_base_type(0) == SQLType::BIGINT)
|
146
|
+
puts "blob=#{results.get_base_type(0)}"
|
147
|
+
puts "blob=#{results.get_base_type(2)}"
|
148
|
+
assert(results.get_base_type(1) == SQLType::BLOB)
|
149
|
+
assert(results.get_base_type(2) == SQLType::CHAR)
|
150
|
+
assert(results.get_base_type(3) == SQLType::DATE)
|
151
|
+
assert(results.get_base_type(4) == SQLType::DECIMAL)
|
152
|
+
assert(results.get_base_type(5) == SQLType::DOUBLE)
|
153
|
+
assert(results.get_base_type(6) == SQLType::FLOAT)
|
154
|
+
assert(results.get_base_type(7) == SQLType::INTEGER)
|
155
|
+
assert(results.get_base_type(8) == SQLType::NUMERIC)
|
156
|
+
assert(results.get_base_type(9) == SQLType::SMALLINT)
|
157
|
+
assert(results.get_base_type(10) == SQLType::TIME)
|
158
|
+
assert(results.get_base_type(11) == SQLType::TIMESTAMP)
|
159
|
+
assert(results.get_base_type(12) == SQLType::VARCHAR)
|
160
|
+
ensure
|
161
|
+
results.close if results != nil
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/test/RoleTest.rb
ADDED
@@ -0,0 +1,104 @@
|
|
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
|
+
# This test case isn't working as I cannot get the basic operation of roles to work under ISQL so I am unsure
|
11
|
+
# how they are supposed to work.
|
12
|
+
|
13
|
+
class DatabaseTest < Test::Unit::TestCase
|
14
|
+
CURDIR = "#{Dir.getwd}"
|
15
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}role_unit_test.ib"
|
16
|
+
|
17
|
+
def setup
|
18
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
19
|
+
if File.exist?(DB_FILE)
|
20
|
+
db = Database.new(DB_FILE)
|
21
|
+
db.drop(DB_USER_NAME, DB_PASSWORD)
|
22
|
+
end
|
23
|
+
|
24
|
+
#connect "role_unit_test.ib" user "newuser" password "password" role sales;
|
25
|
+
#insert into sales_test values(1);
|
26
|
+
Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
27
|
+
createUser
|
28
|
+
createRoleAndAllocate
|
29
|
+
end
|
30
|
+
|
31
|
+
def createUser
|
32
|
+
#create the user we will use to grant permission to
|
33
|
+
sm = ServiceManager.new('localhost')
|
34
|
+
sm.connect(DB_USER_NAME, DB_PASSWORD)
|
35
|
+
|
36
|
+
au = AddUser.new('newuser', 'password', 'first', 'middle', 'last')
|
37
|
+
au.execute(sm)
|
38
|
+
sm.disconnect
|
39
|
+
end
|
40
|
+
|
41
|
+
def createRoleAndAllocate
|
42
|
+
database = Database::new(DB_FILE)
|
43
|
+
@connection = database.connect(DB_USER_NAME, DB_PASSWORD)
|
44
|
+
@connection.execute_immediate( 'create role sales' )
|
45
|
+
@connection.execute_immediate( 'create table sales_test(sales_drone integer)' )
|
46
|
+
@connection.execute_immediate( 'grant insert on sales_test to sales' )
|
47
|
+
# no permission to this table (notsales)
|
48
|
+
@connection.execute_immediate( 'create table notsales(sales_drone integer)' )
|
49
|
+
# add 'newuser' into sales role
|
50
|
+
@connection.execute_immediate( 'grant sales to newuser' )
|
51
|
+
@connection.close
|
52
|
+
end
|
53
|
+
|
54
|
+
def dropRole
|
55
|
+
database = Database::new(DB_FILE)
|
56
|
+
@connection = database.connect(DB_USER_NAME, DB_PASSWORD)
|
57
|
+
@connection.execute_immediate( 'drop role sales' )
|
58
|
+
@connection.close
|
59
|
+
end
|
60
|
+
|
61
|
+
def dropUser
|
62
|
+
sm = ServiceManager.new('localhost')
|
63
|
+
sm.connect(DB_USER_NAME, DB_PASSWORD)
|
64
|
+
|
65
|
+
au = RemoveUser.new('newuser')
|
66
|
+
au.execute(sm)
|
67
|
+
sm.disconnect
|
68
|
+
end
|
69
|
+
|
70
|
+
def teardown
|
71
|
+
dropRole
|
72
|
+
dropUser
|
73
|
+
|
74
|
+
if File::exist?(DB_FILE)
|
75
|
+
db = Database.new(DB_FILE)
|
76
|
+
db.drop(DB_USER_NAME, DB_PASSWORD)
|
77
|
+
end
|
78
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
79
|
+
end
|
80
|
+
|
81
|
+
#test we can connect with the role
|
82
|
+
def test02
|
83
|
+
database = Database::new(DB_FILE)
|
84
|
+
@connection = database.connect('newuser', 'password', Connection::ROLE => 'SALES' )
|
85
|
+
assert_nothing_raised() do
|
86
|
+
|
87
|
+
assert(@connection.execute_immediate( 'insert into sales_test values(1)' ) == 1 )
|
88
|
+
|
89
|
+
end
|
90
|
+
@connection.close
|
91
|
+
end
|
92
|
+
|
93
|
+
def test01
|
94
|
+
database = Database::new(DB_FILE)
|
95
|
+
@connection = database.connect('newuser', 'password' )
|
96
|
+
assert_raise(IBRuby::IBRubyException) do
|
97
|
+
@connection.execute_immediate( 'insert into sales_test values(10)' )
|
98
|
+
|
99
|
+
puts "No role passed but can insert into sales_test!" if TEST_LOGGING
|
100
|
+
end
|
101
|
+
@connection.close
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
@@ -0,0 +1,65 @@
|
|
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 RowCountTest < Test::Unit::TestCase
|
11
|
+
CURDIR = "#{Dir.getwd}"
|
12
|
+
DB_FILE = "#{CURDIR}#{File::SEPARATOR}row_count_test.ib"
|
13
|
+
|
14
|
+
def setup
|
15
|
+
puts "#{self.class.name} started." if TEST_LOGGING
|
16
|
+
# Create the database for use in testing.
|
17
|
+
if File.exist?(DB_FILE)
|
18
|
+
Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
|
19
|
+
end
|
20
|
+
@database = Database.create(DB_FILE, DB_USER_NAME, DB_PASSWORD)
|
21
|
+
|
22
|
+
# Create the test table.
|
23
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
24
|
+
cxn.execute_immediate('create table test(id integer)')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def teardown
|
30
|
+
if File.exist?(DB_FILE)
|
31
|
+
@database.drop(DB_USER_NAME, DB_PASSWORD)
|
32
|
+
end
|
33
|
+
puts "#{self.class.name} finished." if TEST_LOGGING
|
34
|
+
end
|
35
|
+
|
36
|
+
def test01
|
37
|
+
@database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
|
38
|
+
cxn.start_transaction do |tx|
|
39
|
+
assert(cxn.execute_immediate('insert into test values (1000)') == 1)
|
40
|
+
assert(cxn.execute_immediate('insert into test values (1000)') == 1)
|
41
|
+
assert(cxn.execute('insert into test values (2000)', tx) == 1)
|
42
|
+
assert(cxn.execute('insert into test values (2000)', tx) == 1)
|
43
|
+
assert(tx.execute('insert into test values (3000)') == 1)
|
44
|
+
assert(tx.execute('insert into test values (3000)') == 1)
|
45
|
+
assert(tx.execute('insert into test values (4000)') == 1)
|
46
|
+
assert(tx.execute('insert into test values (4000)') == 1)
|
47
|
+
|
48
|
+
assert(cxn.execute_immediate('update test set id = 10000 where '\
|
49
|
+
'id = 1000') == 2)
|
50
|
+
assert(cxn.execute('update test set id = 20000 where id = 2000',
|
51
|
+
tx) == 2)
|
52
|
+
assert(tx.execute('update test set id = 30000 where id = 3000') == 2)
|
53
|
+
|
54
|
+
s = Statement.new(cxn, tx, 'update test set id = 40000 where id = ?',
|
55
|
+
3)
|
56
|
+
assert(s.execute_for([4000]) == 2)
|
57
|
+
|
58
|
+
|
59
|
+
assert(cxn.execute_immediate('delete from test where id = 10000') == 2)
|
60
|
+
assert(cxn.execute('delete from test where id = 20000', tx) == 2)
|
61
|
+
assert(tx.execute('delete from test where id = 30000') == 2)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|