mongoid_relations_dirty_tracking 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activesupport', '~> 5.2.0'
6
+ gem 'mongoid', '~> 7.0'
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: '../../'
4
+
5
+ gem 'activesupport', '~> 6.0.0'
6
+ gem 'mongoid', '~> 7.0'
@@ -1,382 +1,398 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe Mongoid::RelationsDirtyTracking do
5
- subject { TestDocument.create }
6
-
7
- its(:changed?) { should be_false }
8
- its(:children_changed?) { should be_false }
9
- its(:relations_changed?) { should be_false }
10
- its(:changed_with_relations?) { should be_false }
11
-
12
-
13
- context "embeds_one relationship" do
14
- context "when adding document" do
15
- before :each do
16
- @embedded_doc = TestEmbeddedDocument.new
17
- subject.one_document = @embedded_doc
18
- end
19
-
20
- its(:changed?) { should be_true }
21
- its(:children_changed?) { should be_false }
22
- its(:relations_changed?) { should be_true }
23
- its(:changed_with_relations?) { should be_true }
24
- its(:changes_with_relations) { should include(subject.relation_changes) }
25
-
26
- describe "#relation_changes" do
27
- it "returns array with differences" do
28
- expect(subject.relation_changes['one_document']).to eq([nil, @embedded_doc.attributes])
29
- end
30
- end
31
- end
32
-
33
-
34
- context "when removing document" do
35
- before :each do
36
- @embedded_doc = TestEmbeddedDocument.new
37
- subject.one_document = @embedded_doc
38
- subject.save!
39
- subject.one_document = nil
40
- end
41
-
42
- its(:changed?) { should be_true }
43
- its(:children_changed?) { should be_false }
44
- its(:relations_changed?) { should be_true }
45
- its(:changed_with_relations?) { should be_true }
46
- its(:changes_with_relations) { should include(subject.relation_changes) }
47
-
48
- describe "#relation_changes" do
49
- it "returns array with differences" do
50
- expect(subject.relation_changes['one_document']).to eq([@embedded_doc.attributes, nil])
51
- end
52
- end
53
- end
54
-
55
-
56
- context "when changing value on embedded document" do
57
- before :each do
58
- @embedded_doc = TestEmbeddedDocument.new
59
- subject.one_document = @embedded_doc
60
- subject.save!
61
- subject.one_document.title = 'foobar'
62
- end
63
-
64
- its(:changed?) { should be_true }
65
- its(:children_changed?) { should be_true }
66
- its(:relations_changed?) { should be_true }
67
- its(:changed_with_relations?) { should be_true }
68
- its(:changes_with_relations) { should include(subject.relation_changes) }
69
-
70
- describe "#relation_changes" do
71
- it "returns array with differences" do
72
- old_attributes = @embedded_doc.attributes.clone.delete_if {|key, val| key == "title" }
73
- expect(subject.relation_changes['one_document']).to eq([old_attributes, @embedded_doc.attributes])
74
- end
75
- end
76
- end
77
-
78
- context "when just updated_at is changed on embedded document" do
79
- before :each do
80
- embedded_doc = Class.new(TestEmbeddedDocument) { include Mongoid::Timestamps }.new
81
- subject.one_document = @embedded_doc
82
- subject.save!
83
- embedded_doc.updated_at = Time.now
84
- end
85
- its(:changed?) { should be_false }
86
- end
87
- end
88
-
89
- context "embeds_many relationship" do
90
- context "when adding document" do
91
- before :each do
92
- @embedded_doc = TestEmbeddedDocument.new
93
- subject.many_documents << @embedded_doc
94
- end
95
-
96
- its(:changed?) { should be_true }
97
- its(:children_changed?) { should be_false }
98
- its(:relations_changed?) { should be_true }
99
- its(:changed_with_relations?) { should be_true }
100
- its(:changes_with_relations) { should include(subject.relation_changes) }
101
-
102
- describe "#relation_changes" do
103
- it "returns array with differences" do
104
- expect(subject.relation_changes['many_documents']).to eq([[], [@embedded_doc.attributes]])
105
- end
106
- end
107
- end
108
-
109
-
110
- context "when removing document" do
111
- before :each do
112
- @embedded_doc = TestEmbeddedDocument.new
113
- subject.many_documents = [@embedded_doc]
114
- subject.save!
115
- subject.many_documents.delete @embedded_doc
116
- end
117
-
118
- its(:changed?) { should be_true }
119
- its(:children_changed?) { should be_false }
120
- its(:relations_changed?) { should be_true }
121
- its(:changed_with_relations?) { should be_true }
122
- its(:changes_with_relations) { should include(subject.relation_changes) }
123
-
124
- describe "#relation_changes" do
125
- it "returns array with differences" do
126
- expect(subject.relation_changes['many_documents']).to eq([[@embedded_doc.attributes], []])
127
- end
128
- end
129
- end
130
- end
131
-
132
-
133
- context "has_one relationship" do
134
- context "when adding document" do
135
- before :each do
136
- @related_doc = TestRelatedDocument.new
137
- subject.one_related = @related_doc
138
- end
139
-
140
- its(:changed?) { should be_true }
141
- its(:children_changed?) { should be_false }
142
- its(:relations_changed?) { should be_true }
143
- its(:changed_with_relations?) { should be_true }
144
- its(:changes_with_relations) { should include(subject.relation_changes) }
145
-
146
- describe "#relation_changes" do
147
- it "returns array with differences" do
148
- expect(subject.relation_changes['one_related']).to eq([nil, {'_id' => @related_doc._id}])
149
- end
150
- end
151
- end
152
-
153
- context "when removing document" do
154
- before :each do
155
- @related_doc = TestRelatedDocument.new
156
- subject.one_related = @related_doc
157
- subject.save!
158
- subject.one_related = nil
159
- end
160
-
161
- its(:changed?) { should be_true }
162
- its(:children_changed?) { should be_false }
163
- its(:relations_changed?) { should be_true }
164
- its(:changed_with_relations?) { should be_true }
165
- its(:changes_with_relations) { should include(subject.relation_changes) }
166
-
167
- describe "#relation_changes" do
168
- it "returns array with differences" do
169
- expect(subject.relation_changes['one_related']).to eq([{'_id' => @related_doc._id}, nil])
170
- end
171
- end
172
- end
173
-
174
- context "when changing document" do
175
- before :each do
176
- @related_doc = TestRelatedDocument.new
177
- subject.one_related = @related_doc
178
- subject.save!
179
- subject.one_related = @another_related_doc = TestRelatedDocument.new
180
- end
181
-
182
- its(:changed?) { should be_true }
183
- its(:children_changed?) { should be_false }
184
- its(:relations_changed?) { should be_true }
185
- its(:changed_with_relations?) { should be_true }
186
- its(:changes_with_relations) { should include(subject.relation_changes) }
187
-
188
- describe "#relation_changes" do
189
- it "returns array with differences" do
190
- expect(subject.relation_changes['one_related']).to eq([{'_id' => @related_doc._id}, {'_id' => @another_related_doc._id}])
191
- end
192
- end
193
- end
194
-
195
- context "when changing value on referenced document" do
196
- before :each do
197
- @related_doc = TestRelatedDocument.new
198
- subject.one_related = @related_doc
199
- subject.save!
200
- subject.one_related.title = "New title"
201
- end
202
-
203
- its(:changed?) { should be_false }
204
- its(:children_changed?) { should be_false }
205
- its(:relations_changed?) { should be_false }
206
- its(:changed_with_relations?) { should be_false }
207
- its(:relation_changes) { should be_empty }
208
- end
209
- end
210
-
211
-
212
- context "has_many relationship" do
213
- context "when adding document" do
214
- before :each do
215
- @related_doc = TestRelatedDocument.new
216
- subject.many_related << @related_doc
217
- end
218
-
219
- its(:changed?) { should be_true }
220
- its(:children_changed?) { should be_false }
221
- its(:relations_changed?) { should be_true }
222
- its(:changed_with_relations?) { should be_true }
223
- its(:changes_with_relations) { should include(subject.relation_changes) }
224
-
225
- describe "#relation_changes" do
226
- it "returns array with differences" do
227
- expect(subject.relation_changes['many_related']).to eq([[], [{'_id' => @related_doc._id}]])
228
- end
229
- end
230
- end
231
-
232
- context "when removing document" do
233
- before :each do
234
- @related_doc = TestRelatedDocument.new
235
- subject.many_related << @related_doc
236
- subject.save!
237
- subject.many_related.delete @related_doc
238
- end
239
-
240
- its(:changed?) { should be_true }
241
- its(:children_changed?) { should be_false }
242
- its(:relations_changed?) { should be_true }
243
- its(:changed_with_relations?) { should be_true }
244
- its(:changes_with_relations) { should include(subject.relation_changes) }
245
-
246
- describe "#relation_changes" do
247
- it "returns array with differences" do
248
- expect(subject.relation_changes['many_related']).to eq([[{'_id' => @related_doc._id}], []])
249
- end
250
- end
251
- end
252
- end
253
-
254
-
255
- context "has_and_belongs_to_many relationship" do
256
- context "when adding document" do
257
- before :each do
258
- @related_doc = TestRelatedDocument.new
259
- subject.many_to_many_related << @related_doc
260
- end
261
-
262
- its(:changed?) { should be_true }
263
- its(:children_changed?) { should be_false }
264
- its(:relations_changed?) { should be_true }
265
- its(:changed_with_relations?) { should be_true }
266
- its(:changes_with_relations) { should include(subject.relation_changes) }
267
-
268
- describe "#relation_changes" do
269
- it "returns array with differences" do
270
- expect(subject.relation_changes['many_to_many_related']).to eq([[], [{'_id' => @related_doc._id}]])
271
- end
272
- end
273
- end
274
-
275
- context "when removing document" do
276
- before :each do
277
- @related_doc = TestRelatedDocument.new
278
- subject.many_to_many_related << @related_doc
279
- subject.save!
280
- subject.many_to_many_related.delete @related_doc
281
- end
282
-
283
- its(:changed?) { should be_true }
284
- its(:children_changed?) { should be_false }
285
- its(:relations_changed?) { should be_true }
286
- its(:changed_with_relations?) { should be_true }
287
- its(:changes_with_relations) { should include(subject.relation_changes) }
288
-
289
- describe "#relation_changes" do
290
- it "returns array with differences" do
291
- expect(subject.relation_changes['many_to_many_related']).to eq([[{'_id' => @related_doc._id}], []])
292
- end
293
- end
294
- end
295
- end
296
-
297
-
298
- context "belongs_to relationship" do
299
- subject { TestRelatedDocument.create }
300
-
301
- context "when adding document" do
302
- before :each do
303
- @doc = TestDocument.create
304
- subject.test_document = @doc
305
- end
306
-
307
- its(:changed?) { should be_true }
308
- its(:children_changed?) { should be_false }
309
- its(:relations_changed?) { should be_true }
310
- its(:changed_with_relations?) { should be_true }
311
- its(:changes_with_relations) { should include(subject.relation_changes) }
312
-
313
- describe "#relation_changes" do
314
- it "returns array with differences" do
315
- expect(subject.relation_changes['test_document']).to eq([nil, {'test_document_id' => @doc._id}])
316
- end
317
- end
318
- end
319
-
320
- context "when removing document" do
321
- before :each do
322
- @doc = TestDocument.create
323
- subject.test_document = @doc
324
- subject.save!
325
- subject.test_document = nil
326
- end
327
-
328
- its(:changed?) { should be_true }
329
- its(:children_changed?) { should be_false }
330
- its(:relations_changed?) { should be_true }
331
- its(:changed_with_relations?) { should be_true }
332
- its(:changes_with_relations) { should include(subject.relation_changes) }
333
-
334
- describe "#relation_changes" do
335
- it "returns array with differences" do
336
- expect(subject.relation_changes['test_document']).to eq([{'test_document_id' => @doc._id}, nil])
337
- end
338
- end
339
- end
340
- end
341
-
342
-
343
- describe ".track_relation?" do
344
- context "with only options" do
345
- it "do tracks only specified relations" do
346
- expect(TestDocumentWithOnlyOption.track_relation? :many_documents).to be_true
347
- expect(TestDocumentWithOnlyOption.track_relation? :one_related).to be_false
348
- end
349
- end
350
-
351
- context "with except options" do
352
- it "do no track excluded relations" do
353
- expect(TestDocumentWithExceptOption.track_relation? 'many_documents').to be_false
354
- expect(TestDocumentWithExceptOption.track_relation? 'one_related').to be_true
355
- end
356
- end
357
- end
358
-
359
-
360
- describe "by befault the versions relation is not tracked" do
361
- context "when not called 'relations_dirty_tracking'" do
362
- it "'versions' is excluded from tracing" do
363
- expect(Class.new(TestDocument).relations_dirty_tracking_options[:except]).to include('versions')
364
- end
365
- end
366
-
367
- context "when called 'relations_dirty_tracking' with only" do
368
- it "'versions' is excluded from tracing" do
369
- klass = Class.new(TestDocument) { relations_dirty_tracking(only: 'foobar') }
370
- expect(klass.relations_dirty_tracking_options[:except]).to include('versions')
371
- end
372
- end
373
-
374
- context "when called 'relations_dirty_tracking' with except" do
375
- it "'versions' is excluded from tracing" do
376
- klass = Class.new(TestDocument) { relations_dirty_tracking(except: 'foobar') }
377
- expect(klass.relations_dirty_tracking_options[:except]).to include('versions')
378
- expect(klass.relations_dirty_tracking_options[:except]).to include('foobar')
379
- end
380
- end
381
- end
382
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Mongoid::RelationsDirtyTracking do
6
+ subject { TestDocument.create }
7
+
8
+ its(:changed?) { is_expected.to eq false }
9
+ its(:children_changed?) { is_expected.to eq false }
10
+ its(:relations_changed?) { is_expected.to eq false }
11
+ its(:changed_with_relations?) { is_expected.to eq false }
12
+
13
+ context 'embeds_one relationship' do
14
+ context 'when adding document' do
15
+ before :each do
16
+ @embedded_doc = TestEmbeddedDocument.new
17
+ subject.one_document = @embedded_doc
18
+ end
19
+
20
+ its(:changed?) { is_expected.to eq false }
21
+ its(:children_changed?) { is_expected.to eq false }
22
+ its(:relations_changed?) { is_expected.to eq true }
23
+ its(:changed_with_relations?) { is_expected.to eq true }
24
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
25
+
26
+ describe '#relation_changes' do
27
+ it 'returns array with differences' do
28
+ expect(subject.relation_changes['one_document']).to eq([nil, @embedded_doc.attributes])
29
+ end
30
+ end
31
+ end
32
+
33
+ context 'when removing document' do
34
+ before :each do
35
+ @embedded_doc = TestEmbeddedDocument.new
36
+ subject.one_document = @embedded_doc
37
+ subject.save!
38
+ subject.one_document = nil
39
+ end
40
+
41
+ its(:changed?) { is_expected.to eq false }
42
+ its(:children_changed?) { is_expected.to eq false }
43
+ its(:relations_changed?) { is_expected.to eq true }
44
+ its(:changed_with_relations?) { is_expected.to eq true }
45
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
46
+
47
+ describe '#relation_changes' do
48
+ it 'returns array with differences' do
49
+ expect(subject.relation_changes['one_document']).to eq([@embedded_doc.attributes, nil])
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'when changing value on embedded document' do
55
+ before :each do
56
+ @embedded_doc = TestEmbeddedDocument.new
57
+ subject.one_document = @embedded_doc
58
+ subject.save!
59
+ subject.one_document.title = 'foobar'
60
+ end
61
+
62
+ its(:changed?) { is_expected.to eq true }
63
+ its(:children_changed?) { is_expected.to eq true }
64
+ its(:relations_changed?) { is_expected.to eq true }
65
+ its(:changed_with_relations?) { is_expected.to eq true }
66
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
67
+
68
+ describe '#relation_changes' do
69
+ it 'returns array with differences' do
70
+ old_attributes = @embedded_doc.attributes.clone.delete_if { |key, _val| key == 'title' }
71
+ expect(subject.relation_changes['one_document']).to eq([old_attributes, @embedded_doc.attributes])
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'when just updated_at is changed on embedded document' do
77
+ before :each do
78
+ embedded_doc = Class.new(TestEmbeddedDocument) { include Mongoid::Timestamps }.new
79
+ subject.one_document = @embedded_doc
80
+ subject.save!
81
+ embedded_doc.updated_at = Time.now
82
+ end
83
+ its(:changed?) { is_expected.to eq false }
84
+ end
85
+ end
86
+
87
+ context 'embeds_many relationship' do
88
+ context 'when adding document' do
89
+ before :each do
90
+ @embedded_doc = TestEmbeddedDocument.new
91
+ subject.many_documents << @embedded_doc
92
+ end
93
+
94
+ its(:changed?) { is_expected.to eq false }
95
+ its(:children_changed?) { is_expected.to eq false }
96
+ its(:relations_changed?) { is_expected.to eq true }
97
+ its(:changed_with_relations?) { is_expected.to eq true }
98
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
99
+
100
+ describe '#relation_changes' do
101
+ it 'returns array with differences' do
102
+ expect(subject.relation_changes['many_documents']).to eq([[], [@embedded_doc.attributes]])
103
+ end
104
+ end
105
+ end
106
+
107
+ context 'when removing document' do
108
+ before :each do
109
+ @embedded_doc = TestEmbeddedDocument.new
110
+ subject.many_documents = [@embedded_doc]
111
+ subject.save!
112
+ subject.many_documents.delete @embedded_doc
113
+ end
114
+
115
+ its(:changed?) { is_expected.to eq false }
116
+ its(:children_changed?) { is_expected.to eq false }
117
+ its(:relations_changed?) { is_expected.to eq true }
118
+ its(:changed_with_relations?) { is_expected.to eq true }
119
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
120
+
121
+ describe '#relation_changes' do
122
+ it 'returns array with differences' do
123
+ expect(subject.relation_changes['many_documents']).to eq([[@embedded_doc.attributes], []])
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ context 'has_one relationship' do
130
+ context 'when adding document' do
131
+ before :each do
132
+ @related_doc = TestRelatedDocument.new
133
+ subject.one_related = @related_doc
134
+ end
135
+
136
+ its(:changed?) { is_expected.to eq false }
137
+ its(:children_changed?) { is_expected.to eq false }
138
+ its(:relations_changed?) { is_expected.to eq true }
139
+ its(:changed_with_relations?) { is_expected.to eq true }
140
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
141
+
142
+ describe '#relation_changes' do
143
+ it 'returns array with differences' do
144
+ expect(subject.relation_changes['one_related']).to eq([nil, { '_id' => @related_doc._id }])
145
+ end
146
+ end
147
+ end
148
+
149
+ context 'when removing document' do
150
+ before :each do
151
+ @related_doc = TestRelatedDocument.new
152
+ subject.one_related = @related_doc
153
+ subject.save!
154
+ subject.one_related = nil
155
+ end
156
+
157
+ its(:changed?) { is_expected.to eq false }
158
+ its(:children_changed?) { is_expected.to eq false }
159
+ its(:relations_changed?) { is_expected.to eq true }
160
+ its(:changed_with_relations?) { is_expected.to eq true }
161
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
162
+
163
+ describe '#relation_changes' do
164
+ it 'returns array with differences' do
165
+ expect(subject.relation_changes['one_related']).to eq([{ '_id' => @related_doc._id }, nil])
166
+ end
167
+ end
168
+ end
169
+
170
+ context 'when changing document' do
171
+ before :each do
172
+ @related_doc = TestRelatedDocument.new
173
+ subject.one_related = @related_doc
174
+ subject.save!
175
+ subject.one_related = @another_related_doc = TestRelatedDocument.new
176
+ end
177
+
178
+ its(:changed?) { is_expected.to eq false }
179
+ its(:children_changed?) { is_expected.to eq false }
180
+ its(:relations_changed?) { is_expected.to eq true }
181
+ its(:changed_with_relations?) { is_expected.to eq true }
182
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
183
+
184
+ describe '#relation_changes' do
185
+ it 'returns array with differences' do
186
+ expect(subject.relation_changes['one_related']).to eq([{ '_id' => @related_doc._id },
187
+ { '_id' => @another_related_doc._id }])
188
+ end
189
+ end
190
+ end
191
+
192
+ context 'when changing value on referenced document' do
193
+ before :each do
194
+ @related_doc = TestRelatedDocument.new
195
+ subject.one_related = @related_doc
196
+ subject.save!
197
+ subject.one_related.title = 'New title'
198
+ end
199
+
200
+ its(:changed?) { is_expected.to eq false }
201
+ its(:children_changed?) { is_expected.to eq false }
202
+ its(:relations_changed?) { is_expected.to eq false }
203
+ its(:changed_with_relations?) { is_expected.to eq false }
204
+ its(:relation_changes) { is_expected.to be_empty }
205
+ end
206
+ end
207
+
208
+ context 'has_many relationship' do
209
+ context 'when adding document' do
210
+ before :each do
211
+ @related_doc = TestRelatedDocument.new
212
+ subject.many_related << @related_doc
213
+ end
214
+
215
+ its(:changed?) { is_expected.to eq false }
216
+ its(:children_changed?) { is_expected.to eq false }
217
+ its(:relations_changed?) { is_expected.to eq true }
218
+ its(:changed_with_relations?) { is_expected.to eq true }
219
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
220
+
221
+ describe '#relation_changes' do
222
+ it 'returns array with differences' do
223
+ expect(subject.relation_changes['many_related']).to eq([[], [{ '_id' => @related_doc._id }]])
224
+ end
225
+ end
226
+ end
227
+
228
+ context 'when removing document' do
229
+ before :each do
230
+ @related_doc = TestRelatedDocument.new
231
+ subject.many_related << @related_doc
232
+ subject.save!
233
+ subject.many_related.delete(@related_doc)
234
+ end
235
+
236
+ its(:changed?) { is_expected.to eq false }
237
+ its(:children_changed?) { is_expected.to eq false }
238
+ its(:relations_changed?) { is_expected.to eq true }
239
+ its(:changed_with_relations?) { is_expected.to eq true }
240
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
241
+
242
+ describe '#relation_changes' do
243
+ it 'returns array with differences' do
244
+ expect(subject.relation_changes['many_related']).to eq([[{ '_id' => @related_doc._id }], []])
245
+ end
246
+ end
247
+ end
248
+ end
249
+
250
+ context 'has_and_belongs_to_many relationship' do
251
+ context 'when adding document' do
252
+ before :each do
253
+ @related_doc = TestRelatedDocument.new
254
+ subject.many_to_many_related << @related_doc
255
+ end
256
+
257
+ its(:changed?) { is_expected.to eq false }
258
+ its(:children_changed?) { is_expected.to eq false }
259
+ its(:relations_changed?) { is_expected.to eq true }
260
+ its(:changed_with_relations?) { is_expected.to eq true }
261
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
262
+
263
+ describe '#relation_changes' do
264
+ it 'returns array with differences' do
265
+ expect(subject.relation_changes['many_to_many_related']).to eq([[], [{ '_id' => @related_doc._id }]])
266
+ end
267
+ end
268
+ end
269
+
270
+ context 'when removing document' do
271
+ before :each do
272
+ @related_doc = TestRelatedDocument.new
273
+ subject.many_to_many_related << @related_doc
274
+ subject.save!
275
+ subject.many_to_many_related.delete(@related_doc)
276
+ end
277
+
278
+ its(:changed?) { is_expected.to eq false }
279
+ its(:children_changed?) { is_expected.to eq false }
280
+ its(:relations_changed?) { is_expected.to eq true }
281
+ its(:changed_with_relations?) { is_expected.to eq true }
282
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
283
+
284
+ describe '#relation_changes' do
285
+ it 'returns array with differences' do
286
+ expect(subject.relation_changes['many_to_many_related']).to eq([[{ '_id' => @related_doc._id }], []])
287
+ end
288
+ end
289
+ end
290
+ end
291
+
292
+ context 'belongs_to relationship' do
293
+ subject { TestRelatedDocument.create }
294
+
295
+ context 'when adding document' do
296
+ before :each do
297
+ @doc = TestDocument.create
298
+ subject.test_document = @doc
299
+ end
300
+
301
+ its(:changed?) { is_expected.to eq true }
302
+ its(:children_changed?) { is_expected.to eq false }
303
+ its(:relations_changed?) { is_expected.to eq true }
304
+ its(:changed_with_relations?) { is_expected.to eq true }
305
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
306
+
307
+ describe '#relation_changes' do
308
+ it 'returns array with differences' do
309
+ expect(subject.relation_changes['test_document']).to eq([nil, { 'test_document_id' => @doc._id }])
310
+ end
311
+ end
312
+ end
313
+
314
+ context 'when removing document' do
315
+ before :each do
316
+ @doc = TestDocument.create
317
+ subject.test_document = @doc
318
+ subject.save!
319
+ subject.test_document = nil
320
+ end
321
+
322
+ its(:changed?) { is_expected.to eq true }
323
+ its(:children_changed?) { is_expected.to eq false }
324
+ its(:relations_changed?) { is_expected.to eq true }
325
+ its(:changed_with_relations?) { is_expected.to eq true }
326
+ its(:changes_with_relations) { is_expected.to include(subject.relation_changes) }
327
+
328
+ describe '#relation_changes' do
329
+ it 'returns array with differences' do
330
+ expect(subject.relation_changes['test_document']).to eq([{ 'test_document_id' => @doc._id }, nil])
331
+ end
332
+ end
333
+ end
334
+ end
335
+
336
+ describe '.track_relation?' do
337
+ context 'with only options' do
338
+ it 'do tracks only specified relations' do
339
+ expect(TestDocumentWithOnlyOption.track_relation?(:many_documents)).to eq true
340
+ expect(TestDocumentWithOnlyOption.track_relation?(:one_related)).to eq false
341
+ end
342
+ end
343
+
344
+ context 'with except options' do
345
+ it 'do no track excluded relations' do
346
+ expect(TestDocumentWithExceptOption.track_relation?('many_documents')).to eq false
347
+ expect(TestDocumentWithExceptOption.track_relation?('one_related')).to eq true
348
+ end
349
+ end
350
+ end
351
+
352
+ describe 'by default the versions relation is not tracked' do
353
+ context "when not called 'relations_dirty_tracking'" do
354
+ it "'versions' is excluded from tracing" do
355
+ expect(Class.new(TestDocument).relations_dirty_tracking_options[:except]).to include('versions')
356
+ end
357
+ end
358
+
359
+ context "when called 'relations_dirty_tracking' with only" do
360
+ it "'versions' is excluded from tracing" do
361
+ klass = Class.new(TestDocument) { relations_dirty_tracking(only: 'foobar') }
362
+ expect(klass.relations_dirty_tracking_options[:except]).to include('versions')
363
+ end
364
+ end
365
+
366
+ context "when called 'relations_dirty_tracking' with except" do
367
+ it "'versions' is excluded from tracing" do
368
+ klass = Class.new(TestDocument) { relations_dirty_tracking(except: 'foobar') }
369
+ expect(klass.relations_dirty_tracking_options[:except]).to include('versions')
370
+ expect(klass.relations_dirty_tracking_options[:except]).to include('foobar')
371
+ end
372
+ end
373
+ end
374
+
375
+ describe 'global disablement' do
376
+ context 'when adding document' do
377
+ before :each do
378
+ @embedded_doc = TestEmbeddedDocument.new
379
+
380
+ described_class.disable do
381
+ subject.many_documents << @embedded_doc
382
+ end
383
+ end
384
+
385
+ its(:changed?) { is_expected.to eq false }
386
+ its(:children_changed?) { is_expected.to eq false }
387
+ its(:relations_changed?) { is_expected.to eq false }
388
+ its(:changed_with_relations?) { is_expected.to eq false }
389
+ its(:changes_with_relations) { is_expected.to_not include(subject.relation_changes) }
390
+
391
+ describe '#relation_changes' do
392
+ it 'returns array with differences' do
393
+ expect(subject.relation_changes['many_documents']).to eq(nil)
394
+ end
395
+ end
396
+ end
397
+ end
398
+ end