colincasey-sequel 2.10.0 → 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. data/CHANGELOG +7 -1
  2. data/doc/advanced_associations.rdoc +614 -0
  3. data/doc/cheat_sheet.rdoc +223 -0
  4. data/doc/dataset_filtering.rdoc +158 -0
  5. data/doc/prepared_statements.rdoc +104 -0
  6. data/doc/release_notes/1.0.txt +38 -0
  7. data/doc/release_notes/1.1.txt +143 -0
  8. data/doc/release_notes/1.3.txt +101 -0
  9. data/doc/release_notes/1.4.0.txt +53 -0
  10. data/doc/release_notes/1.5.0.txt +155 -0
  11. data/doc/release_notes/2.0.0.txt +298 -0
  12. data/doc/release_notes/2.1.0.txt +271 -0
  13. data/doc/release_notes/2.10.0.txt +328 -0
  14. data/doc/release_notes/2.2.0.txt +253 -0
  15. data/doc/release_notes/2.3.0.txt +88 -0
  16. data/doc/release_notes/2.4.0.txt +106 -0
  17. data/doc/release_notes/2.5.0.txt +137 -0
  18. data/doc/release_notes/2.6.0.txt +157 -0
  19. data/doc/release_notes/2.7.0.txt +166 -0
  20. data/doc/release_notes/2.8.0.txt +171 -0
  21. data/doc/release_notes/2.9.0.txt +97 -0
  22. data/doc/schema.rdoc +29 -0
  23. data/doc/sharding.rdoc +113 -0
  24. data/lib/sequel.rb +1 -0
  25. data/lib/sequel_core/adapters/ado.rb +89 -0
  26. data/lib/sequel_core/adapters/db2.rb +143 -0
  27. data/lib/sequel_core/adapters/dbi.rb +112 -0
  28. data/lib/sequel_core/adapters/do/mysql.rb +38 -0
  29. data/lib/sequel_core/adapters/do/postgres.rb +92 -0
  30. data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
  31. data/lib/sequel_core/adapters/do.rb +205 -0
  32. data/lib/sequel_core/adapters/firebird.rb +298 -0
  33. data/lib/sequel_core/adapters/informix.rb +85 -0
  34. data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
  35. data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
  36. data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
  37. data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
  38. data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
  39. data/lib/sequel_core/adapters/jdbc.rb +491 -0
  40. data/lib/sequel_core/adapters/mysql.rb +369 -0
  41. data/lib/sequel_core/adapters/odbc.rb +174 -0
  42. data/lib/sequel_core/adapters/openbase.rb +68 -0
  43. data/lib/sequel_core/adapters/oracle.rb +107 -0
  44. data/lib/sequel_core/adapters/postgres.rb +456 -0
  45. data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
  46. data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
  47. data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
  48. data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
  49. data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
  50. data/lib/sequel_core/adapters/shared/progress.rb +31 -0
  51. data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
  52. data/lib/sequel_core/adapters/sqlite.rb +248 -0
  53. data/lib/sequel_core/connection_pool.rb +258 -0
  54. data/lib/sequel_core/core_ext.rb +217 -0
  55. data/lib/sequel_core/core_sql.rb +202 -0
  56. data/lib/sequel_core/database/schema.rb +164 -0
  57. data/lib/sequel_core/database.rb +691 -0
  58. data/lib/sequel_core/dataset/callback.rb +13 -0
  59. data/lib/sequel_core/dataset/convenience.rb +237 -0
  60. data/lib/sequel_core/dataset/pagination.rb +96 -0
  61. data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
  62. data/lib/sequel_core/dataset/query.rb +41 -0
  63. data/lib/sequel_core/dataset/schema.rb +15 -0
  64. data/lib/sequel_core/dataset/sql.rb +1010 -0
  65. data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
  66. data/lib/sequel_core/dataset/unsupported.rb +43 -0
  67. data/lib/sequel_core/dataset.rb +511 -0
  68. data/lib/sequel_core/deprecated.rb +26 -0
  69. data/lib/sequel_core/exceptions.rb +44 -0
  70. data/lib/sequel_core/migration.rb +212 -0
  71. data/lib/sequel_core/object_graph.rb +230 -0
  72. data/lib/sequel_core/pretty_table.rb +71 -0
  73. data/lib/sequel_core/schema/generator.rb +320 -0
  74. data/lib/sequel_core/schema/sql.rb +325 -0
  75. data/lib/sequel_core/schema.rb +2 -0
  76. data/lib/sequel_core/sql.rb +887 -0
  77. data/lib/sequel_core/version.rb +11 -0
  78. data/lib/sequel_core.rb +172 -0
  79. data/lib/sequel_model/association_reflection.rb +267 -0
  80. data/lib/sequel_model/associations.rb +499 -0
  81. data/lib/sequel_model/base.rb +523 -0
  82. data/lib/sequel_model/caching.rb +82 -0
  83. data/lib/sequel_model/dataset_methods.rb +26 -0
  84. data/lib/sequel_model/eager_loading.rb +370 -0
  85. data/lib/sequel_model/exceptions.rb +7 -0
  86. data/lib/sequel_model/hooks.rb +101 -0
  87. data/lib/sequel_model/inflector.rb +281 -0
  88. data/lib/sequel_model/plugins.rb +62 -0
  89. data/lib/sequel_model/record.rb +568 -0
  90. data/lib/sequel_model/schema.rb +49 -0
  91. data/lib/sequel_model/validations.rb +429 -0
  92. data/lib/sequel_model.rb +91 -0
  93. data/spec/adapters/ado_spec.rb +46 -0
  94. data/spec/adapters/firebird_spec.rb +376 -0
  95. data/spec/adapters/informix_spec.rb +96 -0
  96. data/spec/adapters/mysql_spec.rb +881 -0
  97. data/spec/adapters/oracle_spec.rb +244 -0
  98. data/spec/adapters/postgres_spec.rb +687 -0
  99. data/spec/adapters/spec_helper.rb +10 -0
  100. data/spec/adapters/sqlite_spec.rb +555 -0
  101. data/spec/integration/dataset_test.rb +134 -0
  102. data/spec/integration/eager_loader_test.rb +696 -0
  103. data/spec/integration/prepared_statement_test.rb +130 -0
  104. data/spec/integration/schema_test.rb +180 -0
  105. data/spec/integration/spec_helper.rb +58 -0
  106. data/spec/integration/type_test.rb +96 -0
  107. data/spec/rcov.opts +6 -0
  108. data/spec/sequel_core/connection_pool_spec.rb +526 -0
  109. data/spec/sequel_core/core_ext_spec.rb +156 -0
  110. data/spec/sequel_core/core_sql_spec.rb +522 -0
  111. data/spec/sequel_core/database_spec.rb +1188 -0
  112. data/spec/sequel_core/dataset_spec.rb +3481 -0
  113. data/spec/sequel_core/expression_filters_spec.rb +363 -0
  114. data/spec/sequel_core/migration_spec.rb +261 -0
  115. data/spec/sequel_core/object_graph_spec.rb +272 -0
  116. data/spec/sequel_core/pretty_table_spec.rb +58 -0
  117. data/spec/sequel_core/schema_generator_spec.rb +167 -0
  118. data/spec/sequel_core/schema_spec.rb +780 -0
  119. data/spec/sequel_core/spec_helper.rb +55 -0
  120. data/spec/sequel_core/version_spec.rb +7 -0
  121. data/spec/sequel_model/association_reflection_spec.rb +93 -0
  122. data/spec/sequel_model/associations_spec.rb +1767 -0
  123. data/spec/sequel_model/base_spec.rb +419 -0
  124. data/spec/sequel_model/caching_spec.rb +215 -0
  125. data/spec/sequel_model/dataset_methods_spec.rb +78 -0
  126. data/spec/sequel_model/eager_loading_spec.rb +1165 -0
  127. data/spec/sequel_model/hooks_spec.rb +485 -0
  128. data/spec/sequel_model/inflector_spec.rb +119 -0
  129. data/spec/sequel_model/model_spec.rb +588 -0
  130. data/spec/sequel_model/plugins_spec.rb +80 -0
  131. data/spec/sequel_model/record_spec.rb +1184 -0
  132. data/spec/sequel_model/schema_spec.rb +90 -0
  133. data/spec/sequel_model/spec_helper.rb +78 -0
  134. data/spec/sequel_model/validations_spec.rb +1067 -0
  135. data/spec/spec.opts +0 -0
  136. data/spec/spec_config.rb.example +10 -0
  137. metadata +177 -3
@@ -0,0 +1,1767 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Sequel::Model, "associate" do
4
+ it "should use explicit class if given a class, symbol, or string" do
5
+ MODEL_DB.reset
6
+ klass = Class.new(Sequel::Model(:nodes))
7
+ class ::ParParent < Sequel::Model
8
+ end
9
+
10
+ klass.associate :many_to_one, :par_parent0, :class=>ParParent
11
+ klass.associate :one_to_many, :par_parent1s, :class=>'ParParent'
12
+ klass.associate :many_to_many, :par_parent2s, :class=>:ParParent
13
+
14
+ klass.association_reflection(:"par_parent0").associated_class.should == ParParent
15
+ klass.association_reflection(:"par_parent1s").associated_class.should == ParParent
16
+ klass.association_reflection(:"par_parent2s").associated_class.should == ParParent
17
+ end
18
+
19
+ it "should add a model_object and association_reflection accessors to the dataset, and return it with the current model object" do
20
+ MODEL_DB.reset
21
+ klass = Class.new(Sequel::Model(:nodes)) do
22
+ columns :id, :a_id
23
+ end
24
+ mod = Module.new do
25
+ def blah
26
+ filter{|o| o.__send__(association_reflection[:key]) > model_object.id*2}
27
+ end
28
+ end
29
+
30
+ klass.associate :many_to_one, :a, :class=>klass
31
+ klass.associate :one_to_many, :bs, :key=>:b_id, :class=>klass, :extend=>mod
32
+ klass.associate :many_to_many, :cs, :class=>klass
33
+
34
+ node = klass.load(:id=>1)
35
+ node.a_dataset.model_object.should == node
36
+ node.bs_dataset.model_object.should == node
37
+ node.cs_dataset.model_object.should == node
38
+
39
+ node.a_dataset.association_reflection.should == klass.association_reflection(:a)
40
+ node.bs_dataset.association_reflection.should == klass.association_reflection(:bs)
41
+ node.cs_dataset.association_reflection.should == klass.association_reflection(:cs)
42
+
43
+ node.bs_dataset.blah.sql.should == 'SELECT * FROM nodes WHERE ((nodes.b_id = 1) AND (b_id > 2))'
44
+ end
45
+
46
+ it "should allow extending the dataset with :extend option" do
47
+ MODEL_DB.reset
48
+ klass = Class.new(Sequel::Model(:nodes)) do
49
+ columns :id, :a_id
50
+ end
51
+ mod = Module.new do
52
+ def blah
53
+ 1
54
+ end
55
+ end
56
+ mod2 = Module.new do
57
+ def blar
58
+ 2
59
+ end
60
+ end
61
+
62
+ klass.associate :many_to_one, :a, :class=>klass, :extend=>mod
63
+ klass.associate :one_to_many, :bs, :class=>klass, :extend=>[mod]
64
+ klass.associate :many_to_many, :cs, :class=>klass, :extend=>[mod, mod2]
65
+
66
+ node = klass.load(:id=>1)
67
+ node.a_dataset.blah.should == 1
68
+ node.bs_dataset.blah.should == 1
69
+ node.cs_dataset.blah.should == 1
70
+ node.cs_dataset.blar.should == 2
71
+ end
72
+
73
+ it "should clone an existing association with the :clone option" do
74
+ MODEL_DB.reset
75
+ klass = Class.new(Sequel::Model(:nodes))
76
+
77
+ klass.many_to_one(:par_parent, :order=>:a){1}
78
+ klass.one_to_many(:par_parent1s, :class=>'ParParent', :limit=>12){4}
79
+ klass.many_to_many(:par_parent2s, :class=>:ParParent, :uniq=>true){2}
80
+
81
+ klass.many_to_one :par, :clone=>:par_parent, :select=>:b
82
+ klass.one_to_many :par1s, :clone=>:par_parent1s, :order=>:b, :limit=>10, :block=>nil
83
+ klass.many_to_many(:par2s, :clone=>:par_parent2s, :order=>:c){3}
84
+
85
+ klass.association_reflection(:par).associated_class.should == ParParent
86
+ klass.association_reflection(:par1s).associated_class.should == ParParent
87
+ klass.association_reflection(:par2s).associated_class.should == ParParent
88
+
89
+ klass.association_reflection(:par)[:order].should == :a
90
+ klass.association_reflection(:par).select.should == :b
91
+ klass.association_reflection(:par)[:block].call.should == 1
92
+ klass.association_reflection(:par1s)[:limit].should == 10
93
+ klass.association_reflection(:par1s)[:order].should == :b
94
+ klass.association_reflection(:par1s)[:block].should == nil
95
+ klass.association_reflection(:par2s)[:after_load].length.should == 1
96
+ klass.association_reflection(:par2s)[:order].should == :c
97
+ klass.association_reflection(:par2s)[:block].call.should == 3
98
+ end
99
+
100
+ end
101
+
102
+ describe Sequel::Model, "many_to_one" do
103
+ before do
104
+ MODEL_DB.reset
105
+
106
+ @c2 = Class.new(Sequel::Model(:nodes)) do
107
+ unrestrict_primary_key
108
+ columns :id, :parent_id, :par_parent_id, :blah
109
+ end
110
+
111
+ @dataset = @c2.dataset
112
+ end
113
+
114
+ it "should use implicit key if omitted" do
115
+ @c2.many_to_one :parent, :class => @c2
116
+
117
+ d = @c2.new(:id => 1, :parent_id => 234)
118
+ p = d.parent
119
+ p.class.should == @c2
120
+ p.values.should == {:x => 1, :id => 1}
121
+
122
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 234) LIMIT 1"]
123
+ end
124
+
125
+ it "should use implicit class if omitted" do
126
+ class ::ParParent < Sequel::Model
127
+ end
128
+
129
+ @c2.many_to_one :par_parent
130
+
131
+ d = @c2.new(:id => 1, :par_parent_id => 234)
132
+ p = d.par_parent
133
+ p.class.should == ParParent
134
+
135
+ MODEL_DB.sqls.should == ["SELECT * FROM par_parents WHERE (par_parents.id = 234) LIMIT 1"]
136
+ end
137
+
138
+ it "should use class inside module if given as a string" do
139
+ module ::Par
140
+ class Parent < Sequel::Model
141
+ end
142
+ end
143
+
144
+ @c2.many_to_one :par_parent, :class=>"Par::Parent"
145
+
146
+ d = @c2.new(:id => 1, :par_parent_id => 234)
147
+ p = d.par_parent
148
+ p.class.should == Par::Parent
149
+
150
+ MODEL_DB.sqls.should == ["SELECT * FROM parents WHERE (parents.id = 234) LIMIT 1"]
151
+ end
152
+
153
+ it "should use explicit key if given" do
154
+ @c2.many_to_one :parent, :class => @c2, :key => :blah
155
+
156
+ d = @c2.new(:id => 1, :blah => 567)
157
+ p = d.parent
158
+ p.class.should == @c2
159
+ p.values.should == {:x => 1, :id => 1}
160
+
161
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 567) LIMIT 1"]
162
+ end
163
+
164
+ it "should use :primary_key option if given" do
165
+ @c2.many_to_one :parent, :class => @c2, :key => :blah, :primary_key => :pk
166
+ @c2.new(:id => 1, :blah => 567).parent
167
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (nodes.pk = 567) LIMIT 1"]
168
+ end
169
+
170
+ it "should use :select option if given" do
171
+ @c2.many_to_one :parent, :class => @c2, :key => :blah, :select=>[:id, :name]
172
+ @c2.new(:id => 1, :blah => 567).parent
173
+ MODEL_DB.sqls.should == ["SELECT id, name FROM nodes WHERE (nodes.id = 567) LIMIT 1"]
174
+ end
175
+
176
+ it "should use :conditions option if given" do
177
+ @c2.many_to_one :parent, :class => @c2, :key => :blah, :conditions=>{:a=>32}
178
+ @c2.new(:id => 1, :blah => 567).parent
179
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE ((nodes.id = 567) AND (a = 32)) LIMIT 1"]
180
+
181
+ @c2.many_to_one :parent, :class => @c2, :key => :blah, :conditions=>:a
182
+ MODEL_DB.sqls.clear
183
+ @c2.new(:id => 1, :blah => 567).parent
184
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE ((nodes.id = 567) AND a) LIMIT 1"]
185
+ end
186
+
187
+ it "should support :order, :limit (only for offset), and :dataset options, as well as a block" do
188
+ c2 = @c2
189
+ @c2.many_to_one :child_20, :class => @c2, :key=>:id, :dataset=>proc{c2.filter(:parent_id=>pk)}, :limit=>[10,20], :order=>:name do |ds|
190
+ ds.filter(:x.sql_number > 1)
191
+ end
192
+ @c2.load(:id => 100).child_20
193
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE ((parent_id = 100) AND (x > 1)) ORDER BY name LIMIT 1 OFFSET 20"]
194
+ end
195
+
196
+ it "should return nil if key value is nil" do
197
+ @c2.many_to_one :parent, :class => @c2
198
+
199
+ d = @c2.new(:id => 1)
200
+ d.parent.should == nil
201
+ end
202
+
203
+ it "should cache negative lookup" do
204
+ @c2.many_to_one :parent, :class => @c2
205
+ ds = @c2.dataset
206
+ def ds.fetch_rows(sql, &block)
207
+ MODEL_DB.sqls << sql
208
+ end
209
+
210
+ d = @c2.new(:id => 1, :parent_id=>555)
211
+ MODEL_DB.sqls.should == []
212
+ d.parent.should == nil
213
+ MODEL_DB.sqls.should == ['SELECT * FROM nodes WHERE (nodes.id = 555) LIMIT 1']
214
+ d.parent.should == nil
215
+ MODEL_DB.sqls.should == ['SELECT * FROM nodes WHERE (nodes.id = 555) LIMIT 1']
216
+ end
217
+
218
+ it "should define a setter method" do
219
+ @c2.many_to_one :parent, :class => @c2
220
+
221
+ d = @c2.new(:id => 1)
222
+ d.parent = @c2.new(:id => 4321)
223
+ d.values.should == {:id => 1, :parent_id => 4321}
224
+
225
+ d.parent = nil
226
+ d.values.should == {:id => 1, :parent_id => nil}
227
+
228
+ e = @c2.new(:id => 6677)
229
+ d.parent = e
230
+ d.values.should == {:id => 1, :parent_id => 6677}
231
+ end
232
+
233
+ it "should have the setter method respect the :primary_key option" do
234
+ @c2.many_to_one :parent, :class => @c2, :primary_key=>:blah
235
+
236
+ d = @c2.new(:id => 1)
237
+ d.parent = @c2.new(:id => 4321, :blah=>444)
238
+ d.values.should == {:id => 1, :parent_id => 444}
239
+
240
+ d.parent = nil
241
+ d.values.should == {:id => 1, :parent_id => nil}
242
+
243
+ e = @c2.new(:id => 6677, :blah=>8)
244
+ d.parent = e
245
+ d.values.should == {:id => 1, :parent_id => 8}
246
+ end
247
+
248
+ it "should not persist changes until saved" do
249
+ @c2.many_to_one :parent, :class => @c2
250
+
251
+ d = @c2.load(:id => 1)
252
+ MODEL_DB.reset
253
+ d.parent = @c2.new(:id => 345)
254
+ MODEL_DB.sqls.should == []
255
+ d.save_changes
256
+ MODEL_DB.sqls.should == ['UPDATE nodes SET parent_id = 345 WHERE (id = 1)']
257
+ end
258
+
259
+ it "should set cached instance variable when accessed" do
260
+ @c2.many_to_one :parent, :class => @c2
261
+
262
+ d = @c2.load(:id => 1)
263
+ MODEL_DB.reset
264
+ d.parent_id = 234
265
+ d.associations[:parent].should == nil
266
+ ds = @c2.dataset
267
+ def ds.fetch_rows(sql, &block); MODEL_DB.sqls << sql; yield({:id=>234}) end
268
+ e = d.parent
269
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 234) LIMIT 1"]
270
+ d.associations[:parent].should == e
271
+ end
272
+
273
+ it "should set cached instance variable when assigned" do
274
+ @c2.many_to_one :parent, :class => @c2
275
+
276
+ d = @c2.create(:id => 1)
277
+ MODEL_DB.reset
278
+ d.associations[:parent].should == nil
279
+ d.parent = @c2.new(:id => 234)
280
+ e = d.parent
281
+ d.associations[:parent].should == e
282
+ MODEL_DB.sqls.should == []
283
+ end
284
+
285
+ it "should use cached instance variable if available" do
286
+ @c2.many_to_one :parent, :class => @c2
287
+
288
+ d = @c2.create(:id => 1, :parent_id => 234)
289
+ MODEL_DB.reset
290
+ d.associations[:parent] = 42
291
+ d.parent.should == 42
292
+ MODEL_DB.sqls.should == []
293
+ end
294
+
295
+ it "should not use cached instance variable if asked to reload" do
296
+ @c2.many_to_one :parent, :class => @c2
297
+
298
+ d = @c2.create(:id => 1)
299
+ MODEL_DB.reset
300
+ d.parent_id = 234
301
+ d.associations[:parent] = 42
302
+ d.parent(true).should_not == 42
303
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 234) LIMIT 1"]
304
+ end
305
+
306
+ it "should have the setter add to the reciprocal one_to_many cached association list if it exists" do
307
+ @c2.many_to_one :parent, :class => @c2
308
+ @c2.one_to_many :children, :class => @c2, :key=>:parent_id
309
+ ds = @c2.dataset
310
+ def ds.fetch_rows(sql, &block)
311
+ MODEL_DB.sqls << sql
312
+ end
313
+
314
+ d = @c2.new(:id => 1)
315
+ e = @c2.new(:id => 2)
316
+ MODEL_DB.sqls.should == []
317
+ d.parent = e
318
+ e.children.should_not(include(d))
319
+ MODEL_DB.sqls.should == ['SELECT * FROM nodes WHERE (nodes.parent_id = 2)']
320
+
321
+ MODEL_DB.reset
322
+ d = @c2.new(:id => 1)
323
+ e = @c2.new(:id => 2)
324
+ e.children.should_not(include(d))
325
+ MODEL_DB.sqls.should == ['SELECT * FROM nodes WHERE (nodes.parent_id = 2)']
326
+ d.parent = e
327
+ e.children.should(include(d))
328
+ MODEL_DB.sqls.should == ['SELECT * FROM nodes WHERE (nodes.parent_id = 2)']
329
+ end
330
+
331
+ it "should have the setter remove the object from the previous associated object's reciprocal one_to_many cached association list if it exists" do
332
+ @c2.many_to_one :parent, :class => @c2
333
+ @c2.one_to_many :children, :class => @c2, :key=>:parent_id
334
+ ds = @c2.dataset
335
+ def ds.fetch_rows(sql, &block)
336
+ MODEL_DB.sqls << sql
337
+ end
338
+
339
+ d = @c2.new(:id => 1)
340
+ e = @c2.new(:id => 2)
341
+ f = @c2.new(:id => 3)
342
+ e.children.should_not(include(d))
343
+ f.children.should_not(include(d))
344
+ MODEL_DB.reset
345
+ d.parent = e
346
+ e.children.should(include(d))
347
+ d.parent = f
348
+ f.children.should(include(d))
349
+ e.children.should_not(include(d))
350
+ d.parent = nil
351
+ f.children.should_not(include(d))
352
+ MODEL_DB.sqls.should == []
353
+ end
354
+
355
+ it "should get all matching records and only return the first if :key option is set to nil" do
356
+ c2 = @c2
357
+ @c2.one_to_many :children, :class => @c2, :key=>:parent_id
358
+ @c2.many_to_one :first_grand_parent, :class => @c2, :key=>nil, :eager_graph=>:children, :dataset=>proc{c2.filter(:children_id=>parent_id)}
359
+ ds = @c2.dataset
360
+ def ds.columns
361
+ [:id, :parent_id, :par_parent_id, :blah]
362
+ end
363
+ def ds.fetch_rows(sql, &block)
364
+ MODEL_DB.sqls << sql
365
+ yield({:id=>1, :parent_id=>0, :par_parent_id=>3, :blah=>4, :children_id=>2, :children_parent_id=>1, :children_par_parent_id=>5, :children_blah=>6})
366
+ end
367
+ p = @c2.new(:parent_id=>2)
368
+ fgp = p.first_grand_parent
369
+ MODEL_DB.sqls.should == ["SELECT nodes.id, nodes.parent_id, nodes.par_parent_id, nodes.blah, children.id AS children_id, children.parent_id AS children_parent_id, children.par_parent_id AS children_par_parent_id, children.blah AS children_blah FROM nodes LEFT OUTER JOIN nodes AS children ON (children.parent_id = nodes.id) WHERE (children_id = 2)"]
370
+ fgp.values.should == {:id=>1, :parent_id=>0, :par_parent_id=>3, :blah=>4}
371
+ fgp.children.first.values.should == {:id=>2, :parent_id=>1, :par_parent_id=>5, :blah=>6}
372
+ end
373
+
374
+ it "should not create the setter method if :read_only option is used" do
375
+ @c2.many_to_one :parent, :class => @c2, :read_only=>true
376
+ @c2.instance_methods.collect{|x| x.to_s}.should(include('parent'))
377
+ @c2.instance_methods.collect{|x| x.to_s}.should_not(include('parent='))
378
+ end
379
+
380
+ it "should not add associations methods directly to class" do
381
+ @c2.many_to_one :parent, :class => @c2
382
+ @c2.instance_methods.collect{|x| x.to_s}.should(include('parent'))
383
+ @c2.instance_methods.collect{|x| x.to_s}.should(include('parent='))
384
+ @c2.instance_methods(false).collect{|x| x.to_s}.should_not(include('parent'))
385
+ @c2.instance_methods(false).collect{|x| x.to_s}.should_not(include('parent='))
386
+ end
387
+
388
+ it "should raise an error if trying to set a model object that doesn't have a valid primary key" do
389
+ @c2.many_to_one :parent, :class => @c2
390
+ p = @c2.new
391
+ c = @c2.load(:id=>123)
392
+ proc{c.parent = p}.should raise_error(Sequel::Error)
393
+ end
394
+
395
+ it "should have belongs_to alias" do
396
+ @c2.belongs_to :parent, :class => @c2
397
+
398
+ d = @c2.load(:id => 1)
399
+ MODEL_DB.reset
400
+ d.parent_id = 234
401
+ d.associations[:parent].should == nil
402
+ ds = @c2.dataset
403
+ def ds.fetch_rows(sql, &block); MODEL_DB.sqls << sql; yield({:id=>234}) end
404
+ e = d.parent
405
+ MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (nodes.id = 234) LIMIT 1"]
406
+ d.associations[:parent].should == e
407
+ end
408
+
409
+ it "should make the change to the foreign_key value inside a _association= method" do
410
+ @c2.many_to_one :parent, :class => @c2
411
+ @c2.private_instance_methods.collect{|x| x.to_s}.sort.should(include("_parent="))
412
+ p = @c2.new
413
+ c = @c2.load(:id=>123)
414
+ def p._parent=(x)
415
+ @x = x
416
+ end
417
+ p.should_not_receive(:parent_id=)
418
+ p.parent = c
419
+ p.instance_variable_get(:@x).should == c
420
+ end
421
+
422
+ it "should support (before|after)_(add|remove) callbacks" do
423
+ h = []
424
+ @c2.many_to_one :parent, :class => @c2, :before_add=>[proc{|x,y| h << x.pk; h << -y.pk}, :blah], :after_add=>proc{h << 3}, :before_remove=>:blah, :after_remove=>[:blahr]
425
+ @c2.class_eval do
426
+ @@blah = h
427
+ def []=(a, v)
428
+ a == :parent_id ? (@@blah << (v ? 4 : 5)) : super
429
+ end
430
+ def blah(x)
431
+ @@blah << x.pk
432
+ end
433
+ def blahr(x)
434
+ @@blah << 6
435
+ end
436
+ end
437
+ p = @c2.load(:id=>10)
438
+ c = @c2.load(:id=>123)
439
+ h.should == []
440
+ p.parent = c
441
+ h.should == [10, -123, 123, 4, 3]
442
+ p.parent = nil
443
+ h.should == [10, -123, 123, 4, 3, 123, 5, 6]
444
+ end
445
+
446
+ it "should support after_load association callback" do
447
+ h = []
448
+ @c2.many_to_one :parent, :class => @c2, :after_load=>[proc{|x,y| h << [x.pk, y.pk]}, :al]
449
+ @c2.class_eval do
450
+ @@blah = h
451
+ def al(v)
452
+ @@blah << v.pk
453
+ end
454
+ def @dataset.fetch_rows(sql)
455
+ yield({:id=>20})
456
+ end
457
+ end
458
+ p = @c2.load(:id=>10, :parent_id=>20)
459
+ parent = p.parent
460
+ h.should == [[10, 20], 20]
461
+ parent.pk.should == 20
462
+ end
463
+
464
+ it "should raise error and not call internal add or remove method if before callback returns false, even if raise_on_save_failure is false" do
465
+ # The reason for this is that assignment in ruby always returns the argument instead of the result
466
+ # of the method, so we can't return nil to signal that the association callback prevented the modification
467
+ p = @c2.new
468
+ c = @c2.load(:id=>123)
469
+ p.raise_on_save_failure = false
470
+ @c2.many_to_one :parent, :class => @c2, :before_add=>:ba, :before_remove=>:br
471
+ p.should_receive(:ba).once.with(c).and_return(false)
472
+ p.should_not_receive(:_parent=)
473
+ proc{p.parent = c}.should raise_error(Sequel::Error)
474
+ p.parent.should == nil
475
+ p.associations[:parent] = c
476
+ p.parent.should == c
477
+ p.should_receive(:br).once.with(c).and_return(false)
478
+ proc{p.parent = nil}.should raise_error(Sequel::Error)
479
+ end
480
+
481
+ it "should raise an error if a callback is not a proc or symbol" do
482
+ @c2.many_to_one :parent, :class => @c2, :before_add=>Object.new
483
+ proc{@c2.new.parent = @c2.load(:id=>1)}.should raise_error(Sequel::Error)
484
+ end
485
+
486
+ it "should call the remove callbacks for the previous object and the add callbacks for the new object" do
487
+ c = @c2.load(:id=>123)
488
+ d = @c2.load(:id=>321)
489
+ p = @c2.new
490
+ p.associations[:parent] = d
491
+ h = []
492
+ @c2.many_to_one :parent, :class => @c2, :before_add=>:ba, :before_remove=>:br, :after_add=>:aa, :after_remove=>:ar
493
+ @c2.class_eval do
494
+ @@blah = h
495
+ def []=(a, v)
496
+ a == :parent_id ? (@@blah << 5) : super
497
+ end
498
+ def ba(x)
499
+ @@blah << x.pk
500
+ end
501
+ def br(x)
502
+ @@blah << x.pk * -1
503
+ end
504
+ def aa(x)
505
+ @@blah << x.pk * 2
506
+ end
507
+ def ar(x)
508
+ @@blah << x.pk * -2
509
+ end
510
+ end
511
+ p.parent = c
512
+ h.should == [-321, 123, 5, 246, -642]
513
+ end
514
+ end
515
+
516
+ describe Sequel::Model, "one_to_many" do
517
+
518
+ before(:each) do
519
+ MODEL_DB.reset
520
+
521
+ @c1 = Class.new(Sequel::Model(:attributes)) do
522
+ unrestrict_primary_key
523
+ columns :id, :node_id
524
+ end
525
+
526
+ @c2 = Class.new(Sequel::Model(:nodes)) do
527
+ unrestrict_primary_key
528
+ attr_accessor :xxx
529
+
530
+ def self.name; 'Node'; end
531
+ def self.to_s; 'Node'; end
532
+ columns :id
533
+ end
534
+ @dataset = @c2.dataset
535
+
536
+ @c2.dataset.extend(Module.new {
537
+ def fetch_rows(sql)
538
+ @db << sql
539
+ yield Hash.new
540
+ end
541
+ })
542
+
543
+ @c1.dataset.extend(Module.new {
544
+ def fetch_rows(sql)
545
+ @db << sql
546
+ yield Hash.new
547
+ end
548
+ })
549
+ end
550
+
551
+ it "should use implicit key if omitted" do
552
+ @c2.one_to_many :attributes, :class => @c1
553
+
554
+ n = @c2.new(:id => 1234)
555
+ a = n.attributes_dataset
556
+ a.should be_a_kind_of(Sequel::Dataset)
557
+ a.sql.should == 'SELECT * FROM attributes WHERE (attributes.node_id = 1234)'
558
+ end
559
+
560
+ it "should use implicit class if omitted" do
561
+ class ::HistoricalValue < Sequel::Model
562
+ end
563
+
564
+ @c2.one_to_many :historical_values
565
+
566
+ n = @c2.new(:id => 1234)
567
+ v = n.historical_values_dataset
568
+ v.should be_a_kind_of(Sequel::Dataset)
569
+ v.sql.should == 'SELECT * FROM historical_values WHERE (historical_values.node_id = 1234)'
570
+ v.model_classes.should == {nil => HistoricalValue}
571
+ end
572
+
573
+ it "should use class inside a module if given as a string" do
574
+ module ::Historical
575
+ class Value < Sequel::Model
576
+ end
577
+ end
578
+
579
+ @c2.one_to_many :historical_values, :class=>'Historical::Value'
580
+
581
+ n = @c2.new(:id => 1234)
582
+ v = n.historical_values_dataset
583
+ v.should be_a_kind_of(Sequel::Dataset)
584
+ v.sql.should == 'SELECT * FROM values WHERE (values.node_id = 1234)'
585
+ v.model_classes.should == {nil => Historical::Value}
586
+ end
587
+
588
+ it "should use explicit key if given" do
589
+ @c2.one_to_many :attributes, :class => @c1, :key => :nodeid
590
+
591
+ n = @c2.new(:id => 1234)
592
+ a = n.attributes_dataset
593
+ a.should be_a_kind_of(Sequel::Dataset)
594
+ a.sql.should == 'SELECT * FROM attributes WHERE (attributes.nodeid = 1234)'
595
+ end
596
+
597
+ it "should define an add_ method" do
598
+ @c2.one_to_many :attributes, :class => @c1
599
+
600
+ n = @c2.new(:id => 1234)
601
+ a = @c1.new(:id => 2345)
602
+ a.save!
603
+ MODEL_DB.reset
604
+ a.should == n.add_attribute(a)
605
+ MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = 1234 WHERE (id = 2345)']
606
+ end
607
+
608
+ it "should define a remove_ method" do
609
+ @c2.one_to_many :attributes, :class => @c1
610
+
611
+ n = @c2.new(:id => 1234)
612
+ a = @c1.new(:id => 2345)
613
+ a.save!
614
+ MODEL_DB.reset
615
+ a.should == n.remove_attribute(a)
616
+ MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = NULL WHERE (id = 2345)']
617
+ end
618
+
619
+ it "should have add_ method respect the :primary_key option" do
620
+ @c2.one_to_many :attributes, :class => @c1, :primary_key=>:xxx
621
+
622
+ n = @c2.new(:id => 1234, :xxx=>5)
623
+ a = @c1.new(:id => 2345)
624
+ a.save!
625
+ MODEL_DB.reset
626
+ a.should == n.add_attribute(a)
627
+ MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = 5 WHERE (id = 2345)']
628
+ end
629
+
630
+
631
+ it "should raise an error in add_ and remove_ if the passed object returns false to save (is not valid)" do
632
+ @c2.one_to_many :attributes, :class => @c1
633
+ n = @c2.new(:id => 1234)
634
+ a = @c1.new(:id => 2345)
635
+ def a.valid?; false; end
636
+ proc{n.add_attribute(a)}.should raise_error(Sequel::Error)
637
+ proc{n.remove_attribute(a)}.should raise_error(Sequel::Error)
638
+ end
639
+
640
+ it "should raise an error if the model object doesn't have a valid primary key" do
641
+ @c2.one_to_many :attributes, :class => @c1
642
+ a = @c2.new
643
+ n = @c1.load(:id=>123)
644
+ proc{a.attributes_dataset}.should raise_error(Sequel::Error)
645
+ proc{a.attributes}.should raise_error(Sequel::Error)
646
+ proc{a.add_attribute(n)}.should raise_error(Sequel::Error)
647
+ proc{a.remove_attribute(n)}.should raise_error(Sequel::Error)
648
+ proc{a.remove_all_attributes}.should raise_error(Sequel::Error)
649
+ end
650
+
651
+ it "should use :primary_key option if given" do
652
+ @c1.one_to_many :nodes, :class => @c2, :primary_key => :node_id, :key=>:id
653
+ n = @c1.load(:id => 1234, :node_id=>4321)
654
+ n.nodes_dataset.sql.should == "SELECT * FROM nodes WHERE (nodes.id = 4321)"
655
+ end
656
+
657
+ it "should support a select option" do
658
+ @c2.one_to_many :attributes, :class => @c1, :select => [:id, :name]
659
+
660
+ n = @c2.new(:id => 1234)
661
+ n.attributes_dataset.sql.should == "SELECT id, name FROM attributes WHERE (attributes.node_id = 1234)"
662
+ end
663
+
664
+ it "should support a conditions option" do
665
+ @c2.one_to_many :attributes, :class => @c1, :conditions => {:a=>32}
666
+ n = @c2.new(:id => 1234)
667
+ n.attributes_dataset.sql.should == "SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (a = 32))"
668
+ @c2.one_to_many :attributes, :class => @c1, :conditions => ~:a
669
+ n = @c2.new(:id => 1234)
670
+ n.attributes_dataset.sql.should == "SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND NOT a)"
671
+ end
672
+
673
+ it "should support an order option" do
674
+ @c2.one_to_many :attributes, :class => @c1, :order => :kind
675
+
676
+ n = @c2.new(:id => 1234)
677
+ n.attributes_dataset.sql.should == "SELECT * FROM attributes WHERE (attributes.node_id = 1234) ORDER BY kind"
678
+ end
679
+
680
+ it "should support an array for the order option" do
681
+ @c2.one_to_many :attributes, :class => @c1, :order => [:kind1, :kind2]
682
+
683
+ n = @c2.new(:id => 1234)
684
+ n.attributes_dataset.sql.should == "SELECT * FROM attributes WHERE (attributes.node_id = 1234) ORDER BY kind1, kind2"
685
+ end
686
+
687
+ it "should return array with all members of the association" do
688
+ @c2.one_to_many :attributes, :class => @c1
689
+
690
+ n = @c2.new(:id => 1234)
691
+ atts = n.attributes
692
+ atts.should be_a_kind_of(Array)
693
+ atts.size.should == 1
694
+ atts.first.should be_a_kind_of(@c1)
695
+ atts.first.values.should == {}
696
+
697
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
698
+ end
699
+
700
+ it "should accept a block" do
701
+ @c2.one_to_many :attributes, :class => @c1 do |ds|
702
+ ds.filter(:xxx => @xxx)
703
+ end
704
+
705
+ n = @c2.new(:id => 1234)
706
+ atts = n.attributes
707
+ atts.should be_a_kind_of(Array)
708
+ atts.size.should == 1
709
+ atts.first.should be_a_kind_of(@c1)
710
+ atts.first.values.should == {}
711
+
712
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (xxx IS NULL))']
713
+ end
714
+
715
+ it "should support :order option with block" do
716
+ @c2.one_to_many :attributes, :class => @c1, :order => :kind do |ds|
717
+ ds.filter(:xxx => @xxx)
718
+ end
719
+
720
+ n = @c2.new(:id => 1234)
721
+ atts = n.attributes
722
+ atts.should be_a_kind_of(Array)
723
+ atts.size.should == 1
724
+ atts.first.should be_a_kind_of(@c1)
725
+ atts.first.values.should == {}
726
+
727
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (xxx IS NULL)) ORDER BY kind']
728
+ end
729
+
730
+ it "should have the block argument affect the _dataset method" do
731
+ @c2.one_to_many :attributes, :class => @c1 do |ds|
732
+ ds.filter(:xxx => 456)
733
+ end
734
+ @c2.new(:id => 1234).attributes_dataset.sql.should == 'SELECT * FROM attributes WHERE ((attributes.node_id = 1234) AND (xxx = 456))'
735
+ end
736
+
737
+ it "should support a :dataset option that is used instead of the default" do
738
+ c1 = @c1
739
+ @c2.one_to_many :all_other_attributes, :class => @c1, :dataset=>proc{c1.filter(:nodeid=>pk).invert}, :order=>:a, :limit=>10 do |ds|
740
+ ds.filter(:xxx => 5)
741
+ end
742
+
743
+ @c2.new(:id => 1234).all_other_attributes_dataset.sql.should == 'SELECT * FROM attributes WHERE ((nodeid != 1234) AND (xxx = 5)) ORDER BY a LIMIT 10'
744
+ n = @c2.new(:id => 1234)
745
+ atts = n.all_other_attributes
746
+ atts.should be_a_kind_of(Array)
747
+ atts.size.should == 1
748
+ atts.first.should be_a_kind_of(@c1)
749
+ atts.first.values.should == {}
750
+
751
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE ((nodeid != 1234) AND (xxx = 5)) ORDER BY a LIMIT 10']
752
+ end
753
+
754
+ it "should support a :limit option" do
755
+ @c2.one_to_many :attributes, :class => @c1 , :limit=>10
756
+ @c2.new(:id => 1234).attributes_dataset.sql.should == 'SELECT * FROM attributes WHERE (attributes.node_id = 1234) LIMIT 10'
757
+ @c2.one_to_many :attributes, :class => @c1 , :limit=>[10,10]
758
+ @c2.new(:id => 1234).attributes_dataset.sql.should == 'SELECT * FROM attributes WHERE (attributes.node_id = 1234) LIMIT 10 OFFSET 10'
759
+ end
760
+
761
+ it "should have the :eager option affect the _dataset method" do
762
+ @c2.one_to_many :attributes, :class => @c2 , :eager=>:attributes
763
+ @c2.new(:id => 1234).attributes_dataset.opts[:eager].should == {:attributes=>nil}
764
+ end
765
+
766
+ it "should set cached instance variable when accessed" do
767
+ @c2.one_to_many :attributes, :class => @c1
768
+
769
+ n = @c2.new(:id => 1234)
770
+ MODEL_DB.reset
771
+ n.associations.include?(:attributes).should == false
772
+ atts = n.attributes
773
+ atts.should == n.associations[:attributes]
774
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
775
+ end
776
+
777
+ it "should use cached instance variable if available" do
778
+ @c2.one_to_many :attributes, :class => @c1
779
+
780
+ n = @c2.new(:id => 1234)
781
+ MODEL_DB.reset
782
+ n.associations[:attributes] = 42
783
+ n.attributes.should == 42
784
+ MODEL_DB.sqls.should == []
785
+ end
786
+
787
+ it "should not use cached instance variable if asked to reload" do
788
+ @c2.one_to_many :attributes, :class => @c1
789
+
790
+ n = @c2.new(:id => 1234)
791
+ MODEL_DB.reset
792
+ n.associations[:attributes] = 42
793
+ n.attributes(true).should_not == 42
794
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
795
+ end
796
+
797
+ it "should add item to cached instance variable if it exists when calling add_" do
798
+ @c2.one_to_many :attributes, :class => @c1
799
+
800
+ n = @c2.new(:id => 1234)
801
+ att = @c1.new(:id => 345)
802
+ MODEL_DB.reset
803
+ a = []
804
+ n.associations[:attributes] = a
805
+ n.add_attribute(att)
806
+ a.should == [att]
807
+ end
808
+
809
+ it "should set object to item's reciprocal cached association variable when calling add_" do
810
+ @c2.one_to_many :attributes, :class => @c1
811
+ @c1.many_to_one :node, :class => @c2
812
+
813
+ n = @c2.new(:id => 1234)
814
+ att = @c1.new(:id => 345)
815
+ n.add_attribute(att)
816
+ att.node.should == n
817
+ end
818
+
819
+ it "should remove item from cached instance variable if it exists when calling remove_" do
820
+ @c2.one_to_many :attributes, :class => @c1
821
+
822
+ n = @c2.load(:id => 1234)
823
+ att = @c1.load(:id => 345)
824
+ MODEL_DB.reset
825
+ a = [att]
826
+ n.associations[:attributes] = a
827
+ n.remove_attribute(att)
828
+ a.should == []
829
+ end
830
+
831
+ it "should remove item's reciprocal cached association variable when calling remove_" do
832
+ @c2.one_to_many :attributes, :class => @c1
833
+ @c1.many_to_one :node, :class => @c2
834
+
835
+ n = @c2.new(:id => 1234)
836
+ att = @c1.new(:id => 345)
837
+ att.associations[:node] = n
838
+ att.node.should == n
839
+ n.remove_attribute(att)
840
+ att.node.should == nil
841
+ end
842
+
843
+ it "should not create the add_, remove_, or remove_all_ methods if :read_only option is used" do
844
+ @c2.one_to_many :attributes, :class => @c1, :read_only=>true
845
+ im = @c2.instance_methods.collect{|x| x.to_s}
846
+ im.should(include('attributes'))
847
+ im.should(include('attributes_dataset'))
848
+ im.should_not(include('add_attribute'))
849
+ im.should_not(include('remove_attribute'))
850
+ im.should_not(include('remove_all_attributes'))
851
+ end
852
+
853
+ it "should not add associations methods directly to class" do
854
+ @c2.one_to_many :attributes, :class => @c1
855
+ im = @c2.instance_methods.collect{|x| x.to_s}
856
+ im.should(include('attributes'))
857
+ im.should(include('attributes_dataset'))
858
+ im.should(include('add_attribute'))
859
+ im.should(include('remove_attribute'))
860
+ im.should(include('remove_all_attributes'))
861
+ im2 = @c2.instance_methods(false).collect{|x| x.to_s}
862
+ im2.should_not(include('attributes'))
863
+ im2.should_not(include('attributes_dataset'))
864
+ im2.should_not(include('add_attribute'))
865
+ im2.should_not(include('remove_attribute'))
866
+ im2.should_not(include('remove_all_attributes'))
867
+ end
868
+
869
+ it "should have has_many alias" do
870
+ @c2.has_many :attributes, :class => @c1
871
+
872
+ n = @c2.new(:id => 1234)
873
+ atts = n.attributes
874
+ atts.should be_a_kind_of(Array)
875
+ atts.size.should == 1
876
+ atts.first.should be_a_kind_of(@c1)
877
+ atts.first.values.should == {}
878
+
879
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
880
+ end
881
+
882
+ it "should populate the reciprocal many_to_one instance variable when loading the one_to_many association" do
883
+ @c2.one_to_many :attributes, :class => @c1, :key => :node_id
884
+ @c1.many_to_one :node, :class => @c2, :key => :node_id
885
+
886
+ n = @c2.new(:id => 1234)
887
+ atts = n.attributes
888
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
889
+ atts.should be_a_kind_of(Array)
890
+ atts.size.should == 1
891
+ atts.first.should be_a_kind_of(@c1)
892
+ atts.first.values.should == {}
893
+ atts.first.node.should == n
894
+
895
+ MODEL_DB.sqls.length.should == 1
896
+ end
897
+
898
+ it "should use an explicit reciprocal instance variable if given" do
899
+ @c2.one_to_many :attributes, :class => @c1, :key => :node_id, :reciprocal=>:wxyz
900
+
901
+ n = @c2.new(:id => 1234)
902
+ atts = n.attributes
903
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
904
+ atts.should be_a_kind_of(Array)
905
+ atts.size.should == 1
906
+ atts.first.should be_a_kind_of(@c1)
907
+ atts.first.values.should == {}
908
+ atts.first.associations[:wxyz].should == n
909
+
910
+ MODEL_DB.sqls.length.should == 1
911
+ end
912
+
913
+ it "should have an remove_all_ method that removes all associations" do
914
+ @c2.one_to_many :attributes, :class => @c1
915
+ @c2.new(:id => 1234).remove_all_attributes
916
+ MODEL_DB.sqls.first.should == 'UPDATE attributes SET node_id = NULL WHERE (node_id = 1234)'
917
+ end
918
+
919
+ it "should have the remove_all_ method respect the :primary_key option" do
920
+ @c2.one_to_many :attributes, :class => @c1, :primary_key=>:xxx
921
+ @c2.new(:id => 1234, :xxx=>5).remove_all_attributes
922
+ MODEL_DB.sqls.first.should == 'UPDATE attributes SET node_id = NULL WHERE (node_id = 5)'
923
+ end
924
+
925
+ it "remove_all should set the cached instance variable to []" do
926
+ @c2.one_to_many :attributes, :class => @c1
927
+ node = @c2.new(:id => 1234)
928
+ node.remove_all_attributes
929
+ node.associations[:attributes].should == []
930
+ end
931
+
932
+ it "remove_all should return the array of previously associated items if the cached instance variable exists" do
933
+ @c2.one_to_many :attributes, :class => @c1
934
+ attrib = @c1.new(:id=>3)
935
+ node = @c2.new(:id => 1234)
936
+ d = @c1.dataset
937
+ def d.fetch_rows(s); end
938
+ node.attributes.should == []
939
+ def attrib.save!; self end
940
+ node.add_attribute(attrib)
941
+ node.associations[:attributes].should == [attrib]
942
+ node.remove_all_attributes.should == [attrib]
943
+ end
944
+
945
+ it "remove_all should return nil if the cached instance variable does not exist" do
946
+ @c2.one_to_many :attributes, :class => @c1
947
+ @c2.new(:id => 1234).remove_all_attributes.should == nil
948
+ end
949
+
950
+ it "remove_all should remove the current item from all reciprocal instance varaibles if it cached instance variable exists" do
951
+ @c2.one_to_many :attributes, :class => @c1
952
+ @c1.many_to_one :node, :class => @c2
953
+ d = @c1.dataset
954
+ def d.fetch_rows(s); end
955
+ d = @c2.dataset
956
+ def d.fetch_rows(s); end
957
+ attrib = @c1.new(:id=>3)
958
+ node = @c2.new(:id => 1234)
959
+ node.attributes.should == []
960
+ attrib.node.should == nil
961
+ def attrib.save!; self end
962
+ node.add_attribute(attrib)
963
+ attrib.associations[:node].should == node
964
+ node.remove_all_attributes
965
+ attrib.associations.fetch(:node, 2).should == nil
966
+ end
967
+
968
+ it "should add a getter method if the :one_to_one option is true" do
969
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true
970
+ att = @c2.new(:id => 1234).attribute
971
+ MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (attributes.node_id = 1234)']
972
+ att.should be_a_kind_of(@c1)
973
+ att.values.should == {}
974
+ end
975
+
976
+ it "should not add a setter method if the :one_to_one option is true and :read_only option is true" do
977
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true, :read_only=>true
978
+ im = @c2.instance_methods.collect{|x| x.to_s}
979
+ im.should(include('attribute'))
980
+ im.should_not(include('attribute='))
981
+ end
982
+
983
+ it "should have the getter method raise an error if more than one record is found" do
984
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true
985
+ d = @c1.dataset
986
+ def d.fetch_rows(s); 2.times{yield Hash.new} end
987
+ proc{@c2.new(:id => 1234).attribute}.should raise_error(Sequel::Error)
988
+ end
989
+
990
+ it "should add a setter method if the :one_to_one option is true" do
991
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true
992
+ attrib = @c1.new(:id=>3)
993
+ d = @c1.dataset
994
+ def d.fetch_rows(s); yield({:id=>3}) end
995
+ @c2.new(:id => 1234).attribute = attrib
996
+ ['INSERT INTO attributes (node_id, id) VALUES (1234, 3)',
997
+ 'INSERT INTO attributes (id, node_id) VALUES (3, 1234)'].should(include(MODEL_DB.sqls.first))
998
+ MODEL_DB.sqls.last.should == 'UPDATE attributes SET node_id = NULL WHERE ((node_id = 1234) AND (id != 3))'
999
+ MODEL_DB.sqls.length.should == 2
1000
+ @c2.new(:id => 1234).attribute.should == attrib
1001
+ MODEL_DB.sqls.clear
1002
+ attrib = @c1.load(:id=>3)
1003
+ @c2.new(:id => 1234).attribute = attrib
1004
+ MODEL_DB.sqls.length.should == 2
1005
+ MODEL_DB.sqls.first.should =~ /UPDATE attributes SET (node_id = 1234, id = 3|id = 3, node_id = 1234) WHERE \(id = 3\)/
1006
+ MODEL_DB.sqls.last.should == 'UPDATE attributes SET node_id = NULL WHERE ((node_id = 1234) AND (id != 3))'
1007
+ end
1008
+
1009
+ it "should have the setter method for the :one_to_one option respect the :primary_key option" do
1010
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true, :primary_key=>:xxx
1011
+ attrib = @c1.new(:id=>3)
1012
+ d = @c1.dataset
1013
+ def d.fetch_rows(s); yield({:id=>3}) end
1014
+ @c2.new(:id => 1234, :xxx=>5).attribute = attrib
1015
+ ['INSERT INTO attributes (node_id, id) VALUES (5, 3)',
1016
+ 'INSERT INTO attributes (id, node_id) VALUES (3, 5)'].should(include(MODEL_DB.sqls.first))
1017
+ MODEL_DB.sqls.last.should == 'UPDATE attributes SET node_id = NULL WHERE ((node_id = 5) AND (id != 3))'
1018
+ MODEL_DB.sqls.length.should == 2
1019
+ @c2.new(:id => 321, :xxx=>5).attribute.should == attrib
1020
+ MODEL_DB.sqls.clear
1021
+ attrib = @c1.load(:id=>3)
1022
+ @c2.new(:id => 621, :xxx=>5).attribute = attrib
1023
+ MODEL_DB.sqls.length.should == 2
1024
+ MODEL_DB.sqls.first.should =~ /UPDATE attributes SET (node_id = 5, id = 3|id = 3, node_id = 5) WHERE \(id = 3\)/
1025
+ MODEL_DB.sqls.last.should == 'UPDATE attributes SET node_id = NULL WHERE ((node_id = 5) AND (id != 3))'
1026
+ end
1027
+
1028
+ it "should raise an error if the one_to_one getter would be the same as the association name" do
1029
+ proc{@c2.one_to_many :song, :class => @c1, :one_to_one=>true}.should raise_error(Sequel::Error)
1030
+ end
1031
+
1032
+ it "should not create remove_ and remove_all methods if :one_to_one option is used" do
1033
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true
1034
+ @c2.new.should_not(respond_to(:remove_attribute))
1035
+ @c2.new.should_not(respond_to(:remove_all_attributes))
1036
+ end
1037
+
1038
+ it "should make non getter and setter methods private if :one_to_one option is used" do
1039
+ @c2.one_to_many :attributes, :class => @c1, :one_to_one=>true do |ds| end
1040
+ meths = @c2.private_instance_methods.collect{|x| x.to_s}
1041
+ meths.should(include("attributes"))
1042
+ meths.should(include("add_attribute"))
1043
+ meths.should(include("attributes_dataset"))
1044
+ end
1045
+
1046
+ it "should call an _add_ method internally to add attributes" do
1047
+ @c2.one_to_many :attributes, :class => @c1
1048
+ @c2.private_instance_methods.collect{|x| x.to_s}.sort.should(include("_add_attribute"))
1049
+ p = @c2.load(:id=>10)
1050
+ c = @c1.load(:id=>123)
1051
+ def p._add_attribute(x)
1052
+ @x = x
1053
+ end
1054
+ c.should_not_receive(:node_id=)
1055
+ p.add_attribute(c)
1056
+ p.instance_variable_get(:@x).should == c
1057
+ end
1058
+
1059
+ it "should call a _remove_ method internally to remove attributes" do
1060
+ @c2.one_to_many :attributes, :class => @c1
1061
+ @c2.private_instance_methods.collect{|x| x.to_s}.sort.should(include("_remove_attribute"))
1062
+ p = @c2.load(:id=>10)
1063
+ c = @c1.load(:id=>123)
1064
+ def p._remove_attribute(x)
1065
+ @x = x
1066
+ end
1067
+ c.should_not_receive(:node_id=)
1068
+ p.remove_attribute(c)
1069
+ p.instance_variable_get(:@x).should == c
1070
+ end
1071
+
1072
+ it "should support (before|after)_(add|remove) callbacks" do
1073
+ h = []
1074
+ @c2.one_to_many :attributes, :class => @c1, :before_add=>[proc{|x,y| h << x.pk; h << -y.pk}, :blah], :after_add=>proc{h << 3}, :before_remove=>:blah, :after_remove=>[:blahr]
1075
+ @c2.class_eval do
1076
+ @@blah = h
1077
+ def _add_attribute(v)
1078
+ @@blah << 4
1079
+ end
1080
+ def _remove_attribute(v)
1081
+ @@blah << 5
1082
+ end
1083
+ def blah(x)
1084
+ @@blah << x.pk
1085
+ end
1086
+ def blahr(x)
1087
+ @@blah << 6
1088
+ end
1089
+ end
1090
+ p = @c2.load(:id=>10)
1091
+ c = @c1.load(:id=>123)
1092
+ h.should == []
1093
+ p.add_attribute(c)
1094
+ h.should == [10, -123, 123, 4, 3]
1095
+ p.remove_attribute(c)
1096
+ h.should == [10, -123, 123, 4, 3, 123, 5, 6]
1097
+ end
1098
+
1099
+ it "should support after_load association callback" do
1100
+ h = []
1101
+ @c2.one_to_many :attributes, :class => @c1, :after_load=>[proc{|x,y| h << [x.pk, y.collect{|z|z.pk}]}, :al]
1102
+ @c2.class_eval do
1103
+ @@blah = h
1104
+ def al(v)
1105
+ v.each{|x| @@blah << x.pk}
1106
+ end
1107
+ end
1108
+ @c1.class_eval do
1109
+ def @dataset.fetch_rows(sql)
1110
+ yield({:id=>20})
1111
+ yield({:id=>30})
1112
+ end
1113
+ end
1114
+ p = @c2.load(:id=>10, :parent_id=>20)
1115
+ attributes = p.attributes
1116
+ h.should == [[10, [20, 30]], 20, 30]
1117
+ attributes.collect{|a| a.pk}.should == [20, 30]
1118
+ end
1119
+
1120
+ it "should raise error and not call internal add or remove method if before callback returns false if raise_on_save_failure is true" do
1121
+ p = @c2.load(:id=>10)
1122
+ c = @c1.load(:id=>123)
1123
+ @c2.one_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
1124
+ p.should_receive(:ba).once.with(c).and_return(false)
1125
+ p.should_not_receive(:_add_attribute)
1126
+ p.should_not_receive(:_remove_attribute)
1127
+ p.associations[:attributes] = []
1128
+ proc{p.add_attribute(c)}.should raise_error(Sequel::Error)
1129
+ p.attributes.should == []
1130
+ p.associations[:attributes] = [c]
1131
+ p.should_receive(:br).once.with(c).and_return(false)
1132
+ proc{p.remove_attribute(c)}.should raise_error(Sequel::Error)
1133
+ p.attributes.should == [c]
1134
+ end
1135
+
1136
+ it "should return nil and not call internal add or remove method if before callback returns false if raise_on_save_failure is false" do
1137
+ p = @c2.load(:id=>10)
1138
+ c = @c1.load(:id=>123)
1139
+ p.raise_on_save_failure = false
1140
+ @c2.one_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
1141
+ p.should_receive(:ba).once.with(c).and_return(false)
1142
+ p.should_not_receive(:_add_attribute)
1143
+ p.should_not_receive(:_remove_attribute)
1144
+ p.associations[:attributes] = []
1145
+ p.add_attribute(c).should == nil
1146
+ p.attributes.should == []
1147
+ p.associations[:attributes] = [c]
1148
+ p.should_receive(:br).once.with(c).and_return(false)
1149
+ p.remove_attribute(c).should == nil
1150
+ p.attributes.should == [c]
1151
+ end
1152
+ end
1153
+
1154
+ describe Sequel::Model, "many_to_many" do
1155
+
1156
+ before(:each) do
1157
+ MODEL_DB.reset
1158
+
1159
+ @c1 = Class.new(Sequel::Model(:attributes)) do
1160
+ unrestrict_primary_key
1161
+ attr_accessor :yyy
1162
+ def self.name; 'Attribute'; end
1163
+ def self.to_s; 'Attribute'; end
1164
+ columns :id
1165
+ end
1166
+
1167
+ @c2 = Class.new(Sequel::Model(:nodes)) do
1168
+ unrestrict_primary_key
1169
+ attr_accessor :xxx
1170
+
1171
+ def self.name; 'Node'; end
1172
+ def self.to_s; 'Node'; end
1173
+ columns :id
1174
+ end
1175
+ @dataset = @c2.dataset
1176
+
1177
+ [@c1, @c2].each do |c|
1178
+ c.dataset.extend(Module.new {
1179
+ def fetch_rows(sql)
1180
+ @db << sql
1181
+ yield Hash.new
1182
+ end
1183
+ })
1184
+ end
1185
+ end
1186
+
1187
+ it "should use implicit key values and join table if omitted" do
1188
+ @c2.many_to_many :attributes, :class => @c1
1189
+
1190
+ n = @c2.new(:id => 1234)
1191
+ a = n.attributes_dataset
1192
+ a.should be_a_kind_of(Sequel::Dataset)
1193
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234))'
1194
+ end
1195
+
1196
+ it "should use implicit class if omitted" do
1197
+ class ::Tag < Sequel::Model
1198
+ end
1199
+
1200
+ @c2.many_to_many :tags
1201
+
1202
+ n = @c2.new(:id => 1234)
1203
+ a = n.tags_dataset
1204
+ a.should be_a_kind_of(Sequel::Dataset)
1205
+ a.sql.should == 'SELECT tags.* FROM tags INNER JOIN nodes_tags ON ((nodes_tags.tag_id = tags.id) AND (nodes_tags.node_id = 1234))'
1206
+ end
1207
+
1208
+ it "should use class inside module if given as a string" do
1209
+ module ::Historical
1210
+ class Tag < Sequel::Model
1211
+ end
1212
+ end
1213
+
1214
+ @c2.many_to_many :tags, :class=>'::Historical::Tag'
1215
+
1216
+ n = @c2.new(:id => 1234)
1217
+ a = n.tags_dataset
1218
+ a.should be_a_kind_of(Sequel::Dataset)
1219
+ a.sql.should == 'SELECT tags.* FROM tags INNER JOIN nodes_tags ON ((nodes_tags.tag_id = tags.id) AND (nodes_tags.node_id = 1234))'
1220
+ end
1221
+
1222
+ it "should use explicit key values and join table if given" do
1223
+ @c2.many_to_many :attributes, :class => @c1, :left_key => :nodeid, :right_key => :attributeid, :join_table => :attribute2node
1224
+
1225
+ n = @c2.new(:id => 1234)
1226
+ a = n.attributes_dataset
1227
+ a.should be_a_kind_of(Sequel::Dataset)
1228
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attribute2node ON ((attribute2node.attributeid = attributes.id) AND (attribute2node.nodeid = 1234))'
1229
+ end
1230
+
1231
+ it "should support a conditions option" do
1232
+ @c2.many_to_many :attributes, :class => @c1, :conditions => {:a=>32}
1233
+ n = @c2.new(:id => 1234)
1234
+ a = n.attributes_dataset
1235
+ a.should be_a_kind_of(Sequel::Dataset)
1236
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) WHERE (a = 32)'
1237
+ @c2.many_to_many :attributes, :class => @c1, :conditions => ['a = ?', 32]
1238
+ n = @c2.new(:id => 1234)
1239
+ a = n.attributes_dataset
1240
+ a.should be_a_kind_of(Sequel::Dataset)
1241
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) WHERE (a = 32)'
1242
+ end
1243
+
1244
+ it "should support an order option" do
1245
+ @c2.many_to_many :attributes, :class => @c1, :order => :blah
1246
+
1247
+ n = @c2.new(:id => 1234)
1248
+ a = n.attributes_dataset
1249
+ a.should be_a_kind_of(Sequel::Dataset)
1250
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) ORDER BY blah'
1251
+ end
1252
+
1253
+ it "should support an array for the order option" do
1254
+ @c2.many_to_many :attributes, :class => @c1, :order => [:blah1, :blah2]
1255
+
1256
+ n = @c2.new(:id => 1234)
1257
+ a = n.attributes_dataset
1258
+ a.should be_a_kind_of(Sequel::Dataset)
1259
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) ORDER BY blah1, blah2'
1260
+ end
1261
+
1262
+ it "should support :left_primary_key and :right_primary_key options" do
1263
+ @c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
1264
+ @c2.new(:id => 1234, :xxx=>5).attributes_dataset.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.yyy) AND (attributes_nodes.node_id = 5))'
1265
+ end
1266
+
1267
+ it "should support a select option" do
1268
+ @c2.many_to_many :attributes, :class => @c1, :select => :blah
1269
+
1270
+ n = @c2.new(:id => 1234)
1271
+ a = n.attributes_dataset
1272
+ a.should be_a_kind_of(Sequel::Dataset)
1273
+ a.sql.should == 'SELECT blah FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234))'
1274
+ end
1275
+
1276
+ it "should support an array for the select option" do
1277
+ @c2.many_to_many :attributes, :class => @c1, :select => [:attributes.*, :attribute_nodes__blah2]
1278
+
1279
+ n = @c2.new(:id => 1234)
1280
+ a = n.attributes_dataset
1281
+ a.should be_a_kind_of(Sequel::Dataset)
1282
+ a.sql.should == 'SELECT attributes.*, attribute_nodes.blah2 FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234))'
1283
+ end
1284
+
1285
+ it "should accept a block" do
1286
+ @c2.many_to_many :attributes, :class => @c1 do |ds|
1287
+ ds.filter(:xxx => @xxx)
1288
+ end
1289
+
1290
+ n = @c2.new(:id => 1234)
1291
+ n.xxx = 555
1292
+ a = n.attributes
1293
+ a.should be_a_kind_of(Array)
1294
+ a.size.should == 1
1295
+ a.first.should be_a_kind_of(@c1)
1296
+ MODEL_DB.sqls.first.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) WHERE (xxx = 555)'
1297
+ end
1298
+
1299
+ it "should allow the :order option while accepting a block" do
1300
+ @c2.many_to_many :attributes, :class => @c1, :order=>[:blah1, :blah2] do |ds|
1301
+ ds.filter(:xxx => @xxx)
1302
+ end
1303
+
1304
+ n = @c2.new(:id => 1234)
1305
+ n.xxx = 555
1306
+ a = n.attributes
1307
+ a.should be_a_kind_of(Array)
1308
+ a.size.should == 1
1309
+ a.first.should be_a_kind_of(@c1)
1310
+ MODEL_DB.sqls.first.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) WHERE (xxx = 555) ORDER BY blah1, blah2'
1311
+ end
1312
+
1313
+ it "should have the block argument affect the _dataset method" do
1314
+ @c2.many_to_many :attributes, :class => @c1 do |ds|
1315
+ ds.filter(:xxx => 456)
1316
+ end
1317
+ @c2.new(:id => 1234).attributes_dataset.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) WHERE (xxx = 456)'
1318
+ end
1319
+
1320
+ it "should support a :dataset option that is used instead of the default" do
1321
+ c1 = @c1
1322
+ @c2.many_to_many :attributes, :class => @c1, :dataset=>proc{c1.join_table(:natural, :an).filter(:an__nodeid=>pk)}, :order=> :a, :limit=>10, :select=>nil do |ds|
1323
+ ds.filter(:xxx => @xxx)
1324
+ end
1325
+
1326
+ n = @c2.new(:id => 1234)
1327
+ n.xxx = 555
1328
+ n.attributes_dataset.sql.should == 'SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 10'
1329
+ a = n.attributes
1330
+ a.should be_a_kind_of(Array)
1331
+ a.size.should == 1
1332
+ a.first.should be_a_kind_of(@c1)
1333
+ MODEL_DB.sqls.first.should == 'SELECT * FROM attributes NATURAL JOIN an WHERE ((an.nodeid = 1234) AND (xxx = 555)) ORDER BY a LIMIT 10'
1334
+ end
1335
+
1336
+ it "should support a :limit option" do
1337
+ @c2.many_to_many :attributes, :class => @c1 , :limit=>10
1338
+ @c2.new(:id => 1234).attributes_dataset.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) LIMIT 10'
1339
+ @c2.many_to_many :attributes, :class => @c1 , :limit=>[10, 10]
1340
+ @c2.new(:id => 1234).attributes_dataset.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)) LIMIT 10 OFFSET 10'
1341
+ end
1342
+
1343
+ it "should have the :eager option affect the _dataset method" do
1344
+ @c2.many_to_many :attributes, :class => @c2 , :eager=>:attributes
1345
+ @c2.new(:id => 1234).attributes_dataset.opts[:eager].should == {:attributes=>nil}
1346
+ end
1347
+
1348
+ it "should define an add_ method" do
1349
+ @c2.many_to_many :attributes, :class => @c1
1350
+
1351
+ n = @c2.new(:id => 1234)
1352
+ a = @c1.new(:id => 2345)
1353
+ a.should == n.add_attribute(a)
1354
+ ['INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)',
1355
+ 'INSERT INTO attributes_nodes (attribute_id, node_id) VALUES (2345, 1234)'
1356
+ ].should(include(MODEL_DB.sqls.first))
1357
+ end
1358
+
1359
+ it "should define a remove_ method" do
1360
+ @c2.many_to_many :attributes, :class => @c1
1361
+
1362
+ n = @c2.new(:id => 1234)
1363
+ a = @c1.new(:id => 2345)
1364
+ a.should == n.remove_attribute(a)
1365
+ MODEL_DB.sqls.first.should == 'DELETE FROM attributes_nodes WHERE ((node_id = 1234) AND (attribute_id = 2345))'
1366
+ end
1367
+
1368
+ it "should have the add_ method respect the :left_primary_key and :right_primary_key options" do
1369
+ @c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
1370
+
1371
+ n = @c2.new(:id => 1234, :xxx=>5)
1372
+ a = @c1.new(:id => 2345, :yyy=>8)
1373
+ a.should == n.add_attribute(a)
1374
+ ['INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (5, 8)',
1375
+ 'INSERT INTO attributes_nodes (attribute_id, node_id) VALUES (8, 5)'
1376
+ ].should(include(MODEL_DB.sqls.first))
1377
+ end
1378
+
1379
+ it "should have the remove_ method respect the :left_primary_key and :right_primary_key options" do
1380
+ @c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx, :right_primary_key=>:yyy
1381
+
1382
+ n = @c2.new(:id => 1234, :xxx=>5)
1383
+ a = @c1.new(:id => 2345, :yyy=>8)
1384
+ a.should == n.remove_attribute(a)
1385
+ MODEL_DB.sqls.first.should == 'DELETE FROM attributes_nodes WHERE ((node_id = 5) AND (attribute_id = 8))'
1386
+ end
1387
+
1388
+ it "should raise an error if the model object doesn't have a valid primary key" do
1389
+ @c2.many_to_many :attributes, :class => @c1
1390
+ a = @c2.new
1391
+ n = @c1.load(:id=>123)
1392
+ proc{a.attributes_dataset}.should raise_error(Sequel::Error)
1393
+ proc{a.attributes}.should raise_error(Sequel::Error)
1394
+ proc{a.add_attribute(n)}.should raise_error(Sequel::Error)
1395
+ proc{a.remove_attribute(n)}.should raise_error(Sequel::Error)
1396
+ proc{a.remove_all_attributes}.should raise_error(Sequel::Error)
1397
+ end
1398
+
1399
+ it "should raise an error if trying to add/remove a model object that doesn't have a valid primary key" do
1400
+ @c2.many_to_many :attributes, :class => @c1
1401
+ n = @c1.new
1402
+ a = @c2.load(:id=>123)
1403
+ proc{a.add_attribute(n)}.should raise_error(Sequel::Error)
1404
+ proc{a.remove_attribute(n)}.should raise_error(Sequel::Error)
1405
+ end
1406
+
1407
+ it "should provide an array with all members of the association" do
1408
+ @c2.many_to_many :attributes, :class => @c1
1409
+
1410
+ n = @c2.new(:id => 1234)
1411
+ atts = n.attributes
1412
+ atts.should be_a_kind_of(Array)
1413
+ atts.size.should == 1
1414
+ atts.first.should be_a_kind_of(@c1)
1415
+
1416
+ MODEL_DB.sqls.first.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234))'
1417
+ end
1418
+
1419
+ it "should set cached instance variable when accessed" do
1420
+ @c2.many_to_many :attributes, :class => @c1
1421
+
1422
+ n = @c2.new(:id => 1234)
1423
+ MODEL_DB.reset
1424
+ n.associations.include?(:attributes).should == false
1425
+ atts = n.attributes
1426
+ atts.should == n.associations[:attributes]
1427
+ MODEL_DB.sqls.length.should == 1
1428
+ end
1429
+
1430
+ it "should use cached instance variable if available" do
1431
+ @c2.many_to_many :attributes, :class => @c1
1432
+
1433
+ n = @c2.new(:id => 1234)
1434
+ MODEL_DB.reset
1435
+ n.associations[:attributes] = 42
1436
+ n.attributes.should == 42
1437
+ MODEL_DB.sqls.should == []
1438
+ end
1439
+
1440
+ it "should not use cached instance variable if asked to reload" do
1441
+ @c2.many_to_many :attributes, :class => @c1
1442
+
1443
+ n = @c2.new(:id => 1234)
1444
+ MODEL_DB.reset
1445
+ n.associations[:attributes] = 42
1446
+ n.attributes(true).should_not == 42
1447
+ MODEL_DB.sqls.length.should == 1
1448
+ end
1449
+
1450
+ it "should add item to cached instance variable if it exists when calling add_" do
1451
+ @c2.many_to_many :attributes, :class => @c1
1452
+
1453
+ n = @c2.new(:id => 1234)
1454
+ att = @c1.new(:id => 345)
1455
+ MODEL_DB.reset
1456
+ a = []
1457
+ n.associations[:attributes] = a
1458
+ n.add_attribute(att)
1459
+ a.should == [att]
1460
+ end
1461
+
1462
+ it "should add item to reciprocal cached instance variable if it exists when calling add_" do
1463
+ @c2.many_to_many :attributes, :class => @c1
1464
+ @c1.many_to_many :nodes, :class => @c2
1465
+
1466
+ n = @c2.new(:id => 1234)
1467
+ att = @c1.new(:id => 345)
1468
+ att.associations[:nodes] = []
1469
+ n.add_attribute(att)
1470
+ att.nodes.should == [n]
1471
+ end
1472
+
1473
+ it "should remove item from cached instance variable if it exists when calling remove_" do
1474
+ @c2.many_to_many :attributes, :class => @c1
1475
+
1476
+ n = @c2.new(:id => 1234)
1477
+ att = @c1.new(:id => 345)
1478
+ MODEL_DB.reset
1479
+ a = [att]
1480
+ n.associations[:attributes] = a
1481
+ n.remove_attribute(att)
1482
+ a.should == []
1483
+ end
1484
+
1485
+ it "should remove item from reciprocal cached instance variable if it exists when calling remove_" do
1486
+ @c2.many_to_many :attributes, :class => @c1
1487
+ @c1.many_to_many :nodes, :class => @c2
1488
+
1489
+ n = @c2.new(:id => 1234)
1490
+ att = @c1.new(:id => 345)
1491
+ att.associations[:nodes] = [n]
1492
+ n.remove_attribute(att)
1493
+ att.nodes.should == []
1494
+ end
1495
+
1496
+ it "should not create the add_, remove_, or remove_all_ methods if :read_only option is used" do
1497
+ @c2.many_to_many :attributes, :class => @c1, :read_only=>true
1498
+ im = @c2.instance_methods.collect{|x| x.to_s}
1499
+ im.should(include('attributes'))
1500
+ im.should(include('attributes_dataset'))
1501
+ im.should_not(include('add_attribute'))
1502
+ im.should_not(include('remove_attribute'))
1503
+ im.should_not(include('remove_all_attributes'))
1504
+ end
1505
+
1506
+ it "should not add associations methods directly to class" do
1507
+ @c2.many_to_many :attributes, :class => @c1
1508
+ im = @c2.instance_methods.collect{|x| x.to_s}
1509
+ im.should(include('attributes'))
1510
+ im.should(include('attributes_dataset'))
1511
+ im.should(include('add_attribute'))
1512
+ im.should(include('remove_attribute'))
1513
+ im.should(include('remove_all_attributes'))
1514
+ im2 = @c2.instance_methods(false).collect{|x| x.to_s}
1515
+ im2.should_not(include('attributes'))
1516
+ im2.should_not(include('attributes_dataset'))
1517
+ im2.should_not(include('add_attribute'))
1518
+ im2.should_not(include('remove_attribute'))
1519
+ im2.should_not(include('remove_all_attributes'))
1520
+ end
1521
+
1522
+ it "should have has_and_belongs_to_many alias" do
1523
+ @c2.has_and_belongs_to_many :attributes, :class => @c1
1524
+
1525
+ n = @c2.new(:id => 1234)
1526
+ a = n.attributes_dataset
1527
+ a.should be_a_kind_of(Sequel::Dataset)
1528
+ a.sql.should == 'SELECT attributes.* FROM attributes INNER JOIN attributes_nodes ON ((attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234))'
1529
+ end
1530
+
1531
+ it "should have an remove_all_ method that removes all associations" do
1532
+ @c2.many_to_many :attributes, :class => @c1
1533
+ @c2.new(:id => 1234).remove_all_attributes
1534
+ MODEL_DB.sqls.first.should == 'DELETE FROM attributes_nodes WHERE (node_id = 1234)'
1535
+ end
1536
+
1537
+ it "should have the remove_all_ method respect the :left_primary_key option" do
1538
+ @c2.many_to_many :attributes, :class => @c1, :left_primary_key=>:xxx
1539
+ @c2.new(:id => 1234, :xxx=>5).remove_all_attributes
1540
+ MODEL_DB.sqls.first.should == 'DELETE FROM attributes_nodes WHERE (node_id = 5)'
1541
+ end
1542
+
1543
+ it "remove_all should set the cached instance variable to []" do
1544
+ @c2.many_to_many :attributes, :class => @c1
1545
+ node = @c2.new(:id => 1234)
1546
+ node.remove_all_attributes
1547
+ node.associations[:attributes].should == []
1548
+ end
1549
+
1550
+ it "remove_all should return the array of previously associated items if the cached instance variable exists" do
1551
+ @c2.many_to_many :attributes, :class => @c1
1552
+ attrib = @c1.new(:id=>3)
1553
+ node = @c2.new(:id => 1234)
1554
+ d = @c1.dataset
1555
+ def d.fetch_rows(s); end
1556
+ node.attributes.should == []
1557
+ node.add_attribute(attrib)
1558
+ node.associations[:attributes].should == [attrib]
1559
+ node.remove_all_attributes.should == [attrib]
1560
+ end
1561
+
1562
+ it "remove_all should return nil if the cached instance variable does not exist" do
1563
+ @c2.many_to_many :attributes, :class => @c1
1564
+ @c2.new(:id => 1234).remove_all_attributes.should == nil
1565
+ end
1566
+
1567
+ it "remove_all should remove the current item from all reciprocal instance varaibles if it cached instance variable exists" do
1568
+ @c2.many_to_many :attributes, :class => @c1
1569
+ @c1.many_to_many :nodes, :class => @c2
1570
+ d = @c1.dataset
1571
+ def d.fetch_rows(s); end
1572
+ d = @c2.dataset
1573
+ def d.fetch_rows(s); end
1574
+ attrib = @c1.new(:id=>3)
1575
+ node = @c2.new(:id => 1234)
1576
+ node.attributes.should == []
1577
+ attrib.nodes.should == []
1578
+ node.add_attribute(attrib)
1579
+ attrib.associations[:nodes].should == [node]
1580
+ node.remove_all_attributes
1581
+ attrib.associations[:nodes].should == []
1582
+ end
1583
+
1584
+ it "should call an _add_ method internally to add attributes" do
1585
+ @c2.many_to_many :attributes, :class => @c1
1586
+ @c2.private_instance_methods.collect{|x| x.to_s}.sort.should(include("_add_attribute"))
1587
+ p = @c2.load(:id=>10)
1588
+ c = @c1.load(:id=>123)
1589
+ def p._add_attribute(x)
1590
+ @x = x
1591
+ end
1592
+ p.add_attribute(c)
1593
+ p.instance_variable_get(:@x).should == c
1594
+ MODEL_DB.sqls.should == []
1595
+ end
1596
+
1597
+ it "should call a _remove_ method internally to remove attributes" do
1598
+ @c2.many_to_many :attributes, :class => @c1
1599
+ @c2.private_instance_methods.collect{|x| x.to_s}.sort.should(include("_remove_attribute"))
1600
+ p = @c2.load(:id=>10)
1601
+ c = @c1.load(:id=>123)
1602
+ def p._remove_attribute(x)
1603
+ @x = x
1604
+ end
1605
+ p.remove_attribute(c)
1606
+ p.instance_variable_get(:@x).should == c
1607
+ MODEL_DB.sqls.should == []
1608
+ end
1609
+
1610
+ it "should support (before|after)_(add|remove) callbacks" do
1611
+ h = []
1612
+ @c2.many_to_many :attributes, :class => @c1, :before_add=>[proc{|x,y| h << x.pk; h << -y.pk}, :blah], :after_add=>proc{h << 3}, :before_remove=>:blah, :after_remove=>[:blahr]
1613
+ @c2.class_eval do
1614
+ @@blah = h
1615
+ def _add_attribute(v)
1616
+ @@blah << 4
1617
+ end
1618
+ def _remove_attribute(v)
1619
+ @@blah << 5
1620
+ end
1621
+ def blah(x)
1622
+ @@blah << x.pk
1623
+ end
1624
+ def blahr(x)
1625
+ @@blah << 6
1626
+ end
1627
+ end
1628
+ p = @c2.load(:id=>10)
1629
+ c = @c1.load(:id=>123)
1630
+ h.should == []
1631
+ p.add_attribute(c)
1632
+ h.should == [10, -123, 123, 4, 3]
1633
+ p.remove_attribute(c)
1634
+ h.should == [10, -123, 123, 4, 3, 123, 5, 6]
1635
+ end
1636
+
1637
+ it "should support after_load association callback" do
1638
+ h = []
1639
+ @c2.many_to_many :attributes, :class => @c1, :after_load=>[proc{|x,y| h << [x.pk, y.collect{|z|z.pk}]}, :al]
1640
+ @c2.class_eval do
1641
+ @@blah = h
1642
+ def al(v)
1643
+ v.each{|x| @@blah << x.pk}
1644
+ end
1645
+ end
1646
+ @c1.class_eval do
1647
+ def @dataset.fetch_rows(sql)
1648
+ yield({:id=>20})
1649
+ yield({:id=>30})
1650
+ end
1651
+ end
1652
+ p = @c2.load(:id=>10, :parent_id=>20)
1653
+ attributes = p.attributes
1654
+ h.should == [[10, [20, 30]], 20, 30]
1655
+ attributes.collect{|a| a.pk}.should == [20, 30]
1656
+ end
1657
+
1658
+ it "should raise error and not call internal add or remove method if before callback returns false if raise_on_save_failure is true" do
1659
+ p = @c2.load(:id=>10)
1660
+ c = @c1.load(:id=>123)
1661
+ @c2.many_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
1662
+ p.should_receive(:ba).once.with(c).and_return(false)
1663
+ p.should_not_receive(:_add_attribute)
1664
+ p.should_not_receive(:_remove_attribute)
1665
+ p.associations[:attributes] = []
1666
+ p.raise_on_save_failure = true
1667
+ proc{p.add_attribute(c)}.should raise_error(Sequel::Error)
1668
+ p.attributes.should == []
1669
+ p.associations[:attributes] = [c]
1670
+ p.should_receive(:br).once.with(c).and_return(false)
1671
+ proc{p.remove_attribute(c)}.should raise_error(Sequel::Error)
1672
+ p.attributes.should == [c]
1673
+ end
1674
+
1675
+ it "should return nil and not call internal add or remove method if before callback returns false if raise_on_save_failure is false" do
1676
+ p = @c2.load(:id=>10)
1677
+ c = @c1.load(:id=>123)
1678
+ p.raise_on_save_failure = false
1679
+ @c2.many_to_many :attributes, :class => @c1, :before_add=>:ba, :before_remove=>:br
1680
+ p.should_receive(:ba).once.with(c).and_return(false)
1681
+ p.should_not_receive(:_add_attribute)
1682
+ p.should_not_receive(:_remove_attribute)
1683
+ p.associations[:attributes] = []
1684
+ p.add_attribute(c).should == nil
1685
+ p.attributes.should == []
1686
+ p.associations[:attributes] = [c]
1687
+ p.should_receive(:br).once.with(c).and_return(false)
1688
+ p.remove_attribute(c).should == nil
1689
+ p.attributes.should == [c]
1690
+ end
1691
+
1692
+ it "should support a :uniq option that removes duplicates from the association" do
1693
+ h = []
1694
+ @c2.many_to_many :attributes, :class => @c1, :uniq=>true
1695
+ @c1.class_eval do
1696
+ def @dataset.fetch_rows(sql)
1697
+ yield({:id=>20})
1698
+ yield({:id=>30})
1699
+ yield({:id=>20})
1700
+ yield({:id=>30})
1701
+ end
1702
+ end
1703
+ @c2.load(:id=>10, :parent_id=>20).attributes.should == [@c1.load(:id=>20), @c1.load(:id=>30)]
1704
+ end
1705
+ end
1706
+
1707
+ describe Sequel::Model, " association reflection methods" do
1708
+ before do
1709
+ MODEL_DB.reset
1710
+ @c1 = Class.new(Sequel::Model(:nodes)) do
1711
+ def self.name; 'Node'; end
1712
+ def self.to_s; 'Node'; end
1713
+ end
1714
+ end
1715
+
1716
+ it "#all_association_reflections should include all association reflection hashes" do
1717
+ @c1.all_association_reflections.should == []
1718
+
1719
+ @c1.associate :many_to_one, :parent, :class => @c1
1720
+ @c1.all_association_reflections.collect{|v| v[:name]}.should == [:parent]
1721
+ @c1.all_association_reflections.collect{|v| v[:type]}.should == [:many_to_one]
1722
+ @c1.all_association_reflections.collect{|v| v[:class]}.should == [@c1]
1723
+
1724
+ @c1.associate :one_to_many, :children, :class => @c1
1725
+ @c1.all_association_reflections.sort_by{|x|x[:name].to_s}
1726
+ @c1.all_association_reflections.sort_by{|x|x[:name].to_s}.collect{|v| v[:name]}.should == [:children, :parent]
1727
+ @c1.all_association_reflections.sort_by{|x|x[:name].to_s}.collect{|v| v[:type]}.should == [:one_to_many, :many_to_one]
1728
+ @c1.all_association_reflections.sort_by{|x|x[:name].to_s}.collect{|v| v[:class]}.should == [@c1, @c1]
1729
+ end
1730
+
1731
+ it "#association_reflection should return nil for nonexistent association" do
1732
+ @c1.association_reflection(:blah).should == nil
1733
+ end
1734
+
1735
+ it "#association_reflection should return association reflection hash if association exists" do
1736
+ @c1.associate :many_to_one, :parent, :class => @c1
1737
+ @c1.association_reflection(:parent).should be_a_kind_of(Sequel::Model::Associations::AssociationReflection)
1738
+ @c1.association_reflection(:parent)[:name].should == :parent
1739
+ @c1.association_reflection(:parent)[:type].should == :many_to_one
1740
+ @c1.association_reflection(:parent)[:class].should == @c1
1741
+
1742
+ @c1.associate :one_to_many, :children, :class => @c1
1743
+ @c1.association_reflection(:children).should be_a_kind_of(Sequel::Model::Associations::AssociationReflection)
1744
+ @c1.association_reflection(:children)[:name].should == :children
1745
+ @c1.association_reflection(:children)[:type].should == :one_to_many
1746
+ @c1.association_reflection(:children)[:class].should == @c1
1747
+ end
1748
+
1749
+ it "#associations should include all association names" do
1750
+ @c1.associations.should == []
1751
+ @c1.associate :many_to_one, :parent, :class => @c1
1752
+ @c1.associations.should == [:parent]
1753
+ @c1.associate :one_to_many, :children, :class => @c1
1754
+ @c1.associations.sort_by{|x|x.to_s}.should == [:children, :parent]
1755
+ end
1756
+
1757
+ it "association reflections should be copied upon subclasing" do
1758
+ @c1.associate :many_to_one, :parent, :class => @c1
1759
+ c = Class.new(@c1)
1760
+ @c1.associations.should == [:parent]
1761
+ c.associations.should == [:parent]
1762
+ c.associate :many_to_one, :parent2, :class => @c1
1763
+ @c1.associations.should == [:parent]
1764
+ c.associations.sort_by{|x| x.to_s}.should == [:parent, :parent2]
1765
+ c.instance_methods.map{|x| x.to_s}.should include('parent')
1766
+ end
1767
+ end