/
postgres_spec.rb
3819 lines (3354 loc) · 156 KB
/
postgres_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
SEQUEL_ADAPTER_TEST = :postgres
require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
def DB.sqls
(@sqls ||= [])
end
logger = Object.new
def logger.method_missing(m, msg)
DB.sqls << msg
end
DB.loggers << logger
DB.extension :pg_array, :pg_hstore, :pg_range, :pg_row, :pg_inet, :pg_json, :pg_enum
begin
DB.extension :pg_interval
rescue LoadError
end
describe "PostgreSQL", '#create_table' do
before do
@db = DB
@db.test_connection
DB.sqls.clear
end
after do
@db.drop_table?(:tmp_dolls, :unlogged_dolls)
end
it "should create a temporary table" do
@db.create_table(:tmp_dolls, :temp => true){text :name}
check_sqls do
@db.sqls.must_equal ['CREATE TEMPORARY TABLE "tmp_dolls" ("name" text)']
end
end
it "temporary table should support :on_commit option" do
@db.drop_table?(:some_table)
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :drop){text :name}
end
@db.table_exists?(:some_table).must_equal false
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :delete_rows){text :name}
@db[:some_table].insert('a')
end
@db.table_exists?(:some_table).must_equal true
@db[:some_table].empty?.must_equal true
@db.drop_table(:some_table)
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :preserve_rows){text :name}
@db[:some_table].insert('a')
end
@db.table_exists?(:some_table).must_equal true
@db[:some_table].count.must_equal 1
@db.drop_table(:some_table)
end
it "temporary table should accept :on_commit with :as option" do
@db.drop_table?(:some_table)
@db.transaction do
@db.create_table(:some_table, :temp => true, :on_commit => :drop, :as => 'select 1')
end
@db.table_exists?(:some_table).must_equal false
end
it ":on_commit should raise error if not used on a temporary table" do
proc{@db.create_table(:some_table, :on_commit => :drop)}.must_raise(Sequel::Error)
end
it ":on_commit should raise error if given unsupported value" do
proc{@db.create_table(:some_table, :temp => true, :on_commit => :unsupported){text :name}}.must_raise(Sequel::Error)
end
it "should create an unlogged table" do
@db.create_table(:unlogged_dolls, :unlogged => true){text :name}
check_sqls do
@db.sqls.must_equal ['CREATE UNLOGGED TABLE "unlogged_dolls" ("name" text)']
end
end
it "should create a table inheriting from another table" do
@db.create_table(:unlogged_dolls){text :name}
@db.create_table(:tmp_dolls, :inherits=>:unlogged_dolls){}
@db[:tmp_dolls].insert('a')
@db[:unlogged_dolls].all.must_equal [{:name=>'a'}]
end
it "should create a table inheriting from multiple tables" do
begin
@db.create_table(:unlogged_dolls){text :name}
@db.create_table(:tmp_dolls){text :bar}
@db.create_table!(:items, :inherits=>[:unlogged_dolls, :tmp_dolls]){text :foo}
@db[:items].insert(:name=>'a', :bar=>'b', :foo=>'c')
@db[:unlogged_dolls].all.must_equal [{:name=>'a'}]
@db[:tmp_dolls].all.must_equal [{:bar=>'b'}]
@db[:items].all.must_equal [{:name=>'a', :bar=>'b', :foo=>'c'}]
ensure
@db.drop_table?(:items)
end
end
it "should not allow to pass both :temp and :unlogged" do
proc do
@db.create_table(:temp_unlogged_dolls, :temp => true, :unlogged => true){text :name}
end.must_raise(Sequel::Error, "can't provide both :temp and :unlogged to create_table")
end
it "should support :if_exists option to drop_column" do
@db.create_table(:tmp_dolls){Integer :a; Integer :b}
2.times do
@db.drop_column :tmp_dolls, :b, :if_exists=>true
@db[:tmp_dolls].columns.must_equal [:a]
end
end if DB.server_version >= 90000
it "should support pg_loose_count extension" do
@db.extension :pg_loose_count
@db.create_table(:tmp_dolls){text :name}
@db.loose_count(:tmp_dolls).must_be_kind_of(Integer)
@db.loose_count(:tmp_dolls).must_equal 0
@db.loose_count(:public__tmp_dolls).must_equal 0
@db[:tmp_dolls].insert('a')
@db << 'VACUUM ANALYZE tmp_dolls'
@db.loose_count(:tmp_dolls).must_equal 1
@db.loose_count(:public__tmp_dolls).must_equal 1
end
end
describe "PostgreSQL views" do
before do
@db = DB
@db.drop_table?(:items, :cascade=>true)
@db.create_table(:items){Integer :number}
@db[:items].insert(10)
@db[:items].insert(20)
end
after do
@opts ||={}
@db.drop_view(:items_view, @opts.merge(:if_exists=>true, :cascade=>true)) rescue nil
@db.drop_table?(:items)
end
it "should support temporary views" do
@db.create_view(:items_view, @db[:items].where(:number=>10), :temp=>true)
@db[:items_view].map(:number).must_equal [10]
@db.create_or_replace_view(:items_view, @db[:items].where(:number=>20), :temp=>true)
@db[:items_view].map(:number).must_equal [20]
end
it "should support recursive views" do
@db.create_view(:items_view, @db[:items].where(:number=>10).union(@db[:items, :items_view].where(Sequel.-(:number, 5)=>:n).select(:number), :all=>true, :from_self=>false), :recursive=>[:n])
@db[:items_view].select_order_map(:n).must_equal [10]
@db[:items].insert(15)
@db[:items_view].select_order_map(:n).must_equal [10, 15, 20]
end if DB.server_version >= 90300
it "should support materialized views" do
@opts = {:materialized=>true}
@db.create_view(:items_view, @db[:items].where{number >= 10}, @opts)
@db[:items_view].select_order_map(:number).must_equal [10, 20]
@db[:items].insert(15)
@db[:items_view].select_order_map(:number).must_equal [10, 20]
@db.refresh_view(:items_view)
@db[:items_view].select_order_map(:number).must_equal [10, 15, 20]
end if DB.server_version >= 90300
it "should support refreshing materialized views concurrently" do
@opts = {:materialized=>true}
@db.create_view(:items_view, @db[:items].where{number >= 10}, @opts)
@db.refresh_view(:items_view)
proc{@db.refresh_view(:items_view, :concurrently=>true)}.must_raise(Sequel::DatabaseError)
@db.add_index :items_view, :number, :unique=>true
@db.refresh_view(:items_view, :concurrently=>true)
end if DB.server_version >= 90400
it "should support :if_exists=>true for not raising an error if the view does not exist" do
@db.drop_view(:items_view, :if_exists=>true)
end
end
describe "PostgreSQL", 'INSERT ON CONFLICT' do
before(:all) do
@db = DB
@db.create_table!(:ic_test){Integer :a; Integer :b; Integer :c; unique :a, :name=>:ic_test_a_uidx; unique [:b, :c], :name=>:ic_test_b_c_uidx}
@ds = @db[:ic_test]
end
before do
@ds.delete
end
after(:all) do
@db.drop_table?(:ic_test)
end
it "Dataset#insert_ignore and insert_conflict should ignore uniqueness violations" do
@ds.insert(1, 2, 3)
proc{@ds.insert(1, 3, 4)}.must_raise Sequel::UniqueConstraintViolation
@ds.insert_ignore.insert(1, 3, 4).must_equal nil
@ds.insert_conflict.insert(1, 3, 4).must_equal nil
@ds.insert_conflict(:target=>:a).insert(1, 3, 4).must_equal nil
@ds.insert_conflict(:constraint=>:ic_test_a_uidx).insert(1, 3, 4).must_equal nil
@ds.all.must_equal [{:a=>1, :b=>2, :c=>3}]
end
it "Dataset#insert_ignore and insert_conflict should work with multi_insert/import" do
@ds.insert(1, 2, 3)
@ds.insert_ignore.multi_insert([{:a=>1, :b=>3, :c=>4}])
@ds.insert_ignore.import([:a, :b, :c], [[1, 3, 4]])
@ds.all.must_equal [{:a=>1, :b=>2, :c=>3}]
@ds.insert_conflict(:target=>:a, :update=>{:b=>3}).import([:a, :b, :c], [[1, 3, 4]])
@ds.all.must_equal [{:a=>1, :b=>3, :c=>3}]
@ds.insert_conflict(:target=>:a, :update=>{:b=>4}).multi_insert([{:a=>1, :b=>5, :c=>6}])
@ds.all.must_equal [{:a=>1, :b=>4, :c=>3}]
end
it "Dataset#insert_conflict should handle upserts" do
@ds.insert(1, 2, 3)
@ds.insert_conflict(:target=>:a, :update=>{:b=>3}).insert(1, 3, 4).must_equal nil
@ds.all.must_equal [{:a=>1, :b=>3, :c=>3}]
@ds.insert_conflict(:target=>[:b, :c], :update=>{:c=>5}).insert(5, 3, 3).must_equal nil
@ds.all.must_equal [{:a=>1, :b=>3, :c=>5}]
@ds.insert_conflict(:constraint=>:ic_test_a_uidx, :update=>{:b=>4}).insert(1, 3).must_equal nil
@ds.all.must_equal [{:a=>1, :b=>4, :c=>5}]
@ds.insert_conflict(:constraint=>:ic_test_a_uidx, :update=>{:b=>5}, :update_where=>{:ic_test__b=>4}).insert(1, 3, 4).must_equal nil
@ds.all.must_equal [{:a=>1, :b=>5, :c=>5}]
@ds.insert_conflict(:constraint=>:ic_test_a_uidx, :update=>{:b=>6}, :update_where=>{:ic_test__b=>4}).insert(1, 3, 4).must_equal nil
@ds.all.must_equal [{:a=>1, :b=>5, :c=>5}]
end
it "Dataset#insert_conflict should respect expressions in the target argument" do
@ds.insert_conflict(:target=>:a).insert_sql(1, 2, 3).must_equal "INSERT INTO \"ic_test\" VALUES (1, 2, 3) ON CONFLICT (\"a\") DO NOTHING"
@ds.insert_conflict(:target=>[:b, :c]).insert_sql(1, 2, 3).must_equal "INSERT INTO \"ic_test\" VALUES (1, 2, 3) ON CONFLICT (\"b\", \"c\") DO NOTHING"
@ds.insert_conflict(:target=>[:b, Sequel.function(:round, :c)]).insert_sql(1, 2, 3).must_equal "INSERT INTO \"ic_test\" VALUES (1, 2, 3) ON CONFLICT (\"b\", round(\"c\")) DO NOTHING"
@ds.insert_conflict(:target=>[:b, Sequel.virtual_row{|o| o.round(:c)}]).insert_sql(1, 2, 3).must_equal "INSERT INTO \"ic_test\" VALUES (1, 2, 3) ON CONFLICT (\"b\", round(\"c\")) DO NOTHING"
end
end if DB.server_version >= 90500
describe "A PostgreSQL database" do
before(:all) do
@db = DB
@db.create_table!(:public__testfk){primary_key :id; foreign_key :i, :public__testfk}
end
after(:all) do
@db.drop_table?(:public__testfk)
end
it "should provide the server version" do
@db.server_version.must_be :>, 70000
end
it "should create a dataset using the VALUES clause via #values" do
@db.values([[1, 2], [3, 4]]).map([:column1, :column2]).must_equal [[1, 2], [3, 4]]
end
it "should support ordering in aggregate functions" do
@db.from(@db.values([['1'], ['2']]).as(:t, [:a])).get{string_agg(:a, '-').order(Sequel.desc(:a)).as(:c)}.must_equal '2-1'
end if DB.server_version >= 90000
it "should support ordering and limiting with #values" do
@db.values([[1, 2], [3, 4]]).reverse(:column2, :column1).limit(1).map([:column1, :column2]).must_equal [[3, 4]]
@db.values([[1, 2], [3, 4]]).reverse(:column2, :column1).offset(1).map([:column1, :column2]).must_equal [[1, 2]]
end
it "should support subqueries with #values" do
@db.values([[1, 2]]).from_self.cross_join(@db.values([[3, 4]]).as(:x, [:c1, :c2])).map([:column1, :column2, :c1, :c2]).must_equal [[1, 2, 3, 4]]
end
it "should respect the :read_only option per-savepoint" do
proc{@db.transaction{@db.transaction(:savepoint=>true, :read_only=>true){@db[:public__testfk].insert}}}.must_raise(Sequel::DatabaseError)
proc{@db.transaction(:auto_savepoint=>true, :read_only=>true){@db.transaction(:read_only=>false){@db[:public__testfk].insert}}}.must_raise(Sequel::DatabaseError)
@db[:public__testfk].delete
@db.transaction{@db[:public__testfk].insert; @db.transaction(:savepoint=>true, :read_only=>true){@db[:public__testfk].all;}}
@db.transaction{@db.transaction(:savepoint=>true, :read_only=>true){}; @db[:public__testfk].insert}
@db.transaction{@db[:public__testfk].all; @db.transaction(:savepoint=>true, :read_only=>true){@db[:public__testfk].all;}}
end
it "should support disable_insert_returning" do
ds = @db[:public__testfk].disable_insert_returning
ds.delete
ds.insert.must_equal nil
id = ds.max(:id)
ds.select_order_map([:id, :i]).must_equal [[id, nil]]
ds.insert(:i=>id).must_equal nil
ds.select_order_map([:id, :i]).must_equal [[id, nil], [id+1, id]]
ds.insert_select(:i=>ds.max(:id)).must_equal nil
ds.select_order_map([:id, :i]).must_equal [[id, nil], [id+1, id]]
c = Class.new(Sequel::Model(ds))
c.class_eval do
def before_create
self.id = model.max(:id)+1
super
end
end
c.create(:i=>id+1).must_equal c.load(:id=>id+2, :i=>id+1)
ds.select_order_map([:id, :i]).must_equal [[id, nil], [id+1, id], [id+2, id+1]]
ds.delete
end
it "should support functions with and without quoting" do
ds = @db[:public__testfk]
ds.delete
ds.insert
ds.get{sum(1)}.must_equal 1
ds.get{Sequel.function('pg_catalog.sum', 1)}.must_equal 1
ds.get{sum.function(1)}.must_equal 1
ds.get{pg_catalog__sum.function(1)}.must_equal 1
ds.delete
end
it "should support a :qualify option to tables and views" do
@db.tables(:qualify=>true).must_include(Sequel.qualify(:public, :testfk))
begin
@db.create_view(:testfkv, @db[:testfk])
@db.views(:qualify=>true).must_include(Sequel.qualify(:public, :testfkv))
ensure
@db.drop_view(:testfkv)
end
end
it "should not typecast the int2vector type incorrectly" do
@db.get(Sequel.cast('10 20', :int2vector)).wont_equal 10
end
cspecify "should not typecast the money type incorrectly", [:do] do
@db.get(Sequel.cast('10.01', :money)).wont_equal 0
end
it "should correctly parse the schema" do
@db.schema(:public__testfk, :reload=>true).map{|c,s| [c, s[:oid]]}.must_equal [[:id, 23], [:i, 23]]
end
it "should parse foreign keys for tables in a schema" do
@db.foreign_key_list(:public__testfk).must_equal [{:on_delete=>:no_action, :on_update=>:no_action, :columns=>[:i], :key=>[:id], :deferrable=>false, :table=>Sequel.qualify(:public, :testfk), :name=>:testfk_i_fkey}]
end
it "should return uuid fields as strings" do
@db.get(Sequel.cast('550e8400-e29b-41d4-a716-446655440000', :uuid)).must_equal '550e8400-e29b-41d4-a716-446655440000'
end
it "should handle inserts with placeholder literal string tables" do
ds = @db.from(Sequel.lit('?', :testfk))
ds.delete
ds.insert(:id=>1)
ds.select_map(:id).must_equal [1]
end
it "should have notice receiver receive notices" do
a = nil
Sequel.connect(DB.opts.merge(:notice_receiver=>proc{|r| a = r.result_error_message})){|db| db.do("BEGIN\nRAISE WARNING 'foo';\nEND;")}
a.must_equal "WARNING: foo\n"
end if DB.adapter_scheme == :postgres && SEQUEL_POSTGRES_USES_PG && DB.server_version >= 90000
# These only test the SQL created, because a true test using file_fdw or postgres_fdw
# requires superuser permissions, and you should not be running the tests as a superuser.
it "should support creating and dropping foreign tables" do
DB.send(:create_table_sql, :t, DB.create_table_generator{Integer :a}, :foreign=>:f, :options=>{:o=>1}).must_equal 'CREATE FOREIGN TABLE "t" ("a" integer) SERVER "f" OPTIONS (o \'1\')'
DB.send(:drop_table_sql, :t, :foreign=>true).must_equal 'DROP FOREIGN TABLE "t"'
end
end
describe "A PostgreSQL database with domain types" do
before(:all) do
@db = DB
@db << "DROP DOMAIN IF EXISTS positive_number CASCADE"
@db << "CREATE DOMAIN positive_number AS numeric(10,2) CHECK (VALUE > 0)"
@db.create_table!(:testfk){positive_number :id, :primary_key=>true}
end
after(:all) do
@db.drop_table?(:testfk)
@db << "DROP DOMAIN positive_number"
end
it "should correctly parse the schema" do
sch = @db.schema(:testfk, :reload=>true)
sch.first.last.delete(:domain_oid).must_be_kind_of(Integer)
sch.first.last[:db_domain_type].must_equal 'positive_number'
end
end
describe "A PostgreSQL dataset" do
before(:all) do
@db = DB
@d = @db[:test]
@db.create_table! :test do
text :name
integer :value, :index => true
end
end
before do
@d.delete
@db.sqls.clear
end
after do
@db.drop_table?(:atest)
end
after(:all) do
@db.drop_table?(:test)
end
it "should quote columns and tables using double quotes if quoting identifiers" do
check_sqls do
@d.select(:name).sql.must_equal 'SELECT "name" FROM "test"'
@d.select(Sequel.lit('COUNT(*)')).sql.must_equal 'SELECT COUNT(*) FROM "test"'
@d.select(Sequel.function(:max, :value)).sql.must_equal 'SELECT max("value") FROM "test"'
@d.select(Sequel.function(:NOW)).sql.must_equal 'SELECT NOW() FROM "test"'
@d.select(Sequel.function(:max, :items__value)).sql.must_equal 'SELECT max("items"."value") FROM "test"'
@d.order(Sequel.desc(:name)).sql.must_equal 'SELECT * FROM "test" ORDER BY "name" DESC'
@d.select(Sequel.lit('test.name AS item_name')).sql.must_equal 'SELECT test.name AS item_name FROM "test"'
@d.select(Sequel.lit('"name"')).sql.must_equal 'SELECT "name" FROM "test"'
@d.select(Sequel.lit('max(test."name") AS "max_name"')).sql.must_equal 'SELECT max(test."name") AS "max_name" FROM "test"'
@d.insert_sql(:x => :y).must_match(/\AINSERT INTO "test" \("x"\) VALUES \("y"\)( RETURNING NULL)?\z/)
@d.select(Sequel.function(:test, :abc, 'hello')).sql.must_equal "SELECT test(\"abc\", 'hello') FROM \"test\""
@d.select(Sequel.function(:test, :abc__def, 'hello')).sql.must_equal "SELECT test(\"abc\".\"def\", 'hello') FROM \"test\""
@d.select(Sequel.function(:test, :abc__def, 'hello').as(:x2)).sql.must_equal "SELECT test(\"abc\".\"def\", 'hello') AS \"x2\" FROM \"test\""
@d.insert_sql(:value => 333).must_match(/\AINSERT INTO "test" \("value"\) VALUES \(333\)( RETURNING NULL)?\z/)
end
end
it "should quote fields correctly when reversing the order if quoting identifiers" do
check_sqls do
@d.reverse_order(:name).sql.must_equal 'SELECT * FROM "test" ORDER BY "name" DESC'
@d.reverse_order(Sequel.desc(:name)).sql.must_equal 'SELECT * FROM "test" ORDER BY "name" ASC'
@d.reverse_order(:name, Sequel.desc(:test)).sql.must_equal 'SELECT * FROM "test" ORDER BY "name" DESC, "test" ASC'
@d.reverse_order(Sequel.desc(:name), :test).sql.must_equal 'SELECT * FROM "test" ORDER BY "name" ASC, "test" DESC'
end
end
it "should support regexps" do
@d << {:name => 'abc', :value => 1}
@d << {:name => 'bcd', :value => 2}
@d.filter(:name => /bc/).count.must_equal 2
@d.filter(:name => /^bc/).count.must_equal 1
end
it "should support NULLS FIRST and NULLS LAST" do
@d << {:name => 'abc'}
@d << {:name => 'bcd'}
@d << {:name => 'bcd', :value => 2}
@d.order(Sequel.asc(:value, :nulls=>:first), :name).select_map(:name).must_equal %w[abc bcd bcd]
@d.order(Sequel.asc(:value, :nulls=>:last), :name).select_map(:name).must_equal %w[bcd abc bcd]
@d.order(Sequel.asc(:value, :nulls=>:first), :name).reverse.select_map(:name).must_equal %w[bcd bcd abc]
end
it "should support selecting from LATERAL functions" do
@d.from{[generate_series(1,3,1).as(:a), pow(:a, 2).lateral.as(:b)]}.select_map([:a, :b])== [[1, 1], [2, 4], [3, 9]]
end if DB.server_version >= 90300
it "should support ordered-set and hypothetical-set aggregate functions" do
@d.from{generate_series(1,3,1).as(:a)}.select{(a.sql_number % 2).as(:a)}.from_self.get{mode{}.within_group(:a)}.must_equal 1
end if DB.server_version >= 90400
it "should support filtered aggregate functions" do
@d.from{generate_series(1,3,1).as(:a)}.select{(a.sql_number % 2).as(:a)}.from_self.get{count(:a).filter(:a=>1)}.must_equal 2
end if DB.server_version >= 90400
it "should support functions with ordinality" do
@d.from{generate_series(1,10,3).with_ordinality}.select_map([:generate_series, :ordinality]).must_equal [[1, 1], [4, 2], [7, 3], [10, 4]]
end if DB.server_version >= 90400
it "#lock should lock tables and yield if a block is given" do
@d.lock('EXCLUSIVE'){@d.insert(:name=>'a')}
end
it "should support exclusion constraints when creating or altering tables" do
@db.create_table!(:atest){Integer :t; exclude [[Sequel.desc(:t, :nulls=>:last), '=']], :using=>:btree, :where=>proc{t > 0}}
@db[:atest].insert(1)
@db[:atest].insert(2)
proc{@db[:atest].insert(2)}.must_raise(Sequel::Postgres::ExclusionConstraintViolation)
@db.create_table!(:atest){Integer :t}
@db.alter_table(:atest){add_exclusion_constraint [[:t, '=']], :using=>:btree, :name=>'atest_ex'}
@db[:atest].insert(1)
@db[:atest].insert(2)
proc{@db[:atest].insert(2)}.must_raise(Sequel::Postgres::ExclusionConstraintViolation)
@db.alter_table(:atest){drop_constraint 'atest_ex'}
end if DB.server_version >= 90000
it "should support deferrable exclusion constraints" do
@db.create_table!(:atest){Integer :t; exclude [[Sequel.desc(:t, :nulls=>:last), '=']], :using=>:btree, :where=>proc{t > 0}, :deferrable => true}
proc do
@db.transaction do
@db[:atest].insert(2)
@db[:atest].insert(2)
end
end.must_raise(Sequel::Postgres::ExclusionConstraintViolation)
end if DB.server_version >= 90000
it "should support Database#error_info for getting info hash on the given error" do
@db.create_table!(:atest){Integer :t; Integer :t2, :null=>false, :default=>1; constraint :f, :t=>0}
begin
@db[:atest].insert(1)
rescue => e
end
e.wont_equal nil
info = @db.error_info(e)
info[:schema].must_equal 'public'
info[:table].must_equal 'atest'
info[:constraint].must_equal 'f'
info[:column].must_equal nil
info[:type].must_equal nil
begin
@db[:atest].insert(0, nil)
rescue => e
end
e.wont_equal nil
info = @db.error_info(e.wrapped_exception)
info[:schema].must_equal 'public'
info[:table].must_equal 'atest'
info[:constraint].must_equal nil
info[:column].must_equal 't2'
info[:type].must_equal nil
end if DB.server_version >= 90300 && DB.adapter_scheme == :postgres && SEQUEL_POSTGRES_USES_PG && Object.const_defined?(:PG) && ::PG.const_defined?(:Constants) && ::PG::Constants.const_defined?(:PG_DIAG_SCHEMA_NAME)
it "should support Database#do for executing anonymous code blocks" do
@db.drop_table?(:btest)
@db.do "BEGIN EXECUTE 'CREATE TABLE btest (a INTEGER)'; EXECUTE 'INSERT INTO btest VALUES (1)'; END"
@db[:btest].select_map(:a).must_equal [1]
@db.do "BEGIN EXECUTE 'DROP TABLE btest; CREATE TABLE atest (a INTEGER)'; EXECUTE 'INSERT INTO atest VALUES (1)'; END", :language=>:plpgsql
@db[:atest].select_map(:a).must_equal [1]
end if DB.server_version >= 90000
it "should support adding foreign key constarints that are not yet valid, and validating them later" do
@db.create_table!(:atest){primary_key :id; Integer :fk}
@db[:atest].insert(1, 5)
@db.alter_table(:atest){add_foreign_key [:fk], :atest, :not_valid=>true, :name=>:atest_fk}
@db[:atest].insert(2, 1)
proc{@db[:atest].insert(3, 4)}.must_raise(Sequel::ForeignKeyConstraintViolation)
proc{@db.alter_table(:atest){validate_constraint :atest_fk}}.must_raise(Sequel::ForeignKeyConstraintViolation)
@db[:atest].where(:id=>1).update(:fk=>2)
@db.alter_table(:atest){validate_constraint :atest_fk}
@db.alter_table(:atest){validate_constraint :atest_fk}
end if DB.server_version >= 90200
it "should support adding check constarints that are not yet valid, and validating them later" do
@db.create_table!(:atest){Integer :a}
@db[:atest].insert(5)
@db.alter_table(:atest){add_constraint({:name=>:atest_check, :not_valid=>true}){a >= 10}}
@db[:atest].insert(10)
proc{@db[:atest].insert(6)}.must_raise(Sequel::CheckConstraintViolation)
proc{@db.alter_table(:atest){validate_constraint :atest_check}}.must_raise(Sequel::CheckConstraintViolation, Sequel::DatabaseError)
@db[:atest].where{a < 10}.update(:a=>Sequel.+(:a, 10))
@db.alter_table(:atest){validate_constraint :atest_check}
@db.alter_table(:atest){validate_constraint :atest_check}
end if DB.server_version >= 90200
it "should support :using when altering a column's type" do
@db.create_table!(:atest){Integer :t}
@db[:atest].insert(1262304000)
@db.alter_table(:atest){set_column_type :t, Time, :using=>Sequel.cast('epoch', Time) + Sequel.cast('1 second', :interval) * :t}
@db[:atest].get(Sequel.extract(:year, :t)).must_equal 2010
end
it "should support :using with a string when altering a column's type" do
@db.create_table!(:atest){Integer :t}
@db[:atest].insert(1262304000)
@db.alter_table(:atest){set_column_type :t, Time, :using=>"'epoch'::timestamp + '1 second'::interval * t"}
@db[:atest].get(Sequel.extract(:year, :t)).must_equal 2010
end
it "should be able to parse the default value for an interval type" do
@db.create_table!(:atest){interval :t, :default=>'1 week'}
@db.schema(:atest).first.last[:ruby_default].must_equal '7 days'
end
it "should have #transaction support various types of synchronous options" do
@db.transaction(:synchronous=>:on){}
@db.transaction(:synchronous=>true){}
@db.transaction(:synchronous=>:off){}
@db.transaction(:synchronous=>false){}
@db.sqls.grep(/synchronous/).must_equal ["SET LOCAL synchronous_commit = on", "SET LOCAL synchronous_commit = on", "SET LOCAL synchronous_commit = off", "SET LOCAL synchronous_commit = off"]
@db.sqls.clear
@db.transaction(:synchronous=>nil){}
check_sqls do
@db.sqls.must_equal ['BEGIN', 'COMMIT']
end
if @db.server_version >= 90100
@db.sqls.clear
@db.transaction(:synchronous=>:local){}
check_sqls do
@db.sqls.grep(/synchronous/).must_equal ["SET LOCAL synchronous_commit = local"]
end
if @db.server_version >= 90200
@db.sqls.clear
@db.transaction(:synchronous=>:remote_write){}
check_sqls do
@db.sqls.grep(/synchronous/).must_equal ["SET LOCAL synchronous_commit = remote_write"]
end
end
end
end
it "should have #transaction support read only transactions" do
@db.transaction(:read_only=>true){}
@db.transaction(:read_only=>false){}
@db.transaction(:isolation=>:serializable, :read_only=>true){}
@db.transaction(:isolation=>:serializable, :read_only=>false){}
@db.sqls.grep(/READ/).must_equal ["SET TRANSACTION READ ONLY", "SET TRANSACTION READ WRITE", "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY", "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE"]
end
it "should have #transaction support deferrable transactions" do
@db.transaction(:deferrable=>true){}
@db.transaction(:deferrable=>false){}
@db.transaction(:deferrable=>true, :read_only=>true){}
@db.transaction(:deferrable=>false, :read_only=>false){}
@db.transaction(:isolation=>:serializable, :deferrable=>true, :read_only=>true){}
@db.transaction(:isolation=>:serializable, :deferrable=>false, :read_only=>false){}
@db.sqls.grep(/DEF/).must_equal ["SET TRANSACTION DEFERRABLE", "SET TRANSACTION NOT DEFERRABLE", "SET TRANSACTION READ ONLY DEFERRABLE", "SET TRANSACTION READ WRITE NOT DEFERRABLE", "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE", "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE NOT DEFERRABLE"]
end if DB.server_version >= 90100
it "should support creating indexes concurrently" do
@db.add_index :test, [:name, :value], :concurrently=>true
check_sqls do
@db.sqls.must_equal ['CREATE INDEX CONCURRENTLY "test_name_value_index" ON "test" ("name", "value")']
end
end
it "should support dropping indexes only if they already exist" do
@db.add_index :test, [:name, :value], :name=>'tnv1'
@db.sqls.clear
@db.drop_index :test, [:name, :value], :if_exists=>true, :name=>'tnv1'
check_sqls do
@db.sqls.must_equal ['DROP INDEX IF EXISTS "tnv1"']
end
end
it "should support CASCADE when dropping indexes" do
@db.add_index :test, [:name, :value], :name=>'tnv2'
@db.sqls.clear
@db.drop_index :test, [:name, :value], :cascade=>true, :name=>'tnv2'
check_sqls do
@db.sqls.must_equal ['DROP INDEX "tnv2" CASCADE']
end
end
it "should support dropping indexes concurrently" do
@db.add_index :test, [:name, :value], :name=>'tnv2'
@db.sqls.clear
@db.drop_index :test, [:name, :value], :concurrently=>true, :name=>'tnv2'
check_sqls do
@db.sqls.must_equal ['DROP INDEX CONCURRENTLY "tnv2"']
end
end if DB.server_version >= 90200
it "#lock should lock table if inside a transaction" do
@db.transaction{@d.lock('EXCLUSIVE'); @d.insert(:name=>'a')}
end
it "#lock should return nil" do
@d.lock('EXCLUSIVE'){@d.insert(:name=>'a')}.must_equal nil
@db.transaction{@d.lock('EXCLUSIVE').must_equal nil; @d.insert(:name=>'a')}
end
it "should raise an error if attempting to update a joined dataset with a single FROM table" do
proc{@db[:test].join(:test, [:name]).update(:name=>'a')}.must_raise(Sequel::Error, 'Need multiple FROM tables if updating/deleting a dataset with JOINs')
end
it "should truncate with options" do
@d << { :name => 'abc', :value => 1}
@d.count.must_equal 1
@d.truncate(:cascade => true)
@d.count.must_equal 0
if @d.db.server_version > 80400
@d << { :name => 'abc', :value => 1}
@d.truncate(:cascade => true, :only=>true, :restart=>true)
@d.count.must_equal 0
end
end
it "should truncate multiple tables at once" do
tables = [:test, :test]
tables.each{|t| @d.from(t).insert}
@d.from(:test, :test).truncate
tables.each{|t| @d.from(t).count.must_equal 0}
end
end
describe "Dataset#distinct" do
before do
@db = DB
@db.create_table!(:a) do
Integer :a
Integer :b
end
@ds = @db[:a]
end
after do
@db.drop_table?(:a)
end
it "#distinct with arguments should return results distinct on those arguments" do
@ds.insert(20, 10)
@ds.insert(30, 10)
@ds.order(:b, :a).distinct.map(:a).must_equal [20, 30]
@ds.order(:b, Sequel.desc(:a)).distinct.map(:a).must_equal [30, 20]
@ds.order(:b, :a).distinct(:b).map(:a).must_equal [20]
@ds.order(:b, Sequel.desc(:a)).distinct(:b).map(:a).must_equal [30]
end
end
if DB.pool.respond_to?(:max_size) and DB.pool.max_size > 1
describe "Dataset#for_update support" do
before do
@db = DB.create_table!(:items) do
primary_key :id
Integer :number
String :name
end
@ds = DB[:items]
end
after do
DB.drop_table?(:items)
DB.disconnect
end
it "should handle FOR UPDATE" do
@ds.insert(:number=>20)
c, t = nil, nil
q = Queue.new
DB.transaction do
@ds.for_update.first(:id=>1)
t = Thread.new do
DB.transaction do
q.push nil
@ds.filter(:id=>1).update(:name=>'Jim')
c = @ds.first(:id=>1)
q.push nil
end
end
q.pop
@ds.filter(:id=>1).update(:number=>30)
end
q.pop
t.join
c.must_equal(:id=>1, :number=>30, :name=>'Jim')
end
it "should handle FOR SHARE" do
@ds.insert(:number=>20)
c, t = nil
q = Queue.new
DB.transaction do
@ds.for_share.first(:id=>1)
t = Thread.new do
DB.transaction do
c = @ds.for_share.filter(:id=>1).first
q.push nil
end
end
q.pop
@ds.filter(:id=>1).update(:name=>'Jim')
c.must_equal(:id=>1, :number=>20, :name=>nil)
end
t.join
end
end
end
describe "A PostgreSQL dataset with a timestamp field" do
before(:all) do
@db = DB
@db.create_table! :test3 do
Date :date
DateTime :time
end
@d = @db[:test3]
end
before do
@d.delete
end
after do
@db.convert_infinite_timestamps = false if @db.adapter_scheme == :postgres
end
after(:all) do
@db.drop_table?(:test3)
end
cspecify "should store milliseconds in time fields for Time objects", [:do], [:swift] do
t = Time.now
@d << {:time=>t}
t2 = @d.get(:time)
@d.literal(t2).must_equal @d.literal(t)
t2.strftime('%Y-%m-%d %H:%M:%S').must_equal t.strftime('%Y-%m-%d %H:%M:%S')
(t2.is_a?(Time) ? t2.usec : t2.strftime('%N').to_i/1000).must_equal t.usec
end
cspecify "should store milliseconds in time fields for DateTime objects", [:do], [:swift] do
t = DateTime.now
@d << {:time=>t}
t2 = @d.get(:time)
@d.literal(t2).must_equal @d.literal(t)
t2.strftime('%Y-%m-%d %H:%M:%S').must_equal t.strftime('%Y-%m-%d %H:%M:%S')
(t2.is_a?(Time) ? t2.usec : t2.strftime('%N').to_i/1000).must_equal t.strftime('%N').to_i/1000
end
if DB.adapter_scheme == :postgres
it "should handle infinite timestamps if convert_infinite_timestamps is set" do
@d << {:time=>Sequel.cast('infinity', DateTime)}
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:time).must_equal nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:time).must_equal 'infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:time).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 'nil'
@db[:test3].get(:time).must_equal nil
@db.convert_infinite_timestamps = 'string'
@db[:test3].get(:time).must_equal 'infinity'
@db.convert_infinite_timestamps = 'float'
@db[:test3].get(:time).must_equal 1.0/0.0
@db.convert_infinite_timestamps = 't'
@db[:test3].get(:time).must_equal 1.0/0.0
if ((Time.parse('infinity'); nil) rescue true)
# Skip for loose time parsing (e.g. old rbx)
@db.convert_infinite_timestamps = 'f'
proc{@db[:test3].get(:time)}.must_raise ArgumentError, Sequel::InvalidValue
@db.convert_infinite_timestamps = nil
proc{@db[:test3].get(:time)}.must_raise ArgumentError, Sequel::InvalidValue
@db.convert_infinite_timestamps = false
proc{@db[:test3].get(:time)}.must_raise ArgumentError, Sequel::InvalidValue
end
@d.update(:time=>Sequel.cast('-infinity', DateTime))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:time).must_equal nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:time).must_equal '-infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:time).must_equal(-1.0/0.0)
end
it "should handle conversions from infinite strings/floats in models" do
c = Class.new(Sequel::Model(:test3))
@db.convert_infinite_timestamps = :float
c.new(:time=>'infinity').time.must_equal 'infinity'
c.new(:time=>'-infinity').time.must_equal '-infinity'
c.new(:time=>1.0/0.0).time.must_equal 1.0/0.0
c.new(:time=>-1.0/0.0).time.must_equal(-1.0/0.0)
end
it "should handle infinite dates if convert_infinite_timestamps is set" do
@d << {:date=>Sequel.cast('infinity', Date)}
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:date).must_equal nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:date).must_equal 'infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:date).must_equal 1.0/0.0
@d.update(:date=>Sequel.cast('-infinity', :timestamp))
@db.convert_infinite_timestamps = :nil
@db[:test3].get(:date).must_equal nil
@db.convert_infinite_timestamps = :string
@db[:test3].get(:date).must_equal '-infinity'
@db.convert_infinite_timestamps = :float
@db[:test3].get(:date).must_equal(-1.0/0.0)
end
it "should handle conversions from infinite strings/floats in models" do
c = Class.new(Sequel::Model(:test3))
@db.convert_infinite_timestamps = :float
c.new(:date=>'infinity').date.must_equal 'infinity'
c.new(:date=>'-infinity').date.must_equal '-infinity'
c.new(:date=>1.0/0.0).date.must_equal 1.0/0.0
c.new(:date=>-1.0/0.0).date.must_equal(-1.0/0.0)
end
end
it "explain and analyze should not raise errors" do
@d = DB[:test3]
@d.explain
@d.analyze
end
it "#locks should be a dataset returning database locks " do
@db.locks.must_be_kind_of(Sequel::Dataset)
@db.locks.all.must_be_kind_of(Array)
end
end
describe "A PostgreSQL database" do
before do
@db = DB
@db.create_table! :test2 do
text :name
integer :value
end
end
after do
@db.drop_table?(:test2)
end
it "should support column operations" do
@db.create_table!(:test2){text :name; integer :value}
@db[:test2] << {}
@db[:test2].columns.must_equal [:name, :value]
@db.add_column :test2, :xyz, :text, :default => '000'
@db[:test2].columns.must_equal [:name, :value, :xyz]
@db[:test2] << {:name => 'mmm', :value => 111}
@db[:test2].first[:xyz].must_equal '000'
@db[:test2].columns.must_equal [:name, :value, :xyz]
@db.drop_column :test2, :xyz
@db[:test2].columns.must_equal [:name, :value]
@db[:test2].delete
@db.add_column :test2, :xyz, :text, :default => '000'
@db[:test2] << {:name => 'mmm', :value => 111, :xyz => 'qqqq'}
@db[:test2].columns.must_equal [:name, :value, :xyz]
@db.rename_column :test2, :xyz, :zyx
@db[:test2].columns.must_equal [:name, :value, :zyx]
@db[:test2].first[:zyx].must_equal 'qqqq'
@db.add_column :test2, :xyz, :float
@db[:test2].delete
@db[:test2] << {:name => 'mmm', :value => 111, :xyz => 56.78}
@db.set_column_type :test2, :xyz, :integer
@db[:test2].first[:xyz].must_equal 57
end
end
describe "A PostgreSQL database" do
before do
@db = DB
@db.drop_table?(:posts)
@db.sqls.clear
end
after do
@db.drop_table?(:posts)
end
it "should support resetting the primary key sequence" do
@db.create_table(:posts){primary_key :a}
@db[:posts].insert(:a=>20).must_equal 20
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
@db[:posts].insert(:a=>10).must_equal 10
@db.reset_primary_key_sequence(:posts).must_equal 21
@db[:posts].insert.must_equal 21
@db[:posts].order(:a).map(:a).must_equal [1, 2, 10, 20, 21]
end
it "should support specifying Integer/Bignum/Fixnum types in primary keys and have them be auto incrementing" do
@db.create_table(:posts){primary_key :a, :type=>Integer}
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
@db.create_table!(:posts){primary_key :a, :type=>Fixnum}
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
@db.create_table!(:posts){primary_key :a, :type=>:Bignum}
@db[:posts].insert.must_equal 1
@db[:posts].insert.must_equal 2
end
it "should not raise an error if attempting to resetting the primary key sequence for a table without a primary key" do
@db.create_table(:posts){Integer :a}
@db.reset_primary_key_sequence(:posts).must_equal nil
end
it "should support opclass specification" do
@db.create_table(:posts){text :title; text :body; integer :user_id; index(:user_id, :opclass => :int4_ops, :type => :btree)}
check_sqls do
@db.sqls.must_equal [
'CREATE TABLE "posts" ("title" text, "body" text, "user_id" integer)',
'CREATE INDEX "posts_user_id_index" ON "posts" USING btree ("user_id" int4_ops)'
]
end
end
it "should support fulltext indexes and searching" do
@db.create_table(:posts){text :title; text :body; full_text_index [:title, :body]; full_text_index :title, :language => 'french', :index_type=>:gist}
@db[:posts].insert(:title=>'ruby rails', :body=>'yowsa')
@db[:posts].insert(:title=>'sequel', :body=>'ruby')
@db[:posts].insert(:title=>'ruby scooby', :body=>'x')
@db[:posts].full_text_search(:title, 'rails').all.must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].full_text_search(:title, 'rails', :headline=>true).all.must_equal [{:title=>'ruby rails', :body=>'yowsa', :headline=>'ruby <b>rails</b>'}]
@db[:posts].full_text_search([:title, :body], ['yowsa', 'rails']).all.must_equal [:title=>'ruby rails', :body=>'yowsa']
@db[:posts].full_text_search(:title, 'scooby', :language => 'french').all.must_equal [{:title=>'ruby scooby', :body=>'x'}]
@db[:posts].full_text_search(:title, :$n).call(:select, :n=>'rails').must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].full_text_search(:title, :$n).prepare(:select, :fts_select).call(:n=>'rails').must_equal [{:title=>'ruby rails', :body=>'yowsa'}]
@db[:posts].insert(:title=>'jruby rubinius ruby maglev mri iron')
@db[:posts].insert(:title=>'ruby jruby maglev mri rubinius iron')
@db[:posts].full_text_search(:title, 'rubinius ruby', :phrase=>true).select_order_map(:title).must_equal ['jruby rubinius ruby maglev mri iron']