forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_document_test.cc
170 lines (142 loc) · 6.03 KB
/
json_document_test.cc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include <set>
#include "rocksdb/utilities/json_document.h"
#include "util/testutil.h"
#include "util/testharness.h"
namespace rocksdb {
namespace {
void AssertField(const JSONDocument& json, const std::string& field) {
ASSERT_TRUE(json.Contains(field));
ASSERT_TRUE(json[field].IsNull());
}
void AssertField(const JSONDocument& json, const std::string& field,
const std::string& expected) {
ASSERT_TRUE(json.Contains(field));
ASSERT_TRUE(json[field].IsString());
ASSERT_EQ(expected, json[field].GetString());
}
void AssertField(const JSONDocument& json, const std::string& field,
int64_t expected) {
ASSERT_TRUE(json.Contains(field));
ASSERT_TRUE(json[field].IsInt64());
ASSERT_EQ(expected, json[field].GetInt64());
}
void AssertField(const JSONDocument& json, const std::string& field,
bool expected) {
ASSERT_TRUE(json.Contains(field));
ASSERT_TRUE(json[field].IsBool());
ASSERT_EQ(expected, json[field].GetBool());
}
void AssertField(const JSONDocument& json, const std::string& field,
double expected) {
ASSERT_TRUE(json.Contains(field));
ASSERT_TRUE(json[field].IsDouble());
ASSERT_EQ(expected, json[field].GetDouble());
}
} // namespace
class JSONDocumentTest {
public:
void AssertSampleJSON(const JSONDocument& json) {
AssertField(json, "title", std::string("json"));
AssertField(json, "type", std::string("object"));
// properties
ASSERT_TRUE(json.Contains("properties"));
ASSERT_TRUE(json["properties"].Contains("flags"));
ASSERT_TRUE(json["properties"]["flags"].IsArray());
ASSERT_EQ(3u, json["properties"]["flags"].Count());
ASSERT_TRUE(json["properties"]["flags"][0].IsInt64());
ASSERT_EQ(10, json["properties"]["flags"][0].GetInt64());
ASSERT_TRUE(json["properties"]["flags"][1].IsString());
ASSERT_EQ("parse", json["properties"]["flags"][1].GetString());
ASSERT_TRUE(json["properties"]["flags"][2].IsObject());
AssertField(json["properties"]["flags"][2], "tag", std::string("no"));
AssertField(json["properties"]["flags"][2], std::string("status"));
AssertField(json["properties"], "age", 110.5e-4);
AssertField(json["properties"], "depth", static_cast<int64_t>(-10));
// test iteration
std::set<std::string> expected({"flags", "age", "depth"});
for (auto item : json["properties"].Items()) {
auto iter = expected.find(item.first);
ASSERT_TRUE(iter != expected.end());
expected.erase(iter);
}
ASSERT_EQ(0U, expected.size());
ASSERT_TRUE(json.Contains("latlong"));
ASSERT_TRUE(json["latlong"].IsArray());
ASSERT_EQ(2u, json["latlong"].Count());
ASSERT_TRUE(json["latlong"][0].IsDouble());
ASSERT_EQ(53.25, json["latlong"][0].GetDouble());
ASSERT_TRUE(json["latlong"][1].IsDouble());
ASSERT_EQ(43.75, json["latlong"][1].GetDouble());
AssertField(json, "enabled", true);
}
const std::string kSampleJSON =
"{ \"title\" : \"json\", \"type\" : \"object\", \"properties\" : { "
"\"flags\": [10, \"parse\", {\"tag\": \"no\", \"status\": null}], "
"\"age\": 110.5e-4, \"depth\": -10 }, \"latlong\": [53.25, 43.75], "
"\"enabled\": true }";
const std::string kSampleJSONDifferent =
"{ \"title\" : \"json\", \"type\" : \"object\", \"properties\" : { "
"\"flags\": [10, \"parse\", {\"tag\": \"no\", \"status\": 2}], "
"\"age\": 110.5e-4, \"depth\": -10 }, \"latlong\": [53.25, 43.75], "
"\"enabled\": true }";
};
TEST(JSONDocumentTest, Parsing) {
JSONDocument x(static_cast<int64_t>(5));
ASSERT_TRUE(x.IsInt64());
// make sure it's correctly parsed
auto parsed_json = JSONDocument::ParseJSON(kSampleJSON.c_str());
ASSERT_TRUE(parsed_json != nullptr);
AssertSampleJSON(*parsed_json);
// test deep copying
JSONDocument copied_json_document(*parsed_json);
AssertSampleJSON(copied_json_document);
ASSERT_TRUE(copied_json_document == *parsed_json);
delete parsed_json;
auto parsed_different_sample =
JSONDocument::ParseJSON(kSampleJSONDifferent.c_str());
ASSERT_TRUE(parsed_different_sample != nullptr);
ASSERT_TRUE(!(*parsed_different_sample == copied_json_document));
delete parsed_different_sample;
// parse error
const std::string kFaultyJSON =
kSampleJSON.substr(0, kSampleJSON.size() - 10);
ASSERT_TRUE(JSONDocument::ParseJSON(kFaultyJSON.c_str()) == nullptr);
}
TEST(JSONDocumentTest, Serialization) {
auto parsed_json = JSONDocument::ParseJSON(kSampleJSON.c_str());
ASSERT_TRUE(parsed_json != nullptr);
std::string serialized;
parsed_json->Serialize(&serialized);
delete parsed_json;
auto deserialized_json = JSONDocument::Deserialize(Slice(serialized));
ASSERT_TRUE(deserialized_json != nullptr);
AssertSampleJSON(*deserialized_json);
delete deserialized_json;
// deserialization failure
ASSERT_TRUE(JSONDocument::Deserialize(
Slice(serialized.data(), serialized.size() - 10)) == nullptr);
}
TEST(JSONDocumentTest, Mutation) {
auto sample_json = JSONDocument::ParseJSON(kSampleJSON.c_str());
ASSERT_TRUE(sample_json != nullptr);
auto different_json = JSONDocument::ParseJSON(kSampleJSONDifferent.c_str());
ASSERT_TRUE(different_json != nullptr);
(*different_json)["properties"]["flags"][2].Set("status", JSONDocument());
ASSERT_TRUE(*different_json == *sample_json);
delete different_json;
delete sample_json;
auto json1 = JSONDocument::ParseJSON("{\"a\": [1, 2, 3]}");
auto json2 = JSONDocument::ParseJSON("{\"a\": [2, 2, 3, 4]}");
ASSERT_TRUE(json1 != nullptr && json2 != nullptr);
(*json1)["a"].SetInArray(0, static_cast<int64_t>(2))->PushBack(
static_cast<int64_t>(4));
ASSERT_TRUE(*json1 == *json2);
delete json1;
delete json2;
}
} // namespace rocksdb
int main(int argc, char** argv) { return rocksdb::test::RunAllTests(); }