aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/test/firebase/firestore/model/document_key_test.cc
blob: 619ee7ff59f4a8f0fc6e9621956ddf62eb7763dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * 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 <initializer_list>
#include <string>
#include <utility>
#include <vector>

#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
#include "gtest/gtest.h"

namespace firebase {
namespace firestore {
namespace model {

TEST(DocumentKey, Constructor_Empty) {
  const DocumentKey default_key;
  EXPECT_TRUE(default_key.path().empty());

  const auto& empty_key = DocumentKey::Empty();
  const auto& another_empty_key = DocumentKey::Empty();
  EXPECT_EQ(default_key, empty_key);
  EXPECT_EQ(empty_key, another_empty_key);
  EXPECT_EQ(&empty_key, &another_empty_key);
}

TEST(DocumentKey, Constructor_FromPath) {
  ResourcePath path{"rooms", "firestore", "messages", "1"};
  const DocumentKey key_from_path_copy{path};
  // path shouldn't have been moved from.
  EXPECT_FALSE(path.empty());
  EXPECT_EQ(key_from_path_copy.path(), path);

  const DocumentKey key_from_moved_path{std::move(path)};
  EXPECT_TRUE(path.empty());  // NOLINT: use after move intended
  EXPECT_FALSE(key_from_moved_path.path().empty());
  EXPECT_EQ(key_from_path_copy.path(), key_from_moved_path.path());
}

TEST(DocumentKey, CopyAndMove) {
  DocumentKey key({"rooms", "firestore", "messages", "1"});
  const std::string path_string = "rooms/firestore/messages/1";
  EXPECT_EQ(path_string, key.path().CanonicalString());

  DocumentKey copied = key;
  EXPECT_EQ(path_string, copied.path().CanonicalString());
  EXPECT_EQ(key, copied);

  const DocumentKey moved = std::move(key);
  EXPECT_EQ(path_string, moved.path().CanonicalString());
  EXPECT_NE(key, moved);  // NOLINT: use after move intended
  EXPECT_TRUE(key.path().empty());

  // Reassignment.

  key = copied;
  EXPECT_EQ(copied, key);
  EXPECT_EQ(path_string, key.path().CanonicalString());

  key = {};
  EXPECT_TRUE(key.path().empty());
  key = std::move(copied);
  EXPECT_NE(copied, key);  // NOLINT: use after move intended
  EXPECT_TRUE(copied.path().empty());
  EXPECT_EQ(path_string, key.path().CanonicalString());
}

TEST(DocumentKey, Constructor_StaticFactory) {
  const auto key_from_segments =
      DocumentKey::FromSegments({"rooms", "firestore", "messages", "1"});
  const std::string path_string = "rooms/firestore/messages/1";
  const auto key_from_string = DocumentKey::FromPathString(path_string);
  EXPECT_EQ(path_string, key_from_string.path().CanonicalString());
  EXPECT_EQ(path_string, key_from_segments.path().CanonicalString());
  EXPECT_EQ(key_from_segments, key_from_string);

  const auto from_empty_path = DocumentKey::FromPathString("");
  EXPECT_EQ(from_empty_path, DocumentKey{});
}

TEST(DocumentKey, Constructor_BadArguments) {
  ASSERT_ANY_THROW(DocumentKey(ResourcePath{"foo"}));
  ASSERT_ANY_THROW(DocumentKey(ResourcePath{"foo", "bar", "baz"}));

  ASSERT_ANY_THROW(DocumentKey::FromSegments({"foo"}));
  ASSERT_ANY_THROW(DocumentKey::FromSegments({"foo", "bar", "baz"}));

  ASSERT_ANY_THROW(DocumentKey::FromPathString("invalid"));
  ASSERT_ANY_THROW(DocumentKey::FromPathString("invalid//string"));
  ASSERT_ANY_THROW(DocumentKey::FromPathString("invalid/key/path"));
}

TEST(DocumentKey, IsDocumentKey) {
  EXPECT_TRUE(DocumentKey::IsDocumentKey({}));
  EXPECT_FALSE(DocumentKey::IsDocumentKey({"foo"}));
  EXPECT_TRUE(DocumentKey::IsDocumentKey({"foo", "bar"}));
  EXPECT_FALSE(DocumentKey::IsDocumentKey({"foo", "bar", "baz"}));
}

TEST(DocumentKey, Comparison) {
  const DocumentKey abcd({"a", "b", "c", "d"});
  const DocumentKey abcd_too({"a", "b", "c", "d"});
  const DocumentKey xyzw({"x", "y", "z", "w"});
  EXPECT_EQ(abcd, abcd_too);
  EXPECT_NE(abcd, xyzw);

  const DocumentKey empty;
  const DocumentKey a({"a", "a"});
  const DocumentKey b({"b", "b"});
  const DocumentKey ab({"a", "a", "b", "b"});

  EXPECT_FALSE(empty < empty);
  EXPECT_TRUE(empty <= empty);
  EXPECT_TRUE(empty < a);
  EXPECT_TRUE(empty <= a);
  EXPECT_TRUE(a > empty);
  EXPECT_TRUE(a >= empty);

  EXPECT_FALSE(a < a);
  EXPECT_TRUE(a <= a);
  EXPECT_FALSE(a > a);
  EXPECT_TRUE(a >= a);
  EXPECT_TRUE(a == a);
  EXPECT_FALSE(a != a);

  EXPECT_TRUE(a < b);
  EXPECT_TRUE(a <= b);
  EXPECT_TRUE(b > a);
  EXPECT_TRUE(b >= a);

  EXPECT_TRUE(a < ab);
  EXPECT_TRUE(a <= ab);
  EXPECT_TRUE(ab > a);
  EXPECT_TRUE(ab >= a);
}

}  // namespace model
}  // namespace firestore
}  // namespace firebase