/Users/deen/code/yugabyte-db/src/yb/docdb/subdocument-test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <string> |
15 | | |
16 | | #include <gtest/gtest.h> |
17 | | |
18 | | #include "yb/docdb/subdocument.h" |
19 | | #include "yb/docdb/value_type.h" |
20 | | |
21 | | #include "yb/util/monotime.h" |
22 | | #include "yb/util/string_trim.h" |
23 | | #include "yb/util/test_macros.h" |
24 | | |
25 | | using std::string; |
26 | | |
27 | | namespace yb { |
28 | | namespace docdb { |
29 | | |
30 | | TEST(SubDocumentTest, TestGetOrAddChild) { |
31 | | SubDocument d; |
32 | | ASSERT_TRUE(d.GetOrAddChild(PrimitiveValue("foo")).second); |
33 | | ASSERT_FALSE(d.GetOrAddChild(PrimitiveValue("foo")).second); // No new subdocument created. |
34 | | ASSERT_TRUE(d.GetOrAddChild(PrimitiveValue("bar")).second); |
35 | | ASSERT_TRUE(d.GetOrAddChild(PrimitiveValue(100)).second); |
36 | | ASSERT_TRUE(d.GetOrAddChild(PrimitiveValue(200)).second); |
37 | | ASSERT_TRUE(d.GetOrAddChild(PrimitiveValue(string("\x00", 1))).second); |
38 | | ASSERT_FALSE(d.GetOrAddChild(PrimitiveValue(string("\x00", 1))).second); // No new subdoc added. |
39 | | ASSERT_STR_EQ_VERBOSE_TRIMMED(R"#( |
40 | | { |
41 | | 100: {}, |
42 | | 200: {}, |
43 | | "\x00": {}, |
44 | | "bar": {}, |
45 | | "foo": {} |
46 | | } |
47 | | )#", d.ToString()); |
48 | | } |
49 | | |
50 | | TEST(SubDocumentTest, TestToString) { |
51 | | SubDocument subdoc(ValueType::kObject); |
52 | | SubDocument mathematicians; |
53 | | SubDocument cs; |
54 | | mathematicians.SetChildPrimitive(PrimitiveValue("Isaac Newton"), PrimitiveValue(1643)); |
55 | | ASSERT_EQ(1, mathematicians.object_num_keys()); |
56 | | mathematicians.SetChildPrimitive(PrimitiveValue("Pythagoras"), PrimitiveValue(-570)); |
57 | | ASSERT_EQ(2, mathematicians.object_num_keys()); |
58 | | mathematicians.SetChildPrimitive(PrimitiveValue("Leonard Euler"), PrimitiveValue(1601)); |
59 | | ASSERT_EQ(3, mathematicians.object_num_keys()); |
60 | | mathematicians.SetChildPrimitive(PrimitiveValue("Blaise Pascal"), PrimitiveValue(1623)); |
61 | | ASSERT_EQ(4, mathematicians.object_num_keys()); |
62 | | mathematicians.SetChildPrimitive(PrimitiveValue("Srinivasa Ramanujan"), PrimitiveValue(1887)); |
63 | | ASSERT_EQ(5, mathematicians.object_num_keys()); |
64 | | mathematicians.SetChildPrimitive(PrimitiveValue("Euclid"), PrimitiveValue("Mid-4th century BCE")); |
65 | | ASSERT_EQ(6, mathematicians.object_num_keys()); |
66 | | |
67 | | cs.SetChildPrimitive(PrimitiveValue("Alan Turing"), PrimitiveValue(1912)); |
68 | | ASSERT_EQ(1, cs.object_num_keys()); |
69 | | cs.SetChildPrimitive(PrimitiveValue("Ada Lovelace"), PrimitiveValue(1815)); |
70 | | ASSERT_EQ(2, cs.object_num_keys()); |
71 | | cs.SetChildPrimitive(PrimitiveValue("Edsger W. Dijkstra"), PrimitiveValue(1930)); |
72 | | ASSERT_EQ(3, cs.object_num_keys()); |
73 | | cs.SetChildPrimitive(PrimitiveValue("John von Neumann"), PrimitiveValue(1903)); |
74 | | ASSERT_EQ(4, cs.object_num_keys()); |
75 | | cs.SetChildPrimitive(PrimitiveValue("Dennis Ritchie"), PrimitiveValue(1941)); |
76 | | ASSERT_EQ(5, cs.object_num_keys()); |
77 | | |
78 | | subdoc.SetChild(PrimitiveValue("Mathematicians"), std::move(mathematicians)); |
79 | | ASSERT_EQ(1, subdoc.object_num_keys()); |
80 | | subdoc.SetChild(PrimitiveValue("Computer Scientists"), std::move(cs)); |
81 | | ASSERT_EQ(2, subdoc.object_num_keys()); |
82 | | |
83 | | ASSERT_STR_EQ_VERBOSE_TRIMMED( |
84 | | R"#( |
85 | | { |
86 | | "Computer Scientists": { |
87 | | "Ada Lovelace": 1815, |
88 | | "Alan Turing": 1912, |
89 | | "Dennis Ritchie": 1941, |
90 | | "Edsger W. Dijkstra": 1930, |
91 | | "John von Neumann": 1903 |
92 | | }, |
93 | | "Mathematicians": { |
94 | | "Blaise Pascal": 1623, |
95 | | "Euclid": "Mid-4th century BCE", |
96 | | "Isaac Newton": 1643, |
97 | | "Leonard Euler": 1601, |
98 | | "Pythagoras": -570, |
99 | | "Srinivasa Ramanujan": 1887 |
100 | | } |
101 | | } |
102 | | )#", subdoc.ToString()); |
103 | | |
104 | | ASSERT_TRUE(subdoc.DeleteChild(PrimitiveValue("Mathematicians"))); |
105 | | ASSERT_EQ(1, subdoc.object_num_keys()); |
106 | | ASSERT_TRUE(subdoc.DeleteChild(PrimitiveValue("Computer Scientists"))); |
107 | | ASSERT_EQ(0, subdoc.object_num_keys()); |
108 | | } |
109 | | |
110 | 0 | TEST(SubDocumentTest, InitializerListConstructor) { |
111 | 0 | ASSERT_STR_EQ_VERBOSE_TRIMMED( |
112 | 0 | R"#( |
113 | 0 | { |
114 | 0 | "France": "Paris", |
115 | 0 | "Germany": "Berlin" |
116 | 0 | } |
117 | 0 | )#", SubDocument({{"France", "Paris"}, {"Germany", "Berlin"}}).ToString()); |
118 | |
|
119 | 0 | SubDocument d2({{"France", "Paris"}, {"Germany", "Berlin"}}); |
120 | 0 | ASSERT_STR_EQ_VERBOSE_TRIMMED( |
121 | 0 | R"#( |
122 | 0 | { |
123 | 0 | 1: 1, |
124 | 0 | 2: 4, |
125 | 0 | 3: 9, |
126 | 0 | 4: 16, |
127 | 0 | 5: 25, |
128 | 0 | 10: 100 |
129 | 0 | } |
130 | 0 | )#", SubDocument({{10, 100}, {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}}).ToString()); |
131 | 0 | } |
132 | | |
133 | | TEST(SubDocumentTest, Equality) { |
134 | | ASSERT_EQ(SubDocument({{1, 2}, {3, 4}}), SubDocument({{1, 2}, {3, 4}})); |
135 | | ASSERT_EQ(SubDocument({{1, 2}, {3, 4}}), SubDocument({{3, 4}, {1, 2}})); |
136 | | ASSERT_NE(SubDocument({{1, 2}, {3, 4}}), SubDocument({{1, 2}, {3, 5}})); |
137 | | ASSERT_NE(SubDocument({{1, 2}, {3, 4}}), SubDocument({{1, 2}, {5, 4}})); |
138 | | } |
139 | | |
140 | | TEST(SubDocumentTest, TestCopyMove) { |
141 | | // Try Copies. |
142 | | SubDocument s1(ValueType::kObject); |
143 | | s1.SetTtl(1000); |
144 | | s1.SetWriteTime(1000); |
145 | | SubDocument s2 = s1; |
146 | | ASSERT_EQ(s1, s2); |
147 | | ASSERT_EQ(s1.GetTtl(), s2.GetTtl()); |
148 | | ASSERT_EQ(s1.GetWriteTime(), s2.GetWriteTime()); |
149 | | |
150 | | SubDocument s3; |
151 | | s3 = s1; |
152 | | ASSERT_EQ(s1, s3); |
153 | | ASSERT_EQ(s1.GetTtl(), s3.GetTtl()); |
154 | | ASSERT_EQ(s1.GetWriteTime(), s3.GetWriteTime()); |
155 | | |
156 | | // Try Moves. |
157 | | SubDocument s4 = std::move(s1); |
158 | | ASSERT_EQ(s3, s4); |
159 | | ASSERT_EQ(s3.GetTtl(), s4.GetTtl()); |
160 | | ASSERT_EQ(s3.GetWriteTime(), s4.GetWriteTime()); |
161 | | ASSERT_EQ(ValueType::kNullLow, s1.value_type()); |
162 | | |
163 | | SubDocument s5; |
164 | | s5 = std::move(s2); |
165 | | ASSERT_EQ(s3, s5); |
166 | | ASSERT_EQ(s3.GetTtl(), s5.GetTtl()); |
167 | | ASSERT_EQ(s3.GetWriteTime(), s5.GetWriteTime()); |
168 | | ASSERT_EQ(ValueType::kNullLow, s2.value_type()); |
169 | | } |
170 | | |
171 | | } // namespace docdb |
172 | | } // namespace yb |