forked from luckyframework/avram
/
delete_operation_spec.cr
137 lines (108 loc) · 3.84 KB
/
delete_operation_spec.cr
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require "../spec_helper"
private class BasicDeleteUser < User::DeleteOperation
end
private class FailedToDeleteUser < User::DeleteOperation
before_delete do
add_error(:nope, "not today")
end
end
private class SoftDeleteItem < SoftDeletableItem::DeleteOperation
end
private class DeleteWithCascade < Business::DeleteOperation
end
private class DeleteOperationWithAccessToModelValues < Post::DeleteOperation
before_delete do
if record.title == "sandbox"
title.add_error("You can't delete your sandbox")
end
end
end
describe "Avram::DeleteOperation" do
describe "delete" do
it "deletes the specified record" do
user = UserFactory.create
BasicDeleteUser.delete(user) do |operation, deleted_user|
operation.valid?.should be_true
operation.delete_status.should eq BasicDeleteUser::DeleteStatus::Deleted
deleted_user.not_nil!.name.should eq user.name
UserQuery.new.select_count.should eq 0
end
end
it "does not delete if the operation is invalid" do
user = UserFactory.create
FailedToDeleteUser.delete(user) do |operation, deleted_user|
operation.valid?.should be_false
operation.delete_status.should eq FailedToDeleteUser::DeleteStatus::DeleteFailed
deleted_user.should eq nil
operation.errors[:nope].should contain "not today"
UserQuery.new.select_count.should eq 1
end
end
end
describe "delete!" do
it "deletes the specified record" do
user = UserFactory.create
deleted_user = BasicDeleteUser.delete!(user)
deleted_user.name.should eq user.name
UserQuery.new.select_count.should eq 0
end
it "raises an exception when unable to delete" do
user = UserFactory.create
expect_raises(Avram::InvalidOperationError) do
FailedToDeleteUser.delete!(user)
end
end
end
describe "soft deletes" do
it "returns a soft deleted object" do
item = SoftDeletableItemFactory.create
deleted_item = SoftDeleteItem.delete!(item)
deleted_item.soft_deleted?.should be_true
SoftDeletableItem::BaseQuery.new.find(deleted_item.id).should eq item
end
end
describe "cascade deletes" do
it "deletes the object and associated" do
business = BusinessFactory.create
EmailAddressFactory.create &.business_id(business.id)
EmailAddress::BaseQuery.new.select_count.should eq(1)
DeleteWithCascade.delete(business) do |operation, _deleted_business|
operation.deleted?.should be_true
EmailAddress::BaseQuery.new.select_count.should eq(0)
end
end
end
describe "publishes" do
it "publishes a successful delete" do
events = [] of Avram::Events::DeleteSuccessEvent
Avram::Events::DeleteSuccessEvent.subscribe do |event|
events << event
end
user = UserFactory.create
BasicDeleteUser.delete!(user)
events.map(&.operation_class).should contain("BasicDeleteUser")
end
it "publishes a failed delete" do
events = [] of Avram::Events::DeleteFailedEvent
Avram::Events::DeleteFailedEvent.subscribe do |event|
events << event
end
user = UserFactory.create
expect_raises(Avram::InvalidOperationError) do
FailedToDeleteUser.delete!(user)
end
events.map(&.operation_class).should contain("FailedToDeleteUser")
events.map(&.error_messages_as_string).should contain("nope not today")
UserQuery.new.select_count.should eq 1
end
end
context "using the model for conditional deletes" do
it "adds the error and fails to save" do
post = PostFactory.create &.title("sandbox")
DeleteOperationWithAccessToModelValues.delete(post) do |operation, _deleted_post|
operation.deleted?.should be_false
operation.errors[:title].should contain("You can't delete your sandbox")
end
end
end
end