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,199 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class RelationsTest < ActiveSupport::TestCase
6
- class ParanoidForest < ActiveRecord::Base
7
- acts_as_paranoid
8
-
9
- scope :rainforest, -> { where(rainforest: true) }
10
-
11
- has_many :paranoid_trees, dependent: :destroy
12
- end
13
-
14
- class ParanoidTree < ActiveRecord::Base
15
- acts_as_paranoid
16
- belongs_to :paranoid_forest
17
- validates_presence_of :name
18
- end
19
-
20
- class NotParanoidBowl < ActiveRecord::Base
21
- has_many :paranoid_chocolates, dependent: :destroy
22
- end
23
-
24
- class ParanoidChocolate < ActiveRecord::Base
25
- acts_as_paranoid
26
- belongs_to :not_paranoid_bowl
27
- validates_presence_of :name
28
- end
29
-
30
- def setup
31
- ActiveRecord::Schema.define(version: 1) do
32
- create_table :paranoid_forests do |t|
33
- t.string :name
34
- t.boolean :rainforest
35
- t.datetime :deleted_at
36
-
37
- timestamps t
38
- end
39
-
40
- create_table :paranoid_trees do |t|
41
- t.integer :paranoid_forest_id
42
- t.string :name
43
- t.datetime :deleted_at
44
-
45
- timestamps t
46
- end
47
-
48
- create_table :not_paranoid_bowls do |t|
49
- t.string :name
50
-
51
- timestamps t
52
- end
53
-
54
- create_table :paranoid_chocolates do |t|
55
- t.integer :not_paranoid_bowl_id
56
- t.string :name
57
- t.datetime :deleted_at
58
-
59
- timestamps t
60
- end
61
- end
62
-
63
- @paranoid_forest_1 = ParanoidForest.create! name: "ParanoidForest #1"
64
- @paranoid_forest_2 = ParanoidForest.create! name: "ParanoidForest #2", rainforest: true
65
- @paranoid_forest_3 = ParanoidForest.create! name: "ParanoidForest #3", rainforest: true
66
-
67
- assert_equal 3, ParanoidForest.count
68
- assert_equal 2, ParanoidForest.rainforest.count
69
-
70
- @paranoid_forest_1.paranoid_trees.create! name: "ParanoidTree #1"
71
- @paranoid_forest_1.paranoid_trees.create! name: "ParanoidTree #2"
72
- @paranoid_forest_2.paranoid_trees.create! name: "ParanoidTree #3"
73
- @paranoid_forest_2.paranoid_trees.create! name: "ParanoidTree #4"
74
-
75
- assert_equal 4, ParanoidTree.count
76
- end
77
-
78
- def teardown
79
- teardown_db
80
- end
81
-
82
- def test_filtering_with_scopes
83
- assert_equal 2, ParanoidForest.rainforest.with_deleted.count
84
- assert_equal 2, ParanoidForest.with_deleted.rainforest.count
85
-
86
- assert_equal 0, ParanoidForest.rainforest.only_deleted.count
87
- assert_equal 0, ParanoidForest.only_deleted.rainforest.count
88
-
89
- ParanoidForest.rainforest.first.destroy
90
- assert_equal 1, ParanoidForest.rainforest.count
91
-
92
- assert_equal 2, ParanoidForest.rainforest.with_deleted.count
93
- assert_equal 2, ParanoidForest.with_deleted.rainforest.count
94
-
95
- assert_equal 1, ParanoidForest.rainforest.only_deleted.count
96
- assert_equal 1, ParanoidForest.only_deleted.rainforest.count
97
- end
98
-
99
- def test_associations_filtered_by_with_deleted
100
- assert_equal 2, @paranoid_forest_1.paranoid_trees.with_deleted.count
101
- assert_equal 2, @paranoid_forest_2.paranoid_trees.with_deleted.count
102
-
103
- @paranoid_forest_1.paranoid_trees.first.destroy
104
- assert_equal 1, @paranoid_forest_1.paranoid_trees.count
105
- assert_equal 2, @paranoid_forest_1.paranoid_trees.with_deleted.count
106
- assert_equal 4, ParanoidTree.with_deleted.count
107
-
108
- @paranoid_forest_2.paranoid_trees.first.destroy
109
- assert_equal 1, @paranoid_forest_2.paranoid_trees.count
110
- assert_equal 2, @paranoid_forest_2.paranoid_trees.with_deleted.count
111
- assert_equal 4, ParanoidTree.with_deleted.count
112
-
113
- @paranoid_forest_1.paranoid_trees.first.destroy
114
- assert_equal 0, @paranoid_forest_1.paranoid_trees.count
115
- assert_equal 2, @paranoid_forest_1.paranoid_trees.with_deleted.count
116
- assert_equal 4, ParanoidTree.with_deleted.count
117
- end
118
-
119
- def test_associations_filtered_by_only_deleted
120
- assert_equal 0, @paranoid_forest_1.paranoid_trees.only_deleted.count
121
- assert_equal 0, @paranoid_forest_2.paranoid_trees.only_deleted.count
122
-
123
- @paranoid_forest_1.paranoid_trees.first.destroy
124
- assert_equal 1, @paranoid_forest_1.paranoid_trees.only_deleted.count
125
- assert_equal 1, ParanoidTree.only_deleted.count
126
-
127
- @paranoid_forest_2.paranoid_trees.first.destroy
128
- assert_equal 1, @paranoid_forest_2.paranoid_trees.only_deleted.count
129
- assert_equal 2, ParanoidTree.only_deleted.count
130
-
131
- @paranoid_forest_1.paranoid_trees.first.destroy
132
- assert_equal 2, @paranoid_forest_1.paranoid_trees.only_deleted.count
133
- assert_equal 3, ParanoidTree.only_deleted.count
134
- end
135
-
136
- def test_fake_removal_through_relation
137
- # destroy: through a relation.
138
- ParanoidForest.rainforest.destroy(@paranoid_forest_3.id)
139
- assert_equal 1, ParanoidForest.rainforest.count
140
- assert_equal 2, ParanoidForest.rainforest.with_deleted.count
141
- assert_equal 1, ParanoidForest.rainforest.only_deleted.count
142
-
143
- # destroy_all: through a relation
144
- @paranoid_forest_2.paranoid_trees.destroy_all
145
- assert_equal 0, @paranoid_forest_2.paranoid_trees.count
146
- assert_equal 2, @paranoid_forest_2.paranoid_trees.with_deleted.count
147
- end
148
-
149
- def test_fake_removal_through_has_many_relation_of_non_paranoid_model
150
- not_paranoid = NotParanoidBowl.create! name: "NotParanoid #1"
151
- not_paranoid.paranoid_chocolates.create! name: "ParanoidChocolate #1"
152
- not_paranoid.paranoid_chocolates.create! name: "ParanoidChocolate #2"
153
-
154
- not_paranoid.paranoid_chocolates.destroy_all
155
- assert_equal 0, not_paranoid.paranoid_chocolates.count
156
- assert_equal 2, not_paranoid.paranoid_chocolates.with_deleted.count
157
- end
158
-
159
- def test_real_removal_through_relation_with_destroy_fully
160
- ParanoidForest.rainforest.destroy_fully!(@paranoid_forest_3)
161
- assert_equal 1, ParanoidForest.rainforest.count
162
- assert_equal 1, ParanoidForest.rainforest.with_deleted.count
163
- assert_equal 0, ParanoidForest.rainforest.only_deleted.count
164
- end
165
-
166
- def test_real_removal_through_relation_with_deprecated_destroy!
167
- ParanoidForest.rainforest.destroy!(@paranoid_forest_3)
168
- assert_equal 1, ParanoidForest.rainforest.count
169
- assert_equal 1, ParanoidForest.rainforest.with_deleted.count
170
- assert_equal 0, ParanoidForest.rainforest.only_deleted.count
171
- end
172
-
173
- def test_two_step_real_removal_through_relation_with_destroy
174
- # destroy: two-step through a relation
175
- paranoid_tree = @paranoid_forest_1.paranoid_trees.first
176
- @paranoid_forest_1.paranoid_trees.destroy(paranoid_tree.id)
177
- @paranoid_forest_1.paranoid_trees.only_deleted.destroy(paranoid_tree.id)
178
- assert_equal 1, @paranoid_forest_1.paranoid_trees.count
179
- assert_equal 1, @paranoid_forest_1.paranoid_trees.with_deleted.count
180
- assert_equal 0, @paranoid_forest_1.paranoid_trees.only_deleted.count
181
- end
182
-
183
- def test_two_step_real_removal_through_relation_with_destroy_all
184
- # destroy_all: two-step through a relation
185
- @paranoid_forest_1.paranoid_trees.destroy_all
186
- @paranoid_forest_1.paranoid_trees.only_deleted.destroy_all
187
- assert_equal 0, @paranoid_forest_1.paranoid_trees.count
188
- assert_equal 0, @paranoid_forest_1.paranoid_trees.with_deleted.count
189
- assert_equal 0, @paranoid_forest_1.paranoid_trees.only_deleted.count
190
- end
191
-
192
- def test_real_removal_through_relation_with_delete_all_bang
193
- # delete_all!: through a relation
194
- @paranoid_forest_2.paranoid_trees.delete_all!
195
- assert_equal 0, @paranoid_forest_2.paranoid_trees.count
196
- assert_equal 0, @paranoid_forest_2.paranoid_trees.with_deleted.count
197
- assert_equal 0, @paranoid_forest_2.paranoid_trees.only_deleted.count
198
- end
199
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class TableNamespaceTest < ActiveSupport::TestCase
6
- module Paranoid
7
- class Blob < ActiveRecord::Base
8
- acts_as_paranoid
9
-
10
- validates_presence_of :name
11
-
12
- def self.table_name_prefix
13
- "paranoid_"
14
- end
15
- end
16
- end
17
-
18
- def setup
19
- ActiveRecord::Schema.define(version: 1) do
20
- create_table :paranoid_blobs do |t|
21
- t.string :name
22
- t.datetime :deleted_at
23
-
24
- timestamps t
25
- end
26
- end
27
- end
28
-
29
- def teardown
30
- teardown_db
31
- end
32
-
33
- def test_correct_table_name
34
- assert_equal "paranoid_blobs", Paranoid::Blob.table_name
35
-
36
- b = Paranoid::Blob.new(name: "hello!")
37
- b.save!
38
- assert_equal b, Paranoid::Blob.first
39
- end
40
- end
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class ValidatesUniquenessTest < ParanoidBaseTest
6
- def test_should_include_deleted_by_default
7
- ParanoidTime.new(name: "paranoid").tap do |record|
8
- assert !record.valid?
9
- ParanoidTime.first.destroy
10
- assert !record.valid?
11
- ParanoidTime.only_deleted.first.destroy!
12
- assert record.valid?
13
- end
14
- end
15
-
16
- def test_should_validate_without_deleted
17
- ParanoidBoolean.new(name: "paranoid").tap do |record|
18
- refute record.valid?
19
- ParanoidBoolean.first.destroy
20
- assert record.valid?
21
- ParanoidBoolean.only_deleted.first.destroy!
22
- assert record.valid?
23
- end
24
- end
25
-
26
- def test_validate_serialized_attribute_without_deleted
27
- ParanoidWithSerializedColumn.create!(name: "ParanoidWithSerializedColumn #1",
28
- colors: %w[Cyan Maroon])
29
- record = ParanoidWithSerializedColumn.new(name: "ParanoidWithSerializedColumn #2")
30
- record.colors = %w[Cyan Maroon]
31
- refute record.valid?
32
-
33
- record.colors = %w[Beige Turquoise]
34
- assert record.valid?
35
- end
36
-
37
- def test_updated_serialized_attribute_validated_without_deleted
38
- record = ParanoidWithSerializedColumn.create!(name: "ParanoidWithSerializedColumn #1",
39
- colors: %w[Cyan Maroon])
40
- record.update!(colors: %w[Beige Turquoise])
41
- assert record.valid?
42
- end
43
-
44
- def test_models_with_scoped_validations_can_be_multiply_deleted
45
- model_a = ParanoidWithScopedValidation.create(name: "Model A", category: "Category A")
46
- model_b = ParanoidWithScopedValidation.create(name: "Model B", category: "Category B")
47
-
48
- ParanoidWithScopedValidation.delete([model_a.id, model_b.id])
49
-
50
- assert_paranoid_deletion(model_a)
51
- assert_paranoid_deletion(model_b)
52
- end
53
-
54
- def test_models_with_scoped_validations_can_be_multiply_destroyed
55
- model_a = ParanoidWithScopedValidation.create(name: "Model A", category: "Category A")
56
- model_b = ParanoidWithScopedValidation.create(name: "Model B", category: "Category B")
57
-
58
- ParanoidWithScopedValidation.destroy([model_a.id, model_b.id])
59
-
60
- assert_paranoid_deletion(model_a)
61
- assert_paranoid_deletion(model_b)
62
- end
63
- end