9
9
namespace firebase {
10
10
namespace firestore {
11
11
12
+ using Type = FieldValue::Type;
13
+
12
14
using ServerTimestampBehavior = DocumentSnapshot::ServerTimestampBehavior;
13
15
16
+ const char * TypeName (Type type) {
17
+ switch (type) {
18
+ case Type::kNull :
19
+ return " kNull" ;
20
+ case Type::kBoolean :
21
+ return " kBoolean" ;
22
+ case Type::kInteger :
23
+ return " kInteger" ;
24
+ case Type::kDouble :
25
+ return " kDouble" ;
26
+ case Type::kTimestamp :
27
+ return " kTimestamp" ;
28
+ case Type::kString :
29
+ return " kString" ;
30
+ case Type::kBlob :
31
+ return " kBlob" ;
32
+ case Type::kReference :
33
+ return " kReference" ;
34
+ case Type::kGeoPoint :
35
+ return " kGeoPoint" ;
36
+ case Type::kArray :
37
+ return " kArray" ;
38
+ case Type::kMap :
39
+ return " kMap" ;
40
+ case Type::kDelete :
41
+ return " kDelete" ;
42
+ case Type::kServerTimestamp :
43
+ return " kServerTimestamp" ;
44
+ case Type::kArrayUnion :
45
+ return " kArrayUnion" ;
46
+ case Type::kArrayRemove :
47
+ return " kArrayRemove" ;
48
+ case Type::kIncrementInteger :
49
+ return " kIncrementInteger" ;
50
+ case Type::kIncrementDouble :
51
+ return " kIncrementDouble" ;
52
+ }
53
+ }
54
+
55
+ void PrintTo (const Type& type, std::ostream* os) {
56
+ *os << " Type::" << TypeName (type);
57
+ }
58
+
59
+ void PrintTo (const FieldValue& f, std::ostream* os) {
60
+ *os << f.ToString () << " (" ;
61
+ PrintTo (f.type (), os);
62
+ *os << " )" ;
63
+ }
64
+
14
65
class NumericTransformsTest : public FirestoreIntegrationTest {
15
66
public:
16
67
NumericTransformsTest () {
@@ -35,20 +86,16 @@ class NumericTransformsTest : public FirestoreIntegrationTest {
35
86
36
87
void ExpectLocalAndRemoteValue (int value) {
37
88
DocumentSnapshot snap = accumulator_.AwaitLocalEvent ();
38
- EXPECT_TRUE (snap.Get (" sum" ).is_integer ());
39
- EXPECT_EQ (value, snap.Get (" sum" ).integer_value ());
89
+ ASSERT_EQ (snap.Get (" sum" ), FieldValue::Integer (value));
40
90
snap = accumulator_.AwaitRemoteEvent ();
41
- EXPECT_TRUE (snap.Get (" sum" ).is_integer ());
42
- EXPECT_EQ (value, snap.Get (" sum" ).integer_value ());
91
+ ASSERT_EQ (snap.Get (" sum" ), FieldValue::Integer (value));
43
92
}
44
93
45
94
void ExpectLocalAndRemoteValue (double value) {
46
95
DocumentSnapshot snap = accumulator_.AwaitLocalEvent ();
47
- EXPECT_TRUE (snap.Get (" sum" ).is_double ());
48
- EXPECT_DOUBLE_EQ (value, snap.Get (" sum" ).double_value ());
96
+ ASSERT_EQ (snap.Get (" sum" ), FieldValue::Double (value));
49
97
snap = accumulator_.AwaitRemoteEvent ();
50
- EXPECT_TRUE (snap.Get (" sum" ).is_double ());
51
- EXPECT_DOUBLE_EQ (value, snap.Get (" sum" ).double_value ());
98
+ ASSERT_EQ (snap.Get (" sum" ), FieldValue::Double (value));
52
99
}
53
100
54
101
// A document reference to read and write.
@@ -85,27 +132,27 @@ TEST_F(NumericTransformsTest, IntegerIncrementWithExistingInteger) {
85
132
}
86
133
87
134
TEST_F (NumericTransformsTest, DoubleIncrementWithExistingDouble) {
88
- WriteInitialData ({{" sum" , FieldValue::Double (13.37 )}});
135
+ WriteInitialData ({{" sum" , FieldValue::Double (0.5 )}});
89
136
90
- Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (0.1 )}}));
137
+ Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (0.25 )}}));
91
138
92
- ExpectLocalAndRemoteValue (13.47 );
139
+ ExpectLocalAndRemoteValue (0.75 );
93
140
}
94
141
95
142
TEST_F (NumericTransformsTest, IntegerIncrementWithExistingDouble) {
96
- WriteInitialData ({{" sum" , FieldValue::Double (13.37 )}});
143
+ WriteInitialData ({{" sum" , FieldValue::Double (0.5 )}});
97
144
98
145
Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (1 )}}));
99
146
100
- ExpectLocalAndRemoteValue (14.37 );
147
+ ExpectLocalAndRemoteValue (1.5 );
101
148
}
102
149
103
150
TEST_F (NumericTransformsTest, DoubleIncrementWithExistingInteger) {
104
- WriteInitialData ({{" sum" , FieldValue::Integer (1337 )}});
151
+ WriteInitialData ({{" sum" , FieldValue::Integer (1 )}});
105
152
106
- Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (0.1 )}}));
153
+ Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (0.5 )}}));
107
154
108
- ExpectLocalAndRemoteValue (1337.1 );
155
+ ExpectLocalAndRemoteValue (1.5 );
109
156
}
110
157
111
158
TEST_F (NumericTransformsTest, IntegerIncrementWithExistingString) {
@@ -119,37 +166,34 @@ TEST_F(NumericTransformsTest, IntegerIncrementWithExistingString) {
119
166
TEST_F (NumericTransformsTest, DoubleIncrementWithExistingString) {
120
167
WriteInitialData ({{" sum" , FieldValue::String (" overwrite" )}});
121
168
122
- Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (13.37 )}}));
169
+ Await (doc_ref_.Update ({{" sum" , FieldValue::Increment (1.5 )}}));
123
170
124
- ExpectLocalAndRemoteValue (13.37 );
171
+ ExpectLocalAndRemoteValue (1.5 );
125
172
}
126
173
127
174
TEST_F (NumericTransformsTest, MultipleDoubleIncrements) {
128
175
WriteInitialData ({{" sum" , FieldValue::Double (0.0 )}});
129
176
130
177
DisableNetwork ();
131
178
132
- doc_ref_.Update ({{" sum" , FieldValue::Increment (0.1 )}});
133
- doc_ref_.Update ({{" sum" , FieldValue::Increment (0.01 )}});
134
- doc_ref_.Update ({{" sum" , FieldValue::Increment (0.001 )}});
179
+ doc_ref_.Update ({{" sum" , FieldValue::Increment (0.5 )}});
180
+ doc_ref_.Update ({{" sum" , FieldValue::Increment (1.0 )}});
181
+ doc_ref_.Update ({{" sum" , FieldValue::Increment (2.0 )}});
135
182
136
183
DocumentSnapshot snap = accumulator_.AwaitLocalEvent ();
137
184
138
- EXPECT_TRUE (snap.Get (" sum" ).is_double ());
139
- EXPECT_DOUBLE_EQ (0.1 , snap.Get (" sum" ).double_value ());
185
+ EXPECT_EQ (snap.Get (" sum" ), FieldValue::Double (0.5 ));
140
186
141
187
snap = accumulator_.AwaitLocalEvent ();
142
- EXPECT_TRUE (snap.Get (" sum" ).is_double ());
143
- EXPECT_DOUBLE_EQ (0.11 , snap.Get (" sum" ).double_value ());
188
+ EXPECT_EQ (snap.Get (" sum" ), FieldValue::Double (1.5 ));
144
189
145
190
snap = accumulator_.AwaitLocalEvent ();
146
- EXPECT_TRUE (snap.Get (" sum" ).is_double ());
147
- EXPECT_DOUBLE_EQ (0.111 , snap.Get (" sum" ).double_value ());
191
+ EXPECT_EQ (snap.Get (" sum" ), FieldValue::Double (3.5 ));
148
192
149
193
EnableNetwork ();
150
194
151
195
snap = accumulator_.AwaitRemoteEvent ();
152
- EXPECT_DOUBLE_EQ ( 0.111 , snap.Get (" sum" ). double_value ( ));
196
+ EXPECT_EQ ( snap.Get (" sum" ), FieldValue::Double ( 3.5 ));
153
197
}
154
198
155
199
TEST_F (NumericTransformsTest, IncrementTwiceInABatch) {
@@ -186,12 +230,12 @@ TEST_F(NumericTransformsTest, ServerTimestampAndIncrement) {
186
230
doc_ref_.Set ({{" sum" , FieldValue::Increment (1 )}});
187
231
188
232
DocumentSnapshot snapshot = accumulator_.AwaitLocalEvent ();
189
- EXPECT_TRUE (
190
- snapshot. Get ( " sum " , ServerTimestampBehavior:: kEstimate ). is_timestamp () );
233
+ EXPECT_EQ (snapshot. Get ( " sum " , ServerTimestampBehavior:: kEstimate ). type (),
234
+ Type:: kTimestamp );
191
235
192
236
DocumentSnapshot snap = accumulator_.AwaitLocalEvent ();
193
237
EXPECT_TRUE (snap.Get (" sum" ).is_integer ());
194
- EXPECT_EQ (1 , snap.Get (" sum" ). integer_value ( ));
238
+ EXPECT_EQ (snap.Get (" sum" ), FieldValue::Integer ( 1 ));
195
239
196
240
EnableNetwork ();
197
241
0 commit comments