From a34b0b1cf54d6af636e4263837a07baf47d41992 Mon Sep 17 00:00:00 2001 From: rsgowman Date: Tue, 22 May 2018 14:45:03 -0400 Subject: [De]serialize non-empty Document instances (#1284) * [De]serialize non-empty Document instances Still TODO: - NoDocument - ErrorHandling --- .../firebase/firestore/remote/serializer_test.cc | 67 +++++++++++++++++++--- 1 file changed, 59 insertions(+), 8 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 a147309..f15363d 100644 --- a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc +++ b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc @@ -219,6 +219,29 @@ class SerializerTest : public ::testing::Test { return proto; } + /** + * Creates entries in the proto that we don't care about. + * + * We ignore create time in our serializer. We never set it, and never read it + * (other than to throw it away). But the server could (and probably does) set + * it, so we need to be able to discard it properly. The ExpectRoundTrip deals + * with this asymmetry. + * + * This method adds these ignored fields to the proto. + */ + void TouchIgnoredBatchGetDocumentsResponseFields( + google::firestore::v1beta1::BatchGetDocumentsResponse* proto) { + // TODO(rsgowman): This method currently assumes that this is a 'found' + // document. We (probably) will need to adjust this to work with NoDocuments + // too. + google::firestore::v1beta1::Document* doc_proto = proto->mutable_found(); + + google::protobuf::Timestamp* create_time_proto = + doc_proto->mutable_create_time(); + create_time_proto->set_seconds(8765); + create_time_proto->set_nanos(4321); + } + private: void ExpectSerializationRoundTrip( const FieldValue& model, @@ -618,17 +641,45 @@ TEST_F(SerializerTest, EncodesEmptyDocument) { update_time_proto->set_seconds(1234); update_time_proto->set_nanos(5678); - // Note that we ignore create time in our serializer. We never set it, and - // never read it (other than to throw it away). But the server could (and - // probably does) set it, so we need to be able to discard it properly. The - // ExpectRoundTrip deals with this asymmetry. - google::protobuf::Timestamp* create_time_proto = - doc_proto->mutable_create_time(); - create_time_proto->set_seconds(8765); - create_time_proto->set_nanos(4321); + TouchIgnoredBatchGetDocumentsResponseFields(&proto); ExpectRoundTrip(key, empty_value, update_time, proto); } +TEST_F(SerializerTest, EncodesNonEmptyDocument) { + DocumentKey key = DocumentKey::FromPathString("path/to/the/doc"); + FieldValue fields = FieldValue::ObjectValueFromMap({ + {"foo", FieldValue::StringValue("bar")}, + {"two", FieldValue::IntegerValue(2)}, + {"nested", FieldValue::ObjectValueFromMap({ + {"fourty-two", FieldValue::IntegerValue(42)}, + })}, + }); + SnapshotVersion update_time = SnapshotVersion{{1234, 5678}}; + + google::firestore::v1beta1::Value inner_proto; + google::protobuf::Map& + inner_fields = *inner_proto.mutable_map_value()->mutable_fields(); + inner_fields["fourty-two"] = ValueProto(int64_t{42}); + + google::firestore::v1beta1::BatchGetDocumentsResponse proto; + google::firestore::v1beta1::Document* doc_proto = proto.mutable_found(); + doc_proto->set_name(serializer.EncodeKey(key)); + google::protobuf::Map& m = + *doc_proto->mutable_fields(); + m["foo"] = ValueProto("bar"); + m["two"] = ValueProto(int64_t{2}); + m["nested"] = inner_proto; + + google::protobuf::Timestamp* update_time_proto = + doc_proto->mutable_update_time(); + update_time_proto->set_seconds(1234); + update_time_proto->set_nanos(5678); + + TouchIgnoredBatchGetDocumentsResponseFields(&proto); + + ExpectRoundTrip(key, fields, update_time, proto); +} + // TODO(rsgowman): Test [en|de]coding multiple protos into the same output // vector. -- cgit v1.2.3