schema_plus 1.5.1 → 1.5.3

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.
@@ -47,126 +47,175 @@ describe ActiveRecord::Migration do
47
47
  t.boolean :bool, :default => true
48
48
  end
49
49
  }.to_not raise_error
50
- @model.create.reload.bool.should be_true
50
+ expect(@model.create.reload.bool).to be true
51
51
  end
52
52
 
53
53
  it "should create auto foreign keys" do
54
54
  recreate_table(@model) do |t|
55
55
  t.integer :user_id
56
56
  end
57
- @model.should reference(:users, :id).on(:user_id)
57
+ expect(@model).to reference(:users, :id).on(:user_id)
58
58
  end
59
59
 
60
60
  it "should create explicit foreign key with default reference" do
61
61
  recreate_table(@model) do |t|
62
62
  t.integer :user, :foreign_key => true
63
63
  end
64
- @model.should reference(:users, :id).on(:user)
64
+ expect(@model).to reference(:users, :id).on(:user)
65
65
  end
66
66
 
67
67
  it "should create foreign key with different reference" do
68
68
  recreate_table(@model) do |t|
69
69
  t.integer :author_id, :foreign_key => { :references => :users }
70
70
  end
71
- @model.should reference(:users, :id).on(:author_id)
71
+ expect(@model).to reference(:users, :id).on(:author_id)
72
72
  end
73
73
 
74
74
  it "should create foreign key with different reference using shortcut" do
75
75
  recreate_table(@model) do |t|
76
76
  t.integer :author_id, :references => :users
77
77
  end
78
- @model.should reference(:users, :id).on(:author_id)
78
+ expect(@model).to reference(:users, :id).on(:author_id)
79
79
  end
80
80
 
81
81
  it "should create foreign key with default name" do
82
82
  recreate_table @model do |t|
83
83
  t.integer :user_id, :foreign_key => true
84
84
  end
85
- @model.should reference(:users, :id).with_name("fk_#{@model.table_name}_user_id")
85
+ expect(@model).to reference(:users, :id).with_name("fk_#{@model.table_name}_user_id")
86
86
  end
87
87
 
88
88
  it "should create foreign key with specified name" do
89
89
  recreate_table @model do |t|
90
90
  t.integer :user_id, :foreign_key => { :name => "wugga" }
91
91
  end
92
- @model.should reference(:users, :id).with_name("wugga")
92
+ expect(@model).to reference(:users, :id).with_name("wugga")
93
93
  end
94
94
 
95
95
  it "should suppress foreign key" do
96
96
  recreate_table(@model) do |t|
97
97
  t.integer :member_id, :foreign_key => false
98
98
  end
99
- @model.should_not reference.on(:member_id)
99
+ expect(@model).not_to reference.on(:member_id)
100
100
  end
101
101
 
102
102
  it "should suppress foreign key using shortcut" do
103
103
  recreate_table(@model) do |t|
104
104
  t.integer :member_id, :references => nil
105
105
  end
106
- @model.should_not reference.on(:member_id)
106
+ expect(@model).not_to reference.on(:member_id)
107
107
  end
108
108
 
109
109
  it "should create foreign key using t.belongs_to" do
110
110
  recreate_table(@model) do |t|
111
111
  t.belongs_to :user
112
112
  end
113
- @model.should reference(:users, :id).on(:user_id)
113
+ expect(@model).to reference(:users, :id).on(:user_id)
114
114
  end
115
115
 
116
116
  it "should not create foreign key using t.belongs_to with :polymorphic => true" do
117
117
  recreate_table(@model) do |t|
118
118
  t.belongs_to :user, :polymorphic => true
119
119
  end
120
- @model.should_not reference(:users, :id).on(:user_id)
120
+ expect(@model).not_to reference(:users, :id).on(:user_id)
121
121
  end
122
122
 
123
123
  it "should create foreign key using t.references" do
124
124
  recreate_table(@model) do |t|
125
125
  t.references :user
126
126
  end
127
- @model.should reference(:users, :id).on(:user_id)
127
+ expect(@model).to reference(:users, :id).on(:user_id)
128
128
  end
129
129
 
130
130
  it "should not create foreign key using t.references with :foreign_key => false" do
131
131
  recreate_table(@model) do |t|
132
132
  t.references :user, :foreign_key => false
133
133
  end
134
- @model.should_not reference(:users, :id).on(:user_id)
134
+ expect(@model).not_to reference(:users, :id).on(:user_id)
135
135
  end
136
136
 
137
137
  it "should not create foreign key using t.references with :polymorphic => true" do
138
138
  recreate_table(@model) do |t|
139
139
  t.references :user, :polymorphic => true
140
140
  end
141
- @model.should_not reference(:users, :id).on(:user_id)
141
+ expect(@model).not_to reference(:users, :id).on(:user_id)
142
142
  end
143
143
 
144
144
  it "should create foreign key to the same table on parent_id" do
145
145
  recreate_table(@model) do |t|
146
146
  t.integer :parent_id
147
147
  end
148
- @model.should reference(@model.table_name, :id).on(:parent_id)
148
+ expect(@model).to reference(@model.table_name, :id).on(:parent_id)
149
149
  end
150
150
 
151
151
  it "should create an index if specified on column" do
152
152
  recreate_table(@model) do |t|
153
153
  t.integer :state, :index => true
154
154
  end
155
- @model.should have_index.on(:state)
155
+ expect(@model).to have_index.on(:state)
156
156
  end
157
157
 
158
158
  it "should create a unique index if specified on column" do
159
159
  recreate_table(@model) do |t|
160
160
  t.integer :state, :index => { :unique => true }
161
161
  end
162
- @model.should have_unique_index.on(:state)
162
+ expect(@model).to have_unique_index.on(:state)
163
163
  end
164
164
 
165
165
  it "should create a unique index if specified on column using shorthand" do
166
166
  recreate_table(@model) do |t|
167
167
  t.integer :state, :index => :unique
168
168
  end
169
- @model.should have_unique_index.on(:state)
169
+ expect(@model).to have_unique_index.on(:state)
170
+ end
171
+
172
+ [:references, :belongs_to].each do |reftype|
173
+
174
+ context "when define #{reftype}" do
175
+
176
+ before(:each) do
177
+ @model = Comment
178
+ end
179
+
180
+ it "should create foreign key" do
181
+ create_reference(reftype, :post)
182
+ expect(@model).to reference(:posts, :id).on(:post_id)
183
+ end
184
+
185
+ it "should not create a foreign_key if polymorphic" do
186
+ create_reference(reftype, :post, :polymorphic => true)
187
+ expect(@model).not_to reference(:posts, :id).on(:post_id)
188
+ end
189
+
190
+ it "should create an index implicitly" do
191
+ create_reference(reftype, :post)
192
+ expect(@model).to have_index.on(:post_id)
193
+ end
194
+
195
+ it "should create exactly one index explicitly (#157)" do
196
+ create_reference(reftype, :post, :index => true)
197
+ expect(@model).to have_index.on(:post_id)
198
+ end
199
+
200
+ it "should respect :unique (#157)" do
201
+ create_reference(reftype, :post, :index => :unique)
202
+ expect(@model).to have_unique_index.on(:post_id)
203
+ end
204
+
205
+ it "should create a two-column index if polymophic and index requested" do
206
+ create_reference(reftype, :post, :polymorphic => true, :index => true)
207
+ expect(@model).to have_index.on([:post_id, :post_type])
208
+ end unless ::ActiveRecord::VERSION::MAJOR.to_i < 4
209
+
210
+ protected
211
+
212
+ def create_reference(reftype, column_name, *args)
213
+ recreate_table(@model) do |t|
214
+ t.send reftype, column_name, *args
215
+ end
216
+ end
217
+
218
+ end
170
219
  end
171
220
 
172
221
  if SchemaPlusHelpers.mysql?
@@ -176,7 +225,7 @@ describe ActiveRecord::Migration do
176
225
  t.string :bar, :index => { :with => :foo, :length => { :foo => 8, :bar => 12 }}
177
226
  end
178
227
  index = @model.indexes.first
179
- Hash[index.columns.zip(index.lengths.map(&:to_i))].should == { "foo" => 8, "bar" => 12}
228
+ expect(Hash[index.columns.zip(index.lengths.map(&:to_i))]).to eq({ "foo" => 8, "bar" => 12})
180
229
  end
181
230
  end
182
231
 
@@ -185,7 +234,7 @@ describe ActiveRecord::Migration do
185
234
  t.integer :state
186
235
  t.index :state
187
236
  end
188
- @model.should have_index.on(:state)
237
+ expect(@model).to have_index.on(:state)
189
238
  end
190
239
 
191
240
  it "should create a unique index if specified explicitly" do
@@ -193,7 +242,7 @@ describe ActiveRecord::Migration do
193
242
  t.integer :state
194
243
  t.index :state, :unique => true
195
244
  end
196
- @model.should have_unique_index.on(:state)
245
+ expect(@model).to have_unique_index.on(:state)
197
246
  end
198
247
 
199
248
  it "should create a multiple-column index if specified" do
@@ -201,7 +250,7 @@ describe ActiveRecord::Migration do
201
250
  t.integer :city
202
251
  t.integer :state, :index => { :with => :city }
203
252
  end
204
- @model.should have_index.on([:state, :city])
253
+ expect(@model).to have_index.on([:state, :city])
205
254
  end
206
255
 
207
256
  it "should auto-index foreign keys only" do
@@ -210,9 +259,9 @@ describe ActiveRecord::Migration do
210
259
  t.integer :application_id, :references => nil
211
260
  t.integer :state
212
261
  end
213
- @model.should have_index.on(:user_id)
214
- @model.should_not have_index.on(:application_id)
215
- @model.should_not have_index.on(:state)
262
+ expect(@model).to have_index.on(:user_id)
263
+ expect(@model).not_to have_index.on(:application_id)
264
+ expect(@model).not_to have_index.on(:state)
216
265
  end
217
266
 
218
267
  it "should override foreign key auto_create positively" do
@@ -220,7 +269,7 @@ describe ActiveRecord::Migration do
220
269
  recreate_table @model, :foreign_keys => {:auto_create => true} do |t|
221
270
  t.integer :user_id
222
271
  end
223
- @model.should reference(:users, :id).on(:user_id)
272
+ expect(@model).to reference(:users, :id).on(:user_id)
224
273
  end
225
274
  end
226
275
 
@@ -229,7 +278,7 @@ describe ActiveRecord::Migration do
229
278
  recreate_table @model, :foreign_keys => {:auto_create => false} do |t|
230
279
  t.integer :user_id
231
280
  end
232
- @model.should_not reference.on(:user_id)
281
+ expect(@model).not_to reference.on(:user_id)
233
282
  end
234
283
  end
235
284
 
@@ -238,7 +287,7 @@ describe ActiveRecord::Migration do
238
287
  recreate_table @model, :foreign_keys => {:auto_index => true} do |t|
239
288
  t.integer :user_id
240
289
  end
241
- @model.should have_index.on(:user_id)
290
+ expect(@model).to have_index.on(:user_id)
242
291
  end
243
292
  end
244
293
 
@@ -268,28 +317,28 @@ describe ActiveRecord::Migration do
268
317
  recreate_table @model do |t|
269
318
  t.integer :user_id, :foreign_key => { :on_update => action }
270
319
  end
271
- @model.should reference.on(:user_id).on_update(action)
320
+ expect(@model).to reference.on(:user_id).on_update(action)
272
321
  end
273
322
 
274
323
  it "should create and detect on_update #{action.inspect} using shortcut" do
275
324
  recreate_table @model do |t|
276
325
  t.integer :user_id, :on_update => action
277
326
  end
278
- @model.should reference.on(:user_id).on_update(action)
327
+ expect(@model).to reference.on(:user_id).on_update(action)
279
328
  end
280
329
 
281
330
  it "should create and detect on_delete #{action.inspect}" do
282
331
  recreate_table @model do |t|
283
332
  t.integer :user_id, :foreign_key => { :on_delete => action }
284
333
  end
285
- @model.should reference.on(:user_id).on_delete(action)
334
+ expect(@model).to reference.on(:user_id).on_delete(action)
286
335
  end
287
336
 
288
337
  it "should create and detect on_delete #{action.inspect} using shortcut" do
289
338
  recreate_table @model do |t|
290
339
  t.integer :user_id, :on_delete => action
291
340
  end
292
- @model.should reference.on(:user_id).on_delete(action)
341
+ expect(@model).to reference.on(:user_id).on_delete(action)
293
342
  end
294
343
  end
295
344
 
@@ -298,7 +347,7 @@ describe ActiveRecord::Migration do
298
347
  recreate_table @model do |t|
299
348
  t.integer :user_id, :on_delete => :cascade, :deferrable => status
300
349
  end
301
- @model.should reference.on(:user_id).deferrable(status)
350
+ expect(@model).to reference.on(:user_id).deferrable(status)
302
351
  end
303
352
  end unless SchemaPlusHelpers.mysql?
304
353
 
@@ -307,7 +356,7 @@ describe ActiveRecord::Migration do
307
356
  recreate_table @model do |t|
308
357
  t.integer :user_id
309
358
  end
310
- @model.should reference.on(:user_id).on_delete(:cascade)
359
+ expect(@model).to reference.on(:user_id).on_delete(:cascade)
311
360
  end
312
361
  end
313
362
 
@@ -316,7 +365,7 @@ describe ActiveRecord::Migration do
316
365
  recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
317
366
  t.integer :user_id
318
367
  end
319
- @model.should reference.on(:user_id).on_update(:restrict)
368
+ expect(@model).to reference.on(:user_id).on_update(:restrict)
320
369
  end
321
370
  end
322
371
 
@@ -325,7 +374,7 @@ describe ActiveRecord::Migration do
325
374
  recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
326
375
  t.integer :user_id
327
376
  end
328
- @model.should reference.on(:user_id).on_delete(:restrict)
377
+ expect(@model).to reference.on(:user_id).on_delete(:restrict)
329
378
  end
330
379
  end
331
380
 
@@ -334,7 +383,7 @@ describe ActiveRecord::Migration do
334
383
  recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
335
384
  t.integer :user_id, :foreign_key => { :on_update => :set_null }
336
385
  end
337
- @model.should reference.on(:user_id).on_update(:set_null)
386
+ expect(@model).to reference.on(:user_id).on_update(:set_null)
338
387
  end
339
388
  end
340
389
 
@@ -343,7 +392,7 @@ describe ActiveRecord::Migration do
343
392
  recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
344
393
  t.integer :user_id, :foreign_key => { :on_delete => :set_null }
345
394
  end
346
- @model.should reference.on(:user_id).on_delete(:set_null)
395
+ expect(@model).to reference.on(:user_id).on_delete(:set_null)
347
396
  end
348
397
  end
349
398
 
@@ -369,7 +418,7 @@ describe ActiveRecord::Migration do
369
418
  recreate_table @model, :foreign_keys => {:auto_index => false} do |t|
370
419
  t.integer :user_id
371
420
  end
372
- @model.should_not have_index.on(:user_id)
421
+ expect(@model).not_to have_index.on(:user_id)
373
422
  end
374
423
  end
375
424
 
@@ -378,7 +427,7 @@ describe ActiveRecord::Migration do
378
427
  recreate_table @model do |t|
379
428
  t.integer :user_id, :index => false
380
429
  end
381
- @model.should_not have_index.on(:user_id)
430
+ expect(@model).not_to have_index.on(:user_id)
382
431
  end
383
432
  end
384
433
 
@@ -397,7 +446,7 @@ describe ActiveRecord::Migration do
397
446
  change_table(@model, :bulk => bulk) do |t|
398
447
  t.integer :state, :index => true
399
448
  end
400
- @model.should have_index.on(:state)
449
+ expect(@model).to have_index.on(:state)
401
450
  end
402
451
 
403
452
  unless SchemaPlusHelpers.sqlite3?
@@ -406,13 +455,13 @@ describe ActiveRecord::Migration do
406
455
  change_table(@model, :bulk => bulk) do |t|
407
456
  t.integer :user_id
408
457
  end
409
- @model.should reference(:users, :id).on(:user_id)
458
+ expect(@model).to reference(:users, :id).on(:user_id)
410
459
  end
411
460
 
412
461
  context "migrate down" do
413
462
  it "should remove a foreign key constraint"+suffix do
414
463
  Comment.reset_column_information
415
- Comment.should reference(:users, :id).on(:user_id)
464
+ expect(Comment).to reference(:users, :id).on(:user_id)
416
465
  migration = Class.new ::ActiveRecord::Migration do
417
466
  define_method(:change) {
418
467
  change_table("comments", :bulk => bulk) do |t|
@@ -424,7 +473,7 @@ describe ActiveRecord::Migration do
424
473
  migration.migrate(:down)
425
474
  end
426
475
  Comment.reset_column_information
427
- Comment.should_not reference(:users, :id).on(:user_id)
476
+ expect(Comment).not_to reference(:users, :id).on(:user_id)
428
477
  end
429
478
  end if ActiveRecord::VERSION::MAJOR >= 4
430
479
 
@@ -432,14 +481,14 @@ describe ActiveRecord::Migration do
432
481
  change_table(@model, :bulk => bulk) do |t|
433
482
  t.references :user
434
483
  end
435
- @model.should reference(:users, :id).on(:user_id)
484
+ expect(@model).to reference(:users, :id).on(:user_id)
436
485
  end
437
486
 
438
487
  it "should create a foreign key constraint using :belongs_to"+suffix do
439
488
  change_table(@model, :bulk => bulk) do |t|
440
489
  t.belongs_to :user
441
490
  end
442
- @model.should reference(:users, :id).on(:user_id)
491
+ expect(@model).to reference(:users, :id).on(:user_id)
443
492
  end
444
493
  end
445
494
  end
@@ -456,87 +505,87 @@ describe ActiveRecord::Migration do
456
505
 
457
506
  it "should create an index" do
458
507
  add_column(:slug, :string, :index => true) do
459
- @model.should have_index.on(:slug)
508
+ expect(@model).to have_index.on(:slug)
460
509
  end
461
510
  end
462
511
 
463
512
  it "should create foreign key" do
464
513
  add_column(:post_id, :integer) do
465
- @model.should reference(:posts, :id).on(:post_id)
514
+ expect(@model).to reference(:posts, :id).on(:post_id)
466
515
  end
467
516
  end
468
517
 
469
518
  it "should create foreign key to explicitly given table" do
470
519
  add_column(:author_id, :integer, :foreign_key => { :references => :users }) do
471
- @model.should reference(:users, :id).on(:author_id)
520
+ expect(@model).to reference(:users, :id).on(:author_id)
472
521
  end
473
522
  end
474
523
 
475
524
  it "should create foreign key to explicitly given table using shortcut" do
476
525
  add_column(:author_id, :integer, :references => :users) do
477
- @model.should reference(:users, :id).on(:author_id)
526
+ expect(@model).to reference(:users, :id).on(:author_id)
478
527
  end
479
528
  end
480
529
 
481
530
  it "should create foreign key to explicitly given table and column name" do
482
531
  add_column(:author_login, :string, :foreign_key => { :references => [:users, :login]}) do
483
- @model.should reference(:users, :login).on(:author_login)
532
+ expect(@model).to reference(:users, :login).on(:author_login)
484
533
  end
485
534
  end
486
535
 
487
536
  it "should create foreign key to the same table on parent_id" do
488
537
  add_column(:parent_id, :integer) do
489
- @model.should reference(@model.table_name, :id).on(:parent_id)
538
+ expect(@model).to reference(@model.table_name, :id).on(:parent_id)
490
539
  end
491
540
  end
492
541
 
493
542
  it "shouldn't create foreign key if column doesn't look like foreign key" do
494
543
  add_column(:views_count, :integer) do
495
- @model.should_not reference.on(:views_count)
544
+ expect(@model).not_to reference.on(:views_count)
496
545
  end
497
546
  end
498
547
 
499
548
  it "shouldn't create foreign key if specified explicitly" do
500
549
  add_column(:post_id, :integer, :foreign_key => false) do
501
- @model.should_not reference.on(:post_id)
550
+ expect(@model).not_to reference.on(:post_id)
502
551
  end
503
552
  end
504
553
 
505
554
  it "shouldn't create foreign key if specified explicitly by shorthand" do
506
555
  add_column(:post_id, :integer, :references => nil) do
507
- @model.should_not reference.on(:post_id)
556
+ expect(@model).not_to reference.on(:post_id)
508
557
  end
509
558
  end
510
559
 
511
560
  it "should create an index if specified" do
512
561
  add_column(:post_id, :integer, :index => true) do
513
- @model.should have_index.on(:post_id)
562
+ expect(@model).to have_index.on(:post_id)
514
563
  end
515
564
  end
516
565
 
517
566
  it "should create a unique index if specified" do
518
567
  add_column(:post_id, :integer, :index => { :unique => true }) do
519
- @model.should have_unique_index.on(:post_id)
568
+ expect(@model).to have_unique_index.on(:post_id)
520
569
  end
521
570
  end
522
571
 
523
572
  it "should create a unique index if specified by shorthand" do
524
573
  add_column(:post_id, :integer, :index => :unique) do
525
- @model.should have_unique_index.on(:post_id)
574
+ expect(@model).to have_unique_index.on(:post_id)
526
575
  end
527
576
  end
528
577
 
529
578
  it "should allow custom name for index" do
530
579
  index_name = 'comments_post_id_unique_index'
531
580
  add_column(:post_id, :integer, :index => { :unique => true, :name => index_name }) do
532
- @model.should have_unique_index(:name => index_name).on(:post_id)
581
+ expect(@model).to have_unique_index(:name => index_name).on(:post_id)
533
582
  end
534
583
  end
535
584
 
536
585
  it "should auto-index if specified in global options" do
537
586
  SchemaPlus.config.foreign_keys.auto_index = true
538
587
  add_column(:post_id, :integer) do
539
- @model.should have_index.on(:post_id)
588
+ expect(@model).to have_index.on(:post_id)
540
589
  end
541
590
  SchemaPlus.config.foreign_keys.auto_index = false
542
591
  end
@@ -544,7 +593,7 @@ describe ActiveRecord::Migration do
544
593
  it "should auto-index foreign keys only" do
545
594
  SchemaPlus.config.foreign_keys.auto_index = true
546
595
  add_column(:state, :integer) do
547
- @model.should_not have_index.on(:state)
596
+ expect(@model).not_to have_index.on(:state)
548
597
  end
549
598
  SchemaPlus.config.foreign_keys.auto_index = false
550
599
  end
@@ -555,7 +604,7 @@ describe ActiveRecord::Migration do
555
604
  # MySQL creates an index on foreign by default
556
605
  # and we can do nothing with that
557
606
  unless SchemaPlusHelpers.mysql?
558
- @model.should_not have_index.on(:post_id)
607
+ expect(@model).not_to have_index.on(:post_id)
559
608
  end
560
609
  end
561
610
  SchemaPlus.config.foreign_keys.auto_index = false
@@ -564,7 +613,7 @@ describe ActiveRecord::Migration do
564
613
  it "should use default on_update action" do
565
614
  SchemaPlus.config.foreign_keys.on_update = :cascade
566
615
  add_column(:post_id, :integer) do
567
- @model.should reference.on(:post_id).on_update(:cascade)
616
+ expect(@model).to reference.on(:post_id).on_update(:cascade)
568
617
  end
569
618
  SchemaPlus.config.foreign_keys.on_update = nil
570
619
  end
@@ -572,7 +621,7 @@ describe ActiveRecord::Migration do
572
621
  it "should use default on_delete action" do
573
622
  SchemaPlus.config.foreign_keys.on_delete = :cascade
574
623
  add_column(:post_id, :integer) do
575
- @model.should reference.on(:post_id).on_delete(:cascade)
624
+ expect(@model).to reference.on(:post_id).on_delete(:cascade)
576
625
  end
577
626
  SchemaPlus.config.foreign_keys.on_delete = nil
578
627
  end
@@ -581,7 +630,7 @@ describe ActiveRecord::Migration do
581
630
  SchemaPlus.config.foreign_keys.on_delete = :cascade
582
631
  SchemaPlus.config.foreign_keys.on_update = :restrict
583
632
  add_column(:post_id, :integer, :foreign_key => { :on_update => :set_null, :on_delete => :set_null}) do
584
- @model.should reference.on(:post_id).on_delete(:set_null).on_update(:set_null)
633
+ expect(@model).to reference.on(:post_id).on_delete(:set_null).on_update(:set_null)
585
634
  end
586
635
  SchemaPlus.config.foreign_keys.on_delete = nil
587
636
  end
@@ -599,43 +648,6 @@ describe ActiveRecord::Migration do
599
648
 
600
649
  end
601
650
 
602
- context "when add reference" do
603
-
604
- before(:each) do
605
- @model = Comment
606
- end
607
-
608
- it "should create foreign key" do
609
- add_reference(:post) do
610
- @model.should reference(:posts, :id).on(:post_id)
611
- end
612
- end
613
-
614
- it "should not create a foreign_key if polymorphic" do
615
- add_reference(:post, :polymorphic => true) do
616
- @model.should_not reference(:posts, :id).on(:post_id)
617
- end
618
- end
619
-
620
- it "should create a two-column index if polymophic and index requested" do
621
- add_reference(:post, :polymorphic => true, :index => true) do
622
- @model.should have_index.on([:post_id, :post_type])
623
- end
624
- end
625
-
626
-
627
- protected
628
- def add_reference(column_name, *args)
629
- table = @model.table_name
630
- ActiveRecord::Migration.suppress_messages do
631
- ActiveRecord::Migration.add_reference(table, column_name, *args)
632
- @model.reset_column_information
633
- yield if block_given?
634
- ActiveRecord::Migration.remove_column(table, "#{column_name}_id")
635
- end
636
- end
637
-
638
- end unless ::ActiveRecord::VERSION::MAJOR.to_i < 4
639
651
 
640
652
  context "when column is changed" do
641
653
 
@@ -645,7 +657,7 @@ describe ActiveRecord::Migration do
645
657
 
646
658
  it "should create foreign key" do
647
659
  change_column :user, :string, :foreign_key => { :references => [:users, :login] }
648
- @model.should reference(:users, :login).on(:user)
660
+ expect(@model).to reference(:users, :login).on(:user)
649
661
  end
650
662
 
651
663
  context "and initially references to users table" do
@@ -657,39 +669,39 @@ describe ActiveRecord::Migration do
657
669
  end
658
670
 
659
671
  it "should have foreign key" do
660
- @model.should reference(:users)
672
+ expect(@model).to reference(:users)
661
673
  end
662
674
 
663
675
  it "should drop foreign key if it is no longer valid" do
664
676
  change_column :user_id, :integer, :foreign_key => { :references => :members }
665
- @model.should_not reference(:users)
677
+ expect(@model).not_to reference(:users)
666
678
  end
667
679
 
668
680
  it "should drop foreign key if requested to do so" do
669
681
  change_column :user_id, :integer, :foreign_key => { :references => nil }
670
- @model.should_not reference(:users)
682
+ expect(@model).not_to reference(:users)
671
683
  end
672
684
 
673
685
  it "should remove auto-created index if foreign key is removed" do
674
- @model.should have_index.on(:user_id) # sanity check that index was auto-created
686
+ expect(@model).to have_index.on(:user_id) # sanity check that index was auto-created
675
687
  change_column :user_id, :integer, :foreign_key => { :references => nil }
676
- @model.should_not have_index.on(:user_id)
688
+ expect(@model).not_to have_index.on(:user_id)
677
689
  end
678
690
 
679
691
  it "should reference pointed table afterwards if new one is created" do
680
692
  change_column :user_id, :integer, :foreign_key => { :references => :members }
681
- @model.should reference(:members)
693
+ expect(@model).to reference(:members)
682
694
  end
683
695
 
684
696
  it "should maintain foreign key if it's unaffected by change" do
685
697
  change_column :user_id, :integer, :default => 0
686
- @model.should reference(:users)
698
+ expect(@model).to reference(:users)
687
699
  end
688
700
 
689
701
  it "should maintain foreign key if it's unaffected by change, even if auto_index is off" do
690
702
  with_fk_config(:auto_create => false) do
691
703
  change_column :user_id, :integer, :default => 0
692
- @model.should reference(:users)
704
+ expect(@model).to reference(:users)
693
705
  end
694
706
  end
695
707
 
@@ -703,7 +715,7 @@ describe ActiveRecord::Migration do
703
715
  end
704
716
 
705
717
  it "should create the index" do
706
- @model.should have_index.on(:user_id)
718
+ expect(@model).to have_index.on(:user_id)
707
719
  end
708
720
 
709
721
  it "adding foreign key should not fail due to attempt to auto-create existing index" do
@@ -731,15 +743,15 @@ describe ActiveRecord::Migration do
731
743
  end
732
744
 
733
745
  it "should remove a foreign key" do
734
- @model.should reference(:posts)
746
+ expect(@model).to reference(:posts)
735
747
  remove_column(:post_id)
736
- @model.should_not reference(:posts)
748
+ expect(@model).not_to reference(:posts)
737
749
  end
738
750
 
739
751
  it "should remove an index" do
740
- @model.should have_index.on(:post_id)
752
+ expect(@model).to have_index.on(:post_id)
741
753
  remove_column(:post_id)
742
- @model.should_not have_index.on(:post_id)
754
+ expect(@model).not_to have_index.on(:post_id)
743
755
  end
744
756
 
745
757
  protected
@@ -779,17 +791,17 @@ describe ActiveRecord::Migration do
779
791
 
780
792
  it "should rename rails-named indexes" do
781
793
  index = ActiveRecord::Base.connection.indexes(:newname).find{|index| index.columns == ['xyz']}
782
- index.name.should =~ /^index_newname_on/
794
+ expect(index.name).to match(/^index_newname_on/)
783
795
  end
784
796
 
785
797
  it "should rename fk indexes" do
786
798
  index = ActiveRecord::Base.connection.indexes(:newname).find{|index| index.columns == ['user_id']}
787
- index.name.should =~ /^fk__newname_/
799
+ expect(index.name).to match(/^fk__newname_/)
788
800
  end
789
801
 
790
802
  unless SchemaPlusHelpers.sqlite3?
791
803
  it "should rename foreign key constraints" do
792
- ActiveRecord::Base.connection.foreign_keys(:newname).first.name.should =~ /newname/
804
+ expect(ActiveRecord::Base.connection.foreign_keys(:newname).first.name).to match(/newname/)
793
805
  end
794
806
  end
795
807
 
@@ -821,7 +833,7 @@ describe ActiveRecord::Migration do
821
833
  end
822
834
  it "should rename foreign key constraints" do
823
835
  names = ActiveRecord::Base.connection.foreign_keys(:newname).map(&:name)
824
- names.grep(/newname/).should == names
836
+ expect(names.grep(/newname/)).to eq(names)
825
837
  end
826
838
  end
827
839