Skip to content

Commit 7a608ac

Browse files
committed
Associations. Improve tests for #delete method
1 parent 14fb5e0 commit 7a608ac

File tree

1 file changed

+134
-22
lines changed

1 file changed

+134
-22
lines changed

spec/dynamoid/persistence_spec.rb

Lines changed: 134 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,10 @@ def log_message
25592559
end
25602560
end
25612561

2562+
context 'destroy' do
2563+
# TODO: adopt test cases for the `delete` method
2564+
end
2565+
25622566
context 'delete' do
25632567
it 'uses dumped value of sort key to call DeleteItem' do
25642568
klass = new_class do
@@ -2598,35 +2602,143 @@ def log_message
25982602
end
25992603
end
26002604

2601-
context 'with associations' do
2602-
let!(:user) { User.create }
2603-
let!(:camel_case) { user.camel_case.create }
2604-
let!(:magazine) { user.books.create }
2605-
let!(:monthly) { user.monthly.create }
2606-
let!(:subscription) { user.subscriptions.create }
2605+
context 'when model has associations' do
2606+
context 'when belongs_to association' do
2607+
context 'when has_many on the other side' do
2608+
let!(:source_model) { User.create }
2609+
let!(:target_model) { source_model.camel_case.create }
26072610

2608-
it 'updates belongs_to association' do
2609-
expect do
2610-
user.delete
2611-
end.to change { CamelCase.find(camel_case.id).users.target }.from([user]).to([])
2611+
it 'disassociates self' do
2612+
expect do
2613+
source_model.delete
2614+
end.to change { CamelCase.find(target_model.id).users.target }.from([source_model]).to([])
2615+
end
2616+
2617+
it 'updates cached ids list in associated model' do
2618+
source_model.delete
2619+
expect(CamelCase.find(target_model.id).users_ids).to eq nil
2620+
end
2621+
2622+
it 'behaves correctly when associated model is linked with several models' do
2623+
source_model2 = User.create
2624+
target_model.users << source_model2
2625+
2626+
expect(CamelCase.find(target_model.id).users.target).to contain_exactly(source_model, source_model2)
2627+
source_model.delete
2628+
expect(CamelCase.find(target_model.id).users.target).to contain_exactly(source_model2)
2629+
expect(CamelCase.find(target_model.id).users_ids).to eq [source_model2.id].to_set
2630+
end
2631+
2632+
it 'does not raise exception when foreign key is broken' do
2633+
source_model.update_attributes!(camel_case_ids: ['fake_id'])
2634+
2635+
expect { source_model.delete }.not_to raise_error
2636+
expect(CamelCase.find(target_model.id).users.target).to eq []
2637+
end
2638+
end
2639+
2640+
context 'when has_one on the other side' do
2641+
let!(:source_model) { Sponsor.create }
2642+
let!(:target_model) { source_model.camel_case.create }
2643+
2644+
it 'disassociates self' do
2645+
expect do
2646+
source_model.delete
2647+
end.to change { CamelCase.find(target_model.id).sponsor.target }.from(source_model).to(nil)
2648+
end
2649+
2650+
it 'updates cached ids list in associated model' do
2651+
source_model.delete
2652+
expect(CamelCase.find(target_model.id).sponsor_ids).to eq nil
2653+
end
2654+
2655+
it 'does not raise exception when foreign key is broken' do
2656+
source_model.update_attributes!(camel_case_ids: ['fake_id'])
2657+
2658+
expect { source_model.delete }.not_to raise_error
2659+
expect(CamelCase.find(target_model.id).sponsor.target).to eq nil
2660+
end
2661+
end
26122662
end
26132663

2614-
it 'updates has_many association' do
2615-
expect do
2616-
user.delete
2617-
end.to change { Magazine.find(magazine.title).owner.target }.from(user).to(nil)
2664+
context 'when has_many association' do
2665+
let!(:source_model) { User.create }
2666+
let!(:target_model) { source_model.books.create }
2667+
2668+
it 'disassociates self' do
2669+
expect do
2670+
source_model.delete
2671+
end.to change { Magazine.find(target_model.title).owner.target }.from(source_model).to(nil)
2672+
end
2673+
2674+
it 'updates cached ids list in associated model' do
2675+
source_model.delete
2676+
expect(Magazine.find(target_model.title).owner_ids).to eq nil
2677+
end
2678+
2679+
it 'does not raise exception when cached foreign key is broken' do
2680+
books_ids_new = source_model.books_ids + ['fake_id']
2681+
source_model.update_attributes!(books_ids: books_ids_new)
2682+
2683+
expect { source_model.delete }.not_to raise_error
2684+
expect(Magazine.find(target_model.title).owner).to eq nil
2685+
end
26182686
end
26192687

2620-
it 'updates has_one association' do
2621-
expect do
2622-
user.delete
2623-
end.to change { Subscription.find(monthly.id).customer.target }.from(user).to(nil)
2688+
context 'when has_one association' do
2689+
let!(:source_model) { User.create }
2690+
let!(:target_model) { source_model.monthly.create }
2691+
2692+
it 'disassociates self' do
2693+
expect do
2694+
source_model.delete
2695+
end.to change { Subscription.find(target_model.id).customer.target }.from(source_model).to(nil)
2696+
end
2697+
2698+
it 'updates cached ids list in associated model' do
2699+
source_model.delete
2700+
expect(Subscription.find(target_model.id).customer_ids).to eq nil
2701+
end
2702+
2703+
it 'does not raise exception when cached foreign key is broken' do
2704+
source_model.update_attributes!(monthly_ids: ['fake_id'])
2705+
2706+
expect { source_model.delete }.not_to raise_error
2707+
end
26242708
end
26252709

2626-
it 'updates has_and_belongs_to_many association' do
2627-
expect do
2628-
user.delete
2629-
end.to change { Subscription.find(subscription.id).users.target }.from([user]).to([])
2710+
context 'when has_and_belongs_to_many association' do
2711+
let!(:source_model) { User.create }
2712+
let!(:target_model) { source_model.subscriptions.create }
2713+
2714+
it 'disassociates self' do
2715+
expect do
2716+
source_model.delete
2717+
end.to change { Subscription.find(target_model.id).users.target }.from([source_model]).to([])
2718+
end
2719+
2720+
it 'updates cached ids list in associated model' do
2721+
source_model.delete
2722+
expect(Subscription.find(target_model.id).users_ids).to eq nil
2723+
end
2724+
2725+
it 'behaves correctly when associated model is linked with several models' do
2726+
source_model2 = User.create
2727+
target_model.users << source_model2
2728+
2729+
expect(Subscription.find(target_model.id).users.target).to contain_exactly(source_model, source_model2)
2730+
source_model.delete
2731+
expect(Subscription.find(target_model.id).users.target).to contain_exactly(source_model2)
2732+
expect(Subscription.find(target_model.id).users_ids).to eq [source_model2.id].to_set
2733+
end
2734+
2735+
it 'does not raise exception when foreign key is broken' do
2736+
subscriptions_ids_new = source_model.subscriptions_ids + ['fake_id']
2737+
source_model.update_attributes!(subscriptions_ids: subscriptions_ids_new)
2738+
2739+
expect { source_model.delete }.not_to raise_error
2740+
expect(Subscription.find(target_model.id).users_ids).to eq nil
2741+
end
26302742
end
26312743
end
26322744
end

0 commit comments

Comments
 (0)