fireruby 0.2.2-i586-linux

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class ConnectionTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}con_unit_test.fdb"
13
+
14
+ def setup
15
+ if File::exist?(DB_FILE)
16
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
17
+ end
18
+
19
+ @database = Database.create(DB_FILE, DB_USER_NAME, DB_PASSWORD, 1024, nil)
20
+ @connections = []
21
+ end
22
+
23
+ def teardown
24
+ @connections.each {|connection| connection.close}
25
+ @connections.clear
26
+
27
+ if File::exist?(DB_FILE)
28
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
29
+ end
30
+ end
31
+
32
+ def test01
33
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
34
+
35
+ assert(@connections[0].user == DB_USER_NAME)
36
+ assert(@connections[0].open?)
37
+ assert(@connections[0].closed? == false)
38
+ assert(@connections[0].database == @database)
39
+ assert(@connections[0].to_s == "#{DB_USER_NAME}@#{DB_FILE} (OPEN)")
40
+
41
+ @connections[0].close
42
+ assert(@connections[0].user == DB_USER_NAME)
43
+ assert(@connections[0].open? == false)
44
+ assert(@connections[0].closed?)
45
+ assert(@connections[0].database == @database)
46
+ assert(@connections[0].to_s == "(CLOSED)")
47
+
48
+ @connections[0] = @database.connect(DB_USER_NAME, DB_PASSWORD)
49
+ assert(@connections[0].open?)
50
+ @connections.push(Connection.new(@database, DB_USER_NAME, DB_PASSWORD))
51
+ assert(@connections[0].open?)
52
+ assert(@connections[1].open?)
53
+ end
54
+
55
+ def test02
56
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
57
+
58
+ tx = @connections[0].start_transaction
59
+ assert(tx != nil)
60
+ assert(tx.class == Transaction)
61
+ assert(tx.active?)
62
+ tx.rollback
63
+
64
+ tx = nil
65
+ @connections[0].start_transaction do |transaction|
66
+ assert(transaction != nil)
67
+ assert(transaction.class == Transaction)
68
+ assert(transaction.active?)
69
+ tx = transaction
70
+ end
71
+ assert(tx != nil)
72
+ assert(tx.class == Transaction)
73
+ assert(tx.active? == false)
74
+ end
75
+
76
+ def test03
77
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
78
+ tx = @connections[0].start_transaction
79
+ total = 0
80
+ @connections[0].execute("SELECT RDB$FIELD_NAME FROM RDB$FIELDS", tx) do |row|
81
+ total = total + 1
82
+ end
83
+ assert(total == 88)
84
+ tx.commit
85
+
86
+ total = 0
87
+ @connections[0].execute_immediate("SELECT RDB$FIELD_NAME FROM RDB$FIELDS") do |row|
88
+ total = total + 1
89
+ end
90
+ assert(total == 88)
91
+ end
92
+ end
data/test/DDLTest.rb ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class DDLTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}ddl_unit_test.fdb"
13
+
14
+ def setup
15
+ if File::exist?(DB_FILE)
16
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
17
+ end
18
+ @database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD, 1024, nil)
19
+ end
20
+
21
+ def teardown
22
+ if File::exist?(DB_FILE)
23
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
24
+ end
25
+ end
26
+
27
+ def test01
28
+ @database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
29
+ cxn.execute_immediate('CREATE TABLE DDL_TABLE_01 (TABLEID '\
30
+ 'INTEGER NOT NULL, '\
31
+ 'FIELD01 FLOAT, FIELD02 CHAR(50), '\
32
+ 'FIELD03 BIGINT, FIELD04 TIMESTAMP '\
33
+ 'NOT NULL, FIELD05 VARCHAR(600))')
34
+
35
+ r = cxn.execute_immediate('SELECT COUNT(*) FROM DDL_TABLE_01')
36
+ assert(r.fetch[0] == 0)
37
+ r.close
38
+
39
+ cxn.execute_immediate('ALTER TABLE DDL_TABLE_01 ADD PRIMARY KEY '\
40
+ '(TABLEID)')
41
+
42
+ cxn.execute_immediate('CREATE UNIQUE INDEX DDL_TABLE_IDX ON '\
43
+ 'DDL_TABLE_01 (TABLEID)')
44
+
45
+ cxn.execute_immediate('DROP INDEX DDL_TABLE_IDX')
46
+
47
+ cxn.execute_immediate('DROP TABLE DDL_TABLE_01')
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class DatabaseTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}db_unit_test.fdb"
13
+ CREATE_FILE = "#{CURDIR}#{File::SEPARATOR}db_create_test.fdb"
14
+
15
+ def setup
16
+ if File.exist?(DB_FILE)
17
+ db = Database.new(DB_FILE)
18
+ db.drop
19
+ end
20
+ if File.exist?(CREATE_FILE)
21
+ db = Database.new(CREATE_FILE)
22
+ db.drop
23
+ end
24
+
25
+ Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD, 1024, 'ASCII')
26
+ end
27
+
28
+ def teardown
29
+ if File::exist?(DB_FILE)
30
+ db = Database.new(DB_FILE)
31
+ db.drop(DB_USER_NAME, DB_PASSWORD)
32
+ end
33
+ if File::exist?(CREATE_FILE)
34
+ db = Database.new(CREATE_FILE)
35
+ db.drop(DB_USER_NAME, DB_PASSWORD)
36
+ end
37
+ end
38
+
39
+
40
+ def test01
41
+ db = Database.create(CREATE_FILE, DB_USER_NAME, DB_PASSWORD, 2048, 'ASCII')
42
+
43
+ assert(File.exist?(CREATE_FILE))
44
+ assert(db.file == CREATE_FILE)
45
+
46
+ begin
47
+ Database.create(CREATE_FILE, DB_USER_NAME, DB_PASSWORD, 2048, 'ASCII')
48
+ assert(false,
49
+ "Successfully created a database file that already exists.")
50
+ rescue Exception
51
+ end
52
+
53
+ db.drop(DB_USER_NAME, DB_PASSWORD)
54
+ assert(File.exist?(CREATE_FILE) == false)
55
+ end
56
+
57
+ def test02
58
+ db = Database.new(DB_FILE);
59
+
60
+ assert(db.file == DB_FILE)
61
+
62
+ c = db.connect(DB_USER_NAME, DB_PASSWORD)
63
+ assert(c != nil)
64
+ c.close
65
+ end
66
+
67
+ def test03
68
+ db = Database.new(DB_FILE);
69
+ c = nil
70
+
71
+ db.connect(DB_USER_NAME, DB_PASSWORD) do |connection|
72
+ assert(connection != nil)
73
+ assert(connection.class == Connection)
74
+ assert(connection.open?)
75
+ c = connection
76
+ end
77
+ assert(c != nil)
78
+ assert(c.class == Connection)
79
+ assert(c.closed?)
80
+ end
81
+ end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class GeneratorTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}generator_unit_test.fdb"
13
+
14
+ def setup
15
+ if File::exist?(DB_FILE)
16
+ Database.new(DB_USER_NAME, DB_PASSWORD, DB_FILE).drop
17
+ end
18
+ @database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD, 1024, nil)
19
+ @connections = []
20
+
21
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
22
+ end
23
+
24
+ def teardown
25
+ @connections.each do |cxn|
26
+ cxn.close if cxn.open?
27
+ end
28
+ @connections.clear
29
+ if File::exist?(DB_FILE)
30
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
31
+ end
32
+ end
33
+
34
+ def test01
35
+ assert(Generator::exists?('TEST_GEN', @connections[0]) == false)
36
+ g = Generator::create('TEST_GEN', @connections[0])
37
+ assert(Generator::exists?('TEST_GEN', @connections[0]))
38
+ assert(g.last == 0)
39
+ assert(g.next(1) == 1)
40
+ assert(g.last == 1)
41
+ assert(g.next(10) == 11)
42
+ assert(g.connection == @connections[0])
43
+ assert(g.name == 'TEST_GEN')
44
+
45
+ g.drop
46
+ assert(Generator::exists?('TEST_GEN', @connections[0]) == false)
47
+ end
48
+ end
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class ResultSetTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}result_set_unit_test.fdb"
13
+
14
+ def setup
15
+ if File::exist?(DB_FILE)
16
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
17
+ end
18
+ @database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD, 1024, nil)
19
+ @connections = []
20
+ @transactions = []
21
+
22
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
23
+
24
+ @connections[0].start_transaction do |tx|
25
+ tx.execute("CREATE TABLE TEST_TABLE (TESTID INTEGER NOT NULL "\
26
+ "primary KEY, TESTINFO VARCHAR(100))")
27
+ end
28
+
29
+ @connections[0].start_transaction do |tx|
30
+ begin
31
+ tx.execute("INSERT INTO TEST_TABLE VALUES (10, 'Record One.')")
32
+ tx.execute("INSERT INTO TEST_TABLE VALUES (20, 'Record Two.')")
33
+ tx.execute("INSERT INTO TEST_TABLE VALUES (30, 'Record Three.')")
34
+ tx.execute("INSERT INTO TEST_TABLE VALUES (40, 'Record Four.')")
35
+ tx.execute("INSERT INTO TEST_TABLE VALUES (50, 'Record Five.')")
36
+ rescue Exception => error
37
+ puts error.message
38
+ error.backtrace.each {|step| puts " #{step}"}
39
+ raise
40
+ end
41
+ end
42
+
43
+ @transactions.push(@connections[0].start_transaction)
44
+ end
45
+
46
+ def teardown
47
+ @transactions.each do |tx|
48
+ tx.rollback if tx.active?
49
+ end
50
+ @transactions.clear
51
+ @connections.each do |cxn|
52
+ cxn.close if cxn.open?
53
+ end
54
+ @connections.clear
55
+ if File::exist?(DB_FILE)
56
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
57
+ end
58
+ end
59
+
60
+ def test01
61
+ s = Statement.new(@connections[0], @transactions[0],
62
+ "SELECT * FROM TEST_TABLE ORDER BY TESTID", 3)
63
+
64
+ r = ResultSet.new(s)
65
+
66
+ assert(r.statement == s)
67
+
68
+ assert(r.fetch != nil)
69
+ assert(r.fetch.class == Row)
70
+ assert(r.fetch[0] == 30)
71
+ assert(r.fetch[1] == 'Record Four.')
72
+ r.fetch
73
+ assert(r.fetch == nil)
74
+
75
+ r = ResultSet.new(s)
76
+ assert(r.column_count == 2)
77
+ assert(r.column_name(0) == 'TESTID')
78
+ assert(r.column_name(1) == 'TESTINFO')
79
+ assert(r.column_name(3) == nil)
80
+ assert(r.column_name(-1) == nil)
81
+ assert(r.column_table(0) == 'TEST_TABLE')
82
+ assert(r.column_table(1) == 'TEST_TABLE')
83
+ assert(r.column_table(2) == nil)
84
+ assert(r.column_table(-1) == nil)
85
+ assert(r.column_alias(0) == 'TESTID')
86
+ assert(r.column_alias(1) == 'TESTINFO')
87
+ assert(r.column_alias(3) == nil)
88
+ assert(r.column_alias(-1) == nil)
89
+
90
+ r = ResultSet.new(s)
91
+ total = 0
92
+ r.each do |row|
93
+ total += 1
94
+ end
95
+ assert(total == 5)
96
+ end
97
+ end
data/test/RowTest.rb ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class RowTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}row_unit_test.fdb"
13
+
14
+ def setup
15
+ if File::exist?(DB_FILE)
16
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
17
+ end
18
+
19
+ database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD, 1024, nil)
20
+ @connection = database.connect(DB_USER_NAME, DB_PASSWORD)
21
+ @transaction = @connection.start_transaction
22
+ @statement = Statement.new(@connection, @transaction,
23
+ 'SELECT * FROM RDB$FIELDS', 3)
24
+ @results = ResultSet.new(@statement)
25
+ @empty = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26
+ 0, 0, 0, 0, 0, 0, 0, 0, 0]
27
+ end
28
+
29
+ def teardown
30
+ @results.close
31
+ @statement.close
32
+ @transaction.rollback
33
+ @connection.close
34
+ if File::exist?(DB_FILE)
35
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
36
+ end
37
+ end
38
+
39
+ def test01
40
+ row = Row.new(@results, @empty, 100)
41
+
42
+ assert(row.column_count == 28)
43
+ assert(row.number == 100)
44
+ assert(row.column_name(0) == 'RDB$FIELD_NAME')
45
+ assert(row.column_alias(10) == 'RDB$FIELD_TYPE')
46
+ assert(row[0] == 0)
47
+ assert(row['RDB$FIELD_TYPE'] == 0)
48
+ end
49
+ end
data/test/SQLTest.rb ADDED
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'TestSetup'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require_gem 'fireruby'
7
+
8
+ include FireRuby
9
+
10
+ class SQLTest < Test::Unit::TestCase
11
+ CURDIR = "#{Dir.getwd}"
12
+ DB_FILE = "#{CURDIR}#{File::SEPARATOR}sql_unit_test.fdb"
13
+ ITERATIONS = 100
14
+
15
+ INSERT_SQL = "INSERT INTO TEST_TABLE VALUES(?, ?, ?, ?, ?, ?)"
16
+
17
+ def setup
18
+ if File::exist?(DB_FILE)
19
+ Database.new(DB_FILE).drop(DB_USER_NAME, DB_PASSWORD)
20
+ end
21
+ @database = Database::create(DB_FILE, DB_USER_NAME, DB_PASSWORD,
22
+ 1024, 'ASCII')
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
+ end
47
+
48
+ def test01
49
+ @database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
50
+ cxn.start_transaction do |tx|
51
+ s = Statement.new(cxn, tx, INSERT_SQL, 3)
52
+ f = 0.0
53
+ t = Time.new
54
+
55
+ 1.upto(ITERATIONS) do |i|
56
+ f += 0.321
57
+ t = Time.at(t.to_i + 5)
58
+ s.execute_for([i, i.to_s, f, t, nil, t])
59
+ end
60
+ end
61
+ end
62
+
63
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
64
+ r = @connections[0].execute_immediate("SELECT COUNT(*) FROM TEST_TABLE")
65
+ assert(r.fetch[0] == ITERATIONS)
66
+ r.close
67
+ end
68
+
69
+ def test02
70
+ f = 0.0
71
+ @database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
72
+ 1.upto(20) do |i|
73
+ f += i
74
+ sql = "INSERT INTO TEST_TABLE VALUES (#{i}, "\
75
+ "#{f.to_s}, #{f}, NULL, NULL, 'NOW')"
76
+ cxn.execute_immediate(sql)
77
+ end
78
+ end
79
+
80
+ @connections.push(@database.connect(DB_USER_NAME, DB_PASSWORD))
81
+ @transactions.push(@connections[0].start_transaction)
82
+ r = @transactions[0].execute("SELECT COUNT(*) FROM TEST_TABLE")
83
+ assert(r.fetch[0] == 20)
84
+ r.close
85
+
86
+ r = @transactions[0].execute("SELECT * FROM TEST_TABLE WHERE TESTID IN "\
87
+ "(2, 4, 6, 8, 10) ORDER BY TESTID ASCENDING")
88
+ a = r.fetch
89
+ assert(a[0] == 2)
90
+ assert(a[1] == '3.0')
91
+ assert(a[2] == 3.0)
92
+ assert(a[3] == nil)
93
+ assert(a[4] == nil)
94
+
95
+ a = r.fetch
96
+ assert(a[0] == 4)
97
+ assert(a[1] == '10.0')
98
+ assert(a[2] == 10.0)
99
+ assert(a[3] == nil)
100
+ assert(a[4] == nil)
101
+
102
+ a = r.fetch
103
+ assert(a[0] == 6)
104
+ assert(a[1] == '21.0')
105
+ assert(a[2] == 21.0)
106
+ assert(a[3] == nil)
107
+ assert(a[4] == nil)
108
+
109
+ a = r.fetch
110
+ assert(a[0] == 8)
111
+ assert(a[1] == '36.0')
112
+ assert(a[2] == 36.0)
113
+ assert(a[3] == nil)
114
+ assert(a[4] == nil)
115
+
116
+ a = r.fetch
117
+ assert(a[0] == 10)
118
+ assert(a[1] == '55.0')
119
+ assert(a[2] == 55.0)
120
+ assert(a[3] == nil)
121
+ assert(a[4] == nil)
122
+
123
+ r.close
124
+
125
+ @transactions[0].commit
126
+
127
+ @connections[0].start_transaction do |tx|
128
+ s = Statement.new(@connections[0], tx,
129
+ "UPDATE TEST_TABLE SET TESTSTAMP = NULL", 3)
130
+ s.execute()
131
+
132
+ r = tx.execute("SELECT TESTSTAMP FROM TEST_TABLE")
133
+ total = 0
134
+ r.each do |row|
135
+ assert(row[0] == nil)
136
+ total = total + 1
137
+ end
138
+ assert(total == 20)
139
+ end
140
+
141
+ a = []
142
+ t = nil
143
+ @connections[0].start_transaction do |tx|
144
+ # Perform an insert via a parameterized statement.
145
+ s = Statement.new(@connections[0], tx,
146
+ "INSERT INTO TEST_TABLE (TESTID, TESTTEXT, "\
147
+ "TESTFLOAT, TESTSTAMP) VALUES(?, ?, ?, ?)", 3)
148
+ t = Time.new
149
+ s.execute_for([25000, 'La la la', 3.14, t])
150
+
151
+ # Fetch the record and check the data.
152
+ r = tx.execute("SELECT TESTTEXT, TESTFLOAT, TESTSTAMP FROM "\
153
+ "TEST_TABLE WHERE TESTID = 25000")
154
+ a = r.fetch
155
+ r.close
156
+ end
157
+ assert(a[0] == 'La la la')
158
+ assert(a[1] == 3.14)
159
+ assert(a[2].to_i == t.to_i)
160
+
161
+ @connections[0].execute_immediate("DELETE FROM TEST_TABLE WHERE TESTID "\
162
+ "IN (1, 3, 5, 7, 9, 12, 14, 16, 18, 20)")
163
+ @database.connect(DB_USER_NAME, DB_PASSWORD) do |cxn|
164
+ r = cxn.execute_immediate("SELECT COUNT(*) FROM TEST_TABLE")
165
+ a = r.fetch
166
+ r.close
167
+ assert(a[0] == 11)
168
+ end
169
+ end
170
+ end
171
+