Skip to content

Commit 0490b4b

Browse files
wilhuffa-maurice
authored andcommitted
Make test failures in numeric transforms tests easier to read
Previously, if the value was of the wrong type, you'd get a message like: Value of: snap.Get("sum").is_integer() Actual: false Expected: true Which would give no indication of what the actual type or value was. Now tests will fail like this: Expected equality of these values: snap.Get("sum") Which is: 1337 (Type::kDouble) FieldValue::Integer(value) Which is: 1337 (Type::kInteger) As an added bonus this also simplifies the calling code because now we can just assert that a value in a snapshot is equal to some expected value and GoogleTest will do the heavy lifting of printing the differences. One unsolved problem with this approach is how to handle equality within epsilon for floating point values. This turns out to be non-trivial without writing custom matchers, which is beyond the scope of what I wanted to tackle here. Instead of solving this I've changed the tests to use values that have an exact representation as doubles. This is easier to do for integral values than for fractional ones so the tests now use integer-valued doubles to achieve the same effect of cumulative addition as before. PiperOrigin-RevId: 321022746
1 parent 082c5d2 commit 0490b4b

File tree

1 file changed

+75
-31
lines changed

1 file changed

+75
-31
lines changed

firestore/src/tests/numeric_transforms_test.cc

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,59 @@
99
namespace firebase {
1010
namespace firestore {
1111

12+
using Type = FieldValue::Type;
13+
1214
using ServerTimestampBehavior = DocumentSnapshot::ServerTimestampBehavior;
1315

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+
1465
class NumericTransformsTest : public FirestoreIntegrationTest {
1566
public:
1667
NumericTransformsTest() {
@@ -35,20 +86,16 @@ class NumericTransformsTest : public FirestoreIntegrationTest {
3586

3687
void ExpectLocalAndRemoteValue(int value) {
3788
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));
4090
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));
4392
}
4493

4594
void ExpectLocalAndRemoteValue(double value) {
4695
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));
4997
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));
5299
}
53100

54101
// A document reference to read and write.
@@ -85,27 +132,27 @@ TEST_F(NumericTransformsTest, IntegerIncrementWithExistingInteger) {
85132
}
86133

87134
TEST_F(NumericTransformsTest, DoubleIncrementWithExistingDouble) {
88-
WriteInitialData({{"sum", FieldValue::Double(13.37)}});
135+
WriteInitialData({{"sum", FieldValue::Double(0.5)}});
89136

90-
Await(doc_ref_.Update({{"sum", FieldValue::Increment(0.1)}}));
137+
Await(doc_ref_.Update({{"sum", FieldValue::Increment(0.25)}}));
91138

92-
ExpectLocalAndRemoteValue(13.47);
139+
ExpectLocalAndRemoteValue(0.75);
93140
}
94141

95142
TEST_F(NumericTransformsTest, IntegerIncrementWithExistingDouble) {
96-
WriteInitialData({{"sum", FieldValue::Double(13.37)}});
143+
WriteInitialData({{"sum", FieldValue::Double(0.5)}});
97144

98145
Await(doc_ref_.Update({{"sum", FieldValue::Increment(1)}}));
99146

100-
ExpectLocalAndRemoteValue(14.37);
147+
ExpectLocalAndRemoteValue(1.5);
101148
}
102149

103150
TEST_F(NumericTransformsTest, DoubleIncrementWithExistingInteger) {
104-
WriteInitialData({{"sum", FieldValue::Integer(1337)}});
151+
WriteInitialData({{"sum", FieldValue::Integer(1)}});
105152

106-
Await(doc_ref_.Update({{"sum", FieldValue::Increment(0.1)}}));
153+
Await(doc_ref_.Update({{"sum", FieldValue::Increment(0.5)}}));
107154

108-
ExpectLocalAndRemoteValue(1337.1);
155+
ExpectLocalAndRemoteValue(1.5);
109156
}
110157

111158
TEST_F(NumericTransformsTest, IntegerIncrementWithExistingString) {
@@ -119,37 +166,34 @@ TEST_F(NumericTransformsTest, IntegerIncrementWithExistingString) {
119166
TEST_F(NumericTransformsTest, DoubleIncrementWithExistingString) {
120167
WriteInitialData({{"sum", FieldValue::String("overwrite")}});
121168

122-
Await(doc_ref_.Update({{"sum", FieldValue::Increment(13.37)}}));
169+
Await(doc_ref_.Update({{"sum", FieldValue::Increment(1.5)}}));
123170

124-
ExpectLocalAndRemoteValue(13.37);
171+
ExpectLocalAndRemoteValue(1.5);
125172
}
126173

127174
TEST_F(NumericTransformsTest, MultipleDoubleIncrements) {
128175
WriteInitialData({{"sum", FieldValue::Double(0.0)}});
129176

130177
DisableNetwork();
131178

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)}});
135182

136183
DocumentSnapshot snap = accumulator_.AwaitLocalEvent();
137184

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));
140186

141187
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));
144189

145190
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));
148192

149193
EnableNetwork();
150194

151195
snap = accumulator_.AwaitRemoteEvent();
152-
EXPECT_DOUBLE_EQ(0.111, snap.Get("sum").double_value());
196+
EXPECT_EQ(snap.Get("sum"), FieldValue::Double(3.5));
153197
}
154198

155199
TEST_F(NumericTransformsTest, IncrementTwiceInABatch) {
@@ -186,12 +230,12 @@ TEST_F(NumericTransformsTest, ServerTimestampAndIncrement) {
186230
doc_ref_.Set({{"sum", FieldValue::Increment(1)}});
187231

188232
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);
191235

192236
DocumentSnapshot snap = accumulator_.AwaitLocalEvent();
193237
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));
195239

196240
EnableNetwork();
197241

0 commit comments

Comments
 (0)