24
24
import static org .junit .Assert .assertNull ;
25
25
26
26
import com .google .firebase .firestore .model .mutation .FieldMask ;
27
+ import com .google .firestore .v1 .MapValue ;
28
+ import com .google .firestore .v1 .Value ;
27
29
import java .util .Map ;
28
30
import org .junit .Test ;
29
31
import org .junit .runner .RunWith ;
32
34
33
35
@ RunWith (RobolectricTestRunner .class )
34
36
@ Config (manifest = Config .NONE )
35
- public class FieldValueTest {
37
+ public class ObjectValueTest {
38
+
39
+ private String fooString = "foo" ;
40
+ private Value fooValue = wrap (fooString );
41
+ private String barString = "bar" ;
42
+ private Value barValue = wrap (barString );
43
+ private Value emptyObject = Value .newBuilder ().setMapValue (MapValue .getDefaultInstance ()).build ();
44
+
45
+ @ Test
46
+ public void testSupportsEmptyObjectValues () {
47
+ ObjectValue objectValue = new ObjectValue ();
48
+ assertEquals (new ObjectValue (), objectValue );
49
+ }
36
50
37
51
@ Test
38
52
public void testExtractsFields () {
@@ -69,6 +83,36 @@ public void testOverwritesExistingFields() {
69
83
assertEquals (wrapObject ("a" , "mod" ), objectValue );
70
84
}
71
85
86
+ @ Test
87
+ public void testOverwritesNestedFields () {
88
+ ObjectValue objectValue = wrapObject ("a" , map ("b" , fooString , "c" , map ("d" , fooString )));
89
+ objectValue .set (field ("a.b" ), barValue );
90
+ objectValue .set (field ("a.c.d" ), barValue );
91
+ assertEquals (wrapObject ("a" , map ("b" , barString , "c" , map ("d" , barString ))), objectValue );
92
+ }
93
+
94
+ @ Test
95
+ public void testOverwritesDeeplyNestedField () {
96
+ ObjectValue objectValue = wrapObject ("a" , map ("b" , fooString ));
97
+ objectValue .set (field ("a.b.c" ), barValue );
98
+ assertEquals (wrapObject ("a" , map ("b" , map ("c" , barString ))), objectValue );
99
+ }
100
+
101
+ @ Test
102
+ public void testOverwritesNestedObject () {
103
+ ObjectValue objectValue = wrapObject ("a" , map ("b" , map ("c" , fooString , "d" , fooString )));
104
+ objectValue .set (field ("a.b" ), barValue );
105
+ assertEquals (wrapObject ("a" , map ("b" , "bar" )), objectValue );
106
+ }
107
+
108
+ @ Test
109
+ public void testReplacesNestedObject () {
110
+ ObjectValue singleValueObject = wrapObject (map ("c" , barString ));
111
+ ObjectValue objectValue = wrapObject ("a" , map ("b" , fooString ));
112
+ objectValue .set (field ("a" ), singleValueObject .get (FieldPath .EMPTY_PATH ));
113
+ assertEquals (wrapObject ("a" , map ("c" , barString )), objectValue );
114
+ }
115
+
72
116
@ Test
73
117
public void testAddsNewFields () {
74
118
ObjectValue objectValue = new ObjectValue ();
@@ -90,6 +134,54 @@ public void testAddsMultipleNewFields() {
90
134
assertEquals (wrapObject ("a" , "a" , "b" , "b" , "c" , "c" ), object );
91
135
}
92
136
137
+ @ Test
138
+ public void testAddsNestedField () {
139
+ ObjectValue objectValue = new ObjectValue ();
140
+ objectValue .set (field ("a.b" ), fooValue );
141
+ objectValue .set (field ("c.d.e" ), fooValue );
142
+ assertEquals (
143
+ wrapObject ("a" , map ("b" , fooString ), "c" , map ("d" , map ("e" , fooString ))), objectValue );
144
+ }
145
+
146
+ @ Test
147
+ public void testAddsFieldInNestedObject () {
148
+ ObjectValue objectValue = new ObjectValue ();
149
+ objectValue .set (field ("a" ), wrapObject ("b" , fooString ).get (FieldPath .EMPTY_PATH ));
150
+ objectValue .set (field ("a.c" ), fooValue );
151
+ assertEquals (wrapObject ("a" , map ("b" , fooString , "c" , fooString )), objectValue );
152
+ }
153
+
154
+ @ Test
155
+ public void testAddsTwoFieldsInNestedObject () {
156
+ ObjectValue objectValue = new ObjectValue ();
157
+ objectValue .set (field ("a.b" ), fooValue );
158
+ objectValue .set (field ("a.c" ), fooValue );
159
+ assertEquals (wrapObject ("a" , map ("b" , fooString , "c" , fooString )), objectValue );
160
+ }
161
+
162
+ @ Test
163
+ public void testAddDeeplyNestedFieldInNestedObject () {
164
+ ObjectValue objectValue = new ObjectValue ();
165
+ objectValue .set (field ("a.b.c.d.e.f" ), fooValue );
166
+ assertEquals (
167
+ wrapObject ("a" , map ("b" , map ("c" , map ("d" , map ("e" , map ("f" , fooString )))))), objectValue );
168
+ }
169
+
170
+ @ Test
171
+ public void testAddsSingleFieldInExistingObject () {
172
+ ObjectValue objectValue = wrapObject ("a" , fooString );
173
+ objectValue .set (field ("b" ), fooValue );
174
+ assertEquals (wrapObject ("a" , fooString , "b" , fooString ), objectValue );
175
+ }
176
+
177
+ @ Test
178
+ public void testSetsNestedFieldMultipleTimes () {
179
+ ObjectValue objectValue = new ObjectValue ();
180
+ objectValue .set (field ("a.c" ), fooValue );
181
+ objectValue .set (field ("a" ), wrapObject ("b" , fooString ).get (FieldPath .EMPTY_PATH ));
182
+ assertEquals (wrapObject ("a" , map ("b" , fooString )), objectValue );
183
+ }
184
+
93
185
@ Test
94
186
public void testImplicitlyCreatesObjects () {
95
187
ObjectValue objectValue = wrapObject ("a" , "old" );
@@ -159,12 +251,38 @@ public void testDeletesNestedKeys() {
159
251
}
160
252
161
253
@ Test
162
- public void testDeletesMultipleFields () {
163
- ObjectValue object = wrapObject ("a" , "a" , "b" , "b" , "c" , "c" );
164
- object .delete (field ("a" ));
165
- object .delete (field ("b" ));
166
- object .delete (field ("c" ));
254
+ public void testDeletesNestedObject () {
255
+ ObjectValue objectValue =
256
+ wrapObject ("a" , map ("b" , map ("c" , fooString , "d" , fooString ), "f" , fooString ));
257
+ objectValue .delete (field ("a.b" ));
258
+ assertEquals (wrapObject ("a" , map ("f" , fooString )), objectValue );
259
+ }
260
+
261
+ @ Test
262
+ public void testAddsAndDeletesField () {
263
+ ObjectValue objectValue = new ObjectValue ();
264
+ objectValue .set (field (fooString ), fooValue );
265
+ objectValue .delete (field (fooString ));
266
+ assertEquals (wrapObject (), objectValue );
267
+ }
167
268
168
- assertEquals (new ObjectValue (), object );
269
+ @ Test
270
+ public void testAddsAndDeletesNestedField () {
271
+ ObjectValue objectValue = new ObjectValue ();
272
+ objectValue .set (field ("a.b.c" ), fooValue );
273
+ objectValue .set (field ("a.b.d" ), fooValue );
274
+ objectValue .set (field ("f.g" ), fooValue );
275
+ objectValue .set (field ("h" ), fooValue );
276
+ objectValue .delete (field ("a.b.c" ));
277
+ objectValue .delete (field ("h" ));
278
+ assertEquals (
279
+ wrapObject ("a" , map ("b" , map ("d" , fooString )), "f" , map ("g" , fooString )), objectValue );
280
+ }
281
+
282
+ @ Test
283
+ public void testMergesExistingObject () {
284
+ ObjectValue objectValue = wrapObject ("a" , map ("b" , fooString ));
285
+ objectValue .set (field ("a.c" ), fooValue );
286
+ assertEquals (wrapObject ("a" , map ("b" , fooString , "c" , fooString )), objectValue );
169
287
}
170
288
}
0 commit comments