/
database_spec.rb
1413 lines (1196 loc) · 45.3 KB
/
database_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require File.join(File.dirname(__FILE__), 'spec_helper')
context "A new Database" do
before do
@db = Sequel::Database.new(1 => 2, :logger => 3)
end
after do
Sequel.quote_identifiers = false
Sequel.identifier_input_method = nil
Sequel.identifier_output_method = nil
end
specify "should receive options" do
@db.opts.should == {1 => 2, :logger => 3}
end
specify "should set the logger from opts[:logger] and opts[:loggers]" do
@db.loggers.should == [3]
Sequel::Database.new(1 => 2, :loggers => 3).loggers.should == [3]
Sequel::Database.new(1 => 2, :loggers => [3]).loggers.should == [3]
Sequel::Database.new(1 => 2, :logger => 4, :loggers => 3).loggers.should == [4,3]
Sequel::Database.new(1 => 2, :logger => [4], :loggers => [3]).loggers.should == [4,3]
end
specify "should create a connection pool" do
@db.pool.should be_a_kind_of(Sequel::ConnectionPool)
@db.pool.max_size.should == 4
Sequel::Database.new(:max_connections => 10).pool.max_size.should == 10
end
specify "should pass the supplied block to the connection pool" do
cc = nil
d = Sequel::Database.new {1234}
d.synchronize {|c| cc = c}
cc.should == 1234
end
specify "should respect the :single_threaded option" do
db = Sequel::Database.new(:single_threaded=>true)
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
db = Sequel::Database.new(:single_threaded=>'t')
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
db = Sequel::Database.new(:single_threaded=>'1')
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
db = Sequel::Database.new(:single_threaded=>false)
db.pool.should be_a_kind_of(Sequel::ConnectionPool)
db = Sequel::Database.new(:single_threaded=>'f')
db.pool.should be_a_kind_of(Sequel::ConnectionPool)
db = Sequel::Database.new(:single_threaded=>'0')
db.pool.should be_a_kind_of(Sequel::ConnectionPool)
end
specify "should respect the :quote_identifiers option" do
db = Sequel::Database.new(:quote_identifiers=>false)
db.quote_identifiers?.should == false
db = Sequel::Database.new(:quote_identifiers=>true)
db.quote_identifiers?.should == true
end
specify "should upcase on input and downcase on output by default" do
db = Sequel::Database.new
db.send(:identifier_input_method_default).should == :upcase
db.send(:identifier_output_method_default).should == :downcase
end
specify "should respect the :identifier_input_method option" do
Sequel.identifier_input_method = nil
Sequel::Database.identifier_input_method.should == ""
db = Sequel::Database.new(:identifier_input_method=>nil)
db.identifier_input_method.should == nil
db.identifier_input_method = :downcase
db.identifier_input_method.should == :downcase
db = Sequel::Database.new(:identifier_input_method=>:upcase)
db.identifier_input_method.should == :upcase
db.identifier_input_method = nil
db.identifier_input_method.should == nil
Sequel.identifier_input_method = :downcase
Sequel::Database.identifier_input_method.should == :downcase
db = Sequel::Database.new(:identifier_input_method=>nil)
db.identifier_input_method.should == nil
db.identifier_input_method = :upcase
db.identifier_input_method.should == :upcase
db = Sequel::Database.new(:identifier_input_method=>:upcase)
db.identifier_input_method.should == :upcase
db.identifier_input_method = nil
db.identifier_input_method.should == nil
end
specify "should respect the :identifier_output_method option" do
Sequel.identifier_output_method = nil
Sequel::Database.identifier_output_method.should == ""
db = Sequel::Database.new(:identifier_output_method=>nil)
db.identifier_output_method.should == nil
db.identifier_output_method = :downcase
db.identifier_output_method.should == :downcase
db = Sequel::Database.new(:identifier_output_method=>:upcase)
db.identifier_output_method.should == :upcase
db.identifier_output_method = nil
db.identifier_output_method.should == nil
Sequel.identifier_output_method = :downcase
Sequel::Database.identifier_output_method.should == :downcase
db = Sequel::Database.new(:identifier_output_method=>nil)
db.identifier_output_method.should == nil
db.identifier_output_method = :upcase
db.identifier_output_method.should == :upcase
db = Sequel::Database.new(:identifier_output_method=>:upcase)
db.identifier_output_method.should == :upcase
db.identifier_output_method = nil
db.identifier_output_method.should == nil
end
specify "should use the default Sequel.quote_identifiers value" do
Sequel.quote_identifiers = true
Sequel::Database.new({}).quote_identifiers?.should == true
Sequel.quote_identifiers = false
Sequel::Database.new({}).quote_identifiers?.should == false
Sequel::Database.quote_identifiers = true
Sequel::Database.new({}).quote_identifiers?.should == true
Sequel::Database.quote_identifiers = false
Sequel::Database.new({}).quote_identifiers?.should == false
end
specify "should use the default Sequel.identifier_input_method value" do
Sequel.identifier_input_method = :downcase
Sequel::Database.new({}).identifier_input_method.should == :downcase
Sequel.identifier_input_method = :upcase
Sequel::Database.new({}).identifier_input_method.should == :upcase
Sequel::Database.identifier_input_method = :downcase
Sequel::Database.new({}).identifier_input_method.should == :downcase
Sequel::Database.identifier_input_method = :upcase
Sequel::Database.new({}).identifier_input_method.should == :upcase
end
specify "should use the default Sequel.identifier_output_method value" do
Sequel.identifier_output_method = :downcase
Sequel::Database.new({}).identifier_output_method.should == :downcase
Sequel.identifier_output_method = :upcase
Sequel::Database.new({}).identifier_output_method.should == :upcase
Sequel::Database.identifier_output_method = :downcase
Sequel::Database.new({}).identifier_output_method.should == :downcase
Sequel::Database.identifier_output_method = :upcase
Sequel::Database.new({}).identifier_output_method.should == :upcase
end
specify "should respect the quote_indentifiers_default method if Sequel.quote_identifiers = nil" do
Sequel.quote_identifiers = nil
Sequel::Database.new({}).quote_identifiers?.should == true
x = Class.new(Sequel::Database){def quote_identifiers_default; false end}
x.new({}).quote_identifiers?.should == false
y = Class.new(Sequel::Database){def quote_identifiers_default; true end}
y.new({}).quote_identifiers?.should == true
end
specify "should respect the identifier_input_method_default method" do
class Sequel::Database
@@identifier_input_method = nil
end
x = Class.new(Sequel::Database){def identifier_input_method_default; :downcase end}
x.new({}).identifier_input_method.should == :downcase
y = Class.new(Sequel::Database){def identifier_input_method_default; :camelize end}
y.new({}).identifier_input_method.should == :camelize
end
specify "should respect the identifier_output_method_default method if Sequel.identifier_output_method is not called" do
class Sequel::Database
@@identifier_output_method = nil
end
x = Class.new(Sequel::Database){def identifier_output_method_default; :upcase end}
x.new({}).identifier_output_method.should == :upcase
y = Class.new(Sequel::Database){def identifier_output_method_default; :underscore end}
y.new({}).identifier_output_method.should == :underscore
end
specify "should just use a :uri option for jdbc with the full connection string" do
Sequel::Database.should_receive(:adapter_class).once.with(:jdbc).and_return(Sequel::Database)
db = Sequel.connect('jdbc:test://host/db_name')
db.should be_a_kind_of(Sequel::Database)
db.opts[:uri].should == 'jdbc:test://host/db_name'
end
specify "should just use a :uri option for do with the full connection string" do
Sequel::Database.should_receive(:adapter_class).once.with(:do).and_return(Sequel::Database)
db = Sequel.connect('do:test://host/db_name')
db.should be_a_kind_of(Sequel::Database)
db.opts[:uri].should == 'do:test://host/db_name'
end
end
context "Database#disconnect" do
specify "should call pool.disconnect" do
d = Sequel::Database.new
p = d.pool
p.should_receive(:disconnect).once.and_return(2)
d.disconnect.should == 2
end
end
context "Sequel.extension" do
specify "should attempt to load the given extension" do
proc{Sequel.extension :blah}.should raise_error(LoadError)
end
end
context "Database#connect" do
specify "should raise NotImplementedError" do
proc {Sequel::Database.new.connect}.should raise_error(NotImplementedError)
end
end
context "Database#uri" do
before do
@c = Class.new(Sequel::Database) do
set_adapter_scheme :mau
end
@db = Sequel.connect('mau://user:pass@localhost:9876/maumau')
end
specify "should return the connection URI for the database" do
@db.uri.should == 'mau://user:pass@localhost:9876/maumau'
end
specify "should be aliased as #url" do
@db.url.should == 'mau://user:pass@localhost:9876/maumau'
end
end
context "Database.adapter_scheme" do
specify "should return the database schema" do
Sequel::Database.adapter_scheme.should be_nil
@c = Class.new(Sequel::Database) do
set_adapter_scheme :mau
end
@c.adapter_scheme.should == :mau
end
end
context "Database#dataset" do
before do
@db = Sequel::Database.new
@ds = @db.dataset
end
specify "should provide a blank dataset through #dataset" do
@ds.should be_a_kind_of(Sequel::Dataset)
@ds.opts.should == {}
@ds.db.should be(@db)
end
specify "should provide a #from dataset" do
d = @db.from(:mau)
d.should be_a_kind_of(Sequel::Dataset)
d.sql.should == 'SELECT * FROM mau'
e = @db[:miu]
e.should be_a_kind_of(Sequel::Dataset)
e.sql.should == 'SELECT * FROM miu'
end
specify "should provide a filtered #from dataset if a block is given" do
d = @db.from(:mau) {:x.sql_number > 100}
d.should be_a_kind_of(Sequel::Dataset)
d.sql.should == 'SELECT * FROM mau WHERE (x > 100)'
end
specify "should provide a #select dataset" do
d = @db.select(:a, :b, :c).from(:mau)
d.should be_a_kind_of(Sequel::Dataset)
d.sql.should == 'SELECT a, b, c FROM mau'
end
specify "should allow #select to take a block" do
d = @db.select(:a, :b){c}.from(:mau)
d.should be_a_kind_of(Sequel::Dataset)
d.sql.should == 'SELECT a, b, c FROM mau'
end
end
context "Database#execute" do
specify "should raise NotImplementedError" do
proc {Sequel::Database.new.execute('blah blah')}.should raise_error(NotImplementedError)
proc {Sequel::Database.new << 'blah blah'}.should raise_error(NotImplementedError)
end
end
context "Database#<< and run" do
before do
sqls = @sqls = []
@c = Class.new(Sequel::Database) do
define_method(:execute_ddl){|sql, *opts| sqls.clear; sqls << sql; sqls.concat(opts)}
end
@db = @c.new({})
end
specify "should pass the supplied sql to #execute_ddl" do
(@db << "DELETE FROM items")
@sqls.should == ["DELETE FROM items", {}]
@db.run("DELETE FROM items2")
@sqls.should == ["DELETE FROM items2", {}]
end
specify "should return nil" do
(@db << "DELETE FROM items").should == nil
@db.run("DELETE FROM items").should == nil
end
specify "should accept options passed to execute_ddl" do
@db.run("DELETE FROM items", :server=>:s1)
@sqls.should == ["DELETE FROM items", {:server=>:s1}]
end
end
context "Database#synchronize" do
before do
@db = Sequel::Database.new(:max_connections => 1)
@db.pool.connection_proc = proc {12345}
end
specify "should wrap the supplied block in pool.hold" do
stop = false
c1, c2 = nil
t1 = Thread.new {@db.synchronize {|c| c1 = c; while !stop;sleep 0.1;end}}
while !c1;end
c1.should == 12345
t2 = Thread.new {@db.synchronize {|c| c2 = c}}
sleep 0.2
@db.pool.available_connections.should be_empty
c2.should be_nil
stop = true
t1.join
sleep 0.1
c2.should == 12345
t2.join
end
end
context "Database#test_connection" do
before do
@db = Sequel::Database.new
@test = nil
@db.pool.connection_proc = proc {@test = rand(100)}
end
specify "should call pool#hold" do
@db.test_connection
@test.should_not be_nil
end
specify "should return true if successful" do
@db.test_connection.should be_true
end
end
class DummyDataset < Sequel::Dataset
def first
raise if @opts[:from] == [:a]
true
end
end
class DummyDatabase < Sequel::Database
attr_reader :sqls
def execute(sql, opts={})
@sqls ||= []
@sqls << sql
end
def transaction; yield; end
def dataset
DummyDataset.new(self)
end
end
context "Database#create_table" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.create_table :test do
primary_key :id, :integer, :null => false
column :name, :text
index :name, :unique => true
end
@db.sqls.should == [
'CREATE TABLE test (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
'CREATE UNIQUE INDEX test_name_index ON test (name)'
]
end
specify "should create a temporary table" do
@db.create_table :test_tmp, :temp => true do
primary_key :id, :integer, :null => false
column :name, :text
index :name, :unique => true
end
@db.sqls.should == [
'CREATE TEMPORARY TABLE test_tmp (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
'CREATE UNIQUE INDEX test_tmp_name_index ON test_tmp (name)'
]
end
end
context "Database#alter_table" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.alter_table :xyz do
add_column :aaa, :text, :null => false, :unique => true
drop_column :bbb
rename_column :ccc, :ddd
set_column_type :eee, :integer
set_column_default :hhh, 'abcd'
add_index :fff, :unique => true
drop_index :ggg
end
@db.sqls.should == [
'ALTER TABLE xyz ADD COLUMN aaa text UNIQUE NOT NULL',
'ALTER TABLE xyz DROP COLUMN bbb',
'ALTER TABLE xyz RENAME COLUMN ccc TO ddd',
'ALTER TABLE xyz ALTER COLUMN eee TYPE integer',
"ALTER TABLE xyz ALTER COLUMN hhh SET DEFAULT 'abcd'",
'CREATE UNIQUE INDEX xyz_fff_index ON xyz (fff)',
'DROP INDEX xyz_ggg_index'
]
end
end
context "Database#add_column" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.add_column :test, :name, :text, :unique => true
@db.sqls.should == [
'ALTER TABLE test ADD COLUMN name text UNIQUE'
]
end
end
context "Database#drop_column" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.drop_column :test, :name
@db.sqls.should == [
'ALTER TABLE test DROP COLUMN name'
]
end
end
context "Database#rename_column" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.rename_column :test, :abc, :def
@db.sqls.should == [
'ALTER TABLE test RENAME COLUMN abc TO def'
]
end
end
context "Database#set_column_type" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.set_column_type :test, :name, :integer
@db.sqls.should == [
'ALTER TABLE test ALTER COLUMN name TYPE integer'
]
end
end
context "Database#set_column_default" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.set_column_default :test, :name, 'zyx'
@db.sqls.should == [
"ALTER TABLE test ALTER COLUMN name SET DEFAULT 'zyx'"
]
end
end
context "Database#add_index" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.add_index :test, :name, :unique => true
@db.sqls.should == [
'CREATE UNIQUE INDEX test_name_index ON test (name)'
]
end
specify "should accept multiple columns" do
@db.add_index :test, [:one, :two]
@db.sqls.should == [
'CREATE INDEX test_one_two_index ON test (one, two)'
]
end
end
context "Database#drop_index" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.drop_index :test, :name
@db.sqls.should == [
'DROP INDEX test_name_index'
]
end
end
class Dummy2Database < Sequel::Database
attr_reader :sql
def execute(sql); @sql = sql; end
def transaction; yield; end
end
context "Database#drop_table" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.drop_table :test
@db.sqls.should == ['DROP TABLE test']
end
specify "should accept multiple table names" do
@db.drop_table :a, :bb, :ccc
@db.sqls.should == [
'DROP TABLE a',
'DROP TABLE bb',
'DROP TABLE ccc'
]
end
end
context "Database#rename_table" do
before do
@db = DummyDatabase.new
end
specify "should construct proper SQL" do
@db.rename_table :abc, :xyz
@db.sqls.should == ['ALTER TABLE abc RENAME TO xyz']
end
end
context "Database#table_exists?" do
specify "should try to select the first record from the table's dataset" do
db2 = DummyDatabase.new
db2.table_exists?(:a).should be_false
db2.table_exists?(:b).should be_true
end
end
class Dummy3Database < Sequel::Database
attr_reader :sql, :transactions
def execute(sql, opts={}); @sql ||= []; @sql << sql; end
class DummyConnection
def initialize(db); @db = db; end
def execute(sql); @db.execute(sql); end
end
end
context "Database#transaction" do
before do
@db = Dummy3Database.new
@db.pool.connection_proc = proc {Dummy3Database::DummyConnection.new(@db)}
end
specify "should wrap the supplied block with BEGIN + COMMIT statements" do
@db.transaction {@db.execute 'DROP TABLE test;'}
@db.sql.should == ['BEGIN', 'DROP TABLE test;', 'COMMIT']
end
specify "should handle returning inside of the block by committing" do
def @db.ret_commit
transaction do
execute 'DROP TABLE test;'
return
execute 'DROP TABLE test2;';
end
end
@db.ret_commit
@db.sql.should == ['BEGIN', 'DROP TABLE test;', 'COMMIT']
end
specify "should issue ROLLBACK if an exception is raised, and re-raise" do
@db.transaction {@db.execute 'DROP TABLE test'; raise RuntimeError} rescue nil
@db.sql.should == ['BEGIN', 'DROP TABLE test', 'ROLLBACK']
proc {@db.transaction {raise RuntimeError}}.should raise_error(RuntimeError)
end
specify "should issue ROLLBACK if Sequel::Rollback is called in the transaction" do
@db.transaction do
@db.drop_table(:a)
raise Sequel::Rollback
@db.drop_table(:b)
end
@db.sql.should == ['BEGIN', 'DROP TABLE a', 'ROLLBACK']
end
specify "should raise database errors when commiting a transaction as Sequel::DatabaseError" do
@db.meta_def(:commit_transaction){raise ArgumentError}
lambda{@db.transaction{}}.should raise_error(ArgumentError)
@db.meta_def(:database_error_classes){[ArgumentError]}
lambda{@db.transaction{}}.should raise_error(Sequel::DatabaseError)
end
specify "should be re-entrant" do
stop = false
cc = nil
t = Thread.new do
@db.transaction {@db.transaction {@db.transaction {|c|
cc = c
while !stop; sleep 0.1; end
}}}
end
while cc.nil?; sleep 0.1; end
cc.should be_a_kind_of(Dummy3Database::DummyConnection)
@db.transactions.should == [t]
stop = true
t.join
@db.transactions.should be_empty
end
end
context "Database#transaction with savepoints" do
before do
@db = Dummy3Database.new
@db.meta_def(:supports_savepoints?){true}
@db.pool.connection_proc = proc {Dummy3Database::DummyConnection.new(@db)}
end
specify "should wrap the supplied block with BEGIN + COMMIT statements" do
@db.transaction {@db.execute 'DROP TABLE test;'}
@db.sql.should == ['BEGIN', 'DROP TABLE test;', 'COMMIT']
end
specify "should use savepoints if given the :savepoint option" do
@db.transaction{@db.transaction(:savepoint=>true){@db.execute 'DROP TABLE test;'}}
@db.sql.should == ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
end
specify "should not use a savepoints if no transaction is in progress" do
@db.transaction(:savepoint=>true){@db.execute 'DROP TABLE test;'}
@db.sql.should == ['BEGIN', 'DROP TABLE test;', 'COMMIT']
end
specify "should reuse the current transaction if no :savepoint option is given" do
@db.transaction{@db.transaction{@db.execute 'DROP TABLE test;'}}
@db.sql.should == ['BEGIN', 'DROP TABLE test;', 'COMMIT']
end
specify "should handle returning inside of the block by committing" do
def @db.ret_commit
transaction do
execute 'DROP TABLE test;'
return
execute 'DROP TABLE test2;';
end
end
@db.ret_commit
@db.sql.should == ['BEGIN', 'DROP TABLE test;', 'COMMIT']
end
specify "should handle returning inside of a savepoint by committing" do
def @db.ret_commit
transaction do
transaction(:savepoint=>true) do
execute 'DROP TABLE test;'
return
execute 'DROP TABLE test2;';
end
end
end
@db.ret_commit
@db.sql.should == ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test;', 'RELEASE SAVEPOINT autopoint_1', 'COMMIT']
end
specify "should issue ROLLBACK if an exception is raised, and re-raise" do
@db.transaction {@db.execute 'DROP TABLE test'; raise RuntimeError} rescue nil
@db.sql.should == ['BEGIN', 'DROP TABLE test', 'ROLLBACK']
proc {@db.transaction {raise RuntimeError}}.should raise_error(RuntimeError)
end
specify "should issue ROLLBACK SAVEPOINT if an exception is raised inside a savepoint, and re-raise" do
@db.transaction{@db.transaction(:savepoint=>true){@db.execute 'DROP TABLE test'; raise RuntimeError}} rescue nil
@db.sql.should == ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE test', 'ROLLBACK TO SAVEPOINT autopoint_1', 'ROLLBACK']
proc {@db.transaction {raise RuntimeError}}.should raise_error(RuntimeError)
end
specify "should issue ROLLBACK if Sequel::Rollback is called in the transaction" do
@db.transaction do
@db.drop_table(:a)
raise Sequel::Rollback
@db.drop_table(:b)
end
@db.sql.should == ['BEGIN', 'DROP TABLE a', 'ROLLBACK']
end
specify "should issue ROLLBACK SAVEPOINT if Sequel::Rollback is called in a savepoint" do
@db.transaction do
@db.transaction(:savepoint=>true) do
@db.drop_table(:a)
raise Sequel::Rollback
end
@db.drop_table(:b)
end
@db.sql.should == ['BEGIN', 'SAVEPOINT autopoint_1', 'DROP TABLE a', 'ROLLBACK TO SAVEPOINT autopoint_1', 'DROP TABLE b', 'COMMIT']
end
specify "should raise database errors when commiting a transaction as Sequel::DatabaseError" do
@db.meta_def(:commit_transaction){raise ArgumentError}
lambda{@db.transaction{}}.should raise_error(ArgumentError)
lambda{@db.transaction{@db.transaction(:savepoint=>true){}}}.should raise_error(ArgumentError)
@db.meta_def(:database_error_classes){[ArgumentError]}
lambda{@db.transaction{}}.should raise_error(Sequel::DatabaseError)
lambda{@db.transaction{@db.transaction(:savepoint=>true){}}}.should raise_error(Sequel::DatabaseError)
end
end
context "A Database adapter with a scheme" do
before do
class CCC < Sequel::Database
if defined?(DISCONNECTS)
DISCONNECTS.clear
else
DISCONNECTS = []
end
set_adapter_scheme :ccc
def disconnect
DISCONNECTS << self
end
end
end
specify "should be registered in the ADAPTER_MAP" do
Sequel::ADAPTER_MAP[:ccc].should == CCC
end
specify "should give the database_type as the adapter scheme by default" do
CCC.new.database_type.should == :ccc
end
specify "should be instantiated when its scheme is specified" do
c = Sequel::Database.connect('ccc://localhost/db')
c.should be_a_kind_of(CCC)
c.opts[:host].should == 'localhost'
c.opts[:database].should == 'db'
end
specify "should be accessible through Sequel.connect" do
c = Sequel.connect 'ccc://localhost/db'
c.should be_a_kind_of(CCC)
c.opts[:host].should == 'localhost'
c.opts[:database].should == 'db'
end
specify "should be accessible through Sequel.connect via a block" do
x = nil
y = nil
z = nil
p = proc do |c|
c.should be_a_kind_of(CCC)
c.opts[:host].should == 'localhost'
c.opts[:database].should == 'db'
z = y
y = x
x = c
end
Sequel::Database.connect('ccc://localhost/db', &p).should == nil
CCC::DISCONNECTS.should == [x]
Sequel.connect('ccc://localhost/db', &p).should == nil
CCC::DISCONNECTS.should == [y, x]
Sequel.send(:def_adapter_method, :ccc)
Sequel.ccc('db', :host=>'localhost', &p).should == nil
CCC::DISCONNECTS.should == [z, y, x]
end
specify "should be accessible through Sequel.<adapter>" do
Sequel.send(:def_adapter_method, :ccc)
# invalid parameters
proc {Sequel.ccc('abc', 'def')}.should raise_error(Sequel::Error)
c = Sequel.ccc('mydb')
c.should be_a_kind_of(CCC)
c.opts.should == {:adapter=>:ccc, :database => 'mydb'}
c = Sequel.ccc('mydb', :host => 'localhost')
c.should be_a_kind_of(CCC)
c.opts.should == {:adapter=>:ccc, :database => 'mydb', :host => 'localhost'}
c = Sequel.ccc
c.should be_a_kind_of(CCC)
c.opts.should == {:adapter=>:ccc}
c = Sequel.ccc(:database => 'mydb', :host => 'localhost')
c.should be_a_kind_of(CCC)
c.opts.should == {:adapter=>:ccc, :database => 'mydb', :host => 'localhost'}
end
specify "should be accessible through Sequel.connect with options" do
c = Sequel.connect(:adapter => :ccc, :database => 'mydb')
c.should be_a_kind_of(CCC)
c.opts.should == {:adapter => :ccc, :database => 'mydb'}
end
specify "should be accessible through Sequel.connect with URL parameters" do
c = Sequel.connect 'ccc:///db?host=/tmp&user=test'
c.should be_a_kind_of(CCC)
c.opts[:host].should == '/tmp'
c.opts[:database].should == 'db'
c.opts[:user].should == 'test'
end
specify "should have URL parameters take precedence over fixed URL parts" do
c = Sequel.connect 'ccc://localhost/db?host=a&database=b'
c.should be_a_kind_of(CCC)
c.opts[:host].should == 'a'
c.opts[:database].should == 'b'
end
specify "should have hash options take predence over URL parameters or parts" do
c = Sequel.connect 'ccc://localhost/db?host=/tmp', :host=>'a', :database=>'b', :user=>'c'
c.should be_a_kind_of(CCC)
c.opts[:host].should == 'a'
c.opts[:database].should == 'b'
c.opts[:user].should == 'c'
end
specify "should unescape values of URL parameters and parts" do
c = Sequel.connect 'ccc:///d%5bb%5d?host=domain%5cinstance'
c.should be_a_kind_of(CCC)
c.opts[:database].should == 'd[b]'
c.opts[:host].should == 'domain\\instance'
end
end
context "Sequel::Database.connect" do
specify "should raise an Error if not given a String or Hash" do
proc{Sequel::Database.connect(nil)}.should raise_error(Sequel::Error)
proc{Sequel::Database.connect(Object.new)}.should raise_error(Sequel::Error)
end
end
context "An unknown database scheme" do
specify "should raise an error in Sequel::Database.connect" do
proc {Sequel::Database.connect('ddd://localhost/db')}.should raise_error(Sequel::AdapterNotFound)
end
specify "should raise an error in Sequel.connect" do
proc {Sequel.connect('ddd://localhost/db')}.should raise_error(Sequel::AdapterNotFound)
end
end
context "A broken adapter (lib is there but the class is not)" do
before do
@fn = File.join(File.dirname(__FILE__), '../../lib/sequel/adapters/blah.rb')
File.open(@fn,'a'){}
end
after do
File.delete(@fn)
end
specify "should raise an error" do
proc {Sequel.connect('blah://blow')}.should raise_error(Sequel::AdapterNotFound)
end
end
context "A single threaded database" do
after do
Sequel::Database.single_threaded = false
end
specify "should use a SingleThreadedPool instead of a ConnectionPool" do
db = Sequel::Database.new(:single_threaded => true)
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
end
specify "should be constructable using :single_threaded => true option" do
db = Sequel::Database.new(:single_threaded => true)
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
end
specify "should be constructable using Database.single_threaded = true" do
Sequel::Database.single_threaded = true
db = Sequel::Database.new
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
end
specify "should be constructable using Sequel.single_threaded = true" do
Sequel.single_threaded = true
db = Sequel::Database.new
db.pool.should be_a_kind_of(Sequel::SingleThreadedPool)
end
end
context "A single threaded database" do
before do
conn = 1234567
@db = Sequel::Database.new(:single_threaded => true) do
conn += 1
end
end
specify "should invoke connection_proc only once" do
@db.pool.hold {|c| c.should == 1234568}
@db.pool.hold {|c| c.should == 1234568}
end
specify "should convert an Exception into a RuntimeError" do
db = Sequel::Database.new(:single_threaded => true) do
raise Exception
end
proc {db.pool.hold {|c|}}.should raise_error(RuntimeError)
end
end
context "A database" do
before do
Sequel::Database.single_threaded = false
end
after do
Sequel::Database.single_threaded = false
end
specify "should have single_threaded? respond to true if in single threaded mode" do
db = Sequel::Database.new(:single_threaded => true)
db.should be_single_threaded
db = Sequel::Database.new(:max_options => 1)
db.should_not be_single_threaded
db = Sequel::Database.new
db.should_not be_single_threaded
Sequel::Database.single_threaded = true
db = Sequel::Database.new
db.should be_single_threaded
db = Sequel::Database.new(:max_options => 4)
db.should be_single_threaded
end
specify "should be able to set loggers via the logger= and loggers= methods" do
db = Sequel::Database.new
s = "I'm a logger"
db.logger = s
db.loggers.should == [s]
db.logger = nil
db.loggers.should == []
db.loggers = [s]
db.loggers.should == [s]