1 /*
2 * Copyright (c) 2023 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
16 #include <climits>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19
20 #include "doc_errno.h"
21 #include "documentdb_test_utils.h"
22 #include "grd_base/grd_db_api.h"
23 #include "grd_base/grd_error.h"
24 #include "grd_base/grd_resultset_api.h"
25 #include "grd_base/grd_type_export.h"
26 #include "grd_document/grd_document_api.h"
27 #include "grd_resultset_inner.h"
28 #include "grd_type_inner.h"
29 #include "rd_log_print.h"
30
31 using namespace testing::ext;
32 using namespace DocumentDBUnitTest;
33
34 namespace {
35 std::string g_path = "./document.db";
36 GRD_DB *g_db = nullptr;
37 constexpr const char *COLLECTION_NAME = "student";
38 constexpr const char *colName = "data_";
39 const int MAX_COLLECTION_NAME = 511;
40
41 const int MAX_ID_LENS = 899;
42 static const char *g_document1 = "{\"_id\" : \"1\", \"name\":\"doc1\",\"item\":\"journal\",\"personInfo\":\
43 {\"school\":\"AB\", \"age\" : 51}}";
44 static const char *g_document2 = "{\"_id\" : \"2\", \"name\":\"doc2\",\"item\": 1, \"personInfo\":\
45 [1, \"my string\", {\"school\":\"AB\", \"age\" : 51}, true, {\"school\":\"CD\", \"age\" : 15}, false]}";
46 static const char *g_document3 = "{\"_id\" : \"3\", \"name\":\"doc3\",\"item\":\"notebook\",\"personInfo\":\
47 [{\"school\":\"C\", \"age\" : 5}]}";
48 static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\"paper\",\"personInfo\":\
49 {\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}";
50 static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\
51 [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";
52 static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\
53 [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15},\
54 {\"school\":\"C\", \"teacher\" : \"moon\", \"age\" : 20}]}";
55
56 static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\
57 [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";
58 static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\
59 [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}";
60 static const char *g_document9 = "{\"_id\" : \"9\", \"name\":\"doc9\",\"item\": true}";
61 static const char *g_document10 = "{\"_id\" : \"10\", \"name\":\"doc10\", \"parent\" : \"kate\"}";
62 static const char *g_document11 = "{\"_id\" : \"11\", \"name\":\"doc11\", \"other\" : \"null\"}";
63 static const char *g_document12 = "{\"_id\" : \"12\", \"name\":\"doc12\",\"other\" : null}";
64 static const char *g_document13 = "{\"_id\" : \"13\", \"name\":\"doc13\",\"item\" : \"shoes\",\"personInfo\":\
65 {\"school\":\"AB\", \"age\" : 15}}";
66 static const char *g_document14 = "{\"_id\" : \"14\", \"name\":\"doc14\",\"item\" : true,\"personInfo\":\
67 [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 85}]}";
68 static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : "
69 "5}]}";
70 static const char *g_document16 = "{\"_id\" : \"16\", \"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\
71 {\"nested4\":\"ABC\", \"field2\":\"CCC\"}}}}";
72 static const char *g_document17 = "{\"_id\" : \"17\", \"name\":\"doc17\",\"personInfo\":\"oh,ok\"}";
73 static const char *g_document18 = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\
74 {\"school\":\"DD\", \"age\":66}, \"color\":\"blue\"}";
75 static const char *g_document19 = "{\"_id\" : \"19\", \"name\":\"doc19\",\"ITEM\" : true,\"PERSONINFO\":\
76 {\"school\":\"AB\", \"age\":15}}";
77 static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\
78 [{\"SCHOOL\":\"B\", \"AGE\":15}, {\"SCHOOL\":\"C\", \"AGE\":35}]}";
79 static const char *g_document23 = "{\"_id\" : \"23\", \"name\":\"doc22\",\"ITEM\" : "
80 "true,\"personInfo\":[{\"school\":\"b\", \"age\":15}, [{\"school\":\"doc23\"}, 10, "
81 "{\"school\":\"doc23\"}, true, {\"school\":\"y\"}], {\"school\":\"b\"}]}";
82 static std::vector<const char *> g_data = { g_document1, g_document2, g_document3, g_document4, g_document5,
83 g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13,
84 g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20, g_document23 };
85
InsertData(GRD_DB * g_db,const char * collectionName)86 static void InsertData(GRD_DB *g_db, const char *collectionName)
87 {
88 for (const auto &item : g_data) {
89 EXPECT_EQ(GRD_InsertDoc(g_db, collectionName, item, 0), GRD_OK);
90 }
91 }
92
CompareValue(const char * value,const char * targetValue)93 static void CompareValue(const char *value, const char *targetValue)
94 {
95 int errCode;
96 DocumentDB::JsonObject valueObj = DocumentDB::JsonObject::Parse(value, errCode);
97 EXPECT_EQ(errCode, DocumentDB::E_OK);
98 DocumentDB::JsonObject targetValueObj = DocumentDB::JsonObject::Parse(targetValue, errCode);
99 EXPECT_EQ(errCode, DocumentDB::E_OK);
100 EXPECT_EQ(valueObj.Print(), targetValueObj.Print());
101 }
102
103 class DocumentDBFindTest : public testing::Test {
104 public:
105 static void SetUpTestCase(void);
106 static void TearDownTestCase(void);
107 void SetUp();
108 void TearDown();
109 void InsertDoc(const char *collectionName, const char *document);
110 };
SetUpTestCase(void)111 void DocumentDBFindTest::SetUpTestCase(void)
112 {
113 int status = GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &g_db);
114 EXPECT_EQ(status, GRD_OK);
115 EXPECT_EQ(GRD_CreateCollection(g_db, COLLECTION_NAME, "", 0), GRD_OK);
116 EXPECT_NE(g_db, nullptr);
117 }
118
TearDownTestCase(void)119 void DocumentDBFindTest::TearDownTestCase(void)
120 {
121 EXPECT_EQ(GRD_DBClose(g_db, 0), GRD_OK);
122 DocumentDBTestUtils::RemoveTestDbFiles(g_path);
123 }
124
SetUp(void)125 void DocumentDBFindTest::SetUp(void)
126 {
127 EXPECT_EQ(GRD_DropCollection(g_db, COLLECTION_NAME, 0), GRD_OK);
128 EXPECT_EQ(GRD_CreateCollection(g_db, COLLECTION_NAME, "", 0), GRD_OK);
129 InsertData(g_db, "student");
130 }
131
TearDown(void)132 void DocumentDBFindTest::TearDown(void) {}
133
134 /**
135 * @tc.name: DocumentDBFindTest001
136 * @tc.desc: Test Insert document db
137 * @tc.type: FUNC
138 * @tc.require:
139 * @tc.author: mazhao
140 */
141 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest001, TestSize.Level1)
142 {
143 /**
144 * @tc.steps: step1. Create filter with _id and get the record according to filter condition.
145 * @tc.expected: step1. Succeed to get the record, the matching record is g_document6.
146 */
147 const char *filter = "{\"_id\" : \"6\"}";
148 GRD_ResultSet *resultSet = nullptr;
149 Query query = { filter, "{}" };
150 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
151 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
152 char *value = nullptr;
153 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
154 CompareValue(value, g_document6);
155 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
156 /**
157 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
158 * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA.
159 */
160 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
161 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
162 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
163 }
164
165 /**
166 * @tc.name: DocumentDBFindTest002
167 * @tc.desc: Test filter with multiple fields and _id.
168 * @tc.type: FUNC
169 * @tc.require:
170 * @tc.author: mazhao
171 */
172 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest002, TestSize.Level1)
173 {
174 /**
175 * @tc.steps: step1. Create filter with multiple and _id. and get the record according to filter condition.
176 * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS,
177 * GRD_GetValue return GRD_NOT_AVAILABLE and GRD_Next return GRD_NO_DATA.
178 */
179 const char *filter = "{\"_id\" : \"6\", \"name\":\"doc6\"}";
180 GRD_ResultSet *resultSet = nullptr;
181 Query query = { filter, "{}" };
182 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
183 /**
184 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
185 * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS.
186 */
187 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
188 char *value = nullptr;
189 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
190 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
191 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
192 }
193
194 /**
195 * @tc.name: DocumentDBFindTest004
196 * @tc.desc: test filter with string filter without _id.
197 * @tc.type: FUNC
198 * @tc.require:
199 * @tc.author: mazhao
200 */
201 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest004, TestSize.Level1)
202 {
203 /**
204 * @tc.steps: step1. Create filter without _id and get the record according to filter condition.
205 * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS,
206 */
207 const char *filter = "{\"name\":\"doc6\"}";
208 GRD_ResultSet *resultSet = nullptr;
209 Query query = { filter, "{}" };
210 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
211
212 /**
213 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
214 * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS.
215 */
216 char *value = nullptr;
217 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
218 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
219 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
220 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
221 }
222
223 /**
224 * @tc.name: DocumentDBFindTest006
225 * @tc.desc: test filter field with id which has different type of value.
226 * @tc.type: FUNC
227 * @tc.require:
228 * @tc.author: mazhao
229 */
230 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest006, TestSize.Level1)
231 {
232 /**
233 * @tc.steps: step1. Create filter with _id which value is string
234 * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS,
235 */
236 GRD_ResultSet *resultSet1 = nullptr;
237 const char *filter1 = "{\"_id\" : \"valstring\"}";
238 Query query1 = { filter1, "{}" };
239 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_OK);
240 EXPECT_EQ(GRD_FreeResultSet(resultSet1), GRD_OK);
241
242 /**
243 * @tc.steps: step2. Create filter with _id which value is number
244 * @tc.expected: step2. Failed to get the record, the result is GRD_INVALID_ARGS,
245 */
246 GRD_ResultSet *resultSet2 = nullptr;
247 const char *filter2 = "{\"_id\" : 1}";
248 Query query2 = { filter2, "{}" };
249 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS);
250
251 /**
252 * @tc.steps: step3. Create filter with _id which value is array
253 * @tc.expected: step3. Failed to get the record, the result is GRD_INVALID_ARGS,
254 */
255 GRD_ResultSet *resultSet3 = nullptr;
256 const char *filter3 = "{\"_id\" : [\"2\", 1]}";
257 Query query3 = { filter3, "{}" };
258 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS);
259
260 /**
261 * @tc.steps: step4. Create filter with _id which value is object
262 * @tc.expected: step4. Failed to get the record, the result is GRD_INVALID_ARGS,
263 */
264 GRD_ResultSet *resultSet4 = nullptr;
265 const char *filter4 = "{\"_id\" : {\"info_val\" : \"1\"}}";
266 Query query4 = { filter4, "{}" };
267 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS);
268
269 /**
270 * @tc.steps: step5. Create filter with _id which value is bool
271 * @tc.expected: step5. Failed to get the record, the result is GRD_INVALID_ARGS,
272 */
273 GRD_ResultSet *resultSet5 = nullptr;
274 const char *filter5 = "{\"_id\" : true}";
275 Query query5 = { filter5, "{}" };
276 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS);
277
278 /**
279 * @tc.steps: step6. Create filter with _id which value is null
280 * @tc.expected: step6. Failed to get the record, the result is GRD_INVALID_ARGS,
281 */
282 GRD_ResultSet *resultSet6 = nullptr;
283 const char *filter6 = "{\"_id\" : null}";
284 Query query6 = { filter6, "{}" };
285 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS);
286 }
287
288 /**
289 * @tc.name: DocumentDBFindTest016
290 * @tc.desc: Test filter with collection Name is invalid.
291 * @tc.type: FUNC
292 * @tc.require:
293 * @tc.author: mazhao
294 */
295 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest016, TestSize.Level1)
296 {
297 const char *colName1 = "grd_type";
298 const char *colName2 = "GM_SYS_sysfff";
299 GRD_ResultSet *resultSet = nullptr;
300 const char *filter = "{\"_id\" : \"1\"}";
301 Query query = { filter, "{}" };
302 EXPECT_EQ(GRD_FindDoc(g_db, colName1, query, 1, &resultSet), GRD_INVALID_FORMAT);
303 EXPECT_EQ(GRD_FindDoc(g_db, colName2, query, 1, &resultSet), GRD_INVALID_FORMAT);
304 }
305
306 /**
307 * @tc.name: DocumentDBFindTest019
308 * @tc.desc: Test filter field with no result
309 * @tc.type: FUNC
310 * @tc.require:
311 * @tc.author: mazhao
312 */
313 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest019, TestSize.Level1)
314 {
315 const char *filter = "{\"_id\" : \"100\"}";
316 GRD_ResultSet *resultSet = nullptr;
317 Query query = { filter, "{}" };
318 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
319 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
320 char *value = nullptr;
321 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
322 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
323 }
324
325 /**
326 * @tc.name: DocumentDBFindTest023
327 * @tc.desc: Test filter field with double find.
328 * @tc.type: FUNC
329 * @tc.require:
330 * @tc.author: mazhao
331 */
332 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest023, TestSize.Level1)
333 {
334 /**
335 * @tc.steps: step1. Create filter with _id and get the record according to filter condition.
336 * @tc.expected: step1. succeed to get the record, the matching record is g_document6.
337 */
338 const char *filter = "{\"_id\" : \"6\"}";
339 GRD_ResultSet *resultSet = nullptr;
340 GRD_ResultSet *resultSet2 = nullptr;
341 Query query = { filter, "{}" };
342 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
343 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet2), GRD_RESOURCE_BUSY);
344
345 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
346 char *value = nullptr;
347 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
348 CompareValue(value, g_document6);
349 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
350 /**
351 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
352 * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA.
353 */
354 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
355 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
356 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
357 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
358 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
359 value = nullptr;
360 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
361 CompareValue(value, g_document6);
362 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
363 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
364 }
365
366 /**
367 * @tc.name: DocumentDBFindTest024
368 * @tc.desc: Test filter field with multi collections
369 * @tc.type: FUNC
370 * @tc.require:
371 * @tc.author: mazhao
372 */
373 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest024, TestSize.Level1)
374 {
375 const char *filter = "{\"_id\" : \"6\"}";
376 GRD_ResultSet *resultSet = nullptr;
377 GRD_ResultSet *resultSet2 = nullptr;
378 Query query = { filter, "{}" };
379 const char *collectionName = "DocumentDBFindTest024";
380 EXPECT_EQ(GRD_CreateCollection(g_db, collectionName, "", 0), GRD_OK);
381 InsertData(g_db, collectionName);
382 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
383 EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, 1, &resultSet2), GRD_OK);
384
385 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
386 char *value = nullptr;
387 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
388 CompareValue(value, g_document6);
389 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
390
391 EXPECT_EQ(GRD_Next(resultSet2), GRD_OK);
392 char *value2 = nullptr;
393 EXPECT_EQ(GRD_GetValue(resultSet2, &value2), GRD_OK);
394 CompareValue(value2, g_document6);
395 EXPECT_EQ(GRD_FreeValue(value2), GRD_OK);
396
397 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
398 EXPECT_EQ(GRD_Next(resultSet2), GRD_NO_DATA);
399 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
400 EXPECT_EQ(GRD_GetValue(resultSet2, &value), GRD_NOT_AVAILABLE);
401 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
402 EXPECT_EQ(GRD_FreeResultSet(resultSet2), GRD_OK);
403
404 EXPECT_EQ(GRD_DropCollection(g_db, collectionName, 0), GRD_OK);
405 }
406
407 /**
408 * @tc.name: DocumentDBFindTest025
409 * @tc.desc: Test nested projection, with viewType equals to 1.
410 * @tc.type: FUNC
411 * @tc.require:
412 * @tc.author: mazhao
413 */
414 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest025, TestSize.Level1)
415 {
416 /**
417 * @tc.steps: step1. Create filter to match g_document16, _id flag is 0.
418 * Create projection to display name,nested4.
419 * @tc.expected: step1. resultSet init successfuly, the result is GRD_OK,
420 */
421 const char *filter = "{\"_id\" : \"16\"}";
422 GRD_ResultSet *resultSet = nullptr;
423 const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4\":true}";
424 const char *targetDocument = "{\"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\
425 {\"nested4\":\"ABC\"}}}}";
426 Query query = { filter, projectionInfo };
427 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
428 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
429 char *value = nullptr;
430 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
431 CompareValue(value, targetDocument);
432 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
433 /**
434 * @tc.steps: step2. After loop, cannot get more record.
435 * @tc.expected: step2. Return GRD_NO_DATA.
436 */
437 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
438 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
439 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
440 /**
441 * @tc.steps: step3. Create filter to match g_document16, _id flag is 0;
442 * Create projection to display name、nested4 with different projection format.
443 * @tc.expected: step3. succeed to get the record.
444 */
445 projectionInfo = "{\"name\": true, \"nested1\":{\"nested2\":{\"nested3\":{\"nested4\":true}}}}";
446 query = { filter, projectionInfo };
447 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
448 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
449 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
450 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
451 /**
452 * @tc.steps: step4. After loop, cannot get more record.
453 * @tc.expected: step4. return GRD_NO_DATA.
454 */
455 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
456 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
457 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
458 /**
459 * @tc.steps: step5. Create filter to match g_document16, _id flag is 0.
460 * Create projection to conceal name,nested4 with different projection format.
461 * @tc.expected: step5. succeed to get the record.
462 */
463 projectionInfo = "{\"name\": 0, \"nested1.nested2.nested3.nested4\":0}";
464 targetDocument = "{\"nested1\":{\"nested2\":{\"nested3\":{\"field2\":\"CCC\"}}}}";
465 query = { filter, projectionInfo };
466 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
467 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
468 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
469 CompareValue(value, targetDocument);
470 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
471 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
472 }
473
474 /**
475 * @tc.name: DocumentDBFindTest027
476 * @tc.desc: Test projection with invalid field, _id field equals to 1.
477 * @tc.type: FUNC
478 * @tc.require:
479 * @tc.author: mazhao
480 */
481 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest027, TestSize.Level1)
482 {
483 /**
484 * @tc.steps: step1. Create filter to match g_document7, _id flag is 0.
485 * Create projection to display name, other _info and non existing field.
486 * @tc.expected: step1. Match the g_document7 and display name, other_info
487 */
488 const char *filter = "{\"_id\" : \"7\"}";
489 GRD_ResultSet *resultSet = nullptr;
490 const char *projectionInfo = "{\"name\": true, \"other_Info\":true, \"non_exist_field\":true}";
491 const char *targetDocument = "{\"name\": \"doc7\", \"other_Info\":[{\"school\":\"BX\", \"age\":15},\
492 {\"school\":\"C\", \"age\":35}]}";
493 Query query = { filter, projectionInfo };
494 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
495 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
496 char *value = nullptr;
497 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
498 CompareValue(value, targetDocument);
499 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
500 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
501
502 /**
503 * @tc.steps: step2. Create filter to match g_document7, _id flag is 0.
504 * Create projection to display name, other _info and existing field with space.
505 * @tc.expected: step2. Return GRD_INVALID_ARGS.
506 */
507 projectionInfo = "{\"name\": true, \"other_Info\":true, \" item \":true}";
508 query = { filter, projectionInfo };
509 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
510
511 /**
512 * @tc.steps: step3. Create filter to match g_document7, _id flag is 0.
513 * Create projection to display name, other _info and existing field with different case.
514 * @tc.expected: step3. Match the g_document7 and display name, other_Info.
515 */
516 projectionInfo = "{\"name\": true, \"other_Info\":true, \"ITEM\": true}";
517 query = { filter, projectionInfo };
518 resultSet = nullptr;
519 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
520 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
521 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
522 CompareValue(value, targetDocument);
523 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
524 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
525 }
526
527 /**
528 * @tc.name: DocumentDBFindTest028
529 * @tc.desc: Test projection with invalid field in Array,_id field equals to 1.
530 * @tc.type: FUNC
531 * @tc.require:
532 * @tc.author: mazhao
533 */
534 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest028, TestSize.Level1)
535 {
536 /**
537 * @tc.steps: step1. Create filter to match g_document7, _id flag is 0.
538 * Create projection to display name, non existing field in array.
539 * @tc.expected: step1. Match the g_document7 and display name, other_info.
540 */
541 const char *filter = "{\"_id\" : \"7\"}";
542 GRD_ResultSet *resultSet = nullptr;
543 const char *projectionInfo = "{\"name\": true, \"other_Info.non_exist_field\":true}";
544 const char *targetDocument = "{\"name\": \"doc7\"}";
545 Query query = { filter, projectionInfo };
546 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
547 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
548 char *value = nullptr;
549 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
550 CompareValue(value, targetDocument);
551 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
552 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
553
554 /**
555 * @tc.steps: step2. Create filter to match g_document7, _id flag is 0.
556 * Create projection to display name, other _info and existing field with space.
557 * @tc.expected: step2. Return GRD_INVALID_ARGS.
558 */
559 projectionInfo = "{\"name\": true, \"other_Info\":{\"non_exist_field\":true}}";
560 query = { filter, projectionInfo };
561 resultSet = nullptr;
562 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
563 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
564 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
565 CompareValue(value, targetDocument);
566 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
567 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
568
569 /**
570 * @tc.steps: step3. Create filter to match g_document7, _id flag is 0.
571 * Create projection to display name, non existing field in array with index format.
572 * @tc.expected: step3. Match the g_document7 and display name, other_Info.
573 */
574 projectionInfo = "{\"name\": true, \"other_Info.0\": true}";
575 query = { filter, projectionInfo };
576 resultSet = nullptr;
577 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
578 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS);
579 }
580
581 /**
582 * @tc.name: DocumentDBFindTest029
583 * @tc.desc: Test projection with path conflict._id field equals to 0.
584 * @tc.type: FUNC
585 * @tc.require:
586 * @tc.author: mazhao
587 */
588 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest029, TestSize.Level1)
589 {
590 /**
591 * @tc.steps: step1. Create filter to match g_document4, _id flag is 0.
592 * Create projection to display conflict path.
593 * @tc.expected: step1. Return GRD_INVALID_ARGS.
594 */
595 const char *filter = "{\"_id\" : \"4\"}";
596 GRD_ResultSet *resultSet = nullptr;
597 const char *projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}";
598 Query query = { filter, projectionInfo };
599 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
600 }
601
602 /**
603 * @tc.name: DocumentDBFindTest030
604 * @tc.desc: Test _id flag and field.None exist field.
605 * @tc.type: FUNC
606 * @tc.require:
607 * @tc.author: mazhao
608 */
609 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest030, TestSize.Level1)
610 {
611 /**
612 * @tc.steps: step1. Create filter to match g_document7, _id flag is 0.
613 * @tc.expected: step1. Match the g_document7 and return empty json.
614 */
615 const char *filter = "{\"_id\" : \"7\"}";
616 GRD_ResultSet *resultSet = nullptr;
617 const char *projectionInfo = "{\"non_exist_field\":true}";
618 int flag = 0;
619 const char *targetDocument = "{}";
620 Query query = { filter, projectionInfo };
621 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
622 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
623 char *value = nullptr;
624 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
625 CompareValue(value, targetDocument);
626 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
627 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
628
629 /**
630 * @tc.steps: step2. Create filter to match g_document7, _id flag is 1.
631 * @tc.expected: step2. Match g_document7, and return a json with _id.
632 */
633 resultSet = nullptr;
634 flag = 1;
635 targetDocument = "{\"_id\": \"7\"}";
636 query = { filter, projectionInfo };
637 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
638 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
639 value = nullptr;
640 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
641 CompareValue(value, targetDocument);
642 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
643 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
644 }
645
646 /**
647 * @tc.name: DocumentDBFindTest031
648 * @tc.desc: Test _id flag and field.Exist field with 1 value.
649 * @tc.type: FUNC
650 * @tc.require:
651 * @tc.author: mazhao
652 */
653 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest031, TestSize.Level1)
654 {
655 /**
656 * @tc.steps: step1. Create filter to match g_document7, _id flag is 0.
657 * @tc.expected: step1. Match the g_document7 and return json with name, item.
658 */
659 const char *filter = "{\"_id\" : \"7\"}";
660 GRD_ResultSet *resultSet = nullptr;
661 const char *projectionInfo = "{\"name\":true, \"item\":true}";
662 int flag = 0;
663 const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}";
664 Query query = { filter, projectionInfo };
665 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
666 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
667 char *value = nullptr;
668 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
669 CompareValue(value, targetDocument);
670 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
671 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
672
673 /**
674 * @tc.steps: step2. Create filter to match g_document7, _id flag is 1.
675 * @tc.expected: step2. Match g_document7, and return a json with _id.
676 */
677 resultSet = nullptr;
678 flag = 1;
679 projectionInfo = "{\"name\": 1, \"item\": 1}";
680 targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}";
681 query = { filter, projectionInfo };
682 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
683 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
684 value = nullptr;
685 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
686 CompareValue(value, targetDocument);
687 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
688 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
689 }
690
691 /**
692 * @tc.name: DocumentDBFindTest032
693 * @tc.desc: Test _id flag and field.Exist field with 1 value.
694 * @tc.type: FUNC
695 * @tc.require:
696 * @tc.author: mazhao
697 */
698 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest032, TestSize.Level1)
699 {
700 /**
701 * @tc.steps: step1. Create filter to match g_document7, _id flag is 0.
702 * @tc.expected: step1. Match the g_document7 and return json with name, item.
703 */
704 const char *filter = "{\"_id\" : \"7\"}";
705 GRD_ResultSet *resultSet = nullptr;
706 const char *projectionInfo = "{\"name\":true, \"item\":true}";
707 int flag = 0;
708 const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}";
709 Query query = { filter, projectionInfo };
710 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
711 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
712 char *value = nullptr;
713 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
714 CompareValue(value, targetDocument);
715 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
716 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
717 /**
718 * @tc.steps: step2. Create filter to match g_document7, _id flag is 1.
719 * @tc.expected: step2. Match g_document7, and return a json with _id.
720 */
721 resultSet = nullptr;
722 flag = 1;
723 projectionInfo = "{\"name\": 1, \"item\": 1}";
724 targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}";
725 query = { filter, projectionInfo };
726 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
727 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
728 value = nullptr;
729 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
730 CompareValue(value, targetDocument);
731 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
732 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
733 /**
734 * @tc.steps: step3. Create filter to match g_document7, _id flag is 1.Projection value is not 0.
735 * @tc.expected: step3. Match g_document7, and return a json with name, item and _id.
736 */
737 resultSet = nullptr;
738 flag = 1;
739 projectionInfo = "{\"name\": 10, \"item\": 10}";
740 targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}";
741 query = { filter, projectionInfo };
742 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
743 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
744 value = nullptr;
745 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
746 CompareValue(value, targetDocument);
747 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
748 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
749 }
750
751 /**
752 * @tc.name: DocumentDBFindTest033
753 * @tc.desc: Test _id flag and field.Exist field with 0 value.
754 * @tc.type: FUNC
755 * @tc.require:
756 * @tc.author: mazhao
757 */
758 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest033, TestSize.Level1)
759 {
760 /**
761 * @tc.steps: step1. Create filter to match g_document7, _id flag is 0.
762 * @tc.expected: step1. Match the g_document7 and return json with name, item and _id
763 */
764 const char *filter = "{\"_id\" : \"7\"}";
765 GRD_ResultSet *resultSet = nullptr;
766 const char *projectionInfo = "{\"name\":false, \"item\":false}";
767 int flag = 0;
768 const char *targetDocument = "{\"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : "
769 "35}]}";
770 Query query = { filter, projectionInfo };
771 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
772 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
773 char *value = nullptr;
774 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
775 CompareValue(value, targetDocument);
776 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
777 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
778 /**
779 * @tc.steps: step2. Create filter to match g_document7, _id flag is 1.
780 * @tc.expected: step2. Match g_document7, and return a json without name and item.
781 */
782 resultSet = nullptr;
783 flag = 1;
784 projectionInfo = "{\"name\": 0, \"item\": 0}";
785 targetDocument = "{\"_id\": \"7\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" "
786 ": 35}]}";
787 query = { filter, projectionInfo };
788 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
789 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
790 value = nullptr;
791 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
792 CompareValue(value, targetDocument);
793 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
794 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
795 }
796
797 /**
798 * @tc.name: DocumentDBFindTest034
799 * @tc.desc: Test projection with nonexist field in nested structure.
800 * @tc.type: FUNC
801 * @tc.require:
802 * @tc.author: mazhao
803 */
804 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest034, TestSize.Level1)
805 {
806 /**
807 * @tc.steps: step1. Create filter to match g_document4, _id flag is 0.
808 * @tc.expected: step1. Match the g_document4 and return json without name
809 */
810 const char *filter = "{\"_id\" : \"4\"}";
811 GRD_ResultSet *resultSet = nullptr;
812 const char *projectionInfo = "{\"name\": 1, \"personInfo.grade1\": 1, \
813 \"personInfo.shool1\": 1, \"personInfo.age1\": 1}";
814 int flag = 0;
815 const char *targetDocument = "{\"name\":\"doc4\"}";
816 Query query = { filter, projectionInfo };
817 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
818 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
819 char *value = nullptr;
820 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
821 CompareValue(value, targetDocument);
822 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
823 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
824
825 /**
826 * @tc.steps: step2. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure.
827 * @tc.expected: step2. Match the g_document4 and return json without name
828 */
829 projectionInfo = "{\"name\": false, \"personInfo.grade1\": false, \
830 \"personInfo.shool1\": false, \"personInfo.age1\": false}";
831 const char *targetDocument2 = "{\"item\":\"paper\",\"personInfo\":{\"grade\" : 1, \"school\":\"A\", \"age\" : "
832 "18}}";
833 query = { filter, projectionInfo };
834 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
835 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
836 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
837 CompareValue(value, targetDocument2);
838 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
839 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
840
841 /**
842 * @tc.steps: step3. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure.
843 * @tc.expected: step3. Match the g_document4 and return json with name, personInfo.school and personInfo.age.
844 */
845 projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age\": 1}";
846 const char *targetDocument3 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\", \"age\" : 18}}";
847 query = { filter, projectionInfo };
848 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
849 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
850 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
851 CompareValue(value, targetDocument3);
852 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
853 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
854
855 /**
856 * @tc.steps: step4. Create filter to match g_document4, _id flag is 0, display part of fields in nested structure.
857 * @tc.expected: step4. Match the g_document4 and return json with name, personInfo.school
858 */
859 projectionInfo = "{\"name\": 1, \"personInfo.school\": 1, \"personInfo.age1\": 1}";
860 const char *targetDocument4 = "{\"name\":\"doc4\", \"personInfo\": {\"school\":\"A\"}}";
861 query = { filter, projectionInfo };
862 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
863 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
864 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
865 CompareValue(value, targetDocument4);
866 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
867 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
868 }
869
870 /**
871 * @tc.name: DocumentDBFindTest035
872 * @tc.desc: test filter with id string filter
873 * @tc.type: FUNC
874 * @tc.require:
875 * @tc.author: mazhao
876 */
877 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest035, TestSize.Level1)
878 {
879 /**
880 * @tc.steps: step1. Create filter with _id and get the record according to filter condition.
881 * @tc.expected: step1. succeed to get the record, the matching record is g_document17
882 */
883 const char *filter = "{\"_id\" : \"17\"}";
884 GRD_ResultSet *resultSet = nullptr;
885 Query query = { filter, "{}" };
886 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, GRD_DOC_ID_DISPLAY, &resultSet), GRD_OK);
887 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
888 char *value = nullptr;
889 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
890 CompareValue(value, g_document17);
891 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
892 /**
893 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
894 * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA.
895 */
896 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
897 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE);
898 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
899 }
900
901 /**
902 * @tc.name: DocumentDBFindTest036
903 * @tc.desc: Test with invalid collectionName.
904 * @tc.type: FUNC
905 * @tc.require:
906 * @tc.author: mazhao
907 */
908 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest036, TestSize.Level1)
909 {
910 /**
911 * @tc.steps: step1. Test with invalid collectionName.
912 * @tc.expected: step1. Return GRD_INVALID_ARGS.
913 */
914 const char *filter = "{\"_id\" : \"17\"}";
915 GRD_ResultSet *resultSet = nullptr;
916 Query query = { filter, "{}" };
917 EXPECT_EQ(GRD_FindDoc(g_db, "", query, 0, &resultSet), GRD_INVALID_ARGS);
918 EXPECT_EQ(GRD_FindDoc(g_db, nullptr, query, 0, &resultSet), GRD_INVALID_ARGS);
919 }
920
921 /**
922 * @tc.name: DocumentDBFindTest037
923 * @tc.desc: Test field with different value.
924 * @tc.type: FUNC
925 * @tc.require:
926 * @tc.author: mazhao
927 */
928 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1)
929 {
930 /**
931 * @tc.steps: step1. Test field with different value.some are 1, other are 0.
932 * @tc.expected: step1. Return GRD_INVALID_ARGS.
933 */
934 const char *filter = "{\"_id\" : \"4\"}";
935 GRD_ResultSet *resultSet = nullptr;
936 const char *projectionInfo = "{\"name\":1, \"personInfo\":0, \"item\":1}";
937 Query query = { filter, projectionInfo };
938 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
939
940 /**
941 * @tc.steps: step2. Test field with different value.some are 2, other are 0.
942 * @tc.expected: step2. Return GRD_INVALID_ARGS.
943 */
944 projectionInfo = "{\"name\":2, \"personInfo\":0, \"item\":2}";
945 query = { filter, projectionInfo };
946 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
947
948 /**
949 * @tc.steps: step3. Test field with different value.some are 0, other are true.
950 * @tc.expected: step3. Return GRD_INVALID_ARGS.
951 */
952 projectionInfo = "{\"name\":true, \"personInfo\":0, \"item\":true}";
953 query = { filter, projectionInfo };
954 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
955
956 /**
957 * @tc.steps: step4. Test field with different value.some are 0, other are "".
958 * @tc.expected: step4. Return GRD_INVALID_ARGS.
959 */
960 projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"item\":\"\"}";
961 query = { filter, projectionInfo };
962 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
963
964 /**
965 * @tc.steps: step5. Test field with different value.some are 1, other are false.
966 * @tc.expected: step5. Return GRD_INVALID_ARGS.
967 */
968 projectionInfo = "{\"name\":false, \"personInfo\":1, \"item\":false";
969 query = { filter, projectionInfo };
970 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT);
971
972 /**
973 * @tc.steps: step6. Test field with different value.some are -1.123, other are false.
974 * @tc.expected: step6. Return GRD_INVALID_ARGS.
975 */
976 projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"item\":false";
977 query = { filter, projectionInfo };
978 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT);
979
980 /**
981 * @tc.steps: step7. Test field with different value.some are true, other are false.
982 * @tc.expected: step7. Return GRD_INVALID_ARGS.
983 */
984 projectionInfo = "{\"name\":false, \"personInfo\":true, \"item\":false";
985 query = { filter, projectionInfo };
986 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT);
987 }
988
989 /**
990 * @tc.name: DocumentDBFindTest038
991 * @tc.desc: Test field with false value.
992 * @tc.type: FUNC
993 * @tc.require:
994 * @tc.author: mazhao
995 */
996 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest038, TestSize.Level1)
997 {
998 /**
999 * @tc.steps: step1. Test field with different false value. Some are false, other are 0. flag is 0.
1000 * @tc.expected: step1. Match the g_document6 and return empty json.
1001 */
1002 const char *filter = "{\"_id\" : \"6\"}";
1003 GRD_ResultSet *resultSet = nullptr;
1004 const char *projectionInfo = "{\"name\":false, \"personInfo\": 0, \"item\":0}";
1005 int flag = 0;
1006 const char *targetDocument = "{}";
1007 Query query = { filter, projectionInfo };
1008 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
1009 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1010 char *value = nullptr;
1011 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1012 CompareValue(value, targetDocument);
1013 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1014 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1015
1016 /**
1017 * @tc.steps: step2. Test field with different false value.Some are false, others are 0. flag is 1.
1018 * @tc.expected: step2. Match g_document6, Return json with _id.
1019 */
1020 targetDocument = "{\"_id\": \"6\"}";
1021 query = { filter, projectionInfo };
1022 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1023 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1024 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1025 CompareValue(value, targetDocument);
1026 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1027 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1028 }
1029
1030 /**
1031 * @tc.name: DocumentDBFindTest039
1032 * @tc.desc: Test field with true value.
1033 * @tc.type: FUNC
1034 * @tc.require:
1035 * @tc.author: mazhao
1036 */
1037 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest039, TestSize.Level1)
1038 {
1039 /**
1040 * @tc.steps: step1. Test field with different true value. Some are true, other are 1. flag is 0.
1041 * @tc.expected: step1. Match the g_document18 and return json with name, item, personInfo.age and color.
1042 */
1043 const char *filter = "{\"_id\" : \"18\"}";
1044 GRD_ResultSet *resultSet = nullptr;
1045 const char *projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : "
1046 "-100}";
1047 const char *targetDocument = "{\"name\":\"doc18\", \"item\":\"mobile phone\", \"personInfo\":\
1048 {\"age\":66}, \"color\":\"blue\"}";
1049 int flag = 0;
1050 Query query = { filter, projectionInfo };
1051 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
1052 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1053 char *value = nullptr;
1054 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1055 CompareValue(value, targetDocument);
1056 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1057 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1058
1059 /**
1060 * @tc.steps: step2. Test field with different true value.Some are false, others are 0. flag is 1.
1061 * @tc.expected: step2. Match g_document18, Return json with name, item, personInfo.age, color and _id.
1062 */
1063 targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\
1064 {\"age\":66}, \"color\":\"blue\"}";
1065 query = { filter, projectionInfo };
1066 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1067 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1068 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1069 CompareValue(value, targetDocument);
1070 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1071 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1072 }
1073
1074 /**
1075 * @tc.name: DocumentDBFindTest040
1076 * @tc.desc: Test field with invalid value.
1077 * @tc.type: FUNC
1078 * @tc.require:
1079 * @tc.author: mazhao
1080 */
1081 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest040, TestSize.Level1)
1082 {
1083 /**
1084 * @tc.steps: step1. Test field with invalid value.Value is array.
1085 * @tc.expected: step1. Match the g_document18 and return GRD_INVALID_ARGS.
1086 */
1087 const char *filter = "{\"_id\" : \"18\"}";
1088 GRD_ResultSet *resultSet = nullptr;
1089 const char *projectionInfo = "{\"personInfo\":[true, 1]}";
1090 int flag = 1;
1091 Query query = { filter, projectionInfo };
1092 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS);
1093
1094 /**
1095 * @tc.steps: step2. Test field with invalid value.Value is null.
1096 * @tc.expected: step2. Match the g_document18 and return GRD_INVALID_ARGS.
1097 */
1098 projectionInfo = "{\"personInfo\":null}";
1099 query = { filter, projectionInfo };
1100 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS);
1101
1102 /**
1103 * @tc.steps: step3. Test field with invalid value.Value is invalid string.
1104 * @tc.expected: step3. Match the g_document18 and return GRD_INVALID_ARGS.
1105 */
1106 projectionInfo = "{\"personInfo\":\"invalid string.\"}";
1107 query = { filter, projectionInfo };
1108 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS);
1109 }
1110
1111 /**
1112 * @tc.name: DocumentDBFindTest042
1113 * @tc.desc: Test field with no existed uppercase filter
1114 * @tc.type: FUNC
1115 * @tc.require:
1116 * @tc.author: mazhao
1117 */
1118 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest042, TestSize.Level1)
1119 {
1120 /**
1121 * @tc.steps: step1. Test field with no existed uppercase filter.
1122 * @tc.expected: step1. not match any item.
1123 */
1124 const char *filter = "{\"_iD\" : \"18\"}";
1125 GRD_ResultSet *resultSet = nullptr;
1126 const char *projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":1, \"COLOR\":10, \"nonExist\" : "
1127 "-100}";
1128 int flag = 0;
1129 Query query = { filter, projectionInfo };
1130 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK);
1131 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
1132 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1133 /**
1134 * @tc.steps: step2. Test field with upper projection.
1135 * @tc.expected: step2. Match g_document18, Return json with item, personInfo.age, color and _id.
1136 */
1137 const char *filter1 = "{\"_id\" : \"18\"}";
1138 const char *targetDocument = "{\"_id\" : \"18\", \"item\" : \"mobile phone\",\"personInfo\":\
1139 {\"age\":66}}";
1140 query = { filter1, projectionInfo };
1141 char *value = nullptr;
1142 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1143 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1144 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1145 CompareValue(value, targetDocument);
1146 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1147 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
1148 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1149 }
1150
1151 /**
1152 * @tc.name: DocumentDBFindTest044
1153 * @tc.desc: Test field with uppercase projection
1154 * @tc.type: FUNC
1155 * @tc.require:
1156 * @tc.author: mazhao
1157 */
1158 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest044, TestSize.Level1)
1159 {
1160 /**
1161 * @tc.steps: step1. Test with false uppercase projection
1162 * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id.
1163 */
1164 const char *filter = "{\"_id\" : \"18\"}";
1165 GRD_ResultSet *resultSet = nullptr;
1166 const char *projectionInfo = "{\"Name\":0, \"personInfo.age\": false, \"personInfo.SCHOOL\": false, \"item\":\
1167 false, \"COLOR\":false, \"nonExist\" : false}";
1168 const char *targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\", \"personInfo\":\
1169 {\"school\":\"DD\"}, \"color\":\"blue\"}";
1170 Query query = { filter, projectionInfo };
1171 char *value = nullptr;
1172 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1173 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1174 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1175 CompareValue(value, targetDocument);
1176 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1177 EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA);
1178 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1179 }
1180
1181 /**
1182 * @tc.name: DocumentDBFindTest045
1183 * @tc.desc: Test field with too long collectionName
1184 * @tc.type: FUNC
1185 * @tc.require:
1186 * @tc.author: mazhao
1187 */
1188 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest045, TestSize.Level1)
1189 {
1190 /**
1191 * @tc.steps: step1. Test with false uppercase projection
1192 * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id.
1193 */
1194 const char *filter = "{\"_id\" : \"18\"}";
1195 GRD_ResultSet *resultSet = nullptr;
1196 Query query = { filter, "{}" };
1197 string collectionName1(MAX_COLLECTION_NAME, 'a');
1198 ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK);
1199 EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK);
1200 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1201 ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK);
1202
1203 string collectionName2(MAX_COLLECTION_NAME + 1, 'a');
1204 EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT);
1205 EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS);
1206 }
1207
1208 /**
1209 * @tc.name: DocumentDBFindTest052
1210 * @tc.desc: Test field when id string len is large than max
1211 * @tc.type: FUNC
1212 * @tc.require:
1213 * @tc.author: mazhao
1214 */
1215 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest052, TestSize.Level1)
1216 {
1217 /**
1218 * @tc.steps: step1. Test with false uppercase projection
1219 * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id.
1220 */
1221 const char *filter = "{\"_id\" : \"18\"}";
1222 GRD_ResultSet *resultSet = nullptr;
1223 Query query = { filter, "{}" };
1224 string collectionName1(MAX_COLLECTION_NAME, 'a');
1225 ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK);
1226 EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK);
1227 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1228 ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK);
1229
1230 string collectionName2(MAX_COLLECTION_NAME + 1, 'a');
1231 EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT);
1232 EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS);
1233 }
1234
1235 /**
1236 * @tc.name: DocumentDBFindTest053
1237 * @tc.desc: Test with invalid flags
1238 * @tc.type: FUNC
1239 * @tc.require:
1240 * @tc.author: mazhao
1241 */
1242 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest053, TestSize.Level1)
1243 {
1244 /**
1245 * @tc.steps: step1. Test with invalid flags which is 3.
1246 * @tc.expected: step1. Return GRD_INVALID_ARGS.
1247 */
1248 const char *filter = "{\"_id\" : \"18\"}";
1249 GRD_ResultSet *resultSet = nullptr;
1250 const char *projectionInfo = "{}";
1251 Query query = { filter, projectionInfo };
1252 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 3, &resultSet), GRD_INVALID_ARGS);
1253
1254 /**
1255 * @tc.steps:step1.parameter flags is int_max
1256 * @tc.expected:step1.GRD_INVALID_ARGS
1257 */
1258 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MAX, &resultSet), GRD_INVALID_ARGS);
1259
1260 /**
1261 * @tc.steps:step1.parameter flags is INT_MIN
1262 * @tc.expected:step1.GRD_INVALID_ARGS
1263 */
1264 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, INT_MIN, &resultSet), GRD_INVALID_ARGS);
1265 }
1266
1267 /**
1268 * @tc.name: DocumentDBFindTest054
1269 * @tc.desc: Test with null g_db and resultSet, filter.
1270 * @tc.type: FUNC
1271 * @tc.require:
1272 * @tc.author: mazhao
1273 */
1274 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest054, TestSize.Level1)
1275 {
1276 /**
1277 * @tc.steps: step1. Test with null g_db.
1278 * @tc.expected: step1. Return GRD_INVALID_ARGS.
1279 */
1280 const char *filter = "{\"_id\" : \"18\"}";
1281 GRD_ResultSet *resultSet = nullptr;
1282 const char *projectionInfo = "{}";
1283 Query query = { filter, projectionInfo };
1284 EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
1285
1286 /**
1287 * @tc.steps: step2. Test with null resultSet.
1288 * @tc.expected: step2. Return GRD_INVALID_ARGS.
1289 */
1290 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, nullptr), GRD_INVALID_ARGS);
1291
1292 /**
1293 * @tc.steps: step1. Test with query that has two nullptr data.
1294 * @tc.expected: step1. Return GRD_INVALID_ARGS.
1295 */
1296 query = { nullptr, nullptr };
1297 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS);
1298 }
1299
1300 /**
1301 * @tc.name: DocumentDBFindTest055
1302 * @tc.desc: Find doc, but filter' _id value lens is larger than MAX_ID_LENS
1303 * @tc.type: FUNC
1304 * @tc.require:
1305 * @tc.author: mazhao
1306 */
1307 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest055, TestSize.Level1)
1308 {
1309 /**
1310 * @tc.steps:step1.Find doc, but filter' _id value lens is larger than MAX_ID_LENS
1311 * @tc.expected:step1.GRD_OVER_LIMIT.
1312 */
1313 string document1 = "{\"_id\" : ";
1314 string document2 = "\"";
1315 string document4 = "\"";
1316 string document5 = "}";
1317 string document_midlle(MAX_ID_LENS + 1, 'k');
1318 string filter = document1 + document2 + document_midlle + document4 + document5;
1319 GRD_ResultSet *resultSet = nullptr;
1320 const char *projectionInfo = "{}";
1321 Query query = { filter.c_str(), projectionInfo };
1322 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OVER_LIMIT);
1323
1324 /**
1325 * @tc.steps:step1.Find doc, filter' _id value lens is equal as MAX_ID_LENS
1326 * @tc.expected:step1.GRD_OK.
1327 */
1328 string document_midlle2(MAX_ID_LENS, 'k');
1329 filter = document1 + document2 + document_midlle2 + document4 + document5;
1330 query = { filter.c_str(), projectionInfo };
1331 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK);
1332 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1333 }
1334
1335 /**
1336 * @tc.name: DocumentDBFindTest056
1337 * @tc.desc: Test findDoc with no _id.
1338 * @tc.type: FUNC
1339 * @tc.require:
1340 * @tc.author: mazhao
1341 */
1342 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest056, TestSize.Level1)
1343 {
1344 /**
1345 * @tc.steps: step1. Create filter with _id and get the record according to filter condition.
1346 * @tc.expected: step1. Succeed to get the record, the matching record is g_document6.
1347 */
1348 const char *filter = "{\"personInfo\" : {\"school\":\"B\"}}";
1349 GRD_ResultSet *resultSet = nullptr;
1350 Query query = { filter, "{}" };
1351 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1352 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1353 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1354 char *value = nullptr;
1355 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1356 CompareValue(value, g_document5);
1357 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1358 /**
1359 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
1360 * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA.
1361 */
1362 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1363 }
1364
1365 /**
1366 * @tc.name: DocumentDBFindTest056
1367 * @tc.desc: Test findDoc with no _id.
1368 * @tc.type: FUNC
1369 * @tc.require:
1370 * @tc.author: mazhao
1371 */
1372 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest057, TestSize.Level1)
1373 {
1374 /**
1375 * @tc.steps: step1. Create filter with _id and get the record according to filter condition.
1376 * @tc.expected: step1. Succeed to get the record, the matching record is g_document6.
1377 */
1378 const char *filter = "{\"personInfo\" : {\"school\":\"B\"}}";
1379 GRD_ResultSet *resultSet = nullptr;
1380 const char *projection = "{\"version\": 1}";
1381 Query query = { filter, projection };
1382 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1383 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1384 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1385 char *value = nullptr;
1386 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1387 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1388 /**
1389 * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet.
1390 * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA.
1391 */
1392 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1393 }
1394 /**
1395 * @tc.name: DocumentDBFindTest058
1396 * @tc.desc: Test findDoc with no _id.
1397 * @tc.type: FUNC
1398 * @tc.require:
1399 * @tc.author: mazhao
1400 */
1401 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest058, TestSize.Level1) {}
1402
1403 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest059, TestSize.Level1)
1404 {
1405 /**
1406 * @tc.steps: step1. Create filter with _id and get the record according to filter condition.
1407 * @tc.expected: step1. Succeed to get the record, the matching record is g_document6.
1408 */
1409 const char *filter = "{\"personInfo\" : {\"school\":\"B\"}}";
1410 GRD_ResultSet *resultSet = nullptr;
1411 const char *projection = R"({"a00001":1, "a00001":1})";
1412 Query query = { filter, projection };
1413 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_FORMAT);
1414 }
1415
1416 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest060, TestSize.Level1)
1417 {
1418 const char *filter = "{}";
1419 GRD_ResultSet *resultSet = nullptr;
1420 const char *projection = R"({"abc123_.":1})";
1421 Query query = { filter, projection };
1422 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS);
1423 }
1424
1425 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest061, TestSize.Level1)
1426 {
1427 const char *document064 = "{\"_id\" : \"64\", \"a\":1, \"doc64\" : 2}";
1428 const char *document063 = "{\"_id\" : \"63\", \"a\":1, \"doc63\" : 2}";
1429 const char *document062 = "{\"_id\" : \"62\", \"a\":1, \"doc62\" : 2}";
1430 const char *document061 = "{\"_id\" : \"61\", \"a\":1, \"doc61\" : 2}";
1431 EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document064, 0), GRD_OK);
1432 EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document063, 0), GRD_OK);
1433 EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document062, 0), GRD_OK);
1434 EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document061, 0), GRD_OK);
1435 const char *filter = "{\"a\":1}";
1436 GRD_ResultSet *resultSet = nullptr;
1437 const char *projection = R"({})";
1438 Query query = { filter, projection };
1439 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK);
1440 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1441 char *value = nullptr;
1442 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1443 CompareValue(value, document061);
1444
1445 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1446 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1447 CompareValue(value, document062);
1448
1449 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1450 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1451 CompareValue(value, document063);
1452
1453 EXPECT_EQ(GRD_Next(resultSet), GRD_OK);
1454 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1455 CompareValue(value, document064);
1456 EXPECT_EQ(GRD_FreeValue(value), GRD_OK);
1457 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1458 }
1459
1460 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest062, TestSize.Level1)
1461 {
1462 const char *filter = R"({"abc123_.":1})";
1463 GRD_ResultSet *resultSet = nullptr;
1464 const char *projection = R"({"abc123_":1})";
1465 Query query = { filter, projection };
1466 EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS);
1467 }
1468
1469 HWTEST_F(DocumentDBFindTest, DocumentDBFindTest063, TestSize.Level1)
1470 {
1471 GRD_DB *test_db = nullptr;
1472 std::string path = "./dataShare.db";
1473 int status = GRD_DBOpen(path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &test_db);
1474 EXPECT_EQ(status, GRD_OK);
1475 EXPECT_EQ(GRD_CreateCollection(test_db, colName, "", 0), GRD_OK);
1476 string document1 = "{\"_id\":\"key2_11_com.acts.ohos.data.datasharetestclient_100\",\
1477 \"bundleName\":\"com.acts.ohos.data.datasharetestclient\",\"key\":\"key2\",\
1478 \"subscriberId\":11,\"timestamp\":1509100700,""\"userId\":100,\"value\":{\"type\":0,";
1479 string document2 = "\"value\":[";
1480 string document3 = "5,";
1481 string document4 = document3;
1482 for (int i = 0; i < 100000; i++) {
1483 document4 += document3;
1484 }
1485 document4.push_back('5');
1486 string document5 = "]}}";
1487 string document0635 = document1 + document2 + document4 + document5;
1488 EXPECT_EQ(GRD_InsertDoc(test_db, colName, document0635.c_str(), 0), GRD_OK);
1489 EXPECT_EQ(status, GRD_OK);
1490 const char *filter = "{}";
1491 GRD_ResultSet *resultSet = nullptr;
1492 const char *projection = "{\"id_\":true, \"timestamp\":true, \"key\":true, \"bundleName\": true, "
1493 "\"subscriberId\": true}";
1494 Query query = { filter, projection };
1495 EXPECT_EQ(GRD_FindDoc(test_db, colName, query, 1, &resultSet), GRD_OK);
1496 char *value;
1497 while (GRD_Next(resultSet) == GRD_OK) {
1498 EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);
1499 GRD_FreeValue(value);
1500 }
1501 EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK);
1502 EXPECT_EQ(GRD_DBClose(test_db, 0), GRD_OK);
1503 DocumentDBTestUtils::RemoveTestDbFiles(path.c_str());
1504 }
1505 } // namespace
1506