Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update associations on object delete #485

Merged
merged 5 commits into from
Jan 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Associations. Improve tests for #delete method
  • Loading branch information
andrykonchin committed Jan 24, 2021
commit 7a608aca41bf57f255ff845317202dcc1ea4599b
156 changes: 134 additions & 22 deletions spec/dynamoid/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2559,6 +2559,10 @@ def log_message
end
end

context 'destroy' do
# TODO: adopt test cases for the `delete` method
end

context 'delete' do
it 'uses dumped value of sort key to call DeleteItem' do
klass = new_class do
Expand Down Expand Up @@ -2598,35 +2602,143 @@ def log_message
end
end

context 'with associations' do
let!(:user) { User.create }
let!(:camel_case) { user.camel_case.create }
let!(:magazine) { user.books.create }
let!(:monthly) { user.monthly.create }
let!(:subscription) { user.subscriptions.create }
context 'when model has associations' do
context 'when belongs_to association' do
context 'when has_many on the other side' do
let!(:source_model) { User.create }
let!(:target_model) { source_model.camel_case.create }

it 'updates belongs_to association' do
expect do
user.delete
end.to change { CamelCase.find(camel_case.id).users.target }.from([user]).to([])
it 'disassociates self' do
expect do
source_model.delete
end.to change { CamelCase.find(target_model.id).users.target }.from([source_model]).to([])
end

it 'updates cached ids list in associated model' do
source_model.delete
expect(CamelCase.find(target_model.id).users_ids).to eq nil
end

it 'behaves correctly when associated model is linked with several models' do
source_model2 = User.create
target_model.users << source_model2

expect(CamelCase.find(target_model.id).users.target).to contain_exactly(source_model, source_model2)
source_model.delete
expect(CamelCase.find(target_model.id).users.target).to contain_exactly(source_model2)
expect(CamelCase.find(target_model.id).users_ids).to eq [source_model2.id].to_set
end

it 'does not raise exception when foreign key is broken' do
source_model.update_attributes!(camel_case_ids: ['fake_id'])

expect { source_model.delete }.not_to raise_error
expect(CamelCase.find(target_model.id).users.target).to eq []
end
end

context 'when has_one on the other side' do
let!(:source_model) { Sponsor.create }
let!(:target_model) { source_model.camel_case.create }

it 'disassociates self' do
expect do
source_model.delete
end.to change { CamelCase.find(target_model.id).sponsor.target }.from(source_model).to(nil)
end

it 'updates cached ids list in associated model' do
source_model.delete
expect(CamelCase.find(target_model.id).sponsor_ids).to eq nil
end

it 'does not raise exception when foreign key is broken' do
source_model.update_attributes!(camel_case_ids: ['fake_id'])

expect { source_model.delete }.not_to raise_error
expect(CamelCase.find(target_model.id).sponsor.target).to eq nil
end
end
end

it 'updates has_many association' do
expect do
user.delete
end.to change { Magazine.find(magazine.title).owner.target }.from(user).to(nil)
context 'when has_many association' do
let!(:source_model) { User.create }
let!(:target_model) { source_model.books.create }

it 'disassociates self' do
expect do
source_model.delete
end.to change { Magazine.find(target_model.title).owner.target }.from(source_model).to(nil)
end

it 'updates cached ids list in associated model' do
source_model.delete
expect(Magazine.find(target_model.title).owner_ids).to eq nil
end

it 'does not raise exception when cached foreign key is broken' do
books_ids_new = source_model.books_ids + ['fake_id']
source_model.update_attributes!(books_ids: books_ids_new)

expect { source_model.delete }.not_to raise_error
expect(Magazine.find(target_model.title).owner).to eq nil
end
end

it 'updates has_one association' do
expect do
user.delete
end.to change { Subscription.find(monthly.id).customer.target }.from(user).to(nil)
context 'when has_one association' do
let!(:source_model) { User.create }
let!(:target_model) { source_model.monthly.create }

it 'disassociates self' do
expect do
source_model.delete
end.to change { Subscription.find(target_model.id).customer.target }.from(source_model).to(nil)
end

it 'updates cached ids list in associated model' do
source_model.delete
expect(Subscription.find(target_model.id).customer_ids).to eq nil
end

it 'does not raise exception when cached foreign key is broken' do
source_model.update_attributes!(monthly_ids: ['fake_id'])

expect { source_model.delete }.not_to raise_error
end
end

it 'updates has_and_belongs_to_many association' do
expect do
user.delete
end.to change { Subscription.find(subscription.id).users.target }.from([user]).to([])
context 'when has_and_belongs_to_many association' do
let!(:source_model) { User.create }
let!(:target_model) { source_model.subscriptions.create }

it 'disassociates self' do
expect do
source_model.delete
end.to change { Subscription.find(target_model.id).users.target }.from([source_model]).to([])
end

it 'updates cached ids list in associated model' do
source_model.delete
expect(Subscription.find(target_model.id).users_ids).to eq nil
end

it 'behaves correctly when associated model is linked with several models' do
source_model2 = User.create
target_model.users << source_model2

expect(Subscription.find(target_model.id).users.target).to contain_exactly(source_model, source_model2)
source_model.delete
expect(Subscription.find(target_model.id).users.target).to contain_exactly(source_model2)
expect(Subscription.find(target_model.id).users_ids).to eq [source_model2.id].to_set
end

it 'does not raise exception when foreign key is broken' do
subscriptions_ids_new = source_model.subscriptions_ids + ['fake_id']
source_model.update_attributes!(subscriptions_ids: subscriptions_ids_new)

expect { source_model.delete }.not_to raise_error
expect(Subscription.find(target_model.id).users_ids).to eq nil
end
end
end
end
Expand Down