sequel 3.28.0 → 3.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +119 -3
- data/Rakefile +5 -3
- data/bin/sequel +1 -5
- data/doc/model_hooks.rdoc +9 -1
- data/doc/opening_databases.rdoc +49 -40
- data/doc/prepared_statements.rdoc +27 -6
- data/doc/release_notes/3.28.0.txt +2 -2
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/sharding.rdoc +7 -1
- data/doc/testing.rdoc +18 -9
- data/doc/transactions.rdoc +41 -1
- data/lib/sequel/adapters/ado.rb +28 -17
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/amalgalite.rb +11 -7
- data/lib/sequel/adapters/db2.rb +122 -70
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do.rb +5 -36
- data/lib/sequel/adapters/do/mysql.rb +0 -5
- data/lib/sequel/adapters/do/postgres.rb +0 -5
- data/lib/sequel/adapters/do/sqlite.rb +0 -5
- data/lib/sequel/adapters/firebird.rb +3 -6
- data/lib/sequel/adapters/ibmdb.rb +24 -16
- data/lib/sequel/adapters/informix.rb +2 -4
- data/lib/sequel/adapters/jdbc.rb +47 -11
- data/lib/sequel/adapters/jdbc/as400.rb +5 -24
- data/lib/sequel/adapters/jdbc/db2.rb +0 -5
- data/lib/sequel/adapters/jdbc/derby.rb +217 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +0 -5
- data/lib/sequel/adapters/jdbc/h2.rb +10 -12
- data/lib/sequel/adapters/jdbc/hsqldb.rb +166 -0
- data/lib/sequel/adapters/jdbc/informix.rb +0 -5
- data/lib/sequel/adapters/jdbc/jtds.rb +0 -5
- data/lib/sequel/adapters/jdbc/mysql.rb +0 -10
- data/lib/sequel/adapters/jdbc/oracle.rb +70 -3
- data/lib/sequel/adapters/jdbc/postgresql.rb +0 -11
- data/lib/sequel/adapters/jdbc/sqlite.rb +0 -5
- data/lib/sequel/adapters/jdbc/sqlserver.rb +0 -5
- data/lib/sequel/adapters/jdbc/transactions.rb +56 -7
- data/lib/sequel/adapters/mock.rb +315 -0
- data/lib/sequel/adapters/mysql.rb +64 -51
- data/lib/sequel/adapters/mysql2.rb +15 -9
- data/lib/sequel/adapters/odbc.rb +13 -6
- data/lib/sequel/adapters/odbc/db2.rb +0 -4
- data/lib/sequel/adapters/odbc/mssql.rb +0 -5
- data/lib/sequel/adapters/openbase.rb +2 -4
- data/lib/sequel/adapters/oracle.rb +333 -51
- data/lib/sequel/adapters/postgres.rb +80 -27
- data/lib/sequel/adapters/shared/access.rb +0 -6
- data/lib/sequel/adapters/shared/db2.rb +13 -15
- data/lib/sequel/adapters/shared/firebird.rb +6 -6
- data/lib/sequel/adapters/shared/mssql.rb +23 -18
- data/lib/sequel/adapters/shared/mysql.rb +6 -6
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +6 -0
- data/lib/sequel/adapters/shared/oracle.rb +185 -30
- data/lib/sequel/adapters/shared/postgres.rb +35 -18
- data/lib/sequel/adapters/shared/progress.rb +0 -6
- data/lib/sequel/adapters/shared/sqlite.rb +116 -37
- data/lib/sequel/adapters/sqlite.rb +16 -8
- data/lib/sequel/adapters/swift.rb +5 -5
- data/lib/sequel/adapters/swift/mysql.rb +0 -5
- data/lib/sequel/adapters/swift/postgres.rb +0 -5
- data/lib/sequel/adapters/swift/sqlite.rb +6 -4
- data/lib/sequel/adapters/tinytds.rb +13 -10
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +8 -0
- data/lib/sequel/core.rb +40 -0
- data/lib/sequel/database/connecting.rb +1 -2
- data/lib/sequel/database/dataset.rb +3 -3
- data/lib/sequel/database/dataset_defaults.rb +58 -0
- data/lib/sequel/database/misc.rb +62 -2
- data/lib/sequel/database/query.rb +113 -49
- data/lib/sequel/database/schema_methods.rb +7 -2
- data/lib/sequel/dataset/actions.rb +37 -19
- data/lib/sequel/dataset/features.rb +24 -0
- data/lib/sequel/dataset/graph.rb +7 -6
- data/lib/sequel/dataset/misc.rb +11 -3
- data/lib/sequel/dataset/mutation.rb +2 -3
- data/lib/sequel/dataset/prepared_statements.rb +6 -4
- data/lib/sequel/dataset/query.rb +46 -15
- data/lib/sequel/dataset/sql.rb +28 -4
- data/lib/sequel/extensions/named_timezones.rb +5 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
- data/lib/sequel/model.rb +2 -1
- data/lib/sequel/model/associations.rb +115 -33
- data/lib/sequel/model/base.rb +91 -31
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/identity_map.rb +1 -1
- data/lib/sequel/plugins/many_through_many.rb +6 -10
- data/lib/sequel/plugins/prepared_statements.rb +12 -1
- data/lib/sequel/plugins/prepared_statements_associations.rb +1 -1
- data/lib/sequel/plugins/rcte_tree.rb +29 -15
- data/lib/sequel/plugins/serialization.rb +6 -1
- data/lib/sequel/plugins/sharding.rb +0 -5
- data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +1 -1
- data/lib/sequel/timezones.rb +42 -42
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/mssql_spec.rb +29 -29
- data/spec/adapters/mysql_spec.rb +86 -104
- data/spec/adapters/oracle_spec.rb +48 -76
- data/spec/adapters/postgres_spec.rb +98 -33
- data/spec/adapters/spec_helper.rb +0 -5
- data/spec/adapters/sqlite_spec.rb +24 -21
- data/spec/core/connection_pool_spec.rb +9 -15
- data/spec/core/core_sql_spec.rb +20 -31
- data/spec/core/database_spec.rb +491 -227
- data/spec/core/dataset_spec.rb +638 -1051
- data/spec/core/expression_filters_spec.rb +0 -1
- data/spec/core/mock_adapter_spec.rb +378 -0
- data/spec/core/object_graph_spec.rb +48 -114
- data/spec/core/schema_generator_spec.rb +3 -3
- data/spec/core/schema_spec.rb +51 -114
- data/spec/core/spec_helper.rb +3 -90
- data/spec/extensions/class_table_inheritance_spec.rb +1 -1
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/instance_hooks_spec.rb +71 -0
- data/spec/extensions/named_timezones_spec.rb +22 -2
- data/spec/extensions/nested_attributes_spec.rb +3 -0
- data/spec/extensions/schema_spec.rb +1 -1
- data/spec/extensions/serialization_modification_detection_spec.rb +1 -0
- data/spec/extensions/serialization_spec.rb +5 -8
- data/spec/extensions/spec_helper.rb +4 -0
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/typecast_on_load_spec.rb +1 -6
- data/spec/integration/associations_test.rb +123 -12
- data/spec/integration/dataset_test.rb +140 -47
- data/spec/integration/eager_loader_test.rb +19 -21
- data/spec/integration/model_test.rb +80 -1
- data/spec/integration/plugin_test.rb +179 -128
- data/spec/integration/prepared_statement_test.rb +92 -91
- data/spec/integration/schema_test.rb +42 -23
- data/spec/integration/spec_helper.rb +25 -31
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +161 -34
- data/spec/integration/type_test.rb +3 -3
- data/spec/model/association_reflection_spec.rb +83 -7
- data/spec/model/associations_spec.rb +393 -676
- data/spec/model/base_spec.rb +186 -116
- data/spec/model/dataset_methods_spec.rb +7 -27
- data/spec/model/eager_loading_spec.rb +343 -867
- data/spec/model/hooks_spec.rb +160 -79
- data/spec/model/model_spec.rb +118 -165
- data/spec/model/plugins_spec.rb +7 -13
- data/spec/model/record_spec.rb +138 -207
- data/spec/model/spec_helper.rb +10 -73
- metadata +14 -8
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
|
|
2
2
|
|
|
3
3
|
describe "Eagerly loading a tree structure" do
|
|
4
|
-
before do
|
|
4
|
+
before(:all) do
|
|
5
5
|
INTEGRATION_DB.instance_variable_set(:@schemas, {})
|
|
6
6
|
INTEGRATION_DB.create_table!(:nodes) do
|
|
7
7
|
primary_key :id
|
|
@@ -66,9 +66,8 @@ describe "Eagerly loading a tree structure" do
|
|
|
66
66
|
Node.insert(:parent_id=>4)
|
|
67
67
|
Node.insert(:parent_id=>5)
|
|
68
68
|
Node.insert(:parent_id=>6)
|
|
69
|
-
clear_sqls
|
|
70
69
|
end
|
|
71
|
-
after do
|
|
70
|
+
after(:all) do
|
|
72
71
|
INTEGRATION_DB.drop_table :nodes
|
|
73
72
|
Object.send(:remove_const, :Node)
|
|
74
73
|
end
|
|
@@ -145,7 +144,6 @@ describe "Association Extensions" do
|
|
|
145
144
|
many_to_one :author
|
|
146
145
|
end
|
|
147
146
|
@author = Author.create
|
|
148
|
-
clear_sqls
|
|
149
147
|
end
|
|
150
148
|
after do
|
|
151
149
|
INTEGRATION_DB.drop_table :authorships, :authors
|
|
@@ -172,7 +170,7 @@ describe "Association Extensions" do
|
|
|
172
170
|
end
|
|
173
171
|
|
|
174
172
|
describe "has_many :through has_many and has_one :through belongs_to" do
|
|
175
|
-
before do
|
|
173
|
+
before(:all) do
|
|
176
174
|
INTEGRATION_DB.instance_variable_set(:@schemas, {})
|
|
177
175
|
INTEGRATION_DB.create_table!(:firms) do
|
|
178
176
|
primary_key :id
|
|
@@ -251,9 +249,8 @@ describe "has_many :through has_many and has_one :through belongs_to" do
|
|
|
251
249
|
@invoice3 = Invoice.create(:client => @client2)
|
|
252
250
|
@invoice4 = Invoice.create(:client => @client3)
|
|
253
251
|
@invoice5 = Invoice.create(:client => @client3)
|
|
254
|
-
clear_sqls
|
|
255
252
|
end
|
|
256
|
-
after do
|
|
253
|
+
after(:all) do
|
|
257
254
|
INTEGRATION_DB.drop_table :invoices, :clients, :firms
|
|
258
255
|
Object.send(:remove_const, :Firm)
|
|
259
256
|
Object.send(:remove_const, :Client)
|
|
@@ -324,7 +321,7 @@ describe "has_many :through has_many and has_one :through belongs_to" do
|
|
|
324
321
|
end
|
|
325
322
|
|
|
326
323
|
describe "Polymorphic Associations" do
|
|
327
|
-
before do
|
|
324
|
+
before(:all) do
|
|
328
325
|
INTEGRATION_DB.instance_variable_set(:@schemas, {})
|
|
329
326
|
INTEGRATION_DB.create_table!(:assets) do
|
|
330
327
|
primary_key :id
|
|
@@ -413,6 +410,9 @@ describe "Polymorphic Associations" do
|
|
|
413
410
|
.update(:attachable_id=>nil, :attachable_type=>nil)
|
|
414
411
|
end
|
|
415
412
|
end
|
|
413
|
+
end
|
|
414
|
+
before do
|
|
415
|
+
[:assets, :posts, :notes].each{|t| INTEGRATION_DB[t].delete}
|
|
416
416
|
@post = Post.create
|
|
417
417
|
Note.create
|
|
418
418
|
@note = Note.create
|
|
@@ -420,9 +420,8 @@ describe "Polymorphic Associations" do
|
|
|
420
420
|
@asset2 = Asset.create(:attachable=>@note)
|
|
421
421
|
@asset1.associations.clear
|
|
422
422
|
@asset2.associations.clear
|
|
423
|
-
clear_sqls
|
|
424
423
|
end
|
|
425
|
-
after do
|
|
424
|
+
after(:all) do
|
|
426
425
|
INTEGRATION_DB.drop_table :assets, :posts, :notes
|
|
427
426
|
Object.send(:remove_const, :Asset)
|
|
428
427
|
Object.send(:remove_const, :Post)
|
|
@@ -483,7 +482,7 @@ describe "Polymorphic Associations" do
|
|
|
483
482
|
end
|
|
484
483
|
|
|
485
484
|
describe "many_to_one/one_to_many not referencing primary key" do
|
|
486
|
-
before do
|
|
485
|
+
before(:all) do
|
|
487
486
|
INTEGRATION_DB.instance_variable_set(:@schemas, {})
|
|
488
487
|
INTEGRATION_DB.create_table!(:clients) do
|
|
489
488
|
primary_key :id
|
|
@@ -540,14 +539,16 @@ describe "many_to_one/one_to_many not referencing primary key" do
|
|
|
540
539
|
self.client_name = (client.name if client)
|
|
541
540
|
end
|
|
542
541
|
end
|
|
543
|
-
|
|
542
|
+
end
|
|
543
|
+
before do
|
|
544
|
+
Client.delete
|
|
545
|
+
Invoice.delete
|
|
544
546
|
@client1 = Client.create(:name=>'X')
|
|
545
547
|
@client2 = Client.create(:name=>'Y')
|
|
546
548
|
@invoice1 = Invoice.create(:client_name=>'X')
|
|
547
549
|
@invoice2 = Invoice.create(:client_name=>'X')
|
|
548
|
-
clear_sqls
|
|
549
550
|
end
|
|
550
|
-
after do
|
|
551
|
+
after(:all) do
|
|
551
552
|
INTEGRATION_DB.drop_table :invoices, :clients
|
|
552
553
|
Object.send(:remove_const, :Client)
|
|
553
554
|
Object.send(:remove_const, :Invoice)
|
|
@@ -618,7 +619,7 @@ describe "many_to_one/one_to_many not referencing primary key" do
|
|
|
618
619
|
end
|
|
619
620
|
|
|
620
621
|
describe "statistics associations" do
|
|
621
|
-
before do
|
|
622
|
+
before(:all) do
|
|
622
623
|
INTEGRATION_DB.create_table!(:projects) do
|
|
623
624
|
primary_key :id
|
|
624
625
|
String :name
|
|
@@ -658,9 +659,8 @@ describe "statistics associations" do
|
|
|
658
659
|
@ticket2 = Ticket.create(:project=>@project1, :hours=>10)
|
|
659
660
|
@ticket3 = Ticket.create(:project=>@project2, :hours=>2)
|
|
660
661
|
@ticket4 = Ticket.create(:project=>@project2, :hours=>20)
|
|
661
|
-
clear_sqls
|
|
662
662
|
end
|
|
663
|
-
after do
|
|
663
|
+
after(:all) do
|
|
664
664
|
INTEGRATION_DB.drop_table :tickets, :projects
|
|
665
665
|
Object.send(:remove_const, :Project)
|
|
666
666
|
Object.send(:remove_const, :Ticket)
|
|
@@ -679,7 +679,7 @@ describe "statistics associations" do
|
|
|
679
679
|
end
|
|
680
680
|
|
|
681
681
|
describe "one to one associations" do
|
|
682
|
-
before do
|
|
682
|
+
before(:all) do
|
|
683
683
|
INTEGRATION_DB.create_table!(:books) do
|
|
684
684
|
primary_key :id
|
|
685
685
|
end
|
|
@@ -703,10 +703,8 @@ describe "one to one associations" do
|
|
|
703
703
|
@page2 = Page.create(:book=>@book1, :page_number=>2)
|
|
704
704
|
@page3 = Page.create(:book=>@book2, :page_number=>1)
|
|
705
705
|
@page4 = Page.create(:book=>@book2, :page_number=>2)
|
|
706
|
-
clear_sqls
|
|
707
706
|
end
|
|
708
|
-
|
|
709
|
-
after do
|
|
707
|
+
after(:all) do
|
|
710
708
|
INTEGRATION_DB.drop_table :pages, :books
|
|
711
709
|
Object.send(:remove_const, :Book)
|
|
712
710
|
Object.send(:remove_const, :Page)
|
|
@@ -3,7 +3,7 @@ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
|
|
|
3
3
|
describe "Sequel::Model basic support" do
|
|
4
4
|
before do
|
|
5
5
|
@db = INTEGRATION_DB
|
|
6
|
-
@db.create_table!(:items) do
|
|
6
|
+
@db.create_table!(:items, :engine=>:InnoDB) do
|
|
7
7
|
primary_key :id
|
|
8
8
|
String :name
|
|
9
9
|
end
|
|
@@ -38,6 +38,14 @@ describe "Sequel::Model basic support" do
|
|
|
38
38
|
Item::Thing.first.should == Item::Thing.load(:id=>1, :name=>'J')
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
specify "should create accessors for all table columns even if all dataset columns aren't selected" do
|
|
42
|
+
c = Class.new(Sequel::Model(@db[:items].select(:id)))
|
|
43
|
+
o = c.new
|
|
44
|
+
o.name = 'A'
|
|
45
|
+
o.save.should == c.load(:id=>1)
|
|
46
|
+
c.select_map(:name).should == ['A']
|
|
47
|
+
end
|
|
48
|
+
|
|
41
49
|
specify "should work correctly when a dataset restricts the colums it selects" do
|
|
42
50
|
class ::Item::Thing < Sequel::Model(@db[:items].select(:name))
|
|
43
51
|
end
|
|
@@ -52,6 +60,68 @@ describe "Sequel::Model basic support" do
|
|
|
52
60
|
Item.count.should == 0
|
|
53
61
|
end
|
|
54
62
|
|
|
63
|
+
specify "#save should return nil if raise_on_save_failure is false and save isn't successful" do
|
|
64
|
+
i = Item.new(:name=>'J')
|
|
65
|
+
i.use_transactions = true
|
|
66
|
+
def i.after_save
|
|
67
|
+
raise Sequel::Rollback
|
|
68
|
+
end
|
|
69
|
+
i.save.should be_nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
specify "#should respect after_commit, after_rollback, after_destroy_commit, and after_destroy_rollback hooks" do
|
|
73
|
+
i = Item.create(:name=>'J')
|
|
74
|
+
i.use_transactions = true
|
|
75
|
+
def i.hooks
|
|
76
|
+
@hooks
|
|
77
|
+
end
|
|
78
|
+
def i.rb=(x)
|
|
79
|
+
@hooks = []
|
|
80
|
+
@rb = x
|
|
81
|
+
end
|
|
82
|
+
def i.after_save
|
|
83
|
+
@hooks << :as
|
|
84
|
+
raise Sequel::Rollback if @rb
|
|
85
|
+
end
|
|
86
|
+
def i.after_destroy
|
|
87
|
+
@hooks << :ad
|
|
88
|
+
raise Sequel::Rollback if @rb
|
|
89
|
+
end
|
|
90
|
+
def i.after_commit
|
|
91
|
+
@hooks << :ac
|
|
92
|
+
end
|
|
93
|
+
def i.after_rollback
|
|
94
|
+
@hooks << :ar
|
|
95
|
+
end
|
|
96
|
+
def i.after_destroy_commit
|
|
97
|
+
@hooks << :adc
|
|
98
|
+
end
|
|
99
|
+
def i.after_destroy_rollback
|
|
100
|
+
@hooks << :adr
|
|
101
|
+
end
|
|
102
|
+
i.name = 'K'
|
|
103
|
+
i.rb = true
|
|
104
|
+
i.save.should be_nil
|
|
105
|
+
i.reload.name.should == 'J'
|
|
106
|
+
i.hooks.should == [:as, :ar]
|
|
107
|
+
|
|
108
|
+
i.rb = true
|
|
109
|
+
i.destroy.should be_nil
|
|
110
|
+
i.exists?.should be_true
|
|
111
|
+
i.hooks.should == [:ad, :adr]
|
|
112
|
+
|
|
113
|
+
i.name = 'K'
|
|
114
|
+
i.rb = false
|
|
115
|
+
i.save.should_not be_nil
|
|
116
|
+
i.reload.name.should == 'K'
|
|
117
|
+
i.hooks.should == [:as, :ac]
|
|
118
|
+
|
|
119
|
+
i.rb = false
|
|
120
|
+
i.destroy.should_not be_nil
|
|
121
|
+
i.exists?.should be_false
|
|
122
|
+
i.hooks.should == [:ad, :adc]
|
|
123
|
+
end
|
|
124
|
+
|
|
55
125
|
specify "#exists? should return whether the item is still in the database" do
|
|
56
126
|
i = Item.create(:name=>'J')
|
|
57
127
|
i.exists?.should == true
|
|
@@ -128,3 +198,12 @@ describe "Sequel::Model basic support" do
|
|
|
128
198
|
end
|
|
129
199
|
end
|
|
130
200
|
end
|
|
201
|
+
|
|
202
|
+
describe "Sequel::Model with no existing table" do
|
|
203
|
+
specify "should not raise an error when setting the dataset" do
|
|
204
|
+
db = INTEGRATION_DB
|
|
205
|
+
db.drop_table(:items) if db.table_exists?(:items)
|
|
206
|
+
proc{class ::Item < Sequel::Model(db); end}.should_not raise_error
|
|
207
|
+
proc{c = Class.new(Sequel::Model); c.set_dataset(db[:items])}.should_not raise_error
|
|
208
|
+
end
|
|
209
|
+
end
|
|
@@ -3,28 +3,32 @@ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper.rb')
|
|
|
3
3
|
# H2 and MSSQL don't support USING joins
|
|
4
4
|
# DB2 does not seem to support USING joins in every version; it seems to be
|
|
5
5
|
# valid expression in DB2 iSeries UDB though.
|
|
6
|
-
unless
|
|
6
|
+
unless Sequel.guarded?(:h2, :mssql, :db2)
|
|
7
7
|
describe "Class Table Inheritance Plugin" do
|
|
8
|
-
before do
|
|
8
|
+
before(:all) do
|
|
9
9
|
@db = INTEGRATION_DB
|
|
10
10
|
@db.instance_variable_set(:@schemas, {})
|
|
11
|
-
@db.
|
|
11
|
+
[:staff, :executives, :managers, :employees].each{|t| @db.drop_table(t) if @db.table_exists?(t)}
|
|
12
|
+
@db.create_table(:employees) do
|
|
12
13
|
primary_key :id
|
|
13
14
|
String :name
|
|
14
15
|
String :kind
|
|
15
16
|
end
|
|
16
|
-
@db.create_table
|
|
17
|
+
@db.create_table(:managers) do
|
|
17
18
|
foreign_key :id, :employees, :primary_key=>true
|
|
18
19
|
Integer :num_staff
|
|
19
20
|
end
|
|
20
|
-
@db.create_table
|
|
21
|
+
@db.create_table(:executives) do
|
|
21
22
|
foreign_key :id, :managers, :primary_key=>true
|
|
22
23
|
Integer :num_managers
|
|
23
24
|
end
|
|
24
|
-
@db.create_table
|
|
25
|
+
@db.create_table(:staff) do
|
|
25
26
|
foreign_key :id, :employees, :primary_key=>true
|
|
26
27
|
foreign_key :manager_id, :managers
|
|
27
28
|
end
|
|
29
|
+
end
|
|
30
|
+
before do
|
|
31
|
+
[:staff, :executives, :managers, :employees].each{|t| @db[t].delete}
|
|
28
32
|
class ::Employee < Sequel::Model(@db)
|
|
29
33
|
plugin :class_table_inheritance, :key=>:kind, :table_map=>{:Staff=>:staff}
|
|
30
34
|
end
|
|
@@ -34,7 +38,7 @@ describe "Class Table Inheritance Plugin" do
|
|
|
34
38
|
class ::Executive < Manager
|
|
35
39
|
end
|
|
36
40
|
class ::Staff < Employee
|
|
37
|
-
many_to_one :manager
|
|
41
|
+
many_to_one :manager, :qualify=>false
|
|
38
42
|
end
|
|
39
43
|
|
|
40
44
|
@i1 =@db[:employees].insert(:name=>'E', :kind=>'Employee')
|
|
@@ -45,13 +49,13 @@ describe "Class Table Inheritance Plugin" do
|
|
|
45
49
|
@db[:managers].insert(:id=>@i4, :num_staff=>5)
|
|
46
50
|
@db[:executives].insert(:id=>@i4, :num_managers=>6)
|
|
47
51
|
@db[:staff].insert(:id=>@i2, :manager_id=>@i4)
|
|
48
|
-
|
|
49
|
-
clear_sqls
|
|
50
52
|
end
|
|
51
53
|
after do
|
|
52
|
-
@db.drop_table :staff, :executives, :managers, :employees
|
|
53
54
|
[:Executive, :Manager, :Staff, :Employee].each{|s| Object.send(:remove_const, s)}
|
|
54
55
|
end
|
|
56
|
+
after(:all) do
|
|
57
|
+
@db.drop_table :staff, :executives, :managers, :employees
|
|
58
|
+
end
|
|
55
59
|
|
|
56
60
|
specify "should return rows as subclass instances" do
|
|
57
61
|
Employee.order(:id).all.should == [
|
|
@@ -122,7 +126,7 @@ describe "Class Table Inheritance Plugin" do
|
|
|
122
126
|
Staff.limit(1).eager(:manager).all.map{|x| x.manager}.should == [Manager[@i4]]
|
|
123
127
|
end
|
|
124
128
|
|
|
125
|
-
|
|
129
|
+
specify "should handle eagerly graphing many_to_one relationships" do
|
|
126
130
|
ss = Staff.eager_graph(:manager).all
|
|
127
131
|
ss.should == [Staff[@i2]]
|
|
128
132
|
ss.map{|x| x.manager}.should == [Manager[@i4]]
|
|
@@ -145,21 +149,25 @@ end
|
|
|
145
149
|
end
|
|
146
150
|
|
|
147
151
|
describe "Many Through Many Plugin" do
|
|
148
|
-
before do
|
|
152
|
+
before(:all) do
|
|
149
153
|
@db = INTEGRATION_DB
|
|
150
154
|
@db.instance_variable_set(:@schemas, {})
|
|
151
|
-
@db.
|
|
155
|
+
[:albums_artists, :albums, :artists].each{|t| @db.drop_table(t) if @db.table_exists?(t)}
|
|
156
|
+
@db.create_table(:albums) do
|
|
152
157
|
primary_key :id
|
|
153
158
|
String :name
|
|
154
159
|
end
|
|
155
|
-
@db.create_table
|
|
160
|
+
@db.create_table(:artists) do
|
|
156
161
|
primary_key :id
|
|
157
162
|
String :name
|
|
158
163
|
end
|
|
159
|
-
@db.create_table
|
|
164
|
+
@db.create_table(:albums_artists) do
|
|
160
165
|
foreign_key :album_id, :albums
|
|
161
166
|
foreign_key :artist_id, :artists
|
|
162
167
|
end
|
|
168
|
+
end
|
|
169
|
+
before do
|
|
170
|
+
[:albums_artists, :albums, :artists].each{|t| @db[t].delete}
|
|
163
171
|
class ::Album < Sequel::Model(@db)
|
|
164
172
|
many_to_many :artists
|
|
165
173
|
end
|
|
@@ -183,26 +191,26 @@ describe "Many Through Many Plugin" do
|
|
|
183
191
|
@album4 = Album.create(:name=>'D')
|
|
184
192
|
@album4.add_artist(@artist1)
|
|
185
193
|
@album4.add_artist(@artist4)
|
|
186
|
-
|
|
187
|
-
clear_sqls
|
|
188
194
|
end
|
|
189
195
|
after do
|
|
190
|
-
@db.drop_table :albums_artists, :albums, :artists
|
|
191
196
|
[:Album, :Artist].each{|s| Object.send(:remove_const, s)}
|
|
192
197
|
end
|
|
198
|
+
after(:all) do
|
|
199
|
+
@db.drop_table :albums_artists, :albums, :artists
|
|
200
|
+
end
|
|
193
201
|
|
|
194
202
|
specify "should handle super simple case with 1 join table" do
|
|
195
203
|
Artist.many_through_many :albums, [[:albums_artists, :artist_id, :album_id]]
|
|
196
|
-
Artist[
|
|
197
|
-
Artist[
|
|
198
|
-
Artist[
|
|
199
|
-
Artist[
|
|
204
|
+
Artist[@artist1.id].albums.map{|x| x.name}.sort.should == %w'A D'
|
|
205
|
+
Artist[@artist2.id].albums.map{|x| x.name}.sort.should == %w'A C'
|
|
206
|
+
Artist[@artist3.id].albums.map{|x| x.name}.sort.should == %w'B C'
|
|
207
|
+
Artist[@artist4.id].albums.map{|x| x.name}.sort.should == %w'B D'
|
|
200
208
|
|
|
201
209
|
Artist.plugin :prepared_statements_associations
|
|
202
|
-
Artist[
|
|
203
|
-
Artist[
|
|
204
|
-
Artist[
|
|
205
|
-
Artist[
|
|
210
|
+
Artist[@artist1.id].albums.map{|x| x.name}.sort.should == %w'A D'
|
|
211
|
+
Artist[@artist2.id].albums.map{|x| x.name}.sort.should == %w'A C'
|
|
212
|
+
Artist[@artist3.id].albums.map{|x| x.name}.sort.should == %w'B C'
|
|
213
|
+
Artist[@artist4.id].albums.map{|x| x.name}.sort.should == %w'B D'
|
|
206
214
|
|
|
207
215
|
Artist.filter(:id=>1).eager(:albums).all.map{|x| x.albums.map{|a| a.name}}.flatten.sort.should == %w'A D'
|
|
208
216
|
Artist.filter(:id=>2).eager(:albums).all.map{|x| x.albums.map{|a| a.name}}.flatten.sort.should == %w'A C'
|
|
@@ -236,26 +244,26 @@ describe "Many Through Many Plugin" do
|
|
|
236
244
|
|
|
237
245
|
specify "should handle typical case with 3 join tables" do
|
|
238
246
|
Artist.many_through_many :related_artists, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id]], :class=>Artist, :distinct=>true
|
|
239
|
-
Artist[
|
|
240
|
-
Artist[
|
|
241
|
-
Artist[
|
|
242
|
-
Artist[
|
|
247
|
+
Artist[@artist1.id].related_artists.map{|x| x.name}.sort.should == %w'1 2 4'
|
|
248
|
+
Artist[@artist2.id].related_artists.map{|x| x.name}.sort.should == %w'1 2 3'
|
|
249
|
+
Artist[@artist3.id].related_artists.map{|x| x.name}.sort.should == %w'2 3 4'
|
|
250
|
+
Artist[@artist4.id].related_artists.map{|x| x.name}.sort.should == %w'1 3 4'
|
|
243
251
|
|
|
244
252
|
Artist.plugin :prepared_statements_associations
|
|
245
|
-
Artist[
|
|
246
|
-
Artist[
|
|
247
|
-
Artist[
|
|
248
|
-
Artist[
|
|
253
|
+
Artist[@artist1.id].related_artists.map{|x| x.name}.sort.should == %w'1 2 4'
|
|
254
|
+
Artist[@artist2.id].related_artists.map{|x| x.name}.sort.should == %w'1 2 3'
|
|
255
|
+
Artist[@artist3.id].related_artists.map{|x| x.name}.sort.should == %w'2 3 4'
|
|
256
|
+
Artist[@artist4.id].related_artists.map{|x| x.name}.sort.should == %w'1 3 4'
|
|
249
257
|
|
|
250
|
-
Artist.filter(:id
|
|
251
|
-
Artist.filter(:id
|
|
252
|
-
Artist.filter(:id
|
|
253
|
-
Artist.filter(:id
|
|
258
|
+
Artist.filter(:id=>@artist1.id).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 2 4'
|
|
259
|
+
Artist.filter(:id=>@artist2.id).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 2 3'
|
|
260
|
+
Artist.filter(:id=>@artist3.id).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'2 3 4'
|
|
261
|
+
Artist.filter(:id=>@artist4.id).eager(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 3 4'
|
|
254
262
|
|
|
255
|
-
Artist.filter(:artists__id
|
|
256
|
-
Artist.filter(:artists__id
|
|
257
|
-
Artist.filter(:artists__id
|
|
258
|
-
Artist.filter(:artists__id
|
|
263
|
+
Artist.filter(:artists__id=>@artist1.id).eager_graph(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 2 4'
|
|
264
|
+
Artist.filter(:artists__id=>@artist2.id).eager_graph(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 2 3'
|
|
265
|
+
Artist.filter(:artists__id=>@artist3.id).eager_graph(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'2 3 4'
|
|
266
|
+
Artist.filter(:artists__id=>@artist4.id).eager_graph(:related_artists).all.map{|x| x.related_artists.map{|a| a.name}}.flatten.sort.should == %w'1 3 4'
|
|
259
267
|
|
|
260
268
|
Artist.filter(:related_artists=>@artist1).all.map{|a| a.name}.sort.should == %w'1 2 4'
|
|
261
269
|
Artist.filter(:related_artists=>@artist2).all.map{|a| a.name}.sort.should == %w'1 2 3'
|
|
@@ -285,26 +293,26 @@ describe "Many Through Many Plugin" do
|
|
|
285
293
|
@album4.add_artist(@artist3)
|
|
286
294
|
@album4.add_artist(@artist4)
|
|
287
295
|
|
|
288
|
-
Artist[
|
|
289
|
-
Artist[
|
|
290
|
-
Artist[
|
|
291
|
-
Artist[
|
|
296
|
+
Artist[@artist1.id].related_albums.map{|x| x.name}.sort.should == %w'A B C'
|
|
297
|
+
Artist[@artist2.id].related_albums.map{|x| x.name}.sort.should == %w'A B C D'
|
|
298
|
+
Artist[@artist3.id].related_albums.map{|x| x.name}.sort.should == %w'A B D'
|
|
299
|
+
Artist[@artist4.id].related_albums.map{|x| x.name}.sort.should == %w'B D'
|
|
292
300
|
|
|
293
301
|
Artist.plugin :prepared_statements_associations
|
|
294
|
-
Artist[
|
|
295
|
-
Artist[
|
|
296
|
-
Artist[
|
|
297
|
-
Artist[
|
|
302
|
+
Artist[@artist1.id].related_albums.map{|x| x.name}.sort.should == %w'A B C'
|
|
303
|
+
Artist[@artist2.id].related_albums.map{|x| x.name}.sort.should == %w'A B C D'
|
|
304
|
+
Artist[@artist3.id].related_albums.map{|x| x.name}.sort.should == %w'A B D'
|
|
305
|
+
Artist[@artist4.id].related_albums.map{|x| x.name}.sort.should == %w'B D'
|
|
298
306
|
|
|
299
|
-
Artist.filter(:id
|
|
300
|
-
Artist.filter(:id
|
|
301
|
-
Artist.filter(:id
|
|
302
|
-
Artist.filter(:id
|
|
307
|
+
Artist.filter(:id=>@artist1.id).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B C'
|
|
308
|
+
Artist.filter(:id=>@artist2.id).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B C D'
|
|
309
|
+
Artist.filter(:id=>@artist3.id).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B D'
|
|
310
|
+
Artist.filter(:id=>@artist4.id).eager(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'B D'
|
|
303
311
|
|
|
304
|
-
Artist.filter(:artists__id
|
|
305
|
-
Artist.filter(:artists__id
|
|
306
|
-
Artist.filter(:artists__id
|
|
307
|
-
Artist.filter(:artists__id
|
|
312
|
+
Artist.filter(:artists__id=>@artist1.id).eager_graph(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B C'
|
|
313
|
+
Artist.filter(:artists__id=>@artist2.id).eager_graph(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B C D'
|
|
314
|
+
Artist.filter(:artists__id=>@artist3.id).eager_graph(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'A B D'
|
|
315
|
+
Artist.filter(:artists__id=>@artist4.id).eager_graph(:related_albums).all.map{|x| x.related_albums.map{|a| a.name}}.flatten.sort.should == %w'B D'
|
|
308
316
|
|
|
309
317
|
Artist.filter(:related_albums=>@album1).all.map{|a| a.name}.sort.should == %w'1 2 3'
|
|
310
318
|
Artist.filter(:related_albums=>@album2).all.map{|a| a.name}.sort.should == %w'1 2 3 4'
|
|
@@ -328,19 +336,20 @@ describe "Many Through Many Plugin" do
|
|
|
328
336
|
end
|
|
329
337
|
|
|
330
338
|
describe "Lazy Attributes plugin" do
|
|
331
|
-
before do
|
|
339
|
+
before(:all) do
|
|
332
340
|
@db = INTEGRATION_DB
|
|
333
341
|
@db.create_table!(:items) do
|
|
334
342
|
primary_key :id
|
|
335
343
|
String :name
|
|
336
344
|
Integer :num
|
|
337
345
|
end
|
|
346
|
+
@db[:items].delete
|
|
338
347
|
class ::Item < Sequel::Model(@db)
|
|
339
348
|
plugin :lazy_attributes, :num
|
|
340
349
|
end
|
|
341
350
|
Item.create(:name=>'J', :num=>1)
|
|
342
351
|
end
|
|
343
|
-
after do
|
|
352
|
+
after(:all) do
|
|
344
353
|
@db.drop_table(:items)
|
|
345
354
|
Object.send(:remove_const, :Item)
|
|
346
355
|
end
|
|
@@ -373,7 +382,7 @@ describe "Lazy Attributes plugin" do
|
|
|
373
382
|
end
|
|
374
383
|
|
|
375
384
|
describe "Tactical Eager Loading Plugin" do
|
|
376
|
-
before do
|
|
385
|
+
before(:all) do
|
|
377
386
|
@db = INTEGRATION_DB
|
|
378
387
|
@db.instance_variable_set(:@schemas, {})
|
|
379
388
|
@db.create_table!(:artists) do
|
|
@@ -385,6 +394,10 @@ describe "Tactical Eager Loading Plugin" do
|
|
|
385
394
|
String :name
|
|
386
395
|
foreign_key :artist_id, :artists
|
|
387
396
|
end
|
|
397
|
+
end
|
|
398
|
+
before do
|
|
399
|
+
@db[:albums].delete
|
|
400
|
+
@db[:artists].delete
|
|
388
401
|
class ::Album < Sequel::Model(@db)
|
|
389
402
|
plugin :tactical_eager_loading
|
|
390
403
|
many_to_one :artist
|
|
@@ -402,13 +415,13 @@ describe "Tactical Eager Loading Plugin" do
|
|
|
402
415
|
@album2 = Album.create(:name=>'B', :artist=>@artist1)
|
|
403
416
|
@album3 = Album.create(:name=>'C', :artist=>@artist2)
|
|
404
417
|
@album4 = Album.create(:name=>'D', :artist=>@artist3)
|
|
405
|
-
|
|
406
|
-
clear_sqls
|
|
407
418
|
end
|
|
408
419
|
after do
|
|
409
|
-
@db.drop_table :albums, :artists
|
|
410
420
|
[:Album, :Artist].each{|s| Object.send(:remove_const, s)}
|
|
411
421
|
end
|
|
422
|
+
after(:all) do
|
|
423
|
+
@db.drop_table :albums, :artists
|
|
424
|
+
end
|
|
412
425
|
|
|
413
426
|
specify "should eagerly load associations for all items when accessing any item" do
|
|
414
427
|
a = Artist.order(:name).all
|
|
@@ -456,7 +469,7 @@ describe "Identity Map plugin" do
|
|
|
456
469
|
end
|
|
457
470
|
|
|
458
471
|
describe "Touch plugin" do
|
|
459
|
-
before do
|
|
472
|
+
before(:all) do
|
|
460
473
|
@db = INTEGRATION_DB
|
|
461
474
|
@db.instance_variable_set(:@schemas, {})
|
|
462
475
|
@db.create_table!(:artists) do
|
|
@@ -470,6 +483,10 @@ describe "Touch plugin" do
|
|
|
470
483
|
foreign_key :artist_id, :artists
|
|
471
484
|
DateTime :updated_at
|
|
472
485
|
end
|
|
486
|
+
end
|
|
487
|
+
before do
|
|
488
|
+
@db[:albums].delete
|
|
489
|
+
@db[:artists].delete
|
|
473
490
|
class ::Album < Sequel::Model(@db)
|
|
474
491
|
many_to_one :artist
|
|
475
492
|
plugin :touch, :associations=>:artist
|
|
@@ -481,9 +498,11 @@ describe "Touch plugin" do
|
|
|
481
498
|
@album = Album.create(:name=>'A', :artist=>@artist)
|
|
482
499
|
end
|
|
483
500
|
after do
|
|
484
|
-
@db.drop_table :albums, :artists
|
|
485
501
|
[:Album, :Artist].each{|s| Object.send(:remove_const, s)}
|
|
486
502
|
end
|
|
503
|
+
after(:all) do
|
|
504
|
+
@db.drop_table :albums, :artists
|
|
505
|
+
end
|
|
487
506
|
|
|
488
507
|
specify "should update the timestamp column when touching the record" do
|
|
489
508
|
@album.updated_at.should == nil
|
|
@@ -538,7 +557,7 @@ describe "Serialization plugin" do
|
|
|
538
557
|
end
|
|
539
558
|
|
|
540
559
|
describe "OptimisticLocking plugin" do
|
|
541
|
-
before do
|
|
560
|
+
before(:all) do
|
|
542
561
|
@db = INTEGRATION_DB
|
|
543
562
|
@db.create_table!(:people) do
|
|
544
563
|
primary_key :id
|
|
@@ -547,30 +566,33 @@ describe "OptimisticLocking plugin" do
|
|
|
547
566
|
end
|
|
548
567
|
class ::Person < Sequel::Model(@db)
|
|
549
568
|
plugin :optimistic_locking
|
|
550
|
-
create(:name=>'John')
|
|
551
569
|
end
|
|
552
570
|
end
|
|
553
|
-
|
|
571
|
+
before do
|
|
572
|
+
@db[:people].delete
|
|
573
|
+
@p = Person.create(:name=>'John')
|
|
574
|
+
end
|
|
575
|
+
after(:all) do
|
|
554
576
|
@db.drop_table(:people)
|
|
555
577
|
Object.send(:remove_const, :Person)
|
|
556
578
|
end
|
|
557
579
|
|
|
558
580
|
specify "should raise an error when updating a stale record" do
|
|
559
|
-
p1 = Person[
|
|
560
|
-
p2 = Person[
|
|
581
|
+
p1 = Person[@p.id]
|
|
582
|
+
p2 = Person[@p.id]
|
|
561
583
|
p1.update(:name=>'Jim')
|
|
562
584
|
proc{p2.update(:name=>'Bob')}.should raise_error(Sequel::Plugins::OptimisticLocking::Error)
|
|
563
585
|
end
|
|
564
586
|
|
|
565
587
|
specify "should raise an error when destroying a stale record" do
|
|
566
|
-
p1 = Person[
|
|
567
|
-
p2 = Person[
|
|
588
|
+
p1 = Person[@p.id]
|
|
589
|
+
p2 = Person[@p.id]
|
|
568
590
|
p1.update(:name=>'Jim')
|
|
569
591
|
proc{p2.destroy}.should raise_error(Sequel::Plugins::OptimisticLocking::Error)
|
|
570
592
|
end
|
|
571
593
|
|
|
572
594
|
specify "should not raise an error when updating the same record twice" do
|
|
573
|
-
p1 = Person[
|
|
595
|
+
p1 = Person[@p.id]
|
|
574
596
|
p1.update(:name=>'Jim')
|
|
575
597
|
proc{p1.update(:name=>'Bob')}.should_not raise_error
|
|
576
598
|
end
|
|
@@ -630,9 +652,9 @@ describe "Composition plugin" do
|
|
|
630
652
|
end
|
|
631
653
|
|
|
632
654
|
# DB2's implemention of CTE is too limited to use this plugin
|
|
633
|
-
if INTEGRATION_DB.dataset.supports_cte? and
|
|
655
|
+
if INTEGRATION_DB.dataset.supports_cte? and !Sequel.guarded?(:db2)
|
|
634
656
|
describe "RcteTree Plugin" do
|
|
635
|
-
before do
|
|
657
|
+
before(:all) do
|
|
636
658
|
@db = INTEGRATION_DB
|
|
637
659
|
@db.create_table!(:nodes) do
|
|
638
660
|
primary_key :id
|
|
@@ -643,21 +665,25 @@ if INTEGRATION_DB.dataset.supports_cte? and INTEGRATION_DB.database_type != :db2
|
|
|
643
665
|
plugin :rcte_tree, :order=>:name
|
|
644
666
|
end
|
|
645
667
|
|
|
646
|
-
@
|
|
647
|
-
@
|
|
648
|
-
@
|
|
649
|
-
@
|
|
650
|
-
@
|
|
651
|
-
@
|
|
652
|
-
@
|
|
653
|
-
@
|
|
654
|
-
@
|
|
655
|
-
@
|
|
656
|
-
@
|
|
657
|
-
@
|
|
658
|
-
@
|
|
659
|
-
|
|
660
|
-
|
|
668
|
+
@nodes = []
|
|
669
|
+
@nodes << @a = Node.create(:name=>'a')
|
|
670
|
+
@nodes << @b = Node.create(:name=>'b')
|
|
671
|
+
@nodes << @aa = Node.create(:name=>'aa', :parent=>@a)
|
|
672
|
+
@nodes << @ab = Node.create(:name=>'ab', :parent=>@a)
|
|
673
|
+
@nodes << @ba = Node.create(:name=>'ba', :parent=>@b)
|
|
674
|
+
@nodes << @bb = Node.create(:name=>'bb', :parent=>@b)
|
|
675
|
+
@nodes << @aaa = Node.create(:name=>'aaa', :parent=>@aa)
|
|
676
|
+
@nodes << @aab = Node.create(:name=>'aab', :parent=>@aa)
|
|
677
|
+
@nodes << @aba = Node.create(:name=>'aba', :parent=>@ab)
|
|
678
|
+
@nodes << @abb = Node.create(:name=>'abb', :parent=>@ab)
|
|
679
|
+
@nodes << @aaaa = Node.create(:name=>'aaaa', :parent=>@aaa)
|
|
680
|
+
@nodes << @aaab = Node.create(:name=>'aaab', :parent=>@aaa)
|
|
681
|
+
@nodes << @aaaaa = Node.create(:name=>'aaaaa', :parent=>@aaaa)
|
|
682
|
+
end
|
|
683
|
+
before do
|
|
684
|
+
@nodes.each{|n| n.associations.clear}
|
|
685
|
+
end
|
|
686
|
+
after(:all) do
|
|
661
687
|
@db.drop_table :nodes
|
|
662
688
|
Object.send(:remove_const, :Node)
|
|
663
689
|
end
|
|
@@ -734,13 +760,15 @@ if INTEGRATION_DB.dataset.supports_cte? and INTEGRATION_DB.database_type != :db2
|
|
|
734
760
|
end
|
|
735
761
|
|
|
736
762
|
specify "should work correctly if not all columns are selected" do
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
nodes
|
|
743
|
-
nodes
|
|
763
|
+
c = Class.new(Sequel::Model(@db[:nodes]))
|
|
764
|
+
c.plugin :rcte_tree, :order=>:name
|
|
765
|
+
c.plugin :lazy_attributes, :name
|
|
766
|
+
c[:name=>'aaaa'].descendants.should == [c.load(:parent_id=>11, :id=>13)]
|
|
767
|
+
c[:name=>'aa'].ancestors.should == [c.load(:parent_id=>nil, :id=>1)]
|
|
768
|
+
nodes = c.filter(:id=>[@a.id, @b.id, @aaa.id]).order(:name).eager(:ancestors, :descendants).all
|
|
769
|
+
nodes.should == [{:parent_id=>nil, :id=>1}, {:parent_id=>3, :id=>7}, {:parent_id=>nil, :id=>2}].map{|x| c.load(x)}
|
|
770
|
+
nodes[2].descendants.should == [{:parent_id=>2, :id=>5}, {:parent_id=>2, :id=>6}].map{|x| c.load(x)}
|
|
771
|
+
nodes[1].ancestors.should == [{:parent_id=>nil, :id=>1}, {:parent_id=>1, :id=>3}].map{|x| c.load(x)}
|
|
744
772
|
end
|
|
745
773
|
|
|
746
774
|
specify "should eagerly load descendants to a given level" do
|
|
@@ -858,7 +886,7 @@ if INTEGRATION_DB.dataset.supports_cte? and INTEGRATION_DB.database_type != :db2
|
|
|
858
886
|
end
|
|
859
887
|
|
|
860
888
|
describe "Instance Filters plugin" do
|
|
861
|
-
before do
|
|
889
|
+
before(:all) do
|
|
862
890
|
@db = INTEGRATION_DB
|
|
863
891
|
@db.create_table!(:items) do
|
|
864
892
|
primary_key :id
|
|
@@ -869,11 +897,14 @@ describe "Instance Filters plugin" do
|
|
|
869
897
|
class ::Item < Sequel::Model(@db)
|
|
870
898
|
plugin :instance_filters
|
|
871
899
|
end
|
|
900
|
+
end
|
|
901
|
+
before do
|
|
902
|
+
@db[:items].delete
|
|
872
903
|
@i = Item.create(:name=>'J', :number=>1, :cost=>2)
|
|
873
904
|
@i.instance_filter(:number=>1)
|
|
874
905
|
@i.set(:name=>'K')
|
|
875
906
|
end
|
|
876
|
-
after do
|
|
907
|
+
after(:all) do
|
|
877
908
|
@db.drop_table(:items)
|
|
878
909
|
Object.send(:remove_const, :Item)
|
|
879
910
|
end
|
|
@@ -918,20 +949,23 @@ describe "Instance Filters plugin" do
|
|
|
918
949
|
end
|
|
919
950
|
|
|
920
951
|
describe "UpdatePrimaryKey plugin" do
|
|
921
|
-
before do
|
|
952
|
+
before(:all) do
|
|
922
953
|
@db = INTEGRATION_DB
|
|
923
954
|
@db.create_table!(:t) do
|
|
924
955
|
Integer :a, :primary_key=>true
|
|
925
956
|
Integer :b
|
|
926
957
|
end
|
|
927
958
|
@ds = @db[:t]
|
|
928
|
-
@ds.insert(:a=>1, :b=>3)
|
|
929
959
|
@c = Class.new(Sequel::Model(@ds))
|
|
930
960
|
@c.set_primary_key(:a)
|
|
931
961
|
@c.unrestrict_primary_key
|
|
932
962
|
@c.plugin :update_primary_key
|
|
933
963
|
end
|
|
934
|
-
|
|
964
|
+
before do
|
|
965
|
+
@ds.delete
|
|
966
|
+
@ds.insert(:a=>1, :b=>3)
|
|
967
|
+
end
|
|
968
|
+
after(:all) do
|
|
935
969
|
@db.drop_table(:t)
|
|
936
970
|
end
|
|
937
971
|
|
|
@@ -973,22 +1007,23 @@ describe "UpdatePrimaryKey plugin" do
|
|
|
973
1007
|
end
|
|
974
1008
|
|
|
975
1009
|
describe "AssociationPks plugin" do
|
|
976
|
-
before do
|
|
1010
|
+
before(:all) do
|
|
977
1011
|
@db = INTEGRATION_DB
|
|
978
|
-
@db.
|
|
1012
|
+
[:albums_tags, :tags, :albums, :artists].each{|t| @db.drop_table(t) if @db.table_exists?(t)}
|
|
1013
|
+
@db.create_table(:artists) do
|
|
979
1014
|
primary_key :id
|
|
980
1015
|
String :name
|
|
981
1016
|
end
|
|
982
|
-
@db.create_table
|
|
1017
|
+
@db.create_table(:albums) do
|
|
983
1018
|
primary_key :id
|
|
984
1019
|
String :name
|
|
985
1020
|
foreign_key :artist_id, :artists
|
|
986
1021
|
end
|
|
987
|
-
@db.create_table
|
|
1022
|
+
@db.create_table(:tags) do
|
|
988
1023
|
primary_key :id
|
|
989
1024
|
String :name
|
|
990
1025
|
end
|
|
991
|
-
@db.create_table
|
|
1026
|
+
@db.create_table(:albums_tags) do
|
|
992
1027
|
foreign_key :album_id, :albums
|
|
993
1028
|
foreign_key :tag_id, :tags
|
|
994
1029
|
end
|
|
@@ -1002,7 +1037,9 @@ describe "AssociationPks plugin" do
|
|
|
1002
1037
|
end
|
|
1003
1038
|
class ::Tag < Sequel::Model
|
|
1004
1039
|
end
|
|
1005
|
-
|
|
1040
|
+
end
|
|
1041
|
+
before do
|
|
1042
|
+
[:albums_tags, :tags, :albums, :artists].each{|t| @db[t].delete}
|
|
1006
1043
|
@ar1 =@db[:artists].insert(:name=>'YJM')
|
|
1007
1044
|
@ar2 =@db[:artists].insert(:name=>'AS')
|
|
1008
1045
|
@al1 =@db[:albums].insert(:name=>'RF', :artist_id=>@ar1)
|
|
@@ -1015,7 +1052,7 @@ describe "AssociationPks plugin" do
|
|
|
1015
1052
|
tids.each{|tid| @db[:albums_tags].insert([aid, tid])}
|
|
1016
1053
|
end
|
|
1017
1054
|
end
|
|
1018
|
-
after do
|
|
1055
|
+
after(:all) do
|
|
1019
1056
|
@db.drop_table :albums_tags, :tags, :albums, :artists
|
|
1020
1057
|
[:Artist, :Album, :Tag].each{|s| Object.send(:remove_const, s)}
|
|
1021
1058
|
end
|
|
@@ -1069,9 +1106,9 @@ end
|
|
|
1069
1106
|
|
|
1070
1107
|
|
|
1071
1108
|
describe "List plugin without a scope" do
|
|
1072
|
-
before do
|
|
1109
|
+
before(:all) do
|
|
1073
1110
|
@db = INTEGRATION_DB
|
|
1074
|
-
@db.create_table
|
|
1111
|
+
@db.create_table!(:sites) do
|
|
1075
1112
|
primary_key :id
|
|
1076
1113
|
String :name
|
|
1077
1114
|
Integer :position
|
|
@@ -1079,13 +1116,14 @@ describe "List plugin without a scope" do
|
|
|
1079
1116
|
|
|
1080
1117
|
@c = Class.new(Sequel::Model(@db[:sites]))
|
|
1081
1118
|
@c.plugin :list
|
|
1119
|
+
end
|
|
1120
|
+
before do
|
|
1082
1121
|
@c.delete
|
|
1083
1122
|
@c.create :name => "hig", :position => 3
|
|
1084
1123
|
@c.create :name => "def", :position => 2
|
|
1085
1124
|
@c.create :name => "abc", :position => 1
|
|
1086
1125
|
end
|
|
1087
|
-
|
|
1088
|
-
after do
|
|
1126
|
+
after(:all) do
|
|
1089
1127
|
@db.drop_table(:sites)
|
|
1090
1128
|
end
|
|
1091
1129
|
|
|
@@ -1142,9 +1180,9 @@ describe "List plugin without a scope" do
|
|
|
1142
1180
|
end
|
|
1143
1181
|
|
|
1144
1182
|
describe "List plugin with a scope" do
|
|
1145
|
-
before do
|
|
1183
|
+
before(:all) do
|
|
1146
1184
|
@db = INTEGRATION_DB
|
|
1147
|
-
@db.create_table
|
|
1185
|
+
@db.create_table!(:pages) do
|
|
1148
1186
|
primary_key :id
|
|
1149
1187
|
String :name
|
|
1150
1188
|
Integer :pos
|
|
@@ -1153,6 +1191,9 @@ describe "List plugin with a scope" do
|
|
|
1153
1191
|
|
|
1154
1192
|
@c = Class.new(Sequel::Model(@db[:pages]))
|
|
1155
1193
|
@c.plugin :list, :field => :pos, :scope => :parent_id
|
|
1194
|
+
end
|
|
1195
|
+
before do
|
|
1196
|
+
@c.delete
|
|
1156
1197
|
p1 = @c.create :name => "Hm", :pos => 1, :parent_id => 0
|
|
1157
1198
|
p2 = @c.create :name => "Ps", :pos => 1, :parent_id => p1.id
|
|
1158
1199
|
@c.create :name => "P1", :pos => 1, :parent_id => p2.id
|
|
@@ -1160,8 +1201,7 @@ describe "List plugin with a scope" do
|
|
|
1160
1201
|
@c.create :name => "P3", :pos => 3, :parent_id => p2.id
|
|
1161
1202
|
@c.create :name => "Au", :pos => 2, :parent_id => p1.id
|
|
1162
1203
|
end
|
|
1163
|
-
|
|
1164
|
-
after do
|
|
1204
|
+
after(:all) do
|
|
1165
1205
|
@db.drop_table(:pages)
|
|
1166
1206
|
end
|
|
1167
1207
|
|
|
@@ -1218,12 +1258,12 @@ describe "List plugin with a scope" do
|
|
|
1218
1258
|
end
|
|
1219
1259
|
|
|
1220
1260
|
describe "Sequel::Plugins::Tree" do
|
|
1221
|
-
before do
|
|
1261
|
+
before(:all) do
|
|
1222
1262
|
@db = INTEGRATION_DB
|
|
1223
1263
|
end
|
|
1224
1264
|
|
|
1225
1265
|
describe "with natural database order" do
|
|
1226
|
-
before do
|
|
1266
|
+
before(:all) do
|
|
1227
1267
|
@db.create_table!(:nodes) do
|
|
1228
1268
|
Integer :id, :primary_key=>true
|
|
1229
1269
|
String :name
|
|
@@ -1249,7 +1289,7 @@ describe "Sequel::Plugins::Tree" do
|
|
|
1249
1289
|
plugin :tree
|
|
1250
1290
|
end
|
|
1251
1291
|
end
|
|
1252
|
-
after do
|
|
1292
|
+
after(:all) do
|
|
1253
1293
|
@db.drop_table(:nodes)
|
|
1254
1294
|
Object.send(:remove_const, :Node)
|
|
1255
1295
|
end
|
|
@@ -1324,12 +1364,12 @@ describe "Sequel::Plugins::Tree" do
|
|
|
1324
1364
|
end
|
|
1325
1365
|
|
|
1326
1366
|
describe "Nodes in specified order" do
|
|
1327
|
-
before do
|
|
1367
|
+
before(:all) do
|
|
1328
1368
|
class ::OrderedNode < Sequel::Model(:nodes)
|
|
1329
1369
|
plugin :tree, :order => :position
|
|
1330
1370
|
end
|
|
1331
1371
|
end
|
|
1332
|
-
after do
|
|
1372
|
+
after(:all) do
|
|
1333
1373
|
Object.send(:remove_const, :OrderedNode)
|
|
1334
1374
|
end
|
|
1335
1375
|
|
|
@@ -1347,7 +1387,7 @@ describe "Sequel::Plugins::Tree" do
|
|
|
1347
1387
|
end
|
|
1348
1388
|
|
|
1349
1389
|
describe "Lorems in specified order" do
|
|
1350
|
-
before do
|
|
1390
|
+
before(:all) do
|
|
1351
1391
|
@db.create_table!(:lorems) do
|
|
1352
1392
|
Integer :id, :primary_key=>true
|
|
1353
1393
|
String :name
|
|
@@ -1365,7 +1405,7 @@ describe "Sequel::Plugins::Tree" do
|
|
|
1365
1405
|
plugin :tree, :key => :ipsum_id, :order => :neque
|
|
1366
1406
|
end
|
|
1367
1407
|
end
|
|
1368
|
-
after do
|
|
1408
|
+
after(:all) do
|
|
1369
1409
|
@db.drop_table(:lorems)
|
|
1370
1410
|
Object.send(:remove_const, :Lorem)
|
|
1371
1411
|
end
|
|
@@ -1383,7 +1423,7 @@ describe "Sequel::Plugins::Tree" do
|
|
|
1383
1423
|
end
|
|
1384
1424
|
|
|
1385
1425
|
describe "Sequel::Plugins::PreparedStatements" do
|
|
1386
|
-
before do
|
|
1426
|
+
before(:all) do
|
|
1387
1427
|
@db = INTEGRATION_DB
|
|
1388
1428
|
@db.create_table!(:ps_test) do
|
|
1389
1429
|
primary_key :id
|
|
@@ -1391,11 +1431,14 @@ describe "Sequel::Plugins::PreparedStatements" do
|
|
|
1391
1431
|
Integer :i
|
|
1392
1432
|
end
|
|
1393
1433
|
@c = Class.new(Sequel::Model(@db[:ps_test]))
|
|
1434
|
+
@c.plugin :prepared_statements_with_pk
|
|
1435
|
+
end
|
|
1436
|
+
before do
|
|
1437
|
+
@c.delete
|
|
1394
1438
|
@foo = @c.create(:name=>'foo', :i=>10)
|
|
1395
1439
|
@bar = @c.create(:name=>'bar', :i=>20)
|
|
1396
|
-
@c.plugin :prepared_statements_with_pk
|
|
1397
1440
|
end
|
|
1398
|
-
after do
|
|
1441
|
+
after(:all) do
|
|
1399
1442
|
@db.drop_table(:ps_test)
|
|
1400
1443
|
end
|
|
1401
1444
|
|
|
@@ -1403,19 +1446,23 @@ describe "Sequel::Plugins::PreparedStatements" do
|
|
|
1403
1446
|
@c[@foo.id].should == @foo
|
|
1404
1447
|
@c[@bar.id].should == @bar
|
|
1405
1448
|
@c[0].should == nil
|
|
1449
|
+
@c[nil].should == nil
|
|
1406
1450
|
end
|
|
1407
1451
|
|
|
1408
1452
|
it "should work with looking up using Dataset#with_pk" do
|
|
1409
1453
|
@c.dataset.with_pk(@foo.id).should == @foo
|
|
1410
1454
|
@c.dataset.with_pk(@bar.id).should == @bar
|
|
1411
1455
|
@c.dataset.with_pk(0).should == nil
|
|
1456
|
+
@c.dataset.with_pk(nil).should == nil
|
|
1412
1457
|
|
|
1413
1458
|
@c.dataset.filter(:i=>0).with_pk(@foo.id).should == nil
|
|
1414
1459
|
@c.dataset.filter(:i=>10).with_pk(@foo.id).should == @foo
|
|
1415
1460
|
@c.dataset.filter(:i=>20).with_pk(@bar.id).should == @bar
|
|
1461
|
+
@c.dataset.filter(:i=>10).with_pk(nil).should == nil
|
|
1416
1462
|
@c.dataset.filter(:name=>'foo').with_pk(@foo.id).should == @foo
|
|
1417
1463
|
@c.dataset.filter(:name=>'bar').with_pk(@bar.id).should == @bar
|
|
1418
1464
|
@c.dataset.filter(:name=>'baz').with_pk(@bar.id).should == nil
|
|
1465
|
+
@c.dataset.filter(:name=>'bar').with_pk(nil).should == nil
|
|
1419
1466
|
end
|
|
1420
1467
|
|
|
1421
1468
|
it "should work with Model#destroy" do
|
|
@@ -1432,6 +1479,8 @@ describe "Sequel::Plugins::PreparedStatements" do
|
|
|
1432
1479
|
@c[@foo.id].should == @c.load(:id=>@foo.id, :name=>'foo3', :i=>30)
|
|
1433
1480
|
@foo.update(:i=>40)
|
|
1434
1481
|
@c[@foo.id].should == @c.load(:id=>@foo.id, :name=>'foo3', :i=>40)
|
|
1482
|
+
@foo.update(:i=>nil)
|
|
1483
|
+
@c[@foo.id].should == @c.load(:id=>@foo.id, :name=>'foo3', :i=>nil)
|
|
1435
1484
|
end
|
|
1436
1485
|
|
|
1437
1486
|
it "should work with Model#create" do
|
|
@@ -1441,5 +1490,7 @@ describe "Sequel::Plugins::PreparedStatements" do
|
|
|
1441
1490
|
@c[o.id].should == @c.load(:id=>o.id, :name=>'foo2', :i=>nil)
|
|
1442
1491
|
o = @c.create(:i=>30)
|
|
1443
1492
|
@c[o.id].should == @c.load(:id=>o.id, :name=>nil, :i=>30)
|
|
1493
|
+
o = @c.create(:name=>nil, :i=>40)
|
|
1494
|
+
@c[o.id].should == @c.load(:id=>o.id, :name=>nil, :i=>40)
|
|
1444
1495
|
end
|
|
1445
1496
|
end
|