Skip to content

Commit 974c1b9

Browse files
committed
operator== and operator!= for DocumentChange.
1 parent 6239baf commit 974c1b9

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

firestore/integration_test_internal/src/document_change_test.cc

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
namespace firebase {
1414
namespace firestore {
1515

16-
#if defined(__ANDROID__)
17-
1816
using DocumentChangeTest = FirestoreIntegrationTest;
1917

18+
#if defined(__ANDROID__)
19+
2020
TEST_F(DocumentChangeTest, Construction) {
2121
testutil::AssertWrapperConstructionContract<DocumentChange,
2222
DocumentChangeInternal>();
@@ -78,5 +78,67 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {
7878

7979
#endif // defined(__ANDROID__)
8080

81+
TEST_F(DocumentChangeTest, Equality) {
82+
DocumentChange invalid_change_1 = DocumentChange();
83+
DocumentChange invalid_change_2 = DocumentChange();
84+
85+
EXPECT_TRUE(invalid_change_1 == invalid_change_2);
86+
EXPECT_FALSE(invalid_change_1 != invalid_change_2);
87+
88+
CollectionReference collection = Collection();
89+
Query query = collection.OrderBy("a");
90+
91+
DocumentReference doc1 = collection.Document("1");
92+
DocumentReference doc2 = collection.Document("2");
93+
94+
TestEventListener<QuerySnapshot> listener("TestDocumentChanges");
95+
ListenerRegistration registration = listener.AttachTo(&query);
96+
Await(listener);
97+
QuerySnapshot snapshot = listener.last_result();
98+
EXPECT_EQ(snapshot.size(), 0);
99+
100+
WriteDocument(doc1, MapFieldValue{{"a", FieldValue::Integer(1)}});
101+
Await(listener);
102+
snapshot = listener.last_result();
103+
104+
std::vector<DocumentChange> changes = snapshot.DocumentChanges();
105+
EXPECT_EQ(changes.size(), 1);
106+
// first_change: Added doc1.
107+
auto first_change = changes[0];
108+
EXPECT_TRUE(first_change == first_change);
109+
EXPECT_TRUE(first_change != invalid_change_1);
110+
EXPECT_FALSE(first_change != first_change);
111+
EXPECT_FALSE(first_change == invalid_change_1);
112+
113+
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(2)}});
114+
Await(listener, 2);
115+
snapshot = listener.last_result();
116+
117+
changes = snapshot.DocumentChanges();
118+
EXPECT_EQ(changes.size(), 1);
119+
// second_change: Added doc2.
120+
auto second_change = changes[0];
121+
EXPECT_TRUE(second_change != first_change);
122+
EXPECT_TRUE(second_change != invalid_change_1);
123+
EXPECT_FALSE(second_change == first_change);
124+
EXPECT_FALSE(second_change == invalid_change_1);
125+
126+
// Make doc2 ordered before doc1.
127+
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(0)}});
128+
Await(listener, 3);
129+
snapshot = listener.last_result();
130+
131+
changes = snapshot.DocumentChanges();
132+
EXPECT_EQ(changes.size(), 1);
133+
// third_change: Modified doc2.
134+
auto third_change = changes[0];
135+
EXPECT_TRUE(third_change != first_change);
136+
EXPECT_TRUE(third_change != second_change);
137+
EXPECT_TRUE(third_change != invalid_change_1);
138+
EXPECT_FALSE(third_change == first_change);
139+
EXPECT_FALSE(third_change == second_change);
140+
EXPECT_FALSE(third_change == invalid_change_1);
141+
}
142+
81143
} // namespace firestore
82144
} // namespace firebase

firestore/src/common/document_change.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,10 @@ std::size_t DocumentChange::new_index() const {
104104
return internal_->new_index();
105105
}
106106

107+
bool operator==(const DocumentChange& lhs, const DocumentChange& rhs) {
108+
return lhs.type() == rhs.type() && lhs.old_index() == rhs.old_index() &&
109+
lhs.new_index() == rhs.new_index() && lhs.document() == rhs.document();
110+
}
111+
107112
} // namespace firestore
108113
} // namespace firebase

firestore/src/include/firebase/firestore/document_change.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ class DocumentChange {
184184
mutable DocumentChangeInternal* internal_ = nullptr;
185185
};
186186

187+
/** Checks `lhs` and `rhs` for equality. */
188+
bool operator==(const DocumentChange& lhs, const DocumentChange& rhs);
189+
190+
/** Checks `lhs` and `rhs` for inequality. */
191+
inline bool operator!=(const DocumentChange& lhs, const DocumentChange& rhs) {
192+
return !(lhs == rhs);
193+
}
194+
187195
} // namespace firestore
188196
} // namespace firebase
189197

0 commit comments

Comments
 (0)