dm-constraints 0.9.11 → 0.10.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.
@@ -1,566 +1,607 @@
1
- require 'pathname'
2
- require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
1
+ require 'spec_helper'
3
2
 
4
- ADAPTERS.each do |adapter|
5
- describe 'DataMapper::Constraints' do
6
- before do
7
- DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[adapter]
3
+ ADAPTERS.each do |name, connection_uri|
4
+ describe 'DataMapper::Constraints', "(with #{name})" do
5
+ before :all do
6
+ @adapter = DataMapper.setup(:default, connection_uri)
7
+ @repository = DataMapper.repository(@adapter.name)
8
8
 
9
- class ::Stable
9
+ class ::Article
10
10
  include DataMapper::Resource
11
11
  include DataMapper::Constraints
12
12
 
13
- property :id, Serial
14
- property :location, String
15
- property :size, Integer
13
+ property :id, Serial
14
+ property :title, String, :nullable => false
15
+ property :content, Text
16
16
 
17
- has n, :cows
17
+ has 1, :revision
18
+ has n, :comments
19
+ has n, :authors, :through => Resource
18
20
  end
19
21
 
20
- class ::Farmer
22
+ class ::Author
21
23
  include DataMapper::Resource
22
24
  include DataMapper::Constraints
23
25
 
24
26
  property :first_name, String, :key => true
25
27
  property :last_name, String, :key => true
26
28
 
27
- has n, :cows
29
+ has n, :comments
30
+ has n, :articles, :through => Resource
28
31
  end
29
32
 
30
- class ::Cow
31
- include DataMapper::Resource
32
- include DataMapper::Constraints
33
-
34
- property :id, Serial
35
- property :name, String
36
- property :breed, String
37
-
38
- belongs_to :stable
39
- belongs_to :farmer
40
- end
41
-
42
- #Used to test a belongs_to association with no has() association
43
- #on the other end
44
- class ::Pig
33
+ class ::Comment
45
34
  include DataMapper::Resource
46
35
  include DataMapper::Constraints
47
36
 
48
37
  property :id, Serial
49
- property :name, String
38
+ property :body, Text
50
39
 
51
- belongs_to :farmer
40
+ belongs_to :article
41
+ belongs_to :author
52
42
  end
53
43
 
54
- #Used to test M:M :through => Resource relationships
55
- class ::Chicken
44
+ # Used to test a belongs_to association with no has() association
45
+ # on the other end
46
+ class ::Revision
56
47
  include DataMapper::Resource
57
48
  include DataMapper::Constraints
58
49
 
59
50
  property :id, Serial
60
- property :name, String
51
+ property :text, String
61
52
 
62
- has n, :tags, :through => Resource
53
+ belongs_to :article
63
54
  end
55
+ end
64
56
 
65
- class ::Tag
66
- include DataMapper::Resource
67
- include DataMapper::Constraints
68
-
69
- property :id, Serial
70
- property :phrase, String
57
+ describe 'create related objects' do
58
+ before :all do
59
+ class ::Comment
60
+ belongs_to :article, :nullable => true
61
+ belongs_to :author, :nullable => true
62
+ end
71
63
 
72
- has n, :chickens, :through => Resource
64
+ class ::Revision
65
+ belongs_to :article, :nullable => true
66
+ end
73
67
  end
74
68
 
75
- DataMapper.auto_migrate!
76
- end # before
77
-
78
- it "is included when DataMapper::Constraints is loaded" do
79
- Cow.new.should be_kind_of(DataMapper::Constraints)
80
- end
81
-
82
- it "should be able to create related objects with a foreign key constraint" do
83
- @s = Stable.create(:location => "Hometown")
84
- @c1 = Cow.create(:name => "Bea", :stable => @s)
85
- end
69
+ it 'should be able to create related objects with a foreign key constraint' do
70
+ @article = Article.create(:title => 'Man on the Moon')
71
+ @comment = @article.comments.create(:body => 'So true!')
72
+ end
86
73
 
87
- it "should be able to create related objects with a composite foreign key constraint" do
88
- @f = Farmer.create(:first_name => "John", :last_name => "Doe")
89
- @c1 = Cow.create(:name => "Bea", :farmer => @f)
90
- end
74
+ it 'should be able to create related objects with a composite foreign key constraint' do
75
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
76
+ @comment = @author.comments.create(:body => 'So true!')
77
+ end
91
78
 
92
- it "should not be able to create related objects with a failing foreign key constraint" do
93
- s = Stable.create
94
- lambda { @c1 = Cow.create(:name => "Bea", :stable_id => s.id + 1) }.should raise_error
79
+ it 'should not be able to create related objects with a failing foreign key constraint' do
80
+ article = Article.create(:title => 'Man on the Moon')
81
+ lambda { Comment.create(:body => 'So true!', :article_id => article_id.id + 1) }.should raise_error
82
+ end
95
83
  end
96
84
 
97
- describe "belongs_to without matching has association" do
85
+ describe 'belongs_to without matching has association' do
98
86
  before do
99
- @f1 = Farmer.create(:first_name => "John", :last_name => "Doe")
100
- @f2 = Farmer.create(:first_name => "Some", :last_name => "Body")
101
- @p = Pig.create(:name => "Bea", :farmer => @f2)
102
- end
103
- it "should destroy the parent if there are no children in the association" do
104
- @f1.destroy.should == true
87
+ @article = Article.create(:title => 'Man on the Moon')
88
+ @other_article = Article.create(:title => 'Dolly cloned')
89
+ @revision = Revision.create(:text => 'Riveting!', :article => @other_article)
105
90
  end
106
91
 
107
- it "the child should be destroyable" do
108
- @p.destroy.should == true
92
+ it 'should destroy the parent if there are no children in the association' do
93
+ @article.destroy.should be_true
94
+ @article.model.get(*@article.key).should be_nil
109
95
  end
110
96
 
97
+ it 'the child should be destroyable' do
98
+ @revision.destroy.should be_true
99
+ @revision.model.get(*@revision.key).should be_nil
100
+ end
111
101
  end
112
102
 
113
- describe "constraint options" do
114
-
115
- describe "when no constraint options are given" do
116
-
117
- it "should destroy the parent if there are no children in the association" do
118
- @f1 = Farmer.create(:first_name => "John", :last_name => "Doe")
119
- @f2 = Farmer.create(:first_name => "Some", :last_name => "Body")
120
- @c1 = Cow.create(:name => "Bea", :farmer => @f2)
121
- @f1.destroy.should == true
103
+ describe 'constraint options' do
104
+ describe 'when no constraint options are given' do
105
+ before do
106
+ @article = Article.create(:title => 'Man on the Moon')
107
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
108
+ @other_author = Author.create(:first_name => 'Joe', :last_name => 'Smith')
109
+ @comment = @other_author.comments.create(:body => 'So true!', :article => @article)
122
110
  end
123
111
 
124
- it "should not destroy the parent if there are children in the association" do
125
- @f = Farmer.create(:first_name => "John", :last_name => "Doe")
126
- @c1 = Cow.create(:name => "Bea", :farmer => @f)
127
- @f.destroy.should == false
112
+ it 'should destroy the parent if there are no children in the association' do
113
+ @author.destroy.should be_true
114
+ @author.model.get(*@author.key).should be_nil
128
115
  end
129
116
 
117
+ it 'should not destroy the parent if there are children in the association' do
118
+ @other_author.destroy.should be_false
119
+ @other_author.model.get(*@other_author.key).should_not be_nil
120
+ end
130
121
  end
131
122
 
132
- describe "when :constraint => :protect is given" do
133
- before do
134
- class ::Farmer
135
- has n, :cows, :constraint => :protect
136
- has 1, :pig, :constraint => :protect
137
- end
138
- class ::Pig
139
- belongs_to :farmer
123
+ describe 'when :constraint => :protect is given' do
124
+ before :all do
125
+ class ::Article
126
+ has 1, :revision, :constraint => :protect
127
+ has n, :comments, :constraint => :protect
128
+ has n, :authors, :constraint => :protect, :through => Resource
140
129
  end
141
- class ::Cow
142
- belongs_to :farmer
130
+
131
+ class ::Author
132
+ has n, :comments, :constraint => :protect
133
+ has n, :articles, :constraint => :protect, :through => Resource
143
134
  end
144
- class ::Chicken
145
- has n, :tags, :through => Resource, :constraint => :protect
135
+
136
+ class ::Comment
137
+ belongs_to :article
138
+ belongs_to :author
146
139
  end
147
- class ::Tag
148
- has n, :chickens, :through => Resource, :constraint => :protect
140
+
141
+ class ::Revision
142
+ belongs_to :article
149
143
  end
150
144
  end
151
145
 
152
- describe "one-to-one associations" do
146
+ describe 'one-to-one associations' do
153
147
  before do
154
- @f1 = Farmer.create(:first_name => "Mary", :last_name => "Smith")
155
- @p1 = Pig.create(:name => "Morton",:farmer => @f1)
148
+ @article = Article.create(:title => 'Man on the Moon')
149
+ @revision = Revision.create(:text => 'Riveting!', :article => @article)
156
150
  end
157
151
 
158
- it "should not destroy the parent if there are children in the association" do
159
- @f1.destroy.should == false
152
+ it 'should not destroy the parent if there are children in the association' do
153
+ @article.destroy.should be_false
154
+ @article.model.get(*@article.key).should_not be_nil
160
155
  end
161
156
 
162
- it "the child should be destroyable" do
163
- @p1.destroy.should == true
157
+ it 'the child should be destroyable' do
158
+ @revision.destroy.should be_true
159
+ @revision.model.get(*@revision.key).should be_nil
164
160
  end
165
161
  end
166
162
 
167
- describe "one-to-many associations" do
163
+ describe 'one-to-many associations' do
168
164
  before do
169
- @f1 = Farmer.create(:first_name => "John", :last_name => "Doe")
170
- @f2 = Farmer.create(:first_name => "Some", :last_name => "Body")
171
- @c1 = Cow.create(:name => "Bea", :farmer => @f2)
165
+ @article = Article.create(:title => 'Man on the Moon')
166
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
167
+ @another_author = Author.create(:first_name => 'Joe', :last_name => 'Smith')
168
+ @comment = @another_author.comments.create(:body => 'So true!', :article => @article)
172
169
  end
173
170
 
174
- it "should destroy the parent if there are no children in the association" do
175
- @f1.destroy.should == true
171
+ it 'should destroy the parent if there are no children in the association' do
172
+ @author.destroy.should be_true
173
+ @author.model.get(*@author.key).should be_nil
176
174
  end
177
175
 
178
- it "should not destroy the parent if there are children in the association" do
179
- @f2.destroy.should == false
176
+ it 'should not destroy the parent if there are children in the association' do
177
+ @another_author.destroy.should be_false
180
178
  end
181
179
 
182
- it "the child should be destroyable" do
183
- @c1.destroy.should == true
180
+ it 'the child should be destroyable' do
181
+ @comment.destroy.should be_true
182
+ @comment.model.get(*@comment.key).should be_nil
184
183
  end
185
184
  end
186
185
 
187
- describe "many-to-many associations" do
186
+ describe 'many-to-many associations' do
188
187
  before do
189
- @t1 = Tag.create(:phrase => "silly chicken")
190
- @t2 = Tag.create(:phrase => "serious chicken")
191
- @chk1 = Chicken.create(:name =>"Frank the Chicken", :tags => [@t2])
188
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
189
+ @another_author = Author.create(:first_name => 'Joe', :last_name => 'Smith')
190
+ @article = Article.create(:title => 'Man on the Moon', :authors => [ @author ])
192
191
  end
193
192
 
194
- it "should destroy the parent if there are no children in the association" do
195
- @t1.destroy.should == true
193
+ it 'should destroy the parent if there are no children in the association' do
194
+ @another_author.destroy.should be_true
195
+ @another_author.model.get(*@another_author.key).should be_nil
196
196
  end
197
197
 
198
- it "should not destroy the parent if there are children in the association" do
199
- @t2.destroy.should == false
198
+ it 'should not destroy the parent if there are children in the association' do
199
+ @author.articles.should_not == []
200
+ @author.destroy.should be_false
200
201
  end
201
202
 
202
- it "the child should be destroyable" do
203
- @chk1.tags.clear
204
- @chk1.save.should == true
205
- @chk1.tags.should be_empty
203
+ it 'the child should be destroyable' do
204
+ @article.authors.clear
205
+ @article.save.should be_true
206
+ @article.authors.should be_empty
206
207
  end
207
208
  end
209
+ end
208
210
 
209
- end # when constraint protect is given
210
-
211
- describe "when :constraint => :destroy! is given" do
212
- before do
213
- class ::Farmer
214
- has n, :cows, :constraint => :destroy!
215
- end
216
- class ::Cow
217
- belongs_to :farmer
211
+ describe 'when :constraint => :destroy! is given' do
212
+ before :all do
213
+ class ::Article
214
+ has 1, :revision, :constraint => :destroy!
215
+ has n, :comments, :constraint => :destroy!
216
+ has n, :authors, :constraint => :destroy!, :through => Resource
218
217
  end
219
- class ::Chicken
220
- has n, :tags, :through => Resource, :constraint => :destroy!
218
+
219
+ class ::Author
220
+ has n, :comments, :constraint => :destroy!
221
+ has n, :articles, :constraint => :destroy!, :through => Resource
221
222
  end
222
- class ::Tag
223
- has n, :chickens, :through => Resource, :constraint => :destroy!
223
+
224
+ class ::Comment
225
+ belongs_to :article
226
+ belongs_to :author
224
227
  end
225
228
 
226
- DataMapper.auto_migrate!
229
+ class ::Revision
230
+ belongs_to :article
231
+ end
227
232
  end
228
233
 
229
- describe "one-to-many associations" do
230
- before(:each) do
231
- @f = Farmer.create(:first_name => "John", :last_name => "Doe")
232
- @c1 = Cow.create(:name => "Bea", :farmer => @f)
233
- @c2 = Cow.create(:name => "Riksa", :farmer => @f)
234
+ describe 'one-to-one associations' do
235
+ before do
236
+ @article = Article.create(:title => 'Man on the Moon')
237
+ @revision = Revision.create(:text => 'Riveting!', :article => @article)
234
238
  end
235
239
 
236
- it "should let the parent to be destroyed" do
237
- @f.destroy.should == true
238
- @f.should be_new_record
240
+ it 'should let the parent to be destroyed' do
241
+ @article.destroy.should be_true
242
+ @article.model.get(*@article.key).should be_nil
239
243
  end
240
244
 
241
- it "should destroy the children" do
242
- @f.destroy
243
- @f.cows.all? { |c| c.should be_new_record }
245
+ it 'should destroy the children' do
246
+ revision = @article.revision
247
+ @article.destroy.should be_true
248
+ revision.model.get(*revision.key).should be_nil
244
249
  end
245
250
 
246
- it "the child should be destroyable" do
247
- @c1.destroy.should == true
251
+ it 'the child should be destroyable' do
252
+ @revision.destroy.should be_true
253
+ @revision.model.get(*@revision.key).should be_nil
248
254
  end
249
-
250
255
  end
251
256
 
252
- describe "many-to-many associations" do
257
+ describe 'one-to-many associations' do
253
258
  before do
254
- @t1 = Tag.create(:phrase => "floozy")
255
- @t2 = Tag.create(:phrase => "dirty")
256
- @chk1 = Chicken.create(:name => "Nancy Chicken", :tags => [@t1, @t2])
259
+ @article = Article.create(:title => 'Man on the Moon')
260
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
261
+ @comment = @author.comments.create(:body => 'So true!', :article => @article)
262
+ @another_comment = @author.comments.create(:body => 'Nice comment', :article => @article)
257
263
  end
258
264
 
259
- it "should destroy! the parent and the children, too" do
260
- @chk1.destroy.should == true
261
- @chk1.should be_new_record
265
+ it 'should let the parent to be destroyed' do
266
+ @author.destroy.should be_true
267
+ @author.model.get(*@author.key).should be_nil
268
+ end
262
269
 
263
- # @t1 & @t2 should still exist, the chicken_tags should have been deleted
264
- ChickenTag.all.should be_empty
265
- @t1.should_not be_new_record
266
- @t2.should_not be_new_record
270
+ it 'should destroy the children' do
271
+ @author.destroy.should be_true
272
+ @author.comments.all? { |comment| comment.should be_new }
267
273
  end
268
274
 
269
- it "the child should be destroyable" do
270
- @chk1.destroy.should == true
275
+ it 'the child should be destroyable' do
276
+ @comment.destroy.should be_true
277
+ @comment.model.get(*@comment.key).should be_nil
271
278
  end
272
279
  end
273
280
 
274
- end # when :constraint => :destroy! is given
281
+ describe 'many-to-many associations' do
282
+ before do
283
+ @article = Article.create(:title => 'Man on the Moon')
284
+ @other_article = Article.create(:title => 'Dolly cloned')
285
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe', :articles => [ @article, @other_article ])
286
+ end
275
287
 
276
- describe "when :constraint => :destroy is given" do
277
- before do
278
- class ::Farmer
279
- has n, :cows, :constraint => :destroy
280
- has 1, :pig, :constraint => :destroy
288
+ it 'should let the parent to be destroyed' do
289
+ @author.destroy.should be_true
290
+ @author.model.get(*@author.key).should be_nil
281
291
  end
282
- class ::Cow
283
- belongs_to :farmer
292
+
293
+ it 'should destroy the children' do
294
+ @author.destroy.should be_true
295
+ @article.model.get(*@article.key).should be_nil
296
+ @other_article.model.get(*@other_article.key).should be_nil
284
297
  end
285
- class ::Chicken
286
- has n, :tags, :through => Resource, :constraint => :destroy
298
+
299
+ it 'the child should be destroyable' do
300
+ @article.destroy.should be_true
301
+ @article.model.get(*@article.key).should be_nil
287
302
  end
288
- class ::Tag
289
- has n, :chickens, :through => Resource, :constraint => :destroy
303
+ end
304
+ end
305
+
306
+ describe 'when :constraint => :destroy is given' do
307
+ before :all do
308
+ class ::Article
309
+ has 1, :revision, :constraint => :destroy
310
+ has n, :comments, :constraint => :destroy
311
+ has n, :authors, :constraint => :destroy, :through => Resource
290
312
  end
291
313
 
292
- DataMapper.auto_migrate!
314
+ class ::Author
315
+ has n, :comments, :constraint => :destroy
316
+ has n, :articles, :constraint => :destroy, :through => Resource
317
+ end
318
+
319
+ class ::Comment
320
+ belongs_to :article
321
+ belongs_to :author
322
+ end
323
+
324
+ class ::Revision
325
+ belongs_to :article
326
+ end
293
327
  end
294
328
 
295
- describe "one-to-one associations" do
329
+ describe 'one-to-one associations' do
296
330
  before do
297
- @f = Farmer.create(:first_name => "Ted", :last_name => "Cornhusker")
298
- @p = Pig.create(:name => "BaconBits", :farmer => @f)
331
+ @article = Article.create(:title => 'Man on the Moon')
332
+ @revision = Revision.create(:text => 'Riveting!', :article => @article)
299
333
  end
300
334
 
301
- it "should let the parent to be destroyed" do
302
- @f.destroy.should == true
303
- @f.should be_new_record
335
+ it 'should let the parent to be destroyed' do
336
+ @article.destroy.should be_true
337
+ @article.model.get(*@article.key).should be_nil
304
338
  end
305
339
 
306
- it "should destroy the children" do
307
- pig = @f.pig
308
- @f.destroy
309
- pig.should be_new_record
340
+ it 'should destroy the children' do
341
+ revision = @article.revision
342
+ @article.destroy.should be_true
343
+ revision.model.get(*revision.key).should be_nil
310
344
  end
311
345
 
312
- it "the child should be destroyable" do
313
- @p.destroy.should == true
346
+ it 'the child should be destroyable' do
347
+ @revision.destroy.should be_true
348
+ @revision.model.get(*@revision.key).should be_nil
314
349
  end
315
350
  end
316
351
 
317
- describe "one-to-many associations" do
318
- before(:each) do
319
- @f = Farmer.create(:first_name => "John", :last_name => "Doe")
320
- @c1 = Cow.create(:name => "Bea", :farmer => @f)
321
- @c2 = Cow.create(:name => "Riksa", :farmer => @f)
352
+ describe 'one-to-many associations' do
353
+ before do
354
+ @article = Article.create(:title => 'Man on the Moon')
355
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
356
+ @comment = @author.comments.create(:body => 'So true!', :article => @article)
357
+ @other_comment = @author.comments.create(:body => "That's nonsense", :article => @article)
322
358
  end
323
359
 
324
- it "should let the parent to be destroyed" do
325
- @f.destroy.should == true
326
- @f.should be_new_record
360
+ it 'should let the parent to be destroyed' do
361
+ @author.destroy.should be_true
362
+ @author.model.get(*@author.key).should be_nil
327
363
  end
328
364
 
329
- it "should destroy the children" do
330
- @f.destroy
331
- @f.cows.all? { |c| c.should be_new_record }
332
- @f.should be_new_record
365
+ it 'should destroy the children' do
366
+ @author.destroy.should be_true
367
+ @author.comments.all? { |comment| comment.should be_new }
333
368
  end
334
369
 
335
- it "the child should be destroyable" do
336
- @c1.destroy.should == true
370
+ it 'the child should be destroyable' do
371
+ @comment.destroy.should be_true
372
+ @comment.model.get(*@comment.key).should be_nil
337
373
  end
338
-
339
374
  end
340
375
 
341
- describe "many-to-many associations" do
376
+ describe 'many-to-many associations' do
342
377
  before do
343
- @t1 = Tag.create :phrase => "floozy"
344
- @t2 = Tag.create :phrase => "dirty"
345
- @chk1 = Chicken.create :name => "Nancy Chicken", :tags => [@t1,@t2]
378
+ @article = Article.create(:title => 'Man on the Moon')
379
+ @other_article = Article.create(:title => 'Dolly cloned')
380
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe', :articles => [ @article, @other_article ])
346
381
  end
347
382
 
348
- it "should destroy the parent and the children, too" do
349
- @chk1.destroy.should == true
350
- @chk1.should be_new_record
383
+ it 'should destroy the parent and the children, too' do
384
+ @author.destroy.should be_true
385
+ @author.model.get(*@author.key).should be_nil
351
386
 
352
- #@t1 & @t2 should still exist, the chicken_tags should have been deleted
353
- ChickenTag.all.should be_empty
354
- @t1.should_not be_new_record
355
- @t2.should_not be_new_record
387
+ @article.model.get(*@article.key).should be_nil
388
+ @other_article.model.get(*@other_article.key).should be_nil
356
389
  end
357
390
 
358
- it "the child should be destroyable" do
359
- @chk1.destroy.should == true
391
+ it 'the child should be destroyable' do
392
+ @article.destroy.should be_true
393
+ @article.model.get(*@article.key).should be_nil
360
394
  end
361
395
  end
396
+ end
362
397
 
363
- end # when :constraint => :destroy is given
398
+ describe 'when :constraint => :set_nil is given' do
399
+ before :all do
400
+ # NOTE: M:M Relationships are not supported by :set_nil,
401
+ # see 'when checking constraint types' tests at bottom
364
402
 
365
- describe "when :constraint => :set_nil is given" do
366
- before do
367
- class ::Farmer
368
- has n, :cows, :constraint => :set_nil
369
- has 1, :pig, :constraint => :set_nil
403
+ class ::Article
404
+ has 1, :revision, :constraint => :set_nil
405
+ has n, :comments, :constraint => :set_nil
370
406
  end
371
- class ::Cow
372
- belongs_to :farmer
407
+
408
+ class ::Author
409
+ has n, :comments, :constraint => :set_nil
410
+ end
411
+
412
+ class ::Comment
413
+ belongs_to :article, :nullable => true
414
+ belongs_to :author, :nullable => true
415
+ end
416
+
417
+ class ::Revision
418
+ belongs_to :article, :nullable => true
373
419
  end
374
- # NOTE: M:M Relationships are not supported,
375
- # see "when checking constraint types" tests at bottom
376
- DataMapper.auto_migrate!
377
420
  end
378
421
 
379
- describe "one-to-one associations" do
422
+ describe 'one-to-one associations' do
380
423
  before do
381
- @f = Farmer.create(:first_name => "Mr", :last_name => "Hands")
382
- @p = Pig.create(:name => "Greasy", :farmer => @f)
424
+ @article = Article.create(:title => 'Man on the Moon')
425
+ @revision = Revision.create(:text => 'Riveting!', :article => @article)
383
426
  end
384
427
 
385
- it "should let the parent to be destroyed" do
386
- @f.destroy.should == true
428
+ it 'should let the parent to be destroyed' do
429
+ @article.destroy.should be_true
430
+ @article.model.get(*@article.key).should be_nil
387
431
  end
388
432
 
389
433
  it "should set the child's foreign_key id to nil" do
390
- pig = @f.pig
391
- @f.destroy.should == true
392
- pig.farmer.should be_nil
434
+ revision = @article.revision
435
+ @article.destroy.should be_true
436
+ revision.article.should be_nil
437
+ revision.model.get(*revision.key).article.should be_nil
393
438
  end
394
439
 
395
- it "the child should be destroyable" do
396
- @p.destroy.should == true
440
+ it 'the child should be destroyable' do
441
+ @revision.destroy.should be_true
442
+ @revision.model.get(*@revision.key).should be_nil
397
443
  end
398
-
399
444
  end
400
445
 
401
- describe "one-to-many associations" do
402
- before(:each) do
403
- @f = Farmer.create(:first_name => "John", :last_name => "Doe")
404
- @c1 = Cow.create(:name => "Bea", :farmer => @f)
405
- @c2 = Cow.create(:name => "Riksa", :farmer => @f)
446
+ describe 'one-to-many associations' do
447
+ before do
448
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
449
+ @comment = @author.comments.create(:body => 'So true!')
450
+ @other_comment = @author.comments.create(:body => "That's nonsense")
406
451
  end
407
452
 
408
- it "should let the parent to be destroyed" do
409
- @f.destroy.should == true
410
- @f.should be_new_record
453
+ it 'should let the parent to be destroyed' do
454
+ @author.destroy.should be_true
455
+ @author.model.get(*@author.key).should be_nil
411
456
  end
412
457
 
413
- it "should set the foreign_key ids of children to nil" do
414
- @f.destroy
415
- @f.cows.all? { |c| c.farmer.should be_nil }
458
+ it 'should set the foreign_key ids of children to nil' do
459
+ @author.destroy.should be_true
460
+ @author.comments.all? { |comment| comment.author.should be_nil }
416
461
  end
417
462
 
418
- it "the children should be destroyable" do
419
- @c1.destroy.should == true
420
- @c2.destroy.should == true
421
- end
463
+ it 'the children should be destroyable' do
464
+ @comment.destroy.should be_true
465
+ @comment.model.get(*@comment.key).should be_nil
422
466
 
467
+ @other_comment.destroy.should be_true
468
+ @other_comment.model.get(*@other_comment.key).should be_nil
469
+ end
423
470
  end
471
+ end
424
472
 
425
- end # describe "when :constraint => :set_nil is given" do
426
-
427
- describe "when :constraint => :skip is given" do
428
- before do
429
- class ::Farmer
430
- has n, :cows, :constraint => :skip
431
- has 1, :pig, :constraint => :skip
473
+ describe 'when :constraint => :skip is given' do
474
+ before :all do
475
+ class ::Article
476
+ has 1, :revision, :constraint => :skip
477
+ has n, :comments, :constraint => :skip
478
+ has n, :authors, :constraint => :skip, :through => Resource
432
479
  end
433
- class ::Cow
434
- belongs_to :farmer
480
+
481
+ class ::Author
482
+ has n, :comments, :constraint => :skip
483
+ has n, :articles, :constraint => :skip, :through => Resource
435
484
  end
436
- class ::Chicken
437
- has n, :tags, :through => Resource, :constraint => :skip
485
+
486
+ class ::Comment
487
+ belongs_to :article
488
+ belongs_to :author
438
489
  end
439
- class ::Tag
440
- has n, :chickens, :through => Resource, :constraint => :skip
490
+
491
+ class ::Revision
492
+ belongs_to :article
441
493
  end
442
- DataMapper.auto_migrate!
443
494
  end
444
495
 
445
- describe "one-to-one associations" do
496
+ describe 'one-to-one associations' do
446
497
  before do
447
- @f = Farmer.create(:first_name => "William", :last_name => "Shepard")
448
- @p = Pig.create(:name => "Jiggles The Pig", :farmer => @f)
498
+ @article = Article.create(:title => 'Man on the Moon')
499
+ @revision = Revision.create(:text => 'Riveting!', :article => @article)
449
500
  end
450
501
 
451
- it "should let the parent be destroyed" do
452
- @f.destroy.should == true
453
- @f.should be_new_record
454
- # @p.farmer.should be_new_record
502
+ it 'should let the parent be destroyed' do
503
+ @article.destroy.should be_true
504
+ @article.model.get(*@article.key).should be_nil
455
505
  end
456
506
 
457
- it "should let the children become orphan records" do
458
- @f.destroy
459
- @p.farmer.should be_new_record
507
+ it 'should let the children become orphan records' do
508
+ @article.destroy.should be_true
509
+ @revision.model.get(*@revision.key).article.should be_nil
460
510
  end
461
511
 
462
- it "the child should be destroyable" do
463
- @p.destroy.should == true
512
+ it 'the child should be destroyable' do
513
+ @revision.destroy.should be_true
514
+ @revision.model.get(*@revision.key).should be_nil
464
515
  end
465
-
466
516
  end
467
517
 
468
- describe "one-to-many associations" do
518
+ describe 'one-to-many associations' do
469
519
  before do
470
- @f = Farmer.create(:first_name => "John", :last_name => "Doe")
471
- @c1 = Cow.create(:name => "Bea", :farmer => @f)
472
- @c2 = Cow.create(:name => "Riksa", :farmer => @f)
520
+ @article = Article.create(:title => 'Man on the Moon')
521
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe')
522
+ @comment = @author.comments.create(:body => 'So true!', :article => @article)
523
+ @other_comment = @author.comments.create(:body => "That's nonsense", :article => @article)
473
524
  end
474
525
 
475
- it "should let the parent to be destroyed" do
476
- @f.destroy.should == true
477
- @f.should be_new_record
526
+ it 'should let the parent to be destroyed' do
527
+ @author.destroy.should be_true
528
+ @author.model.get(*@author.key).should be_nil
478
529
  end
479
530
 
480
- it "should let the children become orphan records" do
481
- @f.destroy
482
- @c1.farmer.should be_new_record
483
- @c2.farmer.should be_new_record
531
+ it 'should let the children become orphan records' do
532
+ @author.destroy.should be_true
533
+ @comment.model.get(*@comment.key).author.should be_nil
534
+ @other_comment.model.get(*@other_comment.key).author.should be_nil
484
535
  end
485
536
 
486
- it "the children should be destroyable" do
487
- @c1.destroy.should == true
488
- @c2.destroy.should == true
537
+ it 'the children should be destroyable' do
538
+ @comment.destroy.should be_true
539
+ @other_comment.destroy.should be_true
540
+ @other_comment.model.get(*@other_comment.key).should be_nil
489
541
  end
490
-
491
542
  end
492
543
 
493
- describe "many-to-many associations" do
544
+ describe 'many-to-many associations' do
494
545
  before do
495
- @t = Tag.create(:phrase => "Richard Pryor's Chicken")
496
- @chk = Chicken.create(:name => "Delicious", :tags => [@t])
546
+ @article = Article.create(:title => 'Man on the Moon')
547
+ @other_article = Article.create(:title => 'Dolly cloned')
548
+ @author = Author.create(:first_name => 'John', :last_name => 'Doe', :articles => [ @article, @other_article ])
497
549
  end
498
550
 
499
- it "the children should be destroyable" do
500
- @chk.destroy.should == true
551
+ it 'the children should be destroyable' do
552
+ @article.destroy.should be_true
553
+ @article.model.get(*@article.key).should be_nil
501
554
  end
502
555
  end
556
+ end
503
557
 
504
- end # describe "when :constraint => :skip is given"
505
-
506
- describe "when checking constraint types" do
507
-
508
- #M:M relationships results in a join table composed of a two part primary key
509
- # setting a portion of the primary key is not possible for two reasons:
558
+ describe 'when checking constraint types' do
559
+ # M:M relationships results in a join table composed of composite (composed of two parts)
560
+ # primary key.
561
+ # Setting a portion of this primary key is not possible for two reasons:
510
562
  # 1. the columns are defined as :nullable => false
511
563
  # 2. there could be duplicate rows if more than one of either of the types
512
564
  # was deleted while being associated to the same type on the other side of the relationshp
513
565
  # Given
514
- # Turkey(Name: Ted, ID: 1) =>
515
- # Tags[Tag(Phrase: awesome, ID: 1), Tag(Phrase: fat, ID: 2)]
516
- # Turkey(Name: Steve, ID: 2) =>
517
- # Tags[Tag(Phrase: awesome, ID: 1), Tag(Phrase: flamboyant, ID: 3)]
566
+ # Author(name: John Doe, ID: 1) =>
567
+ # Articles[Article(title: Man on the Moon, ID: 1), Article(title: Dolly cloned, ID: 2)]
568
+ # Author(Name: James Duncan, ID: 2) =>
569
+ # Articles[Article(title: Man on the Moon, ID: 1), Article(title: The end is nigh, ID: 3)]
518
570
  #
519
- # Table turkeys_tags would look like (turkey_id, tag_id)
571
+ # Table authors_articles would look like (author_id, article_id)
520
572
  # (1, 1)
521
573
  # (1, 2)
522
574
  # (2, 1)
523
575
  # (2, 3)
524
576
  #
525
- # If both turkeys were deleted and pk was set null
577
+ # If both articles were deleted and the primary key was set to null
526
578
  # (null, 1)
527
579
  # (null, 2)
528
- # (null, 1) #at this time there would be a duplicate row error
580
+ # (null, 1) # duplicate error!
529
581
  # (null, 3)
530
582
  #
531
583
  # I would suggest setting :constraint to :skip in this scenario which will leave
532
584
  # you with orphaned rows.
533
- it "should raise an error if :set_nil is given for a M:M relationship" do
534
- lambda{
535
- class ::Chicken
536
- has n, :tags, :through => Resource, :constraint => :set_nil
537
- end
538
- class ::Tag
539
- has n, :chickens, :through => Resource, :constraint => :set_nil
585
+ it 'should raise an error if :set_nil is given for a M:M relationship' do
586
+ lambda {
587
+ class ::Article
588
+ has n, :authors, :through => Resource, :constraint => :set_nil
540
589
  end
541
- }.should raise_error(ArgumentError)
542
- end
543
590
 
544
- # Resource#destroy! is not suppored in dm-core
545
- it "should raise an error if :destroy! is given for a 1:1 relationship" do
546
- lambda do
547
- class ::Farmer
548
- has 1, :pig, :constraint => :destroy!
591
+ class ::Author
592
+ has n, :articles, :through => Resource, :constraint => :set_nil
549
593
  end
550
- end.should raise_error(ArgumentError)
594
+ }.should raise_error(ArgumentError)
551
595
  end
552
596
 
553
- it "should raise an error if an unknown type is given" do
597
+ it 'should raise an error if an unknown type is given' do
554
598
  lambda do
555
- class ::Farmer
556
- has n, :cows, :constraint => :chocolate
599
+ class ::Author
600
+ has n, :articles, :constraint => :chocolate
557
601
  end
558
602
  end.should raise_error(ArgumentError)
559
603
  end
560
-
561
604
  end
562
-
563
- end # describe 'constraint options'
564
-
565
- end # DataMapper::Constraints
566
- end # ADAPTERS.each
605
+ end
606
+ end
607
+ end