Skip to content

operator== and operator!= for QuerySnapshot. #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions firestore/integration_test_internal/src/query_snapshot_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "firebase/firestore.h"
#include "firestore/src/common/wrapper_assertions.h"
#include "firestore_integration_test.h"
#if defined(__ANDROID__)
#include "firestore/src/android/query_snapshot_android.h"
#endif // defined(__ANDROID__)
Expand All @@ -14,9 +15,9 @@
namespace firebase {
namespace firestore {

#if defined(__ANDROID__)
using QuerySnapshotTest = FirestoreIntegrationTest;

using QuerySnapshotTest = testing::Test;
#if defined(__ANDROID__)

TEST_F(QuerySnapshotTest, Construction) {
testutil::AssertWrapperConstructionContract<QuerySnapshot,
Expand All @@ -30,5 +31,42 @@ TEST_F(QuerySnapshotTest, Assignment) {

#endif // defined(__ANDROID__)

TEST_F(QuerySnapshotTest, TestCanQueryWithAndWithoutDocumentKey) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
{"b", {{"k", FieldValue::String("b")}}},
{"c", {{"k", FieldValue::String("c")}}}});
QuerySnapshot snapshot1 = ReadDocuments(collection.Limit(2));
QuerySnapshot snapshot2 = ReadDocuments(collection.Limit(2));
QuerySnapshot snapshot3 = ReadDocuments(collection.Limit(1));
QuerySnapshot snapshot4 = ReadDocuments(collection);
QuerySnapshot snapshot5 =
ReadDocuments(collection.OrderBy("k", Query::Direction::kAscending));
QuerySnapshot snapshot6 =
ReadDocuments(collection.OrderBy("k", Query::Direction::kDescending));

EXPECT_TRUE(snapshot1 == snapshot1);
EXPECT_TRUE(snapshot1 == snapshot2);
EXPECT_TRUE(snapshot1 != snapshot3);
EXPECT_TRUE(snapshot1 != snapshot4);
EXPECT_TRUE(snapshot1 != snapshot5);
EXPECT_TRUE(snapshot1 != snapshot6);
EXPECT_TRUE(snapshot3 != snapshot4);
EXPECT_TRUE(snapshot3 != snapshot5);
EXPECT_TRUE(snapshot3 != snapshot6);
EXPECT_TRUE(snapshot5 != snapshot6);

EXPECT_FALSE(snapshot1 != snapshot1);
EXPECT_FALSE(snapshot1 != snapshot2);
EXPECT_FALSE(snapshot1 == snapshot3);
EXPECT_FALSE(snapshot1 == snapshot4);
EXPECT_FALSE(snapshot1 == snapshot5);
EXPECT_FALSE(snapshot1 == snapshot6);
EXPECT_FALSE(snapshot3 == snapshot4);
EXPECT_FALSE(snapshot3 == snapshot5);
EXPECT_FALSE(snapshot3 == snapshot6);
EXPECT_FALSE(snapshot5 == snapshot6);
}

} // namespace firestore
} // namespace firebase
6 changes: 6 additions & 0 deletions firestore/src/android/query_snapshot_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,11 @@ std::size_t QuerySnapshotInternal::size() const {
return env.Call(obj_, kSize);
}

bool operator==(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs) {
Env env = FirestoreInternal::GetEnv();
return Object::Equals(env, lhs.ToJava(), rhs.ToJava());
}

} // namespace firestore
} // namespace firebase
7 changes: 7 additions & 0 deletions firestore/src/android/query_snapshot_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class QuerySnapshotInternal : public Wrapper {
std::size_t size() const;
};

bool operator==(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs);
inline bool operator!=(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs) {
return !(lhs == rhs);
}

} // namespace firestore
} // namespace firebase

Expand Down
8 changes: 8 additions & 0 deletions firestore/src/common/query_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,13 @@ std::size_t QuerySnapshot::size() const {
return internal_->size();
}

bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs) {
if (!lhs.internal_ || !rhs.internal_) {
return lhs.internal_ == rhs.internal_;
}

return lhs.internal_ == rhs.internal_ || *lhs.internal_ == *rhs.internal_;
}

} // namespace firestore
} // namespace firebase
10 changes: 10 additions & 0 deletions firestore/src/include/firebase/firestore/query_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class QuerySnapshot {
bool is_valid() const { return internal_ != nullptr; }

private:
friend bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs);

friend class EventListenerInternal;
friend class FirestoreInternal;
friend struct ConverterImpl;
Expand All @@ -175,6 +177,14 @@ class QuerySnapshot {
mutable QuerySnapshotInternal* internal_ = nullptr;
};

/** Checks `lhs` and `rhs` for equality. */
bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs);

/** Checks `lhs` and `rhs` for inequality. */
inline bool operator!=(const QuerySnapshot& lhs, const QuerySnapshot& rhs) {
return !(lhs == rhs);
}

} // namespace firestore
} // namespace firebase

Expand Down
5 changes: 5 additions & 0 deletions firestore/src/main/query_snapshot_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,10 @@ std::vector<DocumentSnapshot> QuerySnapshotInternal::documents() const {
return documents_.value();
}

bool operator==(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs) {
return lhs.snapshot_ == rhs.snapshot_;
}

} // namespace firestore
} // namespace firebase
8 changes: 8 additions & 0 deletions firestore/src/main/query_snapshot_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class QuerySnapshotInternal {

std::vector<DocumentSnapshot> documents() const;

friend bool operator==(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs);

private:
api::QuerySnapshot snapshot_;

Expand All @@ -47,6 +50,11 @@ class QuerySnapshotInternal {
mutable bool changes_include_metadata_ = false;
};

inline bool operator!=(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs) {
return !(lhs == rhs);
}

} // namespace firestore
} // namespace firebase

Expand Down