forked from mongoid/mongoid-history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_spec.rb
374 lines (324 loc) · 12.3 KB
/
update_spec.rb
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
require 'spec_helper'
describe Mongoid::History::Attributes::Update do
describe '#attributes' do
describe '#insert_embeds_one_changes' do
context 'Case: relation without alias' do
before :each do
class ModelOne
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_ones
embeds_one :emb_one
track_history on: :fields
end
class EmbOne
include Mongoid::Document
field :em_foo
field :em_bar
embedded_in :model_one
end
end
after :each do
Object.send(:remove_const, :ModelOne)
Object.send(:remove_const, :EmbOne)
end
before :each do
allow(base).to receive(:changes) { changes }
end
let(:obj_one) { ModelOne.new }
let(:base) { described_class.new(obj_one) }
let(:changes) do
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
end
subject { base.attributes }
context 'with permitted attributes' do
before :each do
ModelOne.track_history on: { emb_one: :em_foo }
end
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new' }] }
end
context 'without permitted attributes' do
before :each do
ModelOne.track_history on: :emb_one
end
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
end
context 'when old value soft-deleted' do
before :each do
ModelOne.track_history on: :emb_one
end
let(:changes) do
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo', 'deleted_at' => Time.now }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
end
it { expect(subject['emb_one']).to eq [{}, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
end
context 'when new value soft-deleted' do
before :each do
ModelOne.track_history on: :emb_one
end
let(:changes) do
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'deleted_at' => Time.now }] }
end
it { expect(subject['emb_one']).to eq [{ 'em_foo' => 'Em-Foo' }, {}] }
end
context 'when not tracked' do
before :each do
ModelOne.track_history on: :fields
allow(ModelOne).to receive(:dynamic_enabled?) { false }
end
it { expect(subject['emb_one']).to be_nil }
end
end
context 'Case: relation with alias' do
before :each do
class ModelOne
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_ones
embeds_one :emb_one, store_as: :eon
track_history on: :fields
end
class EmbOne
include Mongoid::Document
field :em_foo
field :em_bar
embedded_in :model_one
end
end
after :each do
Object.send(:remove_const, :ModelOne)
Object.send(:remove_const, :EmbOne)
end
before :each do
ModelOne.track_history on: :emb_one
allow(base).to receive(:changes) { changes }
end
let(:obj_one) { ModelOne.new }
let(:base) { described_class.new(obj_one) }
let(:changes) do
{ 'emb_one' => [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
end
subject { base.attributes }
it { expect(subject['eon']).to eq [{ 'em_foo' => 'Em-Foo' }, { 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }] }
end
context 'when original and modified value same' do
before :each do
class DummyUpdateModel
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :dummy_update_models
embeds_one :dummy_embedded_model
track_history on: :fields
end
class DummyEmbeddedModel
include Mongoid::Document
field :em_foo
field :em_bar
embedded_in :dummy_update_model
end
end
after :each do
Object.send(:remove_const, :DummyUpdateModel)
Object.send(:remove_const, :DummyEmbeddedModel)
end
before :each do
allow(base).to receive(:changes) { changes }
DummyUpdateModel.track_history on: :dummy_embedded_model
end
let(:obj_one) { DummyUpdateModel.new }
let(:base) { described_class.new(obj_one) }
let(:changes) do
{ 'dummy_embedded_model' => [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }, { 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }] }
end
subject { base.attributes }
it { expect(subject.keys).to_not include 'dummy_embedded_model' }
end
end
describe '#insert_embeds_many_changes' do
context 'Case: relation without alias' do
before :each do
class ModelOne
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_ones
if Mongoid::Compatibility::Version.mongoid7_or_newer?
embeds_many :emb_ones
else
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
end
track_history on: :fields
end
class EmbOne
include Mongoid::Document
field :em_foo
field :em_bar
embedded_in :model_one
end
end
before :each do
allow(base).to receive(:changes) { changes }
end
let(:obj_one) { ModelOne.new }
let(:base) { described_class.new(obj_one) }
subject { base.attributes }
context 'with whitelist attributes' do
before :each do
ModelOne.track_history on: { emb_ones: :em_foo }
end
let(:changes) do
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
end
it 'should track only whitelisted attributes' do
expect(subject['emb_ones']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new' }]]
end
end
context 'without whitelist attributes' do
before :each do
ModelOne.track_history(on: :emb_ones)
end
let(:changes) do
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'deleted_at' => Time.now }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
end
it 'should ignore soft-deleted objects' do
expect(subject['emb_ones']).to eq [[], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
end
end
after :each do
Object.send(:remove_const, :ModelOne)
Object.send(:remove_const, :EmbOne)
end
end
context 'Case: relation with alias' do
before :each do
class ModelOne
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_ones
if Mongoid::Compatibility::Version.mongoid7_or_newer?
embeds_many :emb_ones, store_as: :eons
else
embeds_many :emb_ones, store_as: :eons, inverse_class_name: 'EmbOne'
end
track_history on: :fields
end
class EmbOne
include Mongoid::Document
field :em_foo
field :em_bar
embedded_in :model_one
end
end
before :each do
ModelOne.track_history on: :emb_ones
allow(base).to receive(:changes) { changes }
end
let(:obj_one) { ModelOne.new }
let(:base) { described_class.new(obj_one) }
let(:changes) do
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]] }
end
subject { base.attributes }
it 'should save audit history under relation alias' do
expect(subject['eons']).to eq [[{ 'em_foo' => 'Em-Foo' }], [{ 'em_foo' => 'Em-Foo-new', 'em_bar' => 'Em-Bar-new' }]]
end
after :each do
Object.send(:remove_const, :ModelOne)
Object.send(:remove_const, :EmbOne)
end
end
context 'when original and modified value same' do
before :each do
class ModelOne
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :model_ones
if Mongoid::Compatibility::Version.mongoid7_or_newer?
embeds_many :emb_ones
else
embeds_many :emb_ones, inverse_class_name: 'EmbOne'
end
track_history on: :fields
end
class EmbOne
include Mongoid::Document
field :em_foo
field :em_bar
embedded_in :model_one
end
end
before :each do
allow(base).to receive(:changes) { changes }
ModelOne.track_history on: :emb_ones
end
let(:obj_one) { ModelOne.new }
let(:base) { described_class.new(obj_one) }
let(:changes) do
{ 'emb_ones' => [[{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }], [{ 'em_foo' => 'Em-Foo', 'em_bar' => 'Em-Bar' }]] }
end
subject { base.attributes }
it { expect(subject.keys).to_not include 'emb_ones' }
after :each do
Object.send(:remove_const, :ModelOne)
Object.send(:remove_const, :EmbOne)
end
end
end
[false, true].each do |original_nil|
context "when original value #{original_nil ? 'nil' : 'blank'} and modified value #{original_nil ? 'blank' : 'nil'}" do
[nil, false, true].each do |track_blank_changes|
context "when track_blank_changes #{track_blank_changes.nil? ? 'default' : track_blank_changes}" do
before :each do
class DummyParent
include Mongoid::Document
include Mongoid::History::Trackable
store_in collection: :dummy_parents
has_and_belongs_to_many :other_dummy_parents
field :boolean, type: Boolean
field :string, type: String
field :hash, type: Hash
end
class OtherDummyParent
include Mongoid::Document
has_and_belongs_to_many :dummy_parents
end
if track_blank_changes.nil?
DummyParent.track_history on: :fields
else
DummyParent.track_history \
on: :fields,
track_blank_changes: track_blank_changes
end
allow(base).to receive(:changes) { changes }
end
after :each do
Object.send(:remove_const, :DummyParent)
Object.send(:remove_const, :OtherDummyParent)
end
let(:base) { described_class.new(DummyParent.new) }
subject { base.attributes.keys }
# These can't be memoizing methods (i.e. lets) because of limits
# on where those can be used.
cmp = track_blank_changes ? 'should' : 'should_not'
cmp_name = cmp.humanize capitalize: false
[
{ n: 'many-to-many', f: 'other_dummy_parent_ids', v: [] },
{ n: 'boolean', f: 'boolean', v: false },
{ n: 'empty string', f: 'string', v: '' },
{ n: 'all whitespace string', f: 'string', v: " \t\n\r\f\v" }
# The second character in that string is an actual tab (0x9).
].each do |d|
context "#{d[:n]} field" do
let(:changes) do
{ d[:f] => original_nil ? [nil, d[:v]] : [d[:v], nil] }
end
it "changes #{cmp_name} include #{d[:f]}" do
send(cmp, include(d[:f]))
end
end
end
end
end
end
end
end
end