Skip to content

Tomandersen/count test #1206

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 23 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
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
2 changes: 2 additions & 0 deletions firestore/integration_test_internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ set(FIREBASE_INTEGRATION_TEST_PORTABLE_TEST_SRCS
# public API are performed.
src/integration_test.cc
# Internal tests below.
src/aggregate_query_snapshot_test.cc
src/aggregate_query_test.cc
src/bundle_test.cc
src/collection_reference_test.cc
src/cursor_test.cc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "firebase/firestore.h"
#include "firestore_integration_test.h"

#include "gtest/gtest.h"

namespace firebase {
namespace firestore {

using AggregateQuerySnapshotTest = FirestoreIntegrationTest;

std::size_t AggregateQuerySnapshotHash(const AggregateQuerySnapshot& snapshot) {
return snapshot.Hash();
}

TEST_F(AggregateQuerySnapshotTest, Equality) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
{"b", {{"k", FieldValue::String("b")}}},
{"c", {{"k", FieldValue::String("c")}}}});
AggregateQuerySnapshot snapshot1 = ReadAggregate(
collection.WhereEqualTo("k", FieldValue::String("d")).Count());
AggregateQuerySnapshot snapshot2 = ReadAggregate(collection.Limit(1).Count());
AggregateQuerySnapshot snapshot3 = ReadAggregate(collection.Limit(1).Count());
AggregateQuerySnapshot snapshot4 = ReadAggregate(collection.Limit(3).Count());
AggregateQuerySnapshot snapshot5 = ReadAggregate(collection.Count());
AggregateQuerySnapshot snapshot6 = ReadAggregate(collection.Count());

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(snapshot2 == snapshot2);
EXPECT_TRUE(snapshot2 == snapshot3);
EXPECT_TRUE(snapshot2 != snapshot4);
EXPECT_TRUE(snapshot2 != snapshot5);
EXPECT_TRUE(snapshot2 != snapshot6);
EXPECT_TRUE(snapshot4 == snapshot4);
EXPECT_TRUE(snapshot4 != snapshot5);
EXPECT_TRUE(snapshot4 != snapshot6);
EXPECT_TRUE(snapshot5 == snapshot5);
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(snapshot2 != snapshot2);
EXPECT_FALSE(snapshot2 != snapshot3);
EXPECT_FALSE(snapshot2 == snapshot4);
EXPECT_FALSE(snapshot2 == snapshot5);
EXPECT_FALSE(snapshot2 == snapshot6);
EXPECT_FALSE(snapshot4 != snapshot4);
EXPECT_FALSE(snapshot4 == snapshot5);
EXPECT_FALSE(snapshot4 == snapshot6);
EXPECT_FALSE(snapshot5 != snapshot5);
EXPECT_FALSE(snapshot5 != snapshot6);
}

TEST_F(AggregateQuerySnapshotTest, TestHashCode) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
{"b", {{"k", FieldValue::String("b")}}},
{"c", {{"k", FieldValue::String("c")}}}});
AggregateQuerySnapshot snapshot1 = ReadAggregate(
collection.WhereEqualTo("k", FieldValue::String("d")).Count());
AggregateQuerySnapshot snapshot2 = ReadAggregate(collection.Limit(1).Count());
AggregateQuerySnapshot snapshot3 = ReadAggregate(collection.Limit(1).Count());
AggregateQuerySnapshot snapshot4 = ReadAggregate(collection.Limit(3).Count());
AggregateQuerySnapshot snapshot5 = ReadAggregate(collection.Count());
AggregateQuerySnapshot snapshot6 = ReadAggregate(collection.Count());

EXPECT_EQ(AggregateQuerySnapshotHash(snapshot1),
AggregateQuerySnapshotHash(snapshot1));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot1),
AggregateQuerySnapshotHash(snapshot2));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot1),
AggregateQuerySnapshotHash(snapshot3));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot1),
AggregateQuerySnapshotHash(snapshot4));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot1),
AggregateQuerySnapshotHash(snapshot5));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot1),
AggregateQuerySnapshotHash(snapshot6));
EXPECT_EQ(AggregateQuerySnapshotHash(snapshot2),
AggregateQuerySnapshotHash(snapshot2));
EXPECT_EQ(AggregateQuerySnapshotHash(snapshot2),
AggregateQuerySnapshotHash(snapshot3));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot2),
AggregateQuerySnapshotHash(snapshot4));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot2),
AggregateQuerySnapshotHash(snapshot5));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot2),
AggregateQuerySnapshotHash(snapshot6));
EXPECT_EQ(AggregateQuerySnapshotHash(snapshot4),
AggregateQuerySnapshotHash(snapshot4));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot4),
AggregateQuerySnapshotHash(snapshot5));
EXPECT_NE(AggregateQuerySnapshotHash(snapshot4),
AggregateQuerySnapshotHash(snapshot6));
EXPECT_EQ(AggregateQuerySnapshotHash(snapshot5),
AggregateQuerySnapshotHash(snapshot5));
EXPECT_EQ(AggregateQuerySnapshotHash(snapshot5),
AggregateQuerySnapshotHash(snapshot6));
}

} // namespace firestore
} // namespace firebase
46 changes: 46 additions & 0 deletions firestore/integration_test_internal/src/aggregate_query_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "firebase/firestore.h"
#include "firestore_integration_test.h"

#include "gtest/gtest.h"

namespace firebase {
namespace firestore {

using AggrgegateQueryTest = FirestoreIntegrationTest;

size_t AggregateQueryHash(const AggregateQuery& aggregate_query) {
return aggregate_query.Hash();
}

TEST_F(AggrgegateQueryTest, TestHashCode) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
{"b", {{"k", FieldValue::String("b")}}}});
Query query1 =
collection.Limit(2).OrderBy("sort", Query::Direction::kAscending);
Query query2 =
collection.Limit(2).OrderBy("sort", Query::Direction::kDescending);
EXPECT_NE(AggregateQueryHash(query1.Count()),
AggregateQueryHash(query2.Count()));
EXPECT_EQ(AggregateQueryHash(query1.Count()),
AggregateQueryHash(query1.Count()));
}

} // namespace firestore
} // namespace firebase
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ QuerySnapshot FirestoreIntegrationTest::ReadDocuments(
}
}

AggregateQuerySnapshot FirestoreIntegrationTest::ReadAggregate(
const AggregateQuery& reference) const {
SCOPED_TRACE("FirestoreIntegrationTest::ReadDocuments()");
Future<AggregateQuerySnapshot> future =
reference.Get(AggregateSource::kServer);
Stopwatch stopwatch;
const AggregateQuerySnapshot* result = Await(future);
stopwatch.stop();
if (FailIfUnsuccessful("ReadDocuments", future, stopwatch)) {
return {};
} else {
return *result;
}
}

void FirestoreIntegrationTest::DeleteDocument(
DocumentReference reference) const {
SCOPED_TRACE("FirestoreIntegrationTest::DeleteDocument(" + reference.path() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ class FirestoreIntegrationTest : public testing::Test {
// Read documents in the specified collection / query.
QuerySnapshot ReadDocuments(const Query& reference) const;

// Read the aggregate.
AggregateQuerySnapshot ReadAggregate(const AggregateQuery& reference) const;

// Delete the specified document.
void DeleteDocument(DocumentReference reference) const;

Expand Down
Loading