diff options
author | rsgowman <rgowman@google.com> | 2018-02-13 17:46:09 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-13 17:46:09 -0500 |
commit | fe19fca0e521e3765e4ecf6a87b0a3d622a52b92 (patch) | |
tree | 24a4485215689063c94d40c764e08f9dee4758ca /Firestore/core/test | |
parent | 95d0411e207e3eea64c035258745021ae20b676a (diff) |
Serialize and deserialize null (#783)
* Build (grpc's) nanopb with -DPB_FIELD_16BIT
We require (at least) 16 bit fields. (By default, nanopb uses 8 bit
fields, ie allowing up to 256 field tags.)
Also note that this patch adds this to grpc's nanopb, rather than to our
nanopb. We'll need to eventually either:
a) we instruct grpc to use our nanopb
b) we rely on grpc's nanopb instead of using our own.
(^ marked as a TODO for now.)
* Add some dependant protos
Imported from protobuf. Nanopb requires these to be present (though
anything using libprotobuf does not, as these are already built into
that.)
* Add generated nanopb protos based off of newly added proto definitions
* Build the nanopb protos
* Serialize and deserialize null
Diffstat (limited to 'Firestore/core/test')
-rw-r--r-- | Firestore/core/test/firebase/firestore/remote/serializer_test.cc | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc index 1be5a87..35f417e 100644 --- a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc +++ b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc @@ -16,8 +16,15 @@ #include "Firestore/core/src/firebase/firestore/remote/serializer.h" -#include <gtest/gtest.h> +#include <pb.h> #include <pb_encode.h> +#include <vector> + +#include "Firestore/core/src/firebase/firestore/model/field_value.h" +#include "gtest/gtest.h" + +using firebase::firestore::model::FieldValue; +using firebase::firestore::remote::Serializer; TEST(Serializer, CanLinkToNanopb) { // This test doesn't actually do anything interesting as far as actually using @@ -26,3 +33,65 @@ TEST(Serializer, CanLinkToNanopb) { // the test. pb_ostream_from_buffer(NULL, 0); } + +// Fixture for running serializer tests. +class SerializerTest : public ::testing::Test { + public: + SerializerTest() : serializer(/*DatabaseId("p", "d")*/) { + } + Serializer serializer; + + void ExpectRoundTrip(const FieldValue& model, + const Serializer::TypedValue& proto, + FieldValue::Type type) { + EXPECT_EQ(type, model.type()); + EXPECT_EQ(type, proto.type); + Serializer::TypedValue actual_proto = serializer.EncodeFieldValue(model); + EXPECT_EQ(type, actual_proto.type); + EXPECT_EQ(proto, actual_proto); + EXPECT_EQ(model, serializer.DecodeFieldValue(proto)); + } + + void ExpectRoundTrip(const Serializer::TypedValue& proto, + std::vector<uint8_t> bytes, + FieldValue::Type type) { + EXPECT_EQ(type, proto.type); + std::vector<uint8_t> actual_bytes; + Serializer::EncodeTypedValue(proto, &actual_bytes); + EXPECT_EQ(bytes, actual_bytes); + Serializer::TypedValue actual_proto = Serializer::DecodeTypedValue(bytes); + EXPECT_EQ(type, actual_proto.type); + EXPECT_EQ(proto, actual_proto); + } +}; + +TEST_F(SerializerTest, EncodesNullModelToProto) { + FieldValue model = FieldValue::NullValue(); + Serializer::TypedValue proto{FieldValue::Type::Null, + google_firestore_v1beta1_Value_init_default}; + // sanity check (the _init_default above should set this to _NULL_VALUE) + EXPECT_EQ(google_protobuf_NullValue_NULL_VALUE, proto.value.null_value); + ExpectRoundTrip(model, proto, FieldValue::Type::Null); +} + +TEST_F(SerializerTest, EncodesNullProtoToBytes) { + Serializer::TypedValue proto{FieldValue::Type::Null, + google_firestore_v1beta1_Value_init_default}; + // sanity check (the _init_default above should set this to _NULL_VALUE) + EXPECT_EQ(google_protobuf_NullValue_NULL_VALUE, proto.value.null_value); + + /* NB: proto bytes were created via: + echo 'null_value: NULL_VALUE' \ + | ./build/external/protobuf/src/protobuf-build/src/protoc \ + -I./Firestore/Protos/protos \ + -I./build/external/protobuf/src/protobuf/src/ \ + --encode=google.firestore.v1beta1.Value \ + google/firestore/v1beta1/document.proto \ + > output.bin + */ + std::vector<uint8_t> bytes{0x58, 0x00}; + ExpectRoundTrip(proto, bytes, FieldValue::Type::Null); +} + +// TODO(rsgowman): Test [en|de]coding multiple protos into the same output +// vector. |