|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "activejob/helper" |
| 4 | + |
| 5 | +require "models/book_destroy_async" |
| 6 | +require "models/essay_destroy_async" |
| 7 | +require "models/tag" |
| 8 | +require "models/tagging" |
| 9 | +require "models/essay" |
| 10 | +require "models/category" |
| 11 | +require "models/post" |
| 12 | +require "models/content" |
| 13 | +require "models/destroy_async_parent" |
| 14 | +require "models/destroy_async_parent_soft_delete" |
| 15 | +require "models/dl_keyed_belongs_to" |
| 16 | +require "models/dl_keyed_belongs_to_soft_delete" |
| 17 | +require "models/dl_keyed_has_one" |
| 18 | +require "models/dl_keyed_join" |
| 19 | +require "models/dl_keyed_has_many" |
| 20 | +require "models/dl_keyed_has_many_through" |
| 21 | + |
| 22 | +class DestroyAssociationAsyncTest < ActiveRecord::TestCase |
| 23 | + self.use_transactional_tests = false |
| 24 | + |
| 25 | + include ActiveJob::TestHelper |
| 26 | + |
| 27 | + test "destroying a record destroys the has_many :through records using a job" do |
| 28 | + tag = Tag.create!(name: "Der be treasure") |
| 29 | + tag2 = Tag.create!(name: "Der be rum") |
| 30 | + book = BookDestroyAsync.create! |
| 31 | + book.tags << [tag, tag2] |
| 32 | + book.save! |
| 33 | + |
| 34 | + book.destroy |
| 35 | + |
| 36 | + assert_difference -> { Tag.count }, -2 do |
| 37 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 38 | + end |
| 39 | + ensure |
| 40 | + Tag.delete_all |
| 41 | + BookDestroyAsync.delete_all |
| 42 | + end |
| 43 | + |
| 44 | + test "destroying a scoped has_many through only deletes within the scope deleted" do |
| 45 | + tag = Tag.create!(name: "Der be treasure") |
| 46 | + tag2 = Tag.create!(name: "Der be rum") |
| 47 | + parent = BookDestroyAsyncWithScopedTags.create! |
| 48 | + parent.tags << [tag, tag2] |
| 49 | + parent.save! |
| 50 | + |
| 51 | + parent.reload # force the association to be reloaded |
| 52 | + |
| 53 | + parent.destroy |
| 54 | + |
| 55 | + assert_difference -> { Tag.count }, -1 do |
| 56 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 57 | + end |
| 58 | + assert_raises ActiveRecord::RecordNotFound do |
| 59 | + tag2.reload |
| 60 | + end |
| 61 | + assert tag.reload |
| 62 | + ensure |
| 63 | + Tag.delete_all |
| 64 | + Tagging.delete_all |
| 65 | + BookDestroyAsyncWithScopedTags.delete_all |
| 66 | + end |
| 67 | + |
| 68 | + test "enqueues the has_many through to be deleted with custom primary key" do |
| 69 | + dl_keyed_has_many = DlKeyedHasManyThrough.create! |
| 70 | + dl_keyed_has_many2 = DlKeyedHasManyThrough.create! |
| 71 | + parent = DestroyAsyncParent.create! |
| 72 | + parent.dl_keyed_has_many_through << [dl_keyed_has_many2, dl_keyed_has_many] |
| 73 | + parent.save! |
| 74 | + parent.destroy |
| 75 | + |
| 76 | + assert_difference -> { DlKeyedJoin.count }, -2 do |
| 77 | + assert_difference -> { DlKeyedHasManyThrough.count }, -2 do |
| 78 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 79 | + end |
| 80 | + end |
| 81 | + ensure |
| 82 | + DlKeyedHasManyThrough.delete_all |
| 83 | + DestroyAsyncParent.delete_all |
| 84 | + DlKeyedJoin.delete_all |
| 85 | + end |
| 86 | + |
| 87 | + test "belongs to" do |
| 88 | + essay = EssayDestroyAsync.create!(name: "Der be treasure") |
| 89 | + book = BookDestroyAsync.create!(name: "Arr, matey!") |
| 90 | + essay.book = book |
| 91 | + essay.save! |
| 92 | + essay.destroy |
| 93 | + |
| 94 | + assert_difference -> { BookDestroyAsync.count }, -1 do |
| 95 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 96 | + end |
| 97 | + ensure |
| 98 | + EssayDestroyAsync.delete_all |
| 99 | + BookDestroyAsync.delete_all |
| 100 | + end |
| 101 | + |
| 102 | + test "enqueues belongs_to to be deleted with custom primary key" do |
| 103 | + belongs = DlKeyedBelongsTo.create! |
| 104 | + parent = DestroyAsyncParent.create! |
| 105 | + belongs.destroy_async_parent = parent |
| 106 | + belongs.save! |
| 107 | + belongs.destroy |
| 108 | + |
| 109 | + assert_difference -> { DestroyAsyncParent.count }, -1 do |
| 110 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 111 | + end |
| 112 | + ensure |
| 113 | + DlKeyedBelongsTo.delete_all |
| 114 | + DestroyAsyncParent.delete_all |
| 115 | + end |
| 116 | + |
| 117 | + test "has_one" do |
| 118 | + content = Content.create(title: "hello") |
| 119 | + book = BookDestroyAsync.create!(name: "Arr, matey!") |
| 120 | + book.content = content |
| 121 | + book.save! |
| 122 | + book.destroy |
| 123 | + |
| 124 | + assert_difference -> { Content.count }, -1 do |
| 125 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 126 | + end |
| 127 | + ensure |
| 128 | + Content.delete_all |
| 129 | + BookDestroyAsync.delete_all |
| 130 | + end |
| 131 | + |
| 132 | + |
| 133 | + test "enqueues has_one to be deleted with custom primary key" do |
| 134 | + child = DlKeyedHasOne.create! |
| 135 | + parent = DestroyAsyncParent.create! |
| 136 | + parent.dl_keyed_has_one = child |
| 137 | + parent.save! |
| 138 | + parent.destroy |
| 139 | + |
| 140 | + assert_difference -> { DlKeyedHasOne.count }, -1 do |
| 141 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 142 | + end |
| 143 | + ensure |
| 144 | + DlKeyedHasOne.delete_all |
| 145 | + DestroyAsyncParent.delete_all |
| 146 | + end |
| 147 | + |
| 148 | + |
| 149 | + test "has_many" do |
| 150 | + essay = EssayDestroyAsync.create!(name: "Der be treasure") |
| 151 | + essay2 = EssayDestroyAsync.create!(name: "Der be rum") |
| 152 | + book = BookDestroyAsync.create!(name: "Arr, matey!") |
| 153 | + book.essays << [essay, essay2] |
| 154 | + book.save! |
| 155 | + book.destroy |
| 156 | + |
| 157 | + assert_difference -> { EssayDestroyAsync.count }, -2 do |
| 158 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 159 | + end |
| 160 | + ensure |
| 161 | + EssayDestroyAsync.delete_all |
| 162 | + BookDestroyAsync.delete_all |
| 163 | + end |
| 164 | + |
| 165 | + test "has_many with sti parent class destroys all children class records" do |
| 166 | + book = BookDestroyAsync.create! |
| 167 | + LongEssayDestroyAsync.create!(book: book) |
| 168 | + ShortEssayDestroyAsync.create!(book: book) |
| 169 | + book.destroy |
| 170 | + |
| 171 | + assert_difference -> { EssayDestroyAsync.count }, -2 do |
| 172 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + test "enqueues the has_many to be deleted with custom primary key" do |
| 177 | + dl_keyed_has_many = DlKeyedHasMany.new |
| 178 | + parent = DestroyAsyncParent.create! |
| 179 | + parent.dl_keyed_has_many << [dl_keyed_has_many] |
| 180 | + |
| 181 | + parent.save! |
| 182 | + parent.destroy |
| 183 | + |
| 184 | + assert_difference -> { DlKeyedHasMany.count }, -1 do |
| 185 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 186 | + end |
| 187 | + ensure |
| 188 | + DlKeyedHasMany.delete_all |
| 189 | + DestroyAsyncParent.delete_all |
| 190 | + end |
| 191 | + |
| 192 | + test "not enqueue the job if transaction is not committed" do |
| 193 | + dl_keyed_has_many = DlKeyedHasMany.new |
| 194 | + parent = DestroyAsyncParent.create! |
| 195 | + parent.dl_keyed_has_many << [dl_keyed_has_many] |
| 196 | + |
| 197 | + parent.save! |
| 198 | + assert_no_enqueued_jobs do |
| 199 | + DestroyAsyncParent.transaction do |
| 200 | + parent.destroy |
| 201 | + raise ActiveRecord::Rollback |
| 202 | + end |
| 203 | + end |
| 204 | + ensure |
| 205 | + DlKeyedHasMany.delete_all |
| 206 | + DestroyAsyncParent.delete_all |
| 207 | + end |
| 208 | + |
| 209 | + test "has many ensures function for parent" do |
| 210 | + tag = Tag.create!(name: "Der be treasure") |
| 211 | + tag2 = Tag.create!(name: "Der be rum") |
| 212 | + parent = DestroyAsyncParentSoftDelete.create! |
| 213 | + parent.tags << [tag, tag2] |
| 214 | + parent.save! |
| 215 | + |
| 216 | + parent.run_callbacks(:destroy) |
| 217 | + parent.run_callbacks(:commit) |
| 218 | + |
| 219 | + assert_no_difference -> { Tag.count } do |
| 220 | + assert_raises ActiveRecord::DestroyAssociationAsyncError do |
| 221 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 222 | + end |
| 223 | + end |
| 224 | + |
| 225 | + parent.destroy |
| 226 | + assert_difference -> { Tag.count }, -2 do |
| 227 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 228 | + end |
| 229 | + ensure |
| 230 | + Tag.delete_all |
| 231 | + DestroyAsyncParentSoftDelete.delete_all |
| 232 | + end |
| 233 | + |
| 234 | + test "has one ensures function for parent" do |
| 235 | + child = DlKeyedHasOne.create! |
| 236 | + parent = DestroyAsyncParentSoftDelete.create! |
| 237 | + parent.dl_keyed_has_one = child |
| 238 | + parent.save! |
| 239 | + |
| 240 | + parent.run_callbacks(:destroy) |
| 241 | + parent.run_callbacks(:commit) |
| 242 | + |
| 243 | + assert_no_difference -> { DlKeyedHasOne.count } do |
| 244 | + assert_raises ActiveRecord::DestroyAssociationAsyncError do |
| 245 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 246 | + end |
| 247 | + end |
| 248 | + |
| 249 | + parent.destroy |
| 250 | + assert_difference -> { DlKeyedHasOne.count }, -1 do |
| 251 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 252 | + end |
| 253 | + ensure |
| 254 | + DlKeyedHasOne.delete_all |
| 255 | + DestroyAsyncParentSoftDelete.delete_all |
| 256 | + end |
| 257 | + |
| 258 | + test "enqueues belongs_to to be deleted with ensuring function" do |
| 259 | + belongs = DlKeyedBelongsToSoftDelete.create! |
| 260 | + parent = DestroyAsyncParentSoftDelete.create! |
| 261 | + belongs.destroy_async_parent_soft_delete = parent |
| 262 | + belongs.save! |
| 263 | + |
| 264 | + belongs.run_callbacks(:destroy) |
| 265 | + belongs.run_callbacks(:commit) |
| 266 | + |
| 267 | + assert_raises ActiveRecord::DestroyAssociationAsyncError do |
| 268 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 269 | + end |
| 270 | + |
| 271 | + assert_not parent.reload.deleted? |
| 272 | + |
| 273 | + belongs.destroy |
| 274 | + perform_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 275 | + assert parent.reload.deleted? |
| 276 | + ensure |
| 277 | + DlKeyedBelongsToSoftDelete.delete_all |
| 278 | + DestroyAsyncParentSoftDelete.delete_all |
| 279 | + end |
| 280 | + |
| 281 | + test "Don't enqueue with no relations" do |
| 282 | + parent = DestroyAsyncParent.create! |
| 283 | + parent.destroy |
| 284 | + |
| 285 | + assert_no_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 286 | + ensure |
| 287 | + DestroyAsyncParent.delete_all |
| 288 | + end |
| 289 | + |
| 290 | + test "Rollback prevents jobs from being enqueued" do |
| 291 | + tag = Tag.create!(name: "Der be treasure") |
| 292 | + tag2 = Tag.create!(name: "Der be rum") |
| 293 | + book = BookDestroyAsync.create! |
| 294 | + book.tags << [tag, tag2] |
| 295 | + book.save! |
| 296 | + ActiveRecord::Base.transaction do |
| 297 | + book.destroy |
| 298 | + raise ActiveRecord::Rollback |
| 299 | + end |
| 300 | + assert_no_enqueued_jobs only: ActiveRecord::DestroyAssociationAsyncJob |
| 301 | + end |
| 302 | +ensure |
| 303 | + Tag.delete_all |
| 304 | + BookDestroyAsync.delete_all |
| 305 | +end |
0 commit comments