@@ -50,147 +50,77 @@ public class MutationSquashTest {
50
50
@ Test
51
51
public void testSquashOneSetMutation () {
52
52
Map <String , Object > data = map ("foo" , "foo-value" , "baz" , "baz-value" );
53
- MutableDocument setDoc = doc ("collection/key" , 0 , data );
54
- MutableDocument original = setDoc .clone ();
55
-
56
- Timestamp now = Timestamp .now ();
57
- Mutation set = setMutation ("collection/key" , map ("bar" , "bar-value" ));
58
- set .applyToLocalView (setDoc , now );
59
-
60
- Mutation squashed = set .squash (emptyMutation ("collection/key" ), original , now );
61
- squashed .applyToLocalView (original , now );
62
- assertEquals (setDoc , original );
53
+ squashRoundTrips (
54
+ doc ("collection/key" , 0 , data ), setMutation ("collection/key" , map ("bar" , "bar-value" )));
63
55
}
64
56
65
57
@ Test
66
58
public void testSquashOnePatchMutation () {
67
59
Map <String , Object > data = map ("foo" , map ("bar" , "bar-value" ), "baz" , "baz-value" );
68
- MutableDocument patchDoc = doc ("collection/key" , 0 , data );
69
- MutableDocument original = patchDoc .clone ();
70
-
71
- Timestamp now = Timestamp .now ();
72
- Mutation patch = patchMutation ("collection/key" , map ("foo.bar" , "new-bar-value" ));
73
- patch .applyToLocalView (patchDoc , now );
74
-
75
- Mutation squashed = emptyMutation ("collection/key" );
76
- squashed = patch .squash (squashed , original , now );
77
- squashed .applyToLocalView (original , now );
78
- assertEquals (patchDoc , original );
60
+ squashRoundTrips (
61
+ doc ("collection/key" , 0 , data ),
62
+ patchMutation ("collection/key" , map ("foo.bar" , "new-bar-value" )));
79
63
}
80
64
81
65
@ Test
82
66
public void testSquashWithPatchWithMerge () {
83
- MutableDocument mergeDoc = deletedDoc ("collection/key" , 0 );
84
- MutableDocument original = mergeDoc .clone ();
85
-
86
- Timestamp now = Timestamp .now ();
87
67
Mutation upsert =
88
68
mergeMutation (
89
69
"collection/key" , map ("foo.bar" , "new-bar-value" ), Arrays .asList (field ("foo.bar" )));
90
- upsert .applyToLocalView (mergeDoc , now );
91
-
92
- Mutation squashed = emptyMutation ("collection/key" );
93
- squashed = upsert .squash (squashed , original , now );
94
- squashed .applyToLocalView (original , now );
95
- assertEquals (mergeDoc , original );
70
+ squashRoundTrips (deletedDoc ("collection/key" , 0 ), upsert );
96
71
}
97
72
98
73
@ Test
99
74
public void testSquashWithDeleteThenPatch () {
100
75
MutableDocument doc = doc ("collection/key" , 0 , map ("foo" , 1 ));
101
- MutableDocument original = doc .clone ();
102
-
103
- Timestamp now = Timestamp .now ();
104
76
Mutation delete = new DeleteMutation (key ("collection/key" ), Precondition .NONE );
105
- delete .applyToLocalView (doc , now );
106
-
107
- now = Timestamp .now ();
108
77
Mutation patch = patchMutation ("collection/key" , map ("foo.bar" , "new-bar-value" ));
109
- patch .applyToLocalView (doc , now );
110
78
111
- Mutation squashed = emptyMutation ("collection/key" );
112
- squashed = delete .squash (squashed , original , now );
113
- squashed = patch .squash (squashed , original , now );
114
- squashed .applyToLocalView (original , now );
115
- assertEquals (doc , original );
79
+ squashRoundTrips (doc , delete , patch );
116
80
}
117
81
118
82
@ Test
119
83
public void testSquashWithDeleteThenMerge () {
120
84
MutableDocument doc = doc ("collection/key" , 0 , map ("foo" , 1 ));
121
- MutableDocument original = doc .clone ();
122
-
123
- Timestamp now = Timestamp .now ();
124
85
Mutation delete = new DeleteMutation (key ("collection/key" ), Precondition .NONE );
125
- delete .applyToLocalView (doc , now );
126
-
127
- now = Timestamp .now ();
128
86
Mutation patch =
129
87
mergeMutation (
130
88
"collection/key" , map ("foo.bar" , "new-bar-value" ), Arrays .asList (field ("foo.bar" )));
131
- patch .applyToLocalView (doc , now );
132
89
133
- Mutation squashed = emptyMutation ("collection/key" );
134
- squashed = delete .squash (squashed , original , now );
135
- squashed = patch .squash (squashed , original , now );
136
- squashed .applyToLocalView (original , now );
137
- assertEquals (doc , original );
90
+ squashRoundTrips (doc , delete , patch );
138
91
}
139
92
140
93
@ Test
141
94
public void testSquashPatchThenPatchToDeleteField () {
142
95
MutableDocument doc = doc ("collection/key" , 0 , map ("foo" , 1 ));
143
- MutableDocument original = doc .clone ();
144
-
145
- Timestamp now = Timestamp .now ();
146
96
Mutation patch =
147
97
patchMutation (
148
98
"collection/key" , map ("foo" , "foo-patched-value" , "bar.baz" , FieldValue .increment (1 )));
149
- patch .applyToLocalView (doc , now );
150
-
151
99
Mutation patchToDeleteField =
152
100
patchMutation (
153
101
"collection/key" , map ("foo" , "foo-patched-value" , "bar.baz" , FieldValue .delete ()));
154
- patchToDeleteField .applyToLocalView (doc , now );
155
102
156
- Mutation squashed = emptyMutation ("collection/key" );
157
- squashed = patch .squash (squashed , original , now );
158
- squashed = patchToDeleteField .squash (squashed , original , now );
159
- squashed .applyToLocalView (original , now );
160
- assertEquals (doc , original );
103
+ squashRoundTrips (doc , patch , patchToDeleteField );
161
104
}
162
105
163
106
@ Test
164
107
public void testSquashPatchThenMerge () {
165
108
MutableDocument doc = doc ("collection/key" , 0 , map ("foo" , 1 ));
166
- MutableDocument original = doc .clone ();
167
-
168
- Timestamp now = Timestamp .now ();
169
109
Mutation patch =
170
110
patchMutation (
171
111
"collection/key" , map ("foo" , "foo-patched-value" , "bar.baz" , FieldValue .increment (1 )));
172
- patch .applyToLocalView (doc , now );
173
-
174
112
Mutation merge =
175
113
mergeMutation (
176
114
"collection/key" ,
177
115
map ("arrays" , FieldValue .arrayUnion (1 , 2 , 3 )),
178
116
Arrays .asList (field ("arrays" )));
179
- merge .applyToLocalView (doc , now );
180
117
181
- Mutation squashed = emptyMutation ("collection/key" );
182
- squashed = patch .squash (squashed , original , now );
183
- squashed = merge .squash (squashed , original , now );
184
- squashed .applyToLocalView (original , now );
185
- assertEquals (doc , original );
118
+ squashRoundTrips (doc , patch , merge );
186
119
}
187
120
188
121
@ Test
189
122
public void testSquashArrayUnionThenRemove () {
190
123
MutableDocument doc = doc ("collection/key" , 0 , map ("foo" , 1 ));
191
- MutableDocument original = doc .clone ();
192
-
193
- Timestamp now = Timestamp .now ();
194
124
Mutation union =
195
125
mergeMutation (
196
126
"collection/key" , map ("arrays" , FieldValue .arrayUnion (1 , 2 , 3 )), Arrays .asList ());
@@ -199,53 +129,29 @@ public void testSquashArrayUnionThenRemove() {
199
129
"collection/key" ,
200
130
map ("foo" , "xxx" , "arrays" , FieldValue .arrayRemove (2 )),
201
131
Arrays .asList (field ("foo" )));
202
- union .applyToLocalView (doc , now );
203
- remove .applyToLocalView (doc , now );
204
-
205
- Mutation squashed = emptyMutation ("collection/key" );
206
- squashed = union .squash (squashed , original , now );
207
- squashed = remove .squash (squashed , original , now );
208
- squashed .applyToLocalView (original , now );
209
- assertEquals (doc , original );
132
+
133
+ squashRoundTrips (doc , union , remove );
210
134
}
211
135
212
136
@ Test
213
137
public void testSquashSetThenIncrement () {
214
138
MutableDocument doc = doc ("collection/key" , 0 , map ("foo" , 1 ));
215
- MutableDocument original = doc .clone ();
216
-
217
- Timestamp now = Timestamp .now ();
218
139
Mutation set = setMutation ("collection/key" , map ("foo" , 2 ));
219
140
Mutation update = patchMutation ("collection/key" , map ("foo" , FieldValue .increment (2 )));
220
- set .applyToLocalView (doc , now );
221
- update .applyToLocalView (doc , now );
222
-
223
- Mutation squashed = emptyMutation ("collection/key" );
224
- squashed = set .squash (squashed , original , now );
225
- squashed = update .squash (squashed , original , now );
226
- squashed .applyToLocalView (original , now );
227
- assertEquals (doc , original );
141
+
142
+ squashRoundTrips (doc , set , update );
228
143
}
229
144
230
145
@ Test
231
146
public void testSquashSetThenPatchOnDeletedDoc () {
232
147
MutableDocument doc = deletedDoc ("collection/key" , 0 );
233
- MutableDocument original = doc .clone ();
234
-
235
- Timestamp now = Timestamp .now ();
236
148
Mutation set = setMutation ("collection/key" , map ("bar" , "bar-value" ));
237
149
Mutation patch =
238
150
patchMutation (
239
151
"collection/key" ,
240
152
map ("foo" , "foo-patched-value" , "bar.baz" , FieldValue .serverTimestamp ()));
241
- set .applyToLocalView (doc , now );
242
- patch .applyToLocalView (doc , now );
243
-
244
- Mutation squashed = emptyMutation ("collection/key" );
245
- squashed = set .squash (squashed , original , now );
246
- squashed = patch .squash (squashed , original , now );
247
- squashed .applyToLocalView (original , now );
248
- assertEquals (doc , original );
153
+
154
+ squashRoundTrips (doc , set , patch );
249
155
}
250
156
251
157
// TODO(overlay): This test fails because deleting nested fields like bar.baz leaves an empty bar
@@ -256,9 +162,6 @@ public void testSquashSetThenPatchOnDeletedDoc() {
256
162
@ Test
257
163
public void testSquashFieldDeletionOfNestedField () {
258
164
MutableDocument doc = doc ("collection/key" , 0 , map ("bar.baz" , 1 ));
259
- MutableDocument original = doc .clone ();
260
-
261
- Timestamp now = Timestamp .now ();
262
165
Mutation patch1 =
263
166
patchMutation (
264
167
"collection/key" , map ("foo" , "foo-patched-value" , "bar.baz" , FieldValue .increment (1 )));
@@ -269,16 +172,8 @@ public void testSquashFieldDeletionOfNestedField() {
269
172
Mutation patch3 =
270
173
patchMutation (
271
174
"collection/key" , map ("foo" , "foo-patched-value" , "bar.baz" , FieldValue .delete ()));
272
- patch1 .applyToLocalView (doc , now );
273
- patch2 .applyToLocalView (doc , now );
274
- patch3 .applyToLocalView (doc , now );
275
-
276
- Mutation squashed = emptyMutation ("collection/key" );
277
- squashed = patch1 .squash (squashed , original , now );
278
- squashed = patch2 .squash (squashed , original , now );
279
- squashed = patch3 .squash (squashed , original , now );
280
- squashed .applyToLocalView (original , now );
281
- assertEquals (doc , original );
175
+
176
+ squashRoundTrips (doc , patch1 , patch2 , patch3 );
282
177
}
283
178
284
179
// Below tests run on automatically generated mutation list, they are deterministic, but hard to
@@ -494,4 +389,19 @@ private <E> List<List<E>> generatePermutations(List<E> mutations) {
494
389
}
495
390
return returnValue ;
496
391
}
392
+
393
+ private void squashRoundTrips (MutableDocument doc , Mutation ... mutations ) {
394
+ MutableDocument toApplySquashedMutation = doc .clone ();
395
+ Timestamp now = Timestamp .now ();
396
+
397
+ Mutation squashed = emptyMutation (doc .getKey ().getPath ().canonicalString ());
398
+ for (Mutation m : mutations ) {
399
+ m .applyToLocalView (doc , now );
400
+ squashed = m .squash (squashed , toApplySquashedMutation , now );
401
+ }
402
+
403
+ squashed .applyToLocalView (toApplySquashedMutation , now );
404
+
405
+ assertEquals (doc , toApplySquashedMutation );
406
+ }
497
407
}
0 commit comments