-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_foreign_key_spec.rb
65 lines (58 loc) · 1.92 KB
/
add_foreign_key_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
# TODO: remove this test, since it will be of
# limited value once the migration has been run
require 'rails_helper'
require 'rake'
Orangelight::Application.load_tasks
require Rails.root.join('db', 'migrate', '20230419231330_add_user_fk_to_bookmarks.rb')
RSpec.describe AddUserFkToBookmarks do
self.use_transactional_tests = false
let(:valid_user) { FactoryBot.create :valid_princeton_patron }
before do
rollback_described_migration
ActiveRecord::Base.transaction do
[valid_user.id, -1, 999_999_999].each do |user_id|
(1..5).each do |document_id|
bookmark = Bookmark.new
bookmark.user_id = user_id
bookmark.document_id = document_id
bookmark.save
begin
User.find(user_id).bookmarks << bookmark
rescue ActiveRecord::RecordNotFound
'not a valid user'
end
end
end
end
end
it 'deletes bookmarks without a valid user' do
expect { run_described_migration }.to change(Bookmark, :count).by(-10)
end
it 'does not delete bookmarks with a valid user' do
expect(valid_user.reload.bookmarks.count).to eq 5
expect { run_described_migration }.not_to change(valid_user.bookmarks, :count)
end
context 'after migration is applied' do
it 'cleans up bookmarks when a user is deleted' do
run_described_migration
Bookmark.destroy_all
bookmark = Bookmark.new
bookmark.user_id = valid_user.id
bookmark.document_id = 123_456
bookmark.save
expect { valid_user.destroy }.to change(Bookmark, :count).by(-1)
end
end
end
def run_described_migration
system({ 'VERSION' => '20230419231330',
'RAILS_ENV' => 'test' },
'rake db:migrate:up')
end
def rollback_described_migration
system({ 'VERSION' => '20230419231330',
'RAILS_ENV' => 'test' },
'rake db:migrate:down')
Bookmark.reset_column_information
end