From 6ab9a059f28e286e79d4dbc6e5b9edd4a440d589 Mon Sep 17 00:00:00 2001 From: rsgowman Date: Wed, 23 May 2018 10:03:10 -0400 Subject: Deserialize NoDocument instances (#1311) Note that it isn't possible to *serialize* NoDocuments. Still TODO: - Error handling --- .../firebase/firestore/remote/serializer_test.cc | 54 ++++++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) (limited to 'Firestore/core/test') diff --git a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc index dd0fae2..ba9ea47 100644 --- a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc +++ b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc @@ -42,6 +42,7 @@ #include "Firestore/core/src/firebase/firestore/util/status.h" #include "Firestore/core/src/firebase/firestore/util/statusor.h" #include "Firestore/core/test/firebase/firestore/testutil/testutil.h" +#include "absl/types/optional.h" #include "google/protobuf/stubs/common.h" #include "google/protobuf/util/message_differencer.h" #include "gtest/gtest.h" @@ -109,6 +110,13 @@ class SerializerTest : public ::testing::Test { ExpectDeserializationRoundTrip(key, value, update_time, proto); } + void ExpectNoDocumentDeserializationRoundTrip( + const DocumentKey& key, + const SnapshotVersion& read_time, + const v1beta1::BatchGetDocumentsResponse& proto) { + ExpectDeserializationRoundTrip(key, absl::nullopt, read_time, proto); + } + /** * Checks the status. Don't use directly; use one of the relevant macros * instead. eg: @@ -279,8 +287,8 @@ class SerializerTest : public ::testing::Test { bool ok = actual_proto.ParseFromArray(bytes.data(), bytes.size()); EXPECT_TRUE(ok); - // TODO(rsgowman): Right now, we only support Document (and don't support - // NoDocument). That should change in the next PR or so. + // Note that the client can only serialize Documents (and cannot serialize + // NoDocuments) EXPECT_TRUE(proto.has_found()); // Slight weirdness: When we *encode* a document for sending it to the @@ -303,8 +311,8 @@ class SerializerTest : public ::testing::Test { void ExpectDeserializationRoundTrip( const DocumentKey& key, - const FieldValue& value, - const SnapshotVersion& update_time, + const absl::optional value, + const SnapshotVersion& version, // either update_time or read_time const v1beta1::BatchGetDocumentsResponse& proto) { size_t size = proto.ByteSizeLong(); std::vector bytes(size); @@ -316,13 +324,20 @@ class SerializerTest : public ::testing::Test { std::unique_ptr actual_model = std::move(actual_model_status).ValueOrDie(); - // TODO(rsgowman): Right now, we only support Document (and don't support - // NoDocument). That should change in the next PR or so. - EXPECT_EQ(MaybeDocument::Type::Document, actual_model->type()); - Document* actual_doc_model = static_cast(actual_model.get()); EXPECT_EQ(key, actual_model->key()); - EXPECT_EQ(value, actual_doc_model->data()); - EXPECT_EQ(update_time, actual_model->version()); + EXPECT_EQ(version, actual_model->version()); + switch (actual_model->type()) { + case MaybeDocument::Type::Document: { + Document* actual_doc_model = static_cast(actual_model.get()); + EXPECT_EQ(value, actual_doc_model->data()); + break; + } + case MaybeDocument::Type::NoDocument: + EXPECT_FALSE(value.has_value()); + break; + case MaybeDocument::Type::Unknown: + FAIL() << "We somehow created an invalid model object"; + } } std::string message_differences; @@ -679,5 +694,24 @@ TEST_F(SerializerTest, EncodesNonEmptyDocument) { ExpectRoundTrip(key, fields, update_time, proto); } +TEST_F(SerializerTest, DecodesNoDocument) { + // We can't actually *encode* a NoDocument; the method exposed by the + // serializer requires both the document key and contents (as an ObjectValue, + // i.e. map.) The contents can be empty, but not missing. As a result, this + // test will only verify the ability to decode a NoDocument. + + DocumentKey key = DocumentKey::FromPathString("path/to/the/doc"); + SnapshotVersion read_time = + SnapshotVersion{{/*seconds=*/1234, /*nanoseconds=*/5678}}; + + v1beta1::BatchGetDocumentsResponse proto; + proto.set_missing(serializer.EncodeKey(key)); + google::protobuf::Timestamp* read_time_proto = proto.mutable_read_time(); + read_time_proto->set_seconds(read_time.timestamp().seconds()); + read_time_proto->set_nanos(read_time.timestamp().nanoseconds()); + + ExpectNoDocumentDeserializationRoundTrip(key, read_time, proto); +} + // TODO(rsgowman): Test [en|de]coding multiple protos into the same output // vector. -- cgit v1.2.3