KirbyBase 2.5 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ require "tmpdir"
2
+
3
+ module BaseTest
4
+ def setup
5
+ @db = KirbyBase.new(:local, nil, nil, Dir.tmpdir, '.table')
6
+ @encrypted = false
7
+ end
8
+
9
+ def teardown
10
+ @db.tables.each { |table| @db.drop_table table }
11
+ File.unlink('test.txt') if File.exists?('test.txt')
12
+ end
13
+
14
+ def table_fname(table_name)
15
+ File.join @db.path, table_name + @db.ext
16
+ end
17
+ end
@@ -0,0 +1,80 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'test/unit'
4
+ require 'kirbybase'
5
+ require 'base_test'
6
+
7
+ class TestDatabaseLocal < Test::Unit::TestCase
8
+
9
+ include BaseTest
10
+
11
+ def test_version
12
+ assert_equal('2.5.1', KirbyBase::VERSION)
13
+ end
14
+
15
+ def test_local?
16
+ assert(@db.local?)
17
+ end
18
+
19
+ def test_extension
20
+ assert_equal('.table', @db.ext)
21
+ end
22
+
23
+ def test_create_table_001
24
+ tbl = @db.create_table(:empty) { |t| t.encrypt = @encrypted }
25
+ assert_instance_of(KBTable, tbl)
26
+ end
27
+
28
+ def test_create_table_002
29
+ @db.create_table(:empty) { |t| t.encrypt = @encrypted }
30
+
31
+ if @encrypted
32
+ data = "Zp8&1dg|;4p8&1t_tJyMBmsCDnG_;vKfEFrn\n"
33
+ else
34
+ data = "000000|000000|Struct|recno:Integer|\n"
35
+ end
36
+
37
+ assert_equal(data, File.read(table_fname('empty')))
38
+ end
39
+
40
+ def test_create_table_003
41
+ assert_raise(RuntimeError) { @db.create_table(:empty, :name,
42
+ :String, :name, :Integer) { |t| t.encrypt = @encrypted } }
43
+ end
44
+
45
+ def test_drop_table
46
+ @db.create_table(:empty) { |t| t.encrypt = @encrypted }
47
+ @db.drop_table(:empty)
48
+ assert(!@db.table_exists?(:empty))
49
+ end
50
+
51
+ def test_tables
52
+ @db.create_table(:empty1) { |t| t.encrypt = @encrypted }
53
+ @db.create_table(:empty2) { |t| t.encrypt = @encrypted }
54
+ tables = @db.tables.collect { |sym| sym.to_s }
55
+ assert_equal(%w(empty1 empty2), tables.sort)
56
+ end
57
+
58
+ def test_table_exists?
59
+ @db.create_table(:empty) { |t| t.encrypt = @encrypted }
60
+ assert(@db.table_exists?(:empty))
61
+ end
62
+
63
+ def test_get_table
64
+ @db.create_table(:empty) { |t| t.encrypt = @encrypted }
65
+ table = @db.get_table(:empty)
66
+ assert_instance_of(KBTable, table)
67
+ end
68
+
69
+ def test_rename_table_001
70
+ @db.create_table(:empty) { |t| t.encrypt = @encrypted }
71
+ @db.rename_table(:empty, :new_empty)
72
+ assert(@db.table_exists?(:new_empty))
73
+ end
74
+
75
+ def test_rename_table_002
76
+ @db.create_table(:empty) { |t| t.encrypt = @encrypted }
77
+ @db.create_table(:new_empty) { |t| t.encrypt = @encrypted }
78
+ assert_raise(RuntimeError) {@db.rename_table(:empty, :new_empty)}
79
+ end
80
+ end
@@ -0,0 +1,716 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require "test/unit"
4
+ require "kirbybase"
5
+ require "base_test"
6
+
7
+ class TestTableLocal < Test::Unit::TestCase
8
+
9
+ include BaseTest
10
+
11
+ def test_table_name
12
+ tbl = @db.create_table(:empty) { |t| t.encrypt = @encrypted }
13
+ assert_equal(:empty, tbl.name)
14
+ end
15
+
16
+ def test_table_filename
17
+ tbl = @db.create_table(:empty) { |t| t.encrypt = @encrypted }
18
+ assert_equal(table_fname('empty'), tbl.filename)
19
+ end
20
+
21
+ def test_table_field_names
22
+ tbl = @db.create_table(:empty, :one, :String, :two, :Integer) { |t|
23
+ t.encrypt = @encrypted }
24
+ assert_equal([:recno, :one, :two], tbl.field_names)
25
+ end
26
+
27
+ def test_table_field_types
28
+ tbl = @db.create_table(:empty, :one, :String, :two, :Integer) { |t|
29
+ t.encrypt = @encrypted }
30
+ assert_equal([:Integer, :String, :Integer], tbl.field_types)
31
+ end
32
+
33
+ def test_table_field_indexes
34
+ tbl = @db.create_table(:empty, :one, {:DataType=>:String,
35
+ :Index=>1}, :two, {:DataType=>:Integer, :Index=>2}, :three,
36
+ {:DataType=>:Integer, :Index=>1}, :four, :String) { |t|
37
+ t.encrypt = @encrypted }
38
+ assert_equal([nil, 'Index->1', 'Index->2', 'Index->1', nil],
39
+ tbl.field_indexes)
40
+ end
41
+
42
+ def test_table_field_defaults_001
43
+ tbl = @db.create_table(:empty, :one, {:DataType=>:String,
44
+ :Default=>'one'}, :two, :Integer, :three, {:DataType=>:Integer,
45
+ :Default=>3}) { |t| t.encrypt = @encrypted }
46
+ assert_equal([nil, 'one', nil, 3], tbl.field_defaults)
47
+ end
48
+
49
+ def test_table_field_defaults_002
50
+ assert_raise(RuntimeError) { @db.create_table(:empty, :one,
51
+ {:DataType=>:String, :Default=>'one'}, :two, :Integer, :three,
52
+ {:DataType=>:Integer, :Default=>'3'}) { |t|
53
+ t.encrypt = @encrypted } }
54
+ end
55
+
56
+ def test_table_field_requireds_001
57
+ tbl = @db.create_table(:empty, :one, {:DataType=>:String,
58
+ :Required=>true}) { |t| t.encrypt = @encrypted }
59
+ assert_equal([false, true], tbl.field_requireds)
60
+ end
61
+
62
+ def test_table_field_requireds_002
63
+ assert_raise(RuntimeError) { @db.create_table(:empty, :one,
64
+ {:DataType=>:String, :Required=>'true'}) { |t|
65
+ t.encrypt = @encrypted } }
66
+ end
67
+
68
+ def test_pack_table
69
+ num_records = 50
70
+ tbl = @db.create_table(:count, :number, :Integer) { |t|
71
+ t.encrypt = @encrypted }
72
+
73
+ num_records.times { |i| tbl.insert(i) }
74
+ (1..num_records).step(2) do |i|
75
+ tbl.delete { |r| r.number == i }
76
+ end
77
+
78
+ assert_equal(num_records / 2, tbl.pack)
79
+ end
80
+
81
+ def test_insert_001
82
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
83
+ ) { |t| t.encrypt = @encrypted }
84
+
85
+ assert_equal(1, tbl.insert('Spitfire', 500))
86
+ end
87
+
88
+ def test_insert_002
89
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
90
+ ) { |t| t.encrypt = @encrypted }
91
+ tbl.insert('Spitfire', 500)
92
+
93
+ assert_equal([1, 'Spitfire', 500], tbl[1].to_a)
94
+ end
95
+
96
+ def test_insert_003
97
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
98
+ ) { |t| t.encrypt = @encrypted }
99
+ tbl.insert(:name => 'Spitfire', :speed => 500)
100
+
101
+ assert_equal([1, 'Spitfire', 500], tbl[1].to_a)
102
+ end
103
+
104
+ def test_insert_004
105
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
106
+ ) { |t| t.encrypt = @encrypted }
107
+ tbl.insert { |r| r.name = 'Spitfire'; r.speed = 500 }
108
+
109
+ assert_equal([1, 'Spitfire', 500], tbl[1].to_a)
110
+ end
111
+
112
+ def test_insert_005
113
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
114
+ ) { |t| t.encrypt = @encrypted }
115
+ tbl.insert('Spitfire', nil)
116
+
117
+ assert_equal([1, 'Spitfire', nil], tbl[1].to_a)
118
+ end
119
+
120
+ def test_insert_006
121
+ tbl = @db.create_table(:plane, :name, :String,
122
+ :speed, {:DataType=>:Integer, :Default=>300}) { |t|
123
+ t.encrypt = @encrypted }
124
+ tbl.insert('Spitfire', nil)
125
+
126
+ assert_equal([1, 'Spitfire', 300], tbl[1].to_a)
127
+ end
128
+
129
+ def test_insert_007
130
+ tbl = @db.create_table(:plane, :name, :String,
131
+ :speed, {:DataType=>:Integer, :Default=>300, :Required=>true}
132
+ ) { |t| t.encrypt = @encrypted }
133
+ tbl.insert(:name => 'Spitfire')
134
+
135
+ assert_equal([1, 'Spitfire', 300], tbl[1].to_a)
136
+ end
137
+
138
+ def test_insert_008
139
+ tbl = @db.create_table(:plane, :name, :String,
140
+ :speed, {:DataType=>:Integer, :Default=>300}) { |t|
141
+ t.encrypt = @encrypted }
142
+ tbl.insert('Spitfire', 500)
143
+
144
+ assert_equal([1, 'Spitfire', 500], tbl[1].to_a)
145
+ end
146
+
147
+ def test_insert_010
148
+ tbl = @db.create_table(:plane, :name, {:DataType=>:String,
149
+ :Required=>true}, :speed, :Integer) { |t| t.encrypt = @encrypted }
150
+
151
+ assert_raise(ArgumentError) { tbl.insert(nil, 500) }
152
+ end
153
+
154
+ def test_insert_011
155
+ tbl = @db.create_table(:plane, :name, {:DataType=>:String,
156
+ :Required=>true}, :speed, :Integer) { |t| t.encrypt = @encrypted }
157
+
158
+ assert_raise(ArgumentError) { tbl.insert(:speed => 500) }
159
+ end
160
+
161
+ def test_insert_012
162
+ tbl = @db.create_table(:plane, :name, {:DataType=>:String,
163
+ :Required=>true}, :speed, :Integer) { |t| t.encrypt = @encrypted }
164
+
165
+ assert_raise(ArgumentError) { tbl.insert { |r| r.speed = 500 } }
166
+ end
167
+
168
+ def test_update_001
169
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
170
+ ) { |t| t.encrypt = @encrypted }
171
+ tbl.insert('Spitfire', 500)
172
+ tbl.update(:speed => 495) { |r| r.recno == 1 }
173
+
174
+ assert_equal([1, 'Spitfire', 495], tbl[1].to_a)
175
+ end
176
+
177
+ def test_update_002
178
+ tbl = @db.create_table(:test, :name, :String) { |t|
179
+ t.encrypt = @encrypted }
180
+ tbl.insert('me')
181
+ tbl.update_all { |r| r.name = r.name.upcase }
182
+ assert_equal('ME', tbl.select[0].name)
183
+ end
184
+
185
+ def test_update_003
186
+ tbl = @db.create_table(:test, :name, :String) { |t|
187
+ t.encrypt = @encrypted }
188
+ tbl.insert('you')
189
+ tbl.update{ true }.set { |r| r.name = r.name.upcase }
190
+ assert_equal('YOU', tbl.select[0].name)
191
+ end
192
+
193
+ def test_update_004
194
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
195
+ ) { |t| t.encrypt = @encrypted }
196
+ tbl.insert('Spitfire', 500)
197
+ tbl[1] = {:speed => 495}
198
+
199
+ assert_equal([1, 'Spitfire', 495], tbl[1].to_a)
200
+ end
201
+
202
+ def test_update_005
203
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
204
+ ) { |t| t.encrypt = @encrypted }
205
+ tbl.insert('Spitfire', 500)
206
+ assert_raise(RuntimeError) { tbl.update(:recno => 5) { |r|
207
+ r.recno == 1 } }
208
+ end
209
+
210
+ def test_update_006
211
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
212
+ ) { |t| t.encrypt = @encrypted }
213
+ tbl.insert('Spitfire', 500)
214
+ assert_raise(RuntimeError) { tbl.update { |r| r.recno == 1
215
+ }.set { |r| r.recno = 5 } }
216
+ end
217
+
218
+ def test_update_007
219
+ tbl = @db.create_table(:plane, :name, :String,
220
+ :speed, {:DataType=>:Integer, :Default=>300, :Required=>true}
221
+ ) { |t| t.encrypt = @encrypted }
222
+ tbl.insert(:name => 'Spitfire', :speed => nil)
223
+
224
+ assert_raise(ArgumentError) { tbl.update(:speed => nil) { |r|
225
+ r.recno == 1 } }
226
+ end
227
+
228
+ def test_update_010
229
+ tbl = @db.create_table(:plane, :name, {:DataType=>:String,
230
+ :Required=>true}, :speed, :Integer) { |t| t.encrypt = @encrypted }
231
+ tbl.insert('Spitfire', 500)
232
+
233
+ assert_raise(ArgumentError) { tbl.update(:name => nil) { |r|
234
+ r.recno == 1 } }
235
+ end
236
+
237
+ def test_delete_001
238
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
239
+ ) { |t| t.encrypt = @encrypted }
240
+ tbl.insert('Spitfire', 500)
241
+ tbl.delete { |r| r.recno == 1 }
242
+
243
+ assert_equal([], tbl[1].to_a)
244
+ end
245
+
246
+ def test_delete_002
247
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
248
+ ) { |t| t.encrypt = @encrypted }
249
+ tbl.insert('Spitfire', 500)
250
+ tbl.delete { |r| r.name == 'Spitfire' }
251
+
252
+ assert_equal([], tbl[1].to_a)
253
+ end
254
+
255
+ def test_delete_003
256
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
257
+ ) { |t| t.encrypt = @encrypted }
258
+ tbl.insert('Spitfire', 500)
259
+ tbl.insert('P-51', 600)
260
+ tbl.delete { |r| r.recno == 1 }
261
+
262
+ assert_equal(1, tbl.select.size)
263
+ end
264
+
265
+ def test_clear_001
266
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
267
+ ) { |t| t.encrypt = @encrypted }
268
+ tbl.insert('Spitfire', 500)
269
+ tbl.insert('P-51', 600)
270
+ tbl.clear
271
+
272
+ assert_equal(0, tbl.select.size)
273
+ end
274
+
275
+ def test_clear_002
276
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
277
+ ) { |t| t.encrypt = @encrypted }
278
+ tbl.insert('Spitfire', 500)
279
+ tbl.insert('P-51', 600)
280
+ tbl.clear(true)
281
+
282
+ assert_equal(0, tbl.last_rec_no)
283
+ end
284
+
285
+ def test_clear_003
286
+ tbl = @db.create_table(:plane, :name, :String, :speed, :Integer
287
+ ) { |t| t.encrypt = @encrypted }
288
+ tbl.insert('Spitfire', 500)
289
+ tbl.insert('P-51', 600)
290
+
291
+ assert_equal(2, tbl.delete_all)
292
+ end
293
+
294
+ def test_add_column_001
295
+ tbl = @db.create_table(:empty, :one, :String, :two, :Integer
296
+ ) { |t| t.encrypt = @encrypted }
297
+ assert_raise(RuntimeError) { tbl.add_column(:one, :String) }
298
+ end
299
+
300
+ def test_add_column_002
301
+ tbl = @db.create_table(:empty, :one, :String, :two, :Integer
302
+ ) { |t| t.encrypt = @encrypted }
303
+ tbl.add_column(:three, :Boolean)
304
+
305
+ assert_equal([:recno, :one, :two, :three], tbl.field_names)
306
+ end
307
+
308
+ def test_add_column_003
309
+ tbl = @db.create_table(:empty, :one, :String) { |t|
310
+ t.encrypt = @encrypted }
311
+ tbl.add_column(:two, {:DataType => :Boolean, :Index => 1})
312
+
313
+ assert_equal([nil, nil, 'Index->1'], tbl.field_indexes)
314
+ end
315
+
316
+ def test_add_column_004
317
+ tbl = @db.create_table(:empty, :one, :String) { |t|
318
+ t.encrypt = @encrypted }
319
+ tbl.add_column(:two, {:DataType => :Integer, :Index => 1})
320
+ tbl.insert('bob', 3)
321
+ tbl.insert('sue', 5)
322
+
323
+ assert_equal([2, 'sue', 5],
324
+ tbl.select_by_two_index { |r| r.two > 4}.first.to_a)
325
+ end
326
+
327
+ def test_add_column_005
328
+ tbl = @db.create_table(:empty, :one, :String, :three, :Integer
329
+ ) { |t| t.encrypt = @encrypted }
330
+ tbl.add_column(:two, :Boolean, :one)
331
+
332
+ assert_equal([:recno, :one, :two, :three], tbl.field_names)
333
+ end
334
+
335
+ def test_drop_column_001
336
+ tbl = @db.create_table(:empty, :one, :String, :two, :Integer
337
+ ) { |t| t.encrypt = @encrypted }
338
+ tbl.drop_column(:one)
339
+
340
+ assert_equal([:recno, :two], tbl.field_names)
341
+ end
342
+
343
+ def test_drop_column_002
344
+ tbl = @db.create_table(:empty, :one, :String, :two, :Integer
345
+ ) { |t| t.encrypt = @encrypted }
346
+ tbl.drop_column(:two)
347
+
348
+ assert_raise(RuntimeError) { tbl.drop_column(:three) }
349
+ end
350
+
351
+ def test_drop_column_003
352
+ tbl = @db.create_table(:empty, :one, {:DataType => :String,
353
+ :Index => 1}, :two, :Integer) { |t| t.encrypt = @encrypted }
354
+ tbl.drop_column(:one)
355
+
356
+ assert_raise(NoMethodError) { tbl.select_by_one_index { true } }
357
+ end
358
+
359
+ def test_rename_column_001
360
+ tbl = @db.create_table(:empty, :one, :String) { |t|
361
+ t.encrypt = @encrypted }
362
+ tbl.rename_column(:one, :two)
363
+ assert_equal([:recno, :two], tbl.field_names)
364
+ end
365
+
366
+ def test_rename_column_002
367
+ tbl = @db.create_table(:empty, :one, {:DataType => :String,
368
+ :Index => 1}) { |t| t.encrypt = @encrypted }
369
+ tbl.insert('one')
370
+ tbl.rename_column(:one, :two)
371
+ assert_equal(1, tbl.select_by_two_index {|r| r.two = 'one'}.size)
372
+ end
373
+
374
+ def test_rename_column_003
375
+ tbl = @db.create_table(:empty, :one, :String) { |t|
376
+ t.encrypt = @encrypted }
377
+ tbl.rename_column(:one, :two)
378
+ assert_equal([:recno, :two], tbl.field_names)
379
+ end
380
+
381
+ def test_rename_column_004
382
+ tbl = @db.create_table(:empty, :one, :String) { |t|
383
+ t.encrypt = @encrypted }
384
+ assert_raise(RuntimeError) {tbl.rename_column(:one, :recno)}
385
+ end
386
+
387
+ def test_change_column_type_001
388
+ tbl = @db.create_table(:empty, :one, :String) { |t|
389
+ t.encrypt = @encrypted }
390
+ tbl.change_column_type(:one, :Integer)
391
+
392
+ assert_equal([:Integer, :Integer], tbl.field_types)
393
+ end
394
+
395
+ def test_change_column_type_002
396
+ tbl = @db.create_table(:empty, :one, :String) { |t|
397
+ t.encrypt = @encrypted }
398
+
399
+ assert_raise(RuntimeError) {tbl.change_column_type(:one, :integer)}
400
+ end
401
+
402
+ def test_change_column_type_003
403
+ tbl = @db.create_table(:empty, :one, { :DataType => :String,
404
+ :Required => true}) { |t| t.encrypt = @encrypted }
405
+ tbl.change_column_type(:one, :Integer)
406
+
407
+ assert_equal([[:Integer, :Integer], [false, true]],
408
+ [tbl.field_types, tbl.field_requireds])
409
+ end
410
+
411
+ def test_calculated_field_001
412
+ tbl = @db.create_table(:empty, :one, :Integer, :two, :Integer,
413
+ :three, { :DataType => :Integer, :Calculated => 'one + two' }
414
+ ) { |t| t.encrypt = @encrypted }
415
+ tbl.insert(1, 2, nil)
416
+
417
+ assert_equal(3, tbl[1].three)
418
+ end
419
+
420
+ def test_calculated_field_002
421
+ tbl = @db.create_table(:empty, :one, :Integer, :two, :Integer,
422
+ :three, { :DataType => :Integer, :Calculated => 'one + two' }
423
+ ) { |t| t.encrypt = @encrypted }
424
+
425
+ assert_raise(ArgumentError) { tbl.insert(1, 2, 3) }
426
+ end
427
+
428
+ def test_column_required_001
429
+ tbl = @db.create_table(:empty, :one, :String, :two,
430
+ {:DataType => :String, :Required => true}) { |t|
431
+ t.encrypt = @encrypted }
432
+
433
+ assert_raise(ArgumentError) { tbl.insert('one', nil) }
434
+ end
435
+
436
+ def test_column_required_002
437
+ tbl = @db.create_table(:empty, :one, :String, :two,
438
+ {:DataType => :String, :Required => true}) { |t|
439
+ t.encrypt = @encrypted }
440
+
441
+ assert_raise(ArgumentError) { tbl.insert(:one => 'one') }
442
+ end
443
+
444
+ def test_column_required_003
445
+ tbl = @db.create_table(:empty, :one, :String, :two,
446
+ {:DataType => :String, :Required => false}) { |t|
447
+ t.encrypt = @encrypted }
448
+ tbl.insert(:one => 'one')
449
+
450
+ assert_equal([1, 'one', nil], tbl[1].to_a)
451
+ end
452
+
453
+ def test_column_required_004
454
+ tbl = @db.create_table(:empty, :one, :String, :two,
455
+ {:DataType => :String, :Required => true, :Default => 'bob'}
456
+ ) { |t| t.encrypt = @encrypted }
457
+ tbl.insert(:one => 'one')
458
+
459
+ assert_equal([1, 'one', 'bob'], tbl[1].to_a)
460
+ end
461
+
462
+ def test_change_column_required_001
463
+ tbl = @db.create_table(:empty, :one, :String, :two, :String
464
+ ) { |t| t.encrypt = @encrypted }
465
+ tbl.change_column_required(:two, true)
466
+
467
+ assert_raise(ArgumentError) { tbl.insert(:one => 'one') }
468
+ end
469
+
470
+ def test_change_column_required_002
471
+ tbl = @db.create_table(:empty, :one, :String, :two,
472
+ {:DataType => :String, :Required => true}) { |t|
473
+ t.encrypt = @encrypted }
474
+ tbl.change_column_required(:two, false)
475
+
476
+ assert_equal(1, tbl.insert(:one => 'one'))
477
+ end
478
+
479
+ def test_column_default_001
480
+ tbl = @db.create_table(:empty, :one, :String, :two,
481
+ {:DataType => :String, :Default => 'two'}) { |t|
482
+ t.encrypt = @encrypted }
483
+ tbl.insert('one', nil)
484
+ assert_equal([1, 'one', 'two'], tbl[1].to_a)
485
+ end
486
+
487
+ def test_column_default_002
488
+ tbl = @db.create_table(:empty, :one, :String, :two,
489
+ {:DataType => :String, :Default => 'two'}) { |t|
490
+ t.encrypt = @encrypted }
491
+ tbl.insert('one', 'not two')
492
+
493
+ assert_equal([1, 'one', 'not two'], tbl[1].to_a)
494
+ end
495
+
496
+ def test_change_column_default_value_001
497
+ tbl = @db.create_table(:empty, :one, :String, :two, :String
498
+ ) { |t| t.encrypt = @encrypted }
499
+ tbl.change_column_default_value(:two, 'two')
500
+ tbl.insert(:one => 'one')
501
+
502
+ assert_equal([1, 'one', 'two'], tbl[1].to_a)
503
+ end
504
+
505
+ def test_change_column_default_value_002
506
+ tbl = @db.create_table(:empty, :one, :String, :two,
507
+ {:DataType => :String, :Default => 'two'}) { |t|
508
+ t.encrypt = @encrypted }
509
+ tbl.change_column_default_value(:two, nil)
510
+ tbl.insert(:one => 'one')
511
+
512
+ assert_equal([1, 'one', nil], tbl[1].to_a)
513
+ end
514
+
515
+ def test_index_001
516
+ tbl = @db.create_table(:empty, :one, {:DataType => :String,
517
+ :Index => 1}) { |t| t.encrypt = @encrypted }
518
+ tbl.insert('one')
519
+ tbl.insert('two')
520
+
521
+ assert_equal(1, tbl.select_by_one_index {|r| r.one == 'one'}.size)
522
+ end
523
+
524
+ def test_index_002
525
+ tbl = @db.create_table(:empty, :one, {:DataType => :String,
526
+ :Index => 1}, :two, :String, :three,
527
+ {:DataType => :Integer, :Index => 1}) { |t|
528
+ t.encrypt = @encrypted }
529
+ tbl.insert('one', 'two', 3)
530
+ tbl.insert('four', 'five', 6)
531
+
532
+ assert_equal(1, tbl.select_by_one_three_index {|r|
533
+ r.one == 'four' and r.three == 6}.size)
534
+ end
535
+
536
+ def test_index_003
537
+ assert_raise(RuntimeError) {@db.create_table(:empty, :one,
538
+ {:DataType => :YAML, :Index => 1}) { |t| t.encrypt = @encrypted }}
539
+ end
540
+
541
+ def test_add_index_001
542
+ tbl = @db.create_table(:empty, :first, :String, :last, :String
543
+ ) { |t| t.encrypt = @encrypted }
544
+ tbl.insert('John', 'Doe')
545
+ tbl.add_index(:first, :last)
546
+
547
+ assert_equal(1, tbl.select_by_first_last_index {|r|
548
+ r.first == 'John' and r.last == 'Doe'}.size)
549
+ end
550
+
551
+ def test_add_index_002
552
+ tbl = @db.create_table(:empty, :first, :String, :last, :String
553
+ ) { |t| t.encrypt = @encrypted }
554
+ tbl.insert('John', 'Doe')
555
+ assert_raise(RuntimeError) { tbl.add_index(:recno, :first) }
556
+ end
557
+
558
+ def test_drop_index_001
559
+ tbl = @db.create_table(:empty, :one, {:DataType => :String,
560
+ :Index => 1}) { |t| t.encrypt = @encrypted }
561
+ tbl.insert('one')
562
+ tbl.insert('two')
563
+ tbl.drop_index(:one)
564
+
565
+ assert_equal([nil, nil], tbl.field_indexes)
566
+ end
567
+
568
+ def test_drop_index_002
569
+ tbl = @db.create_table(:empty, :one, {:DataType => :String,
570
+ :Index => 1}) { |t| t.encrypt = @encrypted }
571
+ tbl.insert('one')
572
+ tbl.insert('two')
573
+ tbl.drop_index(:one)
574
+
575
+ assert_raise(NoMethodError) { tbl.select_by_one_index { |r|
576
+ r.one == 'one' } }
577
+ end
578
+
579
+ def test_link_many_001
580
+ parent_tbl = @db.create_table(:parent, :one, :String, :two,
581
+ {:DataType => :ResultSet, :Link_many => [:one, :child, :three]}
582
+ ) { |t| t.encrypt = @encrypted }
583
+ child_tbl = @db.create_table(:child, :three, :String) { |t|
584
+ t.encrypt = @encrypted }
585
+ parent_tbl.insert(:one => 'bob')
586
+ child_tbl.insert('bob')
587
+ child_tbl.insert('bob')
588
+ child_tbl.insert('sue')
589
+
590
+ assert_equal(2, parent_tbl.select[0].two.size)
591
+ end
592
+
593
+ def test_link_many_002
594
+ assert_raise(RuntimeError) { @db.create_table(:parent, :one,
595
+ :String, :two, {:DataType => :String, :Link_many =>
596
+ [:one, :child, :three]}) { |t| t.encrypt = @encrypted } }
597
+ end
598
+
599
+ def test_lookup_field_001
600
+ person_tbl = @db.create_table(:person, :person_id, {:DataType =>
601
+ :String, :Key => true}, :name, :String) { |t|
602
+ t.encrypt = @encrypted }
603
+ emp_tbl = @db.create_table(:emp, :person, :person) { |t|
604
+ t.encrypt = @encrypted }
605
+ person_tbl.insert('1', 'John Doe')
606
+ emp_tbl.insert(:person => '1')
607
+
608
+ assert_equal('John Doe', emp_tbl.select[0].person.name)
609
+ end
610
+
611
+ def test_lookup_field_002
612
+ person_tbl = @db.create_table(:person, :person_id, :String,
613
+ :name, :String) { |t| t.encrypt = @encrypted }
614
+ emp_tbl = @db.create_table(:emp, :person, {:DataType => :String,
615
+ :Lookup => [:person, :person_id]}) { |t| t.encrypt = @encrypted }
616
+ person_tbl.insert('1', 'John Doe')
617
+ emp_tbl.insert(:person => '1')
618
+
619
+ assert_equal('John Doe', emp_tbl.select[0].person.name)
620
+ end
621
+
622
+ def test_lookup_field_003
623
+ emp_tbl = @db.create_table(:emp, :person, {:DataType => :String,
624
+ :Lookup => [:person, :person_id]}) { |t| t.encrypt = @encrypted }
625
+ person_tbl = @db.create_table(:person, :person_id, :String,
626
+ :name, :String) { |t| t.encrypt = @encrypted }
627
+ person_tbl.insert('1', 'John Doe')
628
+ emp_tbl.insert(:person => '1')
629
+
630
+ assert_equal('John Doe', emp_tbl.select[0].person.name)
631
+ end
632
+
633
+ def test_lookup_field_004
634
+ emp_tbl = @db.create_table(:emp, :person, {:DataType => :String,
635
+ :Lookup => [:person, :incorrect_field_name]}) { |t|
636
+ t.encrypt = @encrypted }
637
+ person_tbl = @db.create_table(:person, :person_id, :String,
638
+ :name, :String) { |t| t.encrypt = @encrypted }
639
+ person_tbl.insert('1', 'John Doe')
640
+ emp_tbl.insert(:person => '1')
641
+
642
+ assert_raise(NoMethodError) { emp_tbl.select[0].person.name }
643
+ end
644
+
645
+ def test_lookup_field_005
646
+ person_tbl = @db.create_table(:person, :person_id, :String,
647
+ :name, :String) { |t| t.encrypt = @encrypted }
648
+ emp_tbl = @db.create_table(:emp, :person, {:DataType => :String,
649
+ :Lookup => [:incorrect_table_name, :person_id]}) { |t|
650
+ t.encrypt = @encrypted }
651
+ person_tbl.insert('1', 'John Doe')
652
+ emp_tbl.insert(:person => '1')
653
+
654
+ assert_raise(RuntimeError) { emp_tbl.select[0].person.name }
655
+ end
656
+
657
+ def test_memo_001
658
+ tbl = @db.create_table(:empty, :one, :Memo) { |t|
659
+ t.encrypt = @encrypted }
660
+ tbl.insert(KBMemo.new(@db, 'test.txt', 'This is a test.'))
661
+
662
+ assert_equal('This is a test.', tbl[1].one.contents)
663
+ end
664
+
665
+ def test_memo_002
666
+ tbl = @db.create_table(:empty, :one, :Memo) { |t|
667
+ t.encrypt = @encrypted }
668
+ tbl.insert(KBMemo.new(@db, 'test.txt', 'This is a test.'))
669
+ tbl.update { |r| r.recno == 1}.set { |r|
670
+ r.one.contents += ' And so is this.' }
671
+
672
+ assert_equal('This is a test. And so is this.',
673
+ tbl[1].one.contents)
674
+ end
675
+
676
+ class Foobar
677
+ attr_accessor(:recno, :one)
678
+
679
+ def Foobar.kb_create(recno, one)
680
+ Foobar.new do |x|
681
+ x.recno = recno
682
+ x.one = one
683
+ end
684
+ end
685
+
686
+ def initialize(&block)
687
+ instance_eval(&block)
688
+ end
689
+ end
690
+
691
+ def test_record_class_001
692
+ tbl = @db.create_table(:empty, :one, :String) {|t|
693
+ t.record_class = Foobar
694
+ t.encrypt = @encrypted
695
+ }
696
+ tbl.insert('one')
697
+ assert_equal(Foobar, tbl[1].class)
698
+ end
699
+
700
+ def test_yaml_001
701
+ tbl = @db.create_table(:empty, :two, :String, :one, :YAML) { |t|
702
+ t.encrypt = @encrypted }
703
+ tbl.insert(:one => ['one', 2])
704
+
705
+ assert_equal(['one', 2], tbl[1].one)
706
+ end
707
+
708
+ def test_yaml_002
709
+ tbl = @db.create_table(:empty, :two, :String, :one, :YAML) { |t|
710
+ t.encrypt = @encrypted }
711
+ tbl.insert(:one => ['one', 2])
712
+ tbl.update(:one => ['two', 3]) { |r| r.recno == 1 }
713
+ assert_equal(['two', 3], tbl[1].one)
714
+ end
715
+ end
716
+