• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 
17 #include <condition_variable>
18 #include <vector>
19 
20 #include "dev_manager.h"
21 #include "store_manager.h"
22 #include "distributed_kv_data_manager.h"
23 #include "types.h"
24 
25 namespace OHOS::Test {
26 using namespace testing::ext;
27 using namespace OHOS::DistributedKv;
28 class SingleStoreImplGetTopTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31 
32     static void TearDownTestCase(void);
33 
34     void SetUp();
35 
36     void TearDown();
37 
38     static std::shared_ptr<SingleKvStore> singleKvStore; // declare kvstore instance.
39     static Status initStatus;
40 };
41 
42 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
43                                                "\"SCHEMA_MODE\":\"STRICT\","
44                                                "\"SCHEMA_SKIPSIZE\":0,"
45                                                "\"SCHEMA_DEFINE\":{"
46                                                "\"age\":\"INTEGER, NOT NULL\""
47                                                "},"
48                                                "\"SCHEMA_INDEXES\":[\"$.age\"]}";
49 
50 std::shared_ptr<SingleKvStore> SingleStoreImplGetTopTest::singleKvStore = nullptr;
51 Status SingleStoreImplGetTopTest::initStatus = Status::ERROR;
52 
SetUpTestCase(void)53 void SingleStoreImplGetTopTest::SetUpTestCase(void)
54 {
55     DistributedKvDataManager manager;
56     Options options = { .createIfMissing = true, .encrypt = false, .autoSync = true,
57         .kvStoreType = KvStoreType::SINGLE_VERSION };
58     options.area = EL1;
59     options.baseDir = std::string("/data/service/el1/public/database/odmf");
60     AppId appId = { "odmf" };
61     StoreId storeId = { "test_single" }; // define kvstore(database) name.
62     mkdir(options.baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
63     // [create and] open and initialize kvstore instance.
64     initStatus = manager.GetSingleKvStore(options, appId, storeId, singleKvStore);
65 }
66 
TearDownTestCase(void)67 void SingleStoreImplGetTopTest::TearDownTestCase(void)
68 {
69     (void)remove("/data/service/el1/public/database/odmf/key");
70     (void)remove("/data/service/el1/public/database/odmf/kvdb");
71     (void)remove("/data/service/el1/public/database/odmf");
72 }
73 
SetUp(void)74 void SingleStoreImplGetTopTest::SetUp(void)
75 {}
76 
TearDown(void)77 void SingleStoreImplGetTopTest::TearDown(void)
78 {}
79 
80 /**
81 * @tc.name: GetEntries
82 * @tc.desc: get entries order by write time Asc
83 * @tc.type: FUNC
84 * @tc.require:I5OM83
85 * @tc.author:ht
86 */
87 HWTEST_F(SingleStoreImplGetTopTest, GetEntriesOrderByWriteTimeAsc, TestSize.Level0)
88 {
89     ASSERT_NE(singleKvStore, nullptr);
90     std::vector<Entry> input;
91     for (size_t i = 10; i < 30; ++i) {
92         Entry entry;
93         entry.key = std::to_string(i).append("_k");
94         entry.value = std::to_string(i).append("_v");
95         input.push_back(entry);
96         auto status =singleKvStore->Put(
97             entry.key, entry.value);
98         ASSERT_EQ(status, SUCCESS);
99     }
100     DataQuery query;
101     query.KeyPrefix("1");
102     query.OrderByWriteTime(true);
103     std::vector<Entry> output;
104     auto status = singleKvStore->GetEntries(query, output);
105     ASSERT_EQ(status, SUCCESS);
106     ASSERT_EQ(output.size(), 10);
107     for (size_t i = 0; i < output.size(); ++i) {
108         ASSERT_TRUE(input[i].key == output[i].key);
109         ASSERT_TRUE(input[i].value == output[i].value);
110     }
111 }
112 
113 /**
114 * @tc.name: GetEntries
115 * @tc.desc: get entries order by write time Desc
116 * @tc.type: FUNC
117 * @tc.require:I5OM83
118 * @tc.author:ht
119 */
120 HWTEST_F(SingleStoreImplGetTopTest, GetEntriesOrderByWriteTimeDesc, TestSize.Level0)
121 {
122     ASSERT_NE(singleKvStore, nullptr);
123     std::vector<Entry> input;
124     for (size_t i = 10; i < 30; ++i) {
125         Entry entry;
126         entry.key = std::to_string(i).append("_k");
127         entry.value = std::to_string(i).append("_v");
128         input.push_back(entry);
129         auto status =singleKvStore->Put(
130             entry.key, entry.value);
131         ASSERT_EQ(status, SUCCESS);
132     }
133     DataQuery query;
134     query.KeyPrefix("1");
135     query.OrderByWriteTime(false);
136     std::vector<Entry> output;
137     auto status = singleKvStore->GetEntries(query, output);
138     ASSERT_EQ(status, SUCCESS);
139     ASSERT_EQ(output.size(), 10);
140     for (size_t i = 0; i < output.size(); ++i) {
141         ASSERT_TRUE(input[9-i].key == output[i].key);
142         ASSERT_TRUE(input[9-i].value == output[i].value);
143     }
144 }
145 
146 /**
147 * @tc.name: GetEntries
148 * @tc.desc: get entries order by write time no prefix
149 * @tc.type: FUNC
150 * @tc.require:I5OM83
151 * @tc.author:ht
152 */
153 HWTEST_F(SingleStoreImplGetTopTest, GetEntriesOrderByWriteTimeNoPrefix, TestSize.Level0)
154 {
155     ASSERT_NE(singleKvStore, nullptr);
156     std::vector<Entry> input;
157     for (size_t i = 10; i < 30; ++i) {
158         Entry entry;
159         entry.key = std::to_string(i).append("_k");
160         entry.value = std::to_string(i).append("_v");
161         input.push_back(entry);
162         auto status =singleKvStore->Put(
163             entry.key, entry.value);
164         ASSERT_EQ(status, SUCCESS);
165     }
166     singleKvStore->Put("test_key_1", "{\"name\":1}");
167     DataQuery query;
168     query.OrderByWriteTime(true);
169     query.EqualTo("$.name",1);
170     std::vector<Entry> output;
171     auto status = singleKvStore->GetEntries(query, output);
172     ASSERT_EQ(status, NOT_SUPPORT);
173     ASSERT_EQ(output.size(), 0);
174 }
175 
176 /**
177 * @tc.name: GetResultSet
178 * @tc.desc: get result set order by write time Asc
179 * @tc.type: FUNC
180 * @tc.require:I5OM83
181 * @tc.author:ht
182 */
183 HWTEST_F(SingleStoreImplGetTopTest, GetResultSetOrderByWriteTimeAsc, TestSize.Level0)
184 {
185     ASSERT_NE(singleKvStore, nullptr);
186     std::vector<Entry> input;
187     for (size_t i = 10; i < 30; ++i) {
188         Entry entry;
189         entry.key = std::to_string(i).append("_k");
190         entry.value = std::to_string(i).append("_v");
191         input.push_back(entry);
192         auto status =singleKvStore->Put(
193             entry.key, entry.value);
194         ASSERT_EQ(status, SUCCESS);
195     }
196     DataQuery query;
197     query.InKeys({"10_k", "11_k"});
198     query.OrderByWriteTime(true);
199     std::shared_ptr<KvStoreResultSet> output;
200     auto status = singleKvStore->GetResultSet(query, output);
201     ASSERT_EQ(status, SUCCESS);
202     ASSERT_NE(output, nullptr);
203     ASSERT_EQ(output->GetCount(), 2);
204     for (size_t i = 0; i < 2; ++i) {
205         output->MoveToNext();
206         Entry entry;
207         status=output->GetEntry(entry);
208         ASSERT_EQ(status, SUCCESS);
209         ASSERT_TRUE(input[i].key == entry.key);
210         ASSERT_TRUE(input[i].value == entry.value);
211     }
212 }
213 
214 /**
215 * @tc.name: GetResultSet
216 * @tc.desc: get result set order by write time Desc
217 * @tc.type: FUNC
218 * @tc.require:I5OM83
219 * @tc.author:ht
220 */
221 HWTEST_F(SingleStoreImplGetTopTest, GetResultSetOrderByWriteTimeDesc, TestSize.Level0)
222 {
223     ASSERT_NE(singleKvStore, nullptr);
224     std::vector<Entry> input;
__anona794e2090102(const Key &entry, const Key &sentry) 225     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
226     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
227     for (size_t i = 10; i < 30; ++i) {
228         Entry entry;
229         entry.key = std::to_string(i).append("_k");
230         entry.value = std::to_string(i).append("_v");
231         input.push_back(entry);
232         dictionary[entry.key] = entry.value;
233         auto status =singleKvStore->Put(
234             entry.key, entry.value);
235         ASSERT_EQ(status, SUCCESS);
236     }
237     DataQuery query;
238     query.KeyPrefix("1");
239     query.OrderByWriteTime(false);
240     std::shared_ptr<KvStoreResultSet> output;
241     auto status = singleKvStore->GetResultSet(query, output);
242     ASSERT_EQ(status, SUCCESS);
243     ASSERT_NE(output, nullptr);
244     ASSERT_EQ(output->GetCount(), 10);
245     for (size_t i = 0; i < 10; ++i) {
246         output->MoveToNext();
247         Entry entry;
248         output->GetEntry(entry);
249         ASSERT_TRUE(input[9-i].key == entry.key);
250         ASSERT_TRUE(input[9-i].value == entry.value);
251     }
252 }
253 
254 /**
255 * @tc.name: GetResultSet
256 * @tc.desc: get result set order by write time no prefix
257 * @tc.type: FUNC
258 * @tc.require:I5OM83
259 * @tc.author:ht
260 */
261 HWTEST_F(SingleStoreImplGetTopTest, GetResultSetOrderByWriteTimeNoPrefix, TestSize.Level0)
262 {
263     ASSERT_NE(singleKvStore, nullptr);
264     std::vector<Entry> input;
__anona794e2090202(const Key &entry, const Key &sentry) 265     auto cmp = [](const Key &entry, const Key &sentry) { return entry.Data() < sentry.Data(); };
266     std::map<Key, Value, decltype(cmp)> dictionary(cmp);
267     for (size_t i = 10; i < 30; ++i) {
268         Entry entry;
269         entry.key = std::to_string(i).append("_k");
270         entry.value = std::to_string(i).append("_v");
271         input.push_back(entry);
272         dictionary[entry.key] = entry.value;
273         auto status =singleKvStore->Put(
274             entry.key, entry.value);
275         ASSERT_EQ(status, SUCCESS);
276     }
277     singleKvStore->Put("test_key_1", "{\"name\":1}");
278     DataQuery query;
279     query.OrderByWriteTime(true);
280     query.EqualTo("$.name", 1);
281     std::shared_ptr<KvStoreResultSet> output;
282     auto status = singleKvStore->GetResultSet(query, output);
283     ASSERT_EQ(status, NOT_SUPPORT);
284     ASSERT_EQ(output, nullptr);
285 }
286 } // namespace OHOS::Test