fb 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,462 @@
1
+ require 'test/unit'
2
+ require 'test/FbTestCases'
3
+ require 'fb'
4
+ include Fb
5
+
6
+ class ConnectionTestCases < Test::Unit::TestCase
7
+ include FbTestCases
8
+
9
+ def test_execute
10
+ sql_schema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20))"
11
+ sql_select = "SELECT * FROM RDB$DATABASE"
12
+ Database.create(@parms) do |connection|
13
+ assert !connection.transaction_started
14
+ connection.execute(sql_schema)
15
+ connection.execute(sql_select)
16
+ assert connection.transaction_started
17
+ connection.commit
18
+ assert !connection.transaction_started
19
+ connection.drop
20
+ end
21
+ end
22
+
23
+ def test_query_select
24
+ sql_select = "SELECT * FROM RDB$DATABASE"
25
+ Database.create(@parms) do |connection|
26
+ d = connection.query(sql_select)
27
+ assert_instance_of Array, d
28
+ assert_equal 1, d.size
29
+ assert_instance_of Array, d.first
30
+ assert_equal 4, d.first.size
31
+
32
+ a = connection.query(:array, sql_select)
33
+ assert_instance_of Array, a
34
+ assert_equal 1, a.size
35
+ assert_instance_of Array, a.first
36
+ assert_equal 4, a.first.size
37
+
38
+ h = connection.query(:hash, sql_select)
39
+ assert_instance_of Array, h
40
+ assert_equal 1, h.size
41
+ assert_instance_of Hash, h.first
42
+ assert_equal 4, h.first.keys.size
43
+ assert h.first.keys.include?("RDB$DESCRIPTION")
44
+ assert h.first.keys.include?("RDB$RELATION_ID")
45
+ assert h.first.keys.include?("RDB$SECURITY_CLASS")
46
+ assert h.first.keys.include?("RDB$CHARACTER_SET_NAME")
47
+ end
48
+ end
49
+
50
+ def test_query_update
51
+ sql_schema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20))"
52
+ sql_insert = "INSERT INTO TEST (ID, NAME) VALUES (?, ?)"
53
+ sql_update = "UPDATE TEST SET ID = ?, NAME = ? WHERE ID = ?"
54
+ sql_delete = "DELETE FROM TEST WHERE ID = ?"
55
+ sql_select = "SELECT * FROM TEST"
56
+ Database.create(@parms) do |connection|
57
+ su = connection.query(sql_schema)
58
+ assert_equal -1, su
59
+
60
+ i = connection.query(sql_insert, 1, "NAME")
61
+ assert_equal 1, i
62
+
63
+ u = connection.query(sql_update, 1, "NAME2", 1)
64
+ assert_equal 1, u
65
+
66
+ d = connection.query(sql_delete, 1)
67
+ assert_equal 1, d
68
+
69
+ q = connection.query(sql_select)
70
+ assert_instance_of Array, q
71
+ assert_equal 0, q.size
72
+ end
73
+ end
74
+
75
+ def test_insert_blobs_text
76
+ sql_schema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20), MEMO BLOB SUB_TYPE TEXT)"
77
+ sql_insert = "INSERT INTO TEST (ID, NAME, MEMO) VALUES (?, ?, ?)"
78
+ sql_select = "SELECT * FROM TEST ORDER BY ID"
79
+ Database.create(@parms) do |connection|
80
+ connection.execute(sql_schema);
81
+ memo = IO.read("fb.c")
82
+ assert memo.size > 50000
83
+ connection.transaction do
84
+ 10.times do |i|
85
+ connection.execute(sql_insert, i, i.to_s, memo);
86
+ end
87
+ end
88
+ connection.execute(sql_select) do |cursor|
89
+ i = 0
90
+ cursor.each :hash do |row|
91
+ assert_equal i, row["ID"]
92
+ assert_equal i.to_s, row["NAME"]
93
+ assert_equal memo, row["MEMO"]
94
+ i += 1
95
+ end
96
+ end
97
+ connection.drop
98
+ end
99
+ end
100
+
101
+ def test_insert_blobs_binary
102
+ sql_schema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20), ATTACHMENT BLOB SEGMENT SIZE 1000)"
103
+ sql_insert = "INSERT INTO TEST (ID, NAME, ATTACHMENT) VALUES (?, ?, ?)"
104
+ sql_select = "SELECT * FROM TEST ORDER BY ID"
105
+ filename = "fb.c"
106
+ Database.create(@parms) do |connection|
107
+ connection.execute(sql_schema);
108
+ attachment = File.open(filename,"rb") do |f|
109
+ f.read * 3
110
+ end
111
+ assert (attachment.size > 150000), "Not expected size"
112
+ connection.transaction do
113
+ 3.times do |i|
114
+ connection.execute(sql_insert, i, i.to_s, attachment);
115
+ end
116
+ end
117
+ connection.execute(sql_select) do |cursor|
118
+ i = 0
119
+ cursor.each :array do |row|
120
+ assert_equal i, row[0], "ID's do not match"
121
+ assert_equal i.to_s, row[1], "NAME's do not match"
122
+ assert_equal attachment.size, row[2].size, "ATTACHMENT sizes do not match"
123
+ i += 1
124
+ end
125
+ end
126
+ connection.drop
127
+ end
128
+ end
129
+
130
+ def test_rows_affected
131
+ sql_schema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20))"
132
+ sql_insert = "INSERT INTO TEST (ID, NAME) VALUES (?, ?)"
133
+ sql_update = "UPDATE TEST SET NAME = 'no name' WHERE ID < ?"
134
+ sql_delete = "DELETE FROM TEST WHERE ID > ?"
135
+ sql_select = "SELECT * FROM TEST"
136
+ Database.create(@parms) do |connection|
137
+ connection.execute(sql_schema)
138
+ connection.transaction do
139
+ 10.times do |i|
140
+ affected = connection.execute(sql_insert, i, "name");
141
+ assert_equal 1, affected
142
+ end
143
+ end
144
+ affected = connection.execute(sql_update, 5)
145
+ assert_equal 5, affected
146
+ affected = connection.execute(sql_delete, 5)
147
+ assert 4, affected
148
+ rows = connection.execute(sql_select) do |cursor| cursor.fetchall end
149
+ assert 6, rows.size
150
+ end
151
+ end
152
+
153
+ def test_multi_insert
154
+ sql_schema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20))"
155
+ sql_insert = "INSERT INTO TEST (ID, NAME) VALUES (?, ?)"
156
+ sql_select = "SELECT * FROM TEST"
157
+ sql_data = [
158
+ [1, "Name 1"],
159
+ [2, "Name 2"],
160
+ [3, "Name 3"]]
161
+ sql_data1 = [4, "Name 4"]
162
+ sql_data2 = [5, "Name 5"]
163
+ sql_data3 = [6, "Name 6"]
164
+ Database.create(@parms) do |connection|
165
+ connection.execute(sql_schema)
166
+ connection.execute(sql_insert, sql_data)
167
+ rs = connection.query(sql_select)
168
+ assert_equal 3, rs.size
169
+
170
+ connection.execute(sql_insert, sql_data1, sql_data2, sql_data3)
171
+ rs = connection.query(sql_select)
172
+ assert_equal 6, rs.size
173
+ end
174
+ end
175
+
176
+ def test_dialects
177
+ db = Database.create(@parms) do |connection|
178
+ assert_equal 3, connection.dialect
179
+ assert_equal 3, connection.db_dialect
180
+ connection.drop
181
+ end
182
+ end
183
+
184
+ def test_open?
185
+ db = Database.create(@parms);
186
+ connection = db.connect
187
+ assert connection.open?
188
+ connection.close
189
+ assert !connection.open?
190
+ db.drop
191
+ end
192
+
193
+ def test_properties_instance
194
+ db = Database.new(@parms)
195
+ db.create
196
+ db.connect do |connection|
197
+ assert_equal @parms[:database], connection.database
198
+ assert_equal @parms[:username], connection.username
199
+ assert_equal @parms[:password], connection.password
200
+ assert_equal @parms[:role], connection.role
201
+ assert_equal @parms[:charset], connection.charset
202
+ connection.drop
203
+ end
204
+ end
205
+
206
+ def test_properties_singleton
207
+ Database.create(@parms) do |connection|
208
+ assert_equal @parms[:database], connection.database
209
+ assert_equal @parms[:username], connection.username
210
+ assert_equal @parms[:password], connection.password
211
+ assert_equal @parms[:role], connection.role
212
+ assert_equal @parms[:charset], connection.charset
213
+ connection.drop
214
+ end
215
+ end
216
+
217
+ def test_drop_instance
218
+ db = Database.create(@parms)
219
+ assert File.exists?(@db_file)
220
+ connection = db.connect
221
+ assert connection.open?
222
+ connection.drop
223
+ assert !connection.open?
224
+ assert !File.exists?(@db_file)
225
+ end
226
+
227
+ def test_drop_singleton
228
+ Database.create(@parms) do |connection|
229
+ assert File.exists?(@db_file)
230
+ connection.drop
231
+ assert !File.exists?(@db_file)
232
+ end
233
+ end
234
+
235
+ def test_to_s
236
+ db = Database.new(@parms)
237
+ db.create
238
+ connection = db.connect
239
+ begin
240
+ assert_equal "#{@parms[:database]} (OPEN)", connection.to_s
241
+ ensure
242
+ connection.close
243
+ assert_equal "#{@parms[:database]} (CLOSED)", connection.to_s
244
+ end
245
+ end
246
+
247
+ def test_table_names
248
+ sql_schema = <<-END
249
+ CREATE TABLE TEST1 (ID INT);
250
+ CREATE TABLE TEST2 (ID INT);
251
+ END
252
+ Database.create(@parms) do |connection|
253
+ connection.execute_script(sql_schema)
254
+ table_names = connection.table_names
255
+ assert_equal 'TEST1', table_names[0]
256
+ assert_equal 'TEST2', table_names[1]
257
+ end
258
+ end
259
+
260
+ def test_table_names_downcased
261
+ sql_schema = <<-END
262
+ CREATE TABLE TEST1 (ID INT);
263
+ CREATE TABLE "Test2" (ID INT);
264
+ END
265
+ Database.create(@parms.merge(:downcase_names => true)) do |connection|
266
+ connection.execute_script(sql_schema)
267
+ table_names = connection.table_names
268
+ assert_equal 'test1', table_names[0]
269
+ assert_equal 'Test2', table_names[1]
270
+ end
271
+ end
272
+
273
+ def test_generator_names
274
+ sql_schema = <<-END
275
+ CREATE GENERATOR TEST1_SEQ;
276
+ CREATE GENERATOR TEST2_SEQ;
277
+ END
278
+ Database.create(@parms) do |connection|
279
+ connection.execute_script(sql_schema)
280
+ names = connection.generator_names
281
+ assert_equal 'TEST1_SEQ', names[0]
282
+ assert_equal 'TEST2_SEQ', names[1]
283
+ end
284
+ end
285
+
286
+ def test_generator_names_downcased
287
+ sql_schema = <<-END
288
+ CREATE GENERATOR TEST1_SEQ;
289
+ CREATE GENERATOR "TEST2_seq";
290
+ END
291
+ Database.create(@parms.merge(:downcase_names => true)) do |connection|
292
+ connection.execute_script(sql_schema)
293
+ names = connection.generator_names
294
+ assert_equal 'test1_seq', names[0]
295
+ assert_equal 'TEST2_seq', names[1]
296
+ end
297
+ end
298
+
299
+ def test_view_names
300
+ sql_schema = <<-END
301
+ CREATE TABLE TEST1 (ID INT, NAME1 VARCHAR(10));
302
+ CREATE TABLE TEST2 (ID INT, NAME2 VARCHAR(10));
303
+ CREATE VIEW VIEW1 AS SELECT TEST1.ID, TEST1.NAME1, TEST2.NAME2 FROM TEST1 JOIN TEST2 ON TEST1.ID = TEST2.ID;
304
+ CREATE VIEW VIEW2 AS SELECT TEST2.ID, TEST1.NAME1, TEST2.NAME2 FROM TEST1 JOIN TEST2 ON TEST1.NAME1 = TEST2.NAME2;
305
+ END
306
+ Database.create(@parms) do |connection|
307
+ connection.execute_script(sql_schema)
308
+ names = connection.view_names
309
+ assert_equal 'VIEW1', names[0]
310
+ assert_equal 'VIEW2', names[1]
311
+ end
312
+ end
313
+
314
+ def test_view_names_downcased
315
+ sql_schema = <<-END
316
+ CREATE TABLE TEST1 (ID INT, NAME1 VARCHAR(10));
317
+ CREATE TABLE TEST2 (ID INT, NAME2 VARCHAR(10));
318
+ CREATE VIEW VIEW1 AS SELECT TEST1.ID, TEST1.NAME1, TEST2.NAME2 FROM TEST1 JOIN TEST2 ON TEST1.ID = TEST2.ID;
319
+ CREATE VIEW "View2" AS SELECT TEST2.ID, TEST1.NAME1, TEST2.NAME2 FROM TEST1 JOIN TEST2 ON TEST1.NAME1 = TEST2.NAME2;
320
+ END
321
+ Database.create(@parms.merge(:downcase_names => true)) do |connection|
322
+ connection.execute_script(sql_schema)
323
+ names = connection.view_names
324
+ assert_equal 'view1', names[0]
325
+ assert_equal 'View2', names[1]
326
+ end
327
+ end
328
+
329
+ def test_role_names
330
+ sql_schema = <<-END
331
+ create role reader;
332
+ create role writer;
333
+ END
334
+ Database.create(@parms) do |connection|
335
+ connection.execute_script(sql_schema)
336
+ names = connection.role_names
337
+ assert_equal 'READER', names[0]
338
+ assert_equal 'WRITER', names[1]
339
+ end
340
+ end
341
+
342
+ def test_role_names_downcased
343
+ sql_schema = <<-END
344
+ create role reader;
345
+ create role writer;
346
+ END
347
+ Database.create(@parms.merge(:downcase_names => true)) do |connection|
348
+ connection.execute_script(sql_schema)
349
+ names = connection.role_names
350
+ assert_equal 'reader', names[0]
351
+ assert_equal 'writer', names[1]
352
+ end
353
+ end
354
+
355
+ def test_procedure_names
356
+ sql_schema = <<-END_SQL
357
+ CREATE PROCEDURE PLUSONE(NUM1 INTEGER) RETURNS (NUM2 INTEGER) AS
358
+ BEGIN
359
+ NUM2 = NUM1 + 1;
360
+ SUSPEND;
361
+ END;
362
+ END_SQL
363
+ Database.create(@parms) do |connection|
364
+ connection.execute(sql_schema)
365
+ names = connection.procedure_names
366
+ assert_equal 'PLUSONE', names[0]
367
+ end
368
+ end
369
+
370
+ def test_procedure_names_downcased
371
+ sql_schema = <<-END_SQL
372
+ CREATE PROCEDURE PLUSONE(NUM1 INTEGER) RETURNS (NUM2 INTEGER) AS
373
+ BEGIN
374
+ NUM2 = NUM1 + 1;
375
+ SUSPEND;
376
+ END;
377
+ END_SQL
378
+ Database.create(@parms.merge(:downcase_names => true)) do |connection|
379
+ connection.execute(sql_schema)
380
+ names = connection.procedure_names
381
+ assert_equal 'plusone', names[0]
382
+ end
383
+ end
384
+
385
+ def test_index_names
386
+ sql_schema = <<-END
387
+ CREATE TABLE MASTER (ID INT NOT NULL, NAME1 VARCHAR(10));
388
+ CREATE TABLE DETAIL (ID INT NOT NULL, MASTER_ID INT, NAME2 VARCHAR(10));
389
+ ALTER TABLE MASTER ADD CONSTRAINT PK_MASTER PRIMARY KEY(ID);
390
+ ALTER TABLE DETAIL ADD CONSTRAINT PK_DETAIL PRIMARY KEY(ID);
391
+ ALTER TABLE DETAIL ADD CONSTRAINT FK_DETAIL_MASTER_ID FOREIGN KEY(MASTER_ID) REFERENCES MASTER(ID);
392
+ CREATE UNIQUE ASCENDING INDEX IX_MASTER_NAME1 ON MASTER(NAME1);
393
+ CREATE DESCENDING INDEX IX_DETAIL_ID_DESC ON DETAIL(ID);
394
+ END
395
+ Database.create(@parms) do |connection|
396
+ connection.execute_script(sql_schema)
397
+ indexes = connection.indexes # Hash of Structs using index names as keys
398
+ assert_equal 5, indexes.size
399
+ assert indexes.keys.include?('PK_MASTER')
400
+ assert indexes.keys.include?('PK_DETAIL')
401
+ assert indexes.keys.include?('FK_DETAIL_MASTER_ID')
402
+ assert indexes.keys.include?('IX_MASTER_NAME1')
403
+ assert indexes.keys.include?('IX_DETAIL_ID_DESC')
404
+
405
+ assert indexes['PK_MASTER'].columns.include?('ID')
406
+ assert indexes['PK_DETAIL'].columns.include?('ID')
407
+
408
+ master_indexes = indexes.values.select {|ix| ix.table_name == 'MASTER' }
409
+ assert_equal 2, master_indexes.size
410
+
411
+ detail_indexes = indexes.values.select {|ix| ix.table_name == 'DETAIL' }
412
+ assert_equal 3, detail_indexes.size
413
+
414
+ assert_equal 'MASTER', indexes['PK_MASTER'].table_name
415
+ assert indexes['PK_MASTER'].unique
416
+ assert !indexes['PK_MASTER'].descending
417
+
418
+ assert_equal 'MASTER', indexes['IX_MASTER_NAME1'].table_name
419
+ assert indexes['IX_MASTER_NAME1'].unique
420
+ assert !indexes['IX_MASTER_NAME1'].descending
421
+
422
+ assert_equal 'DETAIL', indexes['PK_DETAIL'].table_name
423
+ assert indexes['PK_DETAIL'].unique
424
+ assert !indexes['PK_DETAIL'].descending
425
+
426
+ assert_equal 'DETAIL', indexes['FK_DETAIL_MASTER_ID'].table_name
427
+ assert !indexes['FK_DETAIL_MASTER_ID'].unique
428
+ assert !indexes['FK_DETAIL_MASTER_ID'].descending
429
+
430
+ assert_equal 'DETAIL', indexes['IX_DETAIL_ID_DESC'].table_name
431
+ assert !indexes['IX_DETAIL_ID_DESC'].unique
432
+ assert indexes['IX_DETAIL_ID_DESC'].descending
433
+
434
+ connection.drop
435
+ end
436
+ end
437
+
438
+ def test_index_names_downcased
439
+ sql_schema = <<-END
440
+ CREATE TABLE MASTER (ID INT NOT NULL, NAME1 VARCHAR(10));
441
+ CREATE TABLE DETAIL (ID INT NOT NULL, MASTER_ID INT, NAME2 VARCHAR(10));
442
+ ALTER TABLE MASTER ADD CONSTRAINT PK_MASTER PRIMARY KEY(ID);
443
+ ALTER TABLE DETAIL ADD CONSTRAINT PK_DETAIL PRIMARY KEY(ID);
444
+ ALTER TABLE DETAIL ADD CONSTRAINT FK_DETAIL_MASTER_ID FOREIGN KEY(MASTER_ID) REFERENCES MASTER(ID);
445
+ CREATE UNIQUE ASCENDING INDEX IX_MASTER_NAME1 ON MASTER(NAME1);
446
+ CREATE DESCENDING INDEX "IX_DETAIL_ID_desc" ON DETAIL(ID);
447
+ END
448
+ Database.create(@parms.merge(:downcase_names => true)) do |connection|
449
+ connection.execute_script(sql_schema)
450
+ indexes = connection.indexes # Hash of Structs using index names as keys
451
+ assert_equal 5, indexes.size
452
+ assert indexes.keys.include?('pk_master')
453
+ assert indexes.keys.include?('pk_detail')
454
+ assert indexes.keys.include?('fk_detail_master_id')
455
+ assert indexes.keys.include?('ix_master_name1')
456
+ assert indexes.keys.include?('IX_DETAIL_ID_desc')
457
+ assert indexes['pk_master'].columns.include?('id'), "columns missing id"
458
+ assert indexes['pk_detail'].columns.include?('id'), "columns missing id"
459
+ connection.drop
460
+ end
461
+ end
462
+ end