acts_as_paranoid 0.7.2 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class MultipleDefaultScopesTest < ParanoidBaseTest
6
- def setup
7
- setup_db
8
-
9
- ParanoidPolygon.create! sides: 3
10
- ParanoidPolygon.create! sides: 3
11
- ParanoidPolygon.create! sides: 3
12
- ParanoidPolygon.create! sides: 8
13
-
14
- assert_equal 3, ParanoidPolygon.count
15
- assert_equal 4, ParanoidPolygon.unscoped.count
16
- end
17
-
18
- def test_fake_removal_with_multiple_default_scope
19
- ParanoidPolygon.first.destroy
20
- assert_equal 2, ParanoidPolygon.count
21
- assert_equal 3, ParanoidPolygon.with_deleted.count
22
- assert_equal 1, ParanoidPolygon.only_deleted.count
23
- assert_equal 4, ParanoidPolygon.unscoped.count
24
-
25
- ParanoidPolygon.destroy_all
26
- assert_equal 0, ParanoidPolygon.count
27
- assert_equal 3, ParanoidPolygon.with_deleted.count
28
- assert_equal 3, ParanoidPolygon.with_deleted.count
29
- assert_equal 4, ParanoidPolygon.unscoped.count
30
- end
31
-
32
- def test_real_removal_with_multiple_default_scope
33
- # two-step
34
- ParanoidPolygon.first.destroy
35
- ParanoidPolygon.only_deleted.first.destroy
36
- assert_equal 2, ParanoidPolygon.count
37
- assert_equal 2, ParanoidPolygon.with_deleted.count
38
- assert_equal 0, ParanoidPolygon.only_deleted.count
39
- assert_equal 3, ParanoidPolygon.unscoped.count
40
-
41
- ParanoidPolygon.first.destroy_fully!
42
- assert_equal 1, ParanoidPolygon.count
43
- assert_equal 1, ParanoidPolygon.with_deleted.count
44
- assert_equal 0, ParanoidPolygon.only_deleted.count
45
- assert_equal 2, ParanoidPolygon.unscoped.count
46
-
47
- ParanoidPolygon.delete_all!
48
- assert_equal 0, ParanoidPolygon.count
49
- assert_equal 0, ParanoidPolygon.with_deleted.count
50
- assert_equal 0, ParanoidPolygon.only_deleted.count
51
- assert_equal 1, ParanoidPolygon.unscoped.count
52
- end
53
- end
data/test/test_helper.rb DELETED
@@ -1,565 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler"
4
- begin
5
- Bundler.load
6
- rescue Bundler::BundlerError => e
7
- warn e.message
8
- warn "Run `bundle install` to install missing gems"
9
- exit e.status_code
10
- end
11
-
12
- if RUBY_ENGINE == "jruby"
13
- # Workaround for issue in I18n/JRuby combo.
14
- # See https://github.com/jruby/jruby/issues/6547 and
15
- # https://github.com/ruby-i18n/i18n/issues/555
16
- require "i18n/backend"
17
- require "i18n/backend/simple"
18
- end
19
-
20
- require "simplecov"
21
- SimpleCov.start do
22
- enable_coverage :branch
23
- end
24
-
25
- require "acts_as_paranoid"
26
- require "minitest/autorun"
27
- require "minitest/focus"
28
-
29
- # Silence deprecation halfway through the test
30
- I18n.enforce_available_locales = true
31
-
32
- ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
33
- ActiveRecord::Schema.verbose = false
34
-
35
- # rubocop:disable Metrics/AbcSize
36
- # rubocop:disable Metrics/MethodLength
37
- def setup_db
38
- ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength
39
- create_table :paranoid_times do |t|
40
- t.string :name
41
- t.datetime :deleted_at
42
- t.integer :paranoid_belongs_dependant_id
43
- t.integer :not_paranoid_id
44
-
45
- timestamps t
46
- end
47
-
48
- create_table :paranoid_booleans do |t|
49
- t.string :name
50
- t.boolean :is_deleted
51
- t.integer :paranoid_time_id
52
- t.integer :paranoid_with_counter_caches_count
53
- t.integer :paranoid_with_touch_and_counter_caches_count
54
- t.integer :custom_counter_cache
55
- timestamps t
56
- end
57
-
58
- create_table :paranoid_strings do |t|
59
- t.string :name
60
- t.string :deleted
61
- end
62
-
63
- create_table :not_paranoids do |t|
64
- t.string :name
65
- t.integer :paranoid_time_id
66
-
67
- timestamps t
68
- end
69
-
70
- create_table :has_one_not_paranoids do |t|
71
- t.string :name
72
- t.integer :paranoid_time_id
73
-
74
- timestamps t
75
- end
76
-
77
- create_table :paranoid_has_many_dependants do |t|
78
- t.string :name
79
- t.datetime :deleted_at
80
- t.integer :paranoid_time_id
81
- t.string :paranoid_time_polymorphic_with_deleted_type
82
- t.integer :paranoid_belongs_dependant_id
83
-
84
- timestamps t
85
- end
86
-
87
- create_table :paranoid_belongs_dependants do |t|
88
- t.string :name
89
- t.datetime :deleted_at
90
-
91
- timestamps t
92
- end
93
-
94
- create_table :paranoid_has_one_dependants do |t|
95
- t.string :name
96
- t.datetime :deleted_at
97
- t.integer :paranoid_boolean_id
98
-
99
- timestamps t
100
- end
101
-
102
- create_table :paranoid_with_callbacks do |t|
103
- t.string :name
104
- t.datetime :deleted_at
105
-
106
- timestamps t
107
- end
108
-
109
- create_table :paranoid_destroy_companies do |t|
110
- t.string :name
111
- t.datetime :deleted_at
112
-
113
- timestamps t
114
- end
115
-
116
- create_table :paranoid_delete_companies do |t|
117
- t.string :name
118
- t.datetime :deleted_at
119
-
120
- timestamps t
121
- end
122
-
123
- create_table :paranoid_products do |t|
124
- t.integer :paranoid_destroy_company_id
125
- t.integer :paranoid_delete_company_id
126
- t.string :name
127
- t.datetime :deleted_at
128
-
129
- timestamps t
130
- end
131
-
132
- create_table :super_paranoids do |t|
133
- t.string :type
134
- t.references :has_many_inherited_super_paranoidz,
135
- index: { name: "index__sp_id_on_has_many_isp" }
136
- t.datetime :deleted_at
137
-
138
- timestamps t
139
- end
140
-
141
- create_table :has_many_inherited_super_paranoidzs do |t|
142
- t.references :super_paranoidz, index: { name: "index_has_many_isp_on_sp_id" }
143
- t.datetime :deleted_at
144
-
145
- timestamps t
146
- end
147
-
148
- create_table :paranoid_many_many_parent_lefts do |t|
149
- t.string :name
150
- timestamps t
151
- end
152
-
153
- create_table :paranoid_many_many_parent_rights do |t|
154
- t.string :name
155
- timestamps t
156
- end
157
-
158
- create_table :paranoid_many_many_children do |t|
159
- t.integer :paranoid_many_many_parent_left_id
160
- t.integer :paranoid_many_many_parent_right_id
161
- t.datetime :deleted_at
162
- timestamps t
163
- end
164
-
165
- create_table :paranoid_with_scoped_validations do |t|
166
- t.string :name
167
- t.string :category
168
- t.datetime :deleted_at
169
- timestamps t
170
- end
171
-
172
- create_table :paranoid_forests do |t|
173
- t.string :name
174
- t.boolean :rainforest
175
- t.datetime :deleted_at
176
-
177
- timestamps t
178
- end
179
-
180
- create_table :paranoid_trees do |t|
181
- t.integer :paranoid_forest_id
182
- t.string :name
183
- t.datetime :deleted_at
184
-
185
- timestamps t
186
- end
187
-
188
- create_table :paranoid_polygons do |t|
189
- t.integer :sides
190
- t.datetime :deleted_at
191
-
192
- timestamps t
193
- end
194
-
195
- create_table :paranoid_androids do |t|
196
- t.datetime :deleted_at
197
- end
198
-
199
- create_table :paranoid_sections do |t|
200
- t.integer :paranoid_time_id
201
- t.integer :paranoid_thing_id
202
- t.string :paranoid_thing_type
203
- t.datetime :deleted_at
204
- end
205
-
206
- create_table :paranoid_boolean_not_nullables do |t|
207
- t.string :name
208
- t.boolean :deleted, :boolean, null: false, default: false
209
- end
210
-
211
- create_table :paranoid_belongs_to_polymorphics do |t|
212
- t.string :name
213
- t.string :parent_type
214
- t.integer :parent_id
215
- t.datetime :deleted_at
216
-
217
- timestamps t
218
- end
219
-
220
- create_table :not_paranoid_has_many_as_parents do |t|
221
- t.string :name
222
-
223
- timestamps t
224
- end
225
-
226
- create_table :paranoid_has_many_as_parents do |t|
227
- t.string :name
228
- t.datetime :deleted_at
229
-
230
- timestamps t
231
- end
232
-
233
- create_table :paranoid_no_double_tap_destroys_fullies do |t|
234
- t.datetime :deleted_at
235
- end
236
-
237
- create_table :paranoid_with_counter_caches do |t|
238
- t.string :name
239
- t.datetime :deleted_at
240
- t.integer :paranoid_boolean_id
241
-
242
- timestamps t
243
- end
244
- end
245
- end
246
- # rubocop:enable Metrics/AbcSize
247
- # rubocop:enable Metrics/MethodLength
248
-
249
- def timestamps(table)
250
- table.column :created_at, :timestamp, null: false
251
- table.column :updated_at, :timestamp, null: false
252
- end
253
-
254
- def teardown_db
255
- ActiveRecord::Base.connection.data_sources.each do |table|
256
- ActiveRecord::Base.connection.drop_table(table)
257
- end
258
- end
259
-
260
- class ParanoidTime < ActiveRecord::Base
261
- acts_as_paranoid
262
-
263
- validates_uniqueness_of :name
264
-
265
- has_many :paranoid_has_many_dependants, dependent: :destroy
266
- has_many :paranoid_booleans, dependent: :destroy
267
- has_many :not_paranoids, dependent: :delete_all
268
- has_many :paranoid_sections, dependent: :destroy
269
-
270
- has_one :has_one_not_paranoid, dependent: :destroy
271
-
272
- belongs_to :not_paranoid, dependent: :destroy
273
- end
274
-
275
- class ParanoidBoolean < ActiveRecord::Base
276
- acts_as_paranoid column_type: "boolean", column: "is_deleted"
277
- validates_as_paranoid
278
- validates_uniqueness_of_without_deleted :name
279
-
280
- belongs_to :paranoid_time
281
- has_one :paranoid_has_one_dependant, dependent: :destroy
282
- has_many :paranoid_with_counter_cache, dependent: :destroy
283
- has_many :paranoid_with_custom_counter_cache, dependent: :destroy
284
- has_many :paranoid_with_touch_and_counter_cache, dependent: :destroy
285
- has_many :paranoid_with_touch, dependent: :destroy
286
- end
287
-
288
- class ParanoidString < ActiveRecord::Base
289
- acts_as_paranoid column_type: "string", column: "deleted", deleted_value: "dead"
290
- end
291
-
292
- class NotParanoid < ActiveRecord::Base
293
- end
294
-
295
- class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
296
- acts_as_paranoid double_tap_destroys_fully: false
297
- end
298
-
299
- class HasOneNotParanoid < ActiveRecord::Base
300
- belongs_to :paranoid_time, with_deleted: true
301
- end
302
-
303
- class DoubleHasOneNotParanoid < HasOneNotParanoid
304
- belongs_to :paranoid_time, with_deleted: true
305
- begin
306
- verbose = $VERBOSE
307
- $VERBOSE = false
308
- belongs_to :paranoid_time, with_deleted: true
309
- ensure
310
- $VERBOSE = verbose
311
- end
312
- end
313
-
314
- class ParanoidWithCounterCache < ActiveRecord::Base
315
- acts_as_paranoid
316
- belongs_to :paranoid_boolean, counter_cache: true
317
- end
318
-
319
- class ParanoidWithCustomCounterCache < ActiveRecord::Base
320
- self.table_name = "paranoid_with_counter_caches"
321
-
322
- acts_as_paranoid
323
- belongs_to :paranoid_boolean, counter_cache: :custom_counter_cache
324
- end
325
-
326
- class ParanoidWithCounterCacheOnOptionalBelognsTo < ActiveRecord::Base
327
- self.table_name = "paranoid_with_counter_caches"
328
-
329
- acts_as_paranoid
330
- if ActiveRecord::VERSION::MAJOR < 5
331
- belongs_to :paranoid_boolean, counter_cache: true, required: false
332
- else
333
- belongs_to :paranoid_boolean, counter_cache: true, optional: true
334
- end
335
- end
336
-
337
- class ParanoidWithTouch < ActiveRecord::Base
338
- self.table_name = "paranoid_with_counter_caches"
339
- acts_as_paranoid
340
- belongs_to :paranoid_boolean, touch: true
341
- end
342
-
343
- class ParanoidWithTouchAndCounterCache < ActiveRecord::Base
344
- self.table_name = "paranoid_with_counter_caches"
345
- acts_as_paranoid
346
- belongs_to :paranoid_boolean, touch: true, counter_cache: true
347
- end
348
-
349
- class ParanoidHasManyDependant < ActiveRecord::Base
350
- acts_as_paranoid
351
- belongs_to :paranoid_time
352
- belongs_to :paranoid_time_with_scope,
353
- -> { where(name: "hello").includes(:not_paranoid) },
354
- class_name: "ParanoidTime", foreign_key: :paranoid_time_id
355
- belongs_to :paranoid_time_with_deleted, class_name: "ParanoidTime",
356
- foreign_key: :paranoid_time_id, with_deleted: true
357
- belongs_to :paranoid_time_with_scope_with_deleted,
358
- -> { where(name: "hello").includes(:not_paranoid) },
359
- class_name: "ParanoidTime", foreign_key: :paranoid_time_id, with_deleted: true
360
- belongs_to :paranoid_time_polymorphic_with_deleted, class_name: "ParanoidTime",
361
- foreign_key: :paranoid_time_id,
362
- polymorphic: true, with_deleted: true
363
-
364
- belongs_to :paranoid_belongs_dependant, dependent: :destroy
365
- end
366
-
367
- class ParanoidBelongsDependant < ActiveRecord::Base
368
- acts_as_paranoid
369
-
370
- has_many :paranoid_has_many_dependants
371
- end
372
-
373
- class ParanoidHasOneDependant < ActiveRecord::Base
374
- acts_as_paranoid
375
-
376
- belongs_to :paranoid_boolean
377
- end
378
-
379
- class ParanoidWithCallback < ActiveRecord::Base
380
- acts_as_paranoid
381
-
382
- attr_accessor :called_before_destroy, :called_after_destroy,
383
- :called_after_commit_on_destroy, :called_before_recover,
384
- :called_after_recover
385
-
386
- before_destroy :call_me_before_destroy
387
- after_destroy :call_me_after_destroy
388
-
389
- after_commit :call_me_after_commit_on_destroy, on: :destroy
390
-
391
- before_recover :call_me_before_recover
392
- after_recover :call_me_after_recover
393
-
394
- def initialize(*attrs)
395
- @called_before_destroy = @called_after_destroy = @called_after_commit_on_destroy = false
396
- super(*attrs)
397
- end
398
-
399
- def call_me_before_destroy
400
- @called_before_destroy = true
401
- end
402
-
403
- def call_me_after_destroy
404
- @called_after_destroy = true
405
- end
406
-
407
- def call_me_after_commit_on_destroy
408
- @called_after_commit_on_destroy = true
409
- end
410
-
411
- def call_me_before_recover
412
- @called_before_recover = true
413
- end
414
-
415
- def call_me_after_recover
416
- @called_after_recover = true
417
- end
418
- end
419
-
420
- class ParanoidDestroyCompany < ActiveRecord::Base
421
- acts_as_paranoid
422
- validates :name, presence: true
423
- has_many :paranoid_products, dependent: :destroy
424
- end
425
-
426
- class ParanoidDeleteCompany < ActiveRecord::Base
427
- acts_as_paranoid
428
- validates :name, presence: true
429
- has_many :paranoid_products, dependent: :delete_all
430
- end
431
-
432
- class ParanoidProduct < ActiveRecord::Base
433
- acts_as_paranoid
434
- belongs_to :paranoid_destroy_company
435
- belongs_to :paranoid_delete_company
436
- validates_presence_of :name
437
- end
438
-
439
- class SuperParanoid < ActiveRecord::Base
440
- acts_as_paranoid
441
- belongs_to :has_many_inherited_super_paranoidz
442
- end
443
-
444
- class HasManyInheritedSuperParanoidz < ActiveRecord::Base
445
- has_many :super_paranoidz, class_name: "InheritedParanoid", dependent: :destroy
446
- end
447
-
448
- class InheritedParanoid < SuperParanoid
449
- acts_as_paranoid
450
- end
451
-
452
- class ParanoidManyManyParentLeft < ActiveRecord::Base
453
- has_many :paranoid_many_many_children
454
- has_many :paranoid_many_many_parent_rights, through: :paranoid_many_many_children
455
- end
456
-
457
- class ParanoidManyManyParentRight < ActiveRecord::Base
458
- has_many :paranoid_many_many_children
459
- has_many :paranoid_many_many_parent_lefts, through: :paranoid_many_many_children
460
- end
461
-
462
- class ParanoidManyManyChild < ActiveRecord::Base
463
- acts_as_paranoid
464
- belongs_to :paranoid_many_many_parent_left
465
- belongs_to :paranoid_many_many_parent_right
466
- end
467
-
468
- class ParanoidWithScopedValidation < ActiveRecord::Base
469
- acts_as_paranoid
470
- validates_uniqueness_of :name, scope: :category
471
- end
472
-
473
- class ParanoidBelongsToPolymorphic < ActiveRecord::Base
474
- acts_as_paranoid
475
- belongs_to :parent, polymorphic: true, with_deleted: true
476
- end
477
-
478
- class NotParanoidHasManyAsParent < ActiveRecord::Base
479
- has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
480
- end
481
-
482
- class ParanoidHasManyAsParent < ActiveRecord::Base
483
- acts_as_paranoid
484
- has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
485
- end
486
-
487
- class ParanoidBaseTest < ActiveSupport::TestCase
488
- def setup
489
- setup_db
490
-
491
- ["paranoid", "really paranoid", "extremely paranoid"].each do |name|
492
- ParanoidTime.create! name: name
493
- ParanoidBoolean.create! name: name
494
- end
495
-
496
- ParanoidString.create! name: "strings can be paranoid"
497
- NotParanoid.create! name: "no paranoid goals"
498
- ParanoidWithCallback.create! name: "paranoid with callbacks"
499
- end
500
-
501
- def teardown
502
- teardown_db
503
- end
504
-
505
- def assert_empty(collection)
506
- assert(collection.respond_to?(:empty?) && collection.empty?)
507
- end
508
-
509
- def assert_paranoid_deletion(model)
510
- row = find_row(model)
511
- assert_not_nil row, "#{model.class} entirely deleted"
512
- assert_not_nil row["deleted_at"], "Deleted at not set"
513
- end
514
-
515
- def assert_non_paranoid_deletion(model)
516
- row = find_row(model)
517
- assert_nil row, "#{model.class} still exists"
518
- end
519
-
520
- def find_row(model)
521
- sql = "select deleted_at from #{model.class.table_name} where id = #{model.id}"
522
- # puts sql here if you want to debug
523
- model.class.connection.select_one(sql)
524
- end
525
- end
526
-
527
- class ParanoidForest < ActiveRecord::Base
528
- acts_as_paranoid
529
-
530
- ActiveRecord::Base.logger = Logger.new(StringIO.new)
531
-
532
- scope :rainforest, -> { where(rainforest: true) }
533
-
534
- has_many :paranoid_trees, dependent: :destroy
535
- end
536
-
537
- class ParanoidTree < ActiveRecord::Base
538
- acts_as_paranoid
539
- belongs_to :paranoid_forest
540
- validates_presence_of :name
541
- end
542
-
543
- class ParanoidPolygon < ActiveRecord::Base
544
- acts_as_paranoid
545
- default_scope { where("sides = ?", 3) }
546
- end
547
-
548
- class ParanoidAndroid < ActiveRecord::Base
549
- acts_as_paranoid
550
- end
551
-
552
- class ParanoidSection < ActiveRecord::Base
553
- acts_as_paranoid
554
- belongs_to :paranoid_time
555
- belongs_to :paranoid_thing, polymorphic: true, dependent: :destroy
556
- end
557
-
558
- class ParanoidBooleanNotNullable < ActiveRecord::Base
559
- acts_as_paranoid column: "deleted", column_type: "boolean", allow_nulls: false
560
- end
561
-
562
- class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
563
- acts_as_paranoid
564
- self.table_name = "explicit_table"
565
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class InheritanceTest < ParanoidBaseTest
6
- def test_destroy_dependents_with_inheritance
7
- has_many_inherited_super_paranoidz = HasManyInheritedSuperParanoidz.new
8
- has_many_inherited_super_paranoidz.save
9
- has_many_inherited_super_paranoidz.super_paranoidz.create
10
- assert_nothing_raised { has_many_inherited_super_paranoidz.destroy }
11
- end
12
-
13
- def test_class_instance_variables_are_inherited
14
- assert_nothing_raised { InheritedParanoid.paranoid_column }
15
- end
16
- end