From 2ceea9b66eb8ea516702e1ef409e801fec068682 Mon Sep 17 00:00:00 2001 From: zxu Date: Thu, 12 Apr 2018 21:32:19 -0400 Subject: Port `Precondition` to C++ (#1040) * port FieldMask to C++ * address changes * address changes * fix test * address change * Port transform operations (FSTTransformOperation, FSTServerTimestampTransform) to C++ * address changes * address changes * address changes * implement `FieldTransform` in C++ * port `FieldTransform` * make `fieldTransforms` shared inside `context` * Implement Precondition in C++ w/o test yet * add unit test for `Precondition` * port `Precondition` * address changes * address changes * fix bugs for integration test * address changes * fix lint * address changes * address changes --- .../test/firebase/firestore/model/CMakeLists.txt | 1 + .../firebase/firestore/model/precondition_test.cc | 77 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 Firestore/core/test/firebase/firestore/model/precondition_test.cc (limited to 'Firestore/core/test/firebase/firestore/model') diff --git a/Firestore/core/test/firebase/firestore/model/CMakeLists.txt b/Firestore/core/test/firebase/firestore/model/CMakeLists.txt index 3bac89d..9c94677 100644 --- a/Firestore/core/test/firebase/firestore/model/CMakeLists.txt +++ b/Firestore/core/test/firebase/firestore/model/CMakeLists.txt @@ -24,6 +24,7 @@ cc_test( field_value_test.cc maybe_document_test.cc no_document_test.cc + precondition_test.cc resource_path_test.cc snapshot_version_test.cc transform_operations_test.cc diff --git a/Firestore/core/test/firebase/firestore/model/precondition_test.cc b/Firestore/core/test/firebase/firestore/model/precondition_test.cc new file mode 100644 index 0000000..3ddb2ba --- /dev/null +++ b/Firestore/core/test/firebase/firestore/model/precondition_test.cc @@ -0,0 +1,77 @@ +/* + * Copyright 2018 Google + * + * 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 "Firestore/core/src/firebase/firestore/model/precondition.h" + +#include "Firestore/core/src/firebase/firestore/model/document.h" +#include "Firestore/core/src/firebase/firestore/model/no_document.h" +#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h" +#include "Firestore/core/test/firebase/firestore/testutil/testutil.h" + +#include "gtest/gtest.h" + +namespace firebase { +namespace firestore { +namespace model { + +TEST(Precondition, None) { + const Precondition none = Precondition::None(); + EXPECT_EQ(Precondition::Type::None, none.type()); + EXPECT_TRUE(none.IsNone()); + EXPECT_EQ(SnapshotVersion::None(), none.update_time()); + + const NoDocument deleted_doc = testutil::DeletedDoc("foo/doc", 1234567); + const Document doc = testutil::Doc("bar/doc", 7654321); + EXPECT_TRUE(none.IsValidFor(deleted_doc)); + EXPECT_TRUE(none.IsValidFor(doc)); +} + +TEST(Precondition, Exists) { + const Precondition exists = Precondition::Exists(true); + const Precondition no_exists = Precondition::Exists(false); + EXPECT_EQ(Precondition::Type::Exists, exists.type()); + EXPECT_EQ(Precondition::Type::Exists, no_exists.type()); + EXPECT_FALSE(exists.IsNone()); + EXPECT_FALSE(no_exists.IsNone()); + EXPECT_EQ(SnapshotVersion::None(), exists.update_time()); + EXPECT_EQ(SnapshotVersion::None(), no_exists.update_time()); + + const NoDocument deleted_doc = testutil::DeletedDoc("foo/doc", 1234567); + const Document doc = testutil::Doc("bar/doc", 7654321); + EXPECT_FALSE(exists.IsValidFor(deleted_doc)); + EXPECT_TRUE(exists.IsValidFor(doc)); + EXPECT_TRUE(no_exists.IsValidFor(deleted_doc)); + EXPECT_FALSE(no_exists.IsValidFor(doc)); +} + +TEST(Precondition, UpdateTime) { + const Precondition update_time = + Precondition::UpdateTime(testutil::Version(1234567)); + EXPECT_EQ(Precondition::Type::UpdateTime, update_time.type()); + EXPECT_FALSE(update_time.IsNone()); + EXPECT_EQ(testutil::Version(1234567), update_time.update_time()); + + const NoDocument deleted_doc = testutil::DeletedDoc("foo/doc", 1234567); + const Document not_match = testutil::Doc("bar/doc", 7654321); + const Document match = testutil::Doc("baz/doc", 1234567); + EXPECT_FALSE(update_time.IsValidFor(deleted_doc)); + EXPECT_FALSE(update_time.IsValidFor(not_match)); + EXPECT_TRUE(update_time.IsValidFor(match)); +} + +} // namespace model +} // namespace firestore +} // namespace firebase -- cgit v1.2.3