acts_as_paranoid 0.7.3 → 0.8.0

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