aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/test
diff options
context:
space:
mode:
authorGravatar rsgowman <rgowman@google.com>2018-05-04 15:21:22 -0400
committerGravatar GitHub <noreply@github.com>2018-05-04 15:21:22 -0400
commite12b997c6147663c692233a7e29a7433f4aee6f6 (patch)
treec19fe24c70e057fac00de00ebe5c63214972d72d /Firestore/core/test
parentadc514a894b819c0a63e85c083b766907b68e6ea (diff)
[De]serialize DocumentKeys via the remote Serializer (#1212)
Diffstat (limited to 'Firestore/core/test')
-rw-r--r--Firestore/core/test/firebase/firestore/remote/serializer_test.cc42
1 files changed, 41 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 ecfbf32..1dded05 100644
--- a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc
+++ b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc
@@ -37,14 +37,17 @@
#include "Firestore/core/src/firebase/firestore/model/field_value.h"
#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 "google/protobuf/stubs/common.h"
#include "google/protobuf/util/message_differencer.h"
#include "gtest/gtest.h"
using firebase::firestore::FirestoreErrorCode;
+using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::FieldValue;
using firebase::firestore::model::ObjectValue;
using firebase::firestore::remote::Serializer;
+using firebase::firestore::testutil::Key;
using firebase::firestore::util::Status;
using firebase::firestore::util::StatusOr;
using google::protobuf::util::MessageDifferencer;
@@ -65,8 +68,10 @@ TEST(Serializer, CanLinkToNanopb) {
// Fixture for running serializer tests.
class SerializerTest : public ::testing::Test {
public:
- SerializerTest() : serializer(/*DatabaseId("p", "d")*/) {
+ SerializerTest() : serializer(kDatabaseId) {
}
+
+ const DatabaseId kDatabaseId{"p", "d"};
Serializer serializer;
void ExpectRoundTrip(const FieldValue& model,
@@ -425,5 +430,40 @@ TEST_F(SerializerTest, IncompleteTag) {
Status(FirestoreErrorCode::DataLoss, "ignored"), bytes);
}
+TEST_F(SerializerTest, EncodesKey) {
+ EXPECT_EQ("projects/p/databases/d/documents", serializer.EncodeKey(Key("")));
+ EXPECT_EQ("projects/p/databases/d/documents/one/two/three/four",
+ serializer.EncodeKey(Key("one/two/three/four")));
+}
+
+TEST_F(SerializerTest, DecodesKey) {
+ EXPECT_EQ(Key(""), serializer.DecodeKey("projects/p/databases/d/documents"));
+ EXPECT_EQ(Key("one/two/three/four"),
+ serializer.DecodeKey(
+ "projects/p/databases/d/documents/one/two/three/four"));
+ // Same, but with a leading slash
+ EXPECT_EQ(Key("one/two/three/four"),
+ serializer.DecodeKey(
+ "/projects/p/databases/d/documents/one/two/three/four"));
+}
+
+TEST_F(SerializerTest, BadKey) {
+ std::vector<std::string> bad_cases{
+ "", // empty (and too short)
+ "projects/p", // too short
+ "projects/p/databases/d", // too short
+ "projects/p/databases/d/documents/odd_number_of_local_elements",
+ "projects_spelled_wrong/p/databases/d/documents",
+ "projects/p/databases_spelled_wrong/d/documents",
+ "projects/not_project_p/databases/d/documents",
+ "projects/p/databases/not_database_d/documents",
+ "projects/p/databases/d/not_documents",
+ };
+
+ for (const std::string& bad_key : bad_cases) {
+ EXPECT_ANY_THROW(serializer.DecodeKey(bad_key));
+ }
+}
+
// TODO(rsgowman): Test [en|de]coding multiple protos into the same output
// vector.