diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp index 8165c73b2d17300929e07ee105f245950929671d..b1ba79740b6ddd1d13b99730b0454ee60253feff 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_delete_test.cpp @@ -138,14 +138,21 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest001, TestSize.Level1) { /** * @tc.steps:step1. Delete all the document - * @tc.expected: step1. GRD_INVALID_ARGS + * @tc.expected: step1. 1, means 1 document has been deleted. */ EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL_JSON_STR, 0), 1); + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL_JSON_STR, 0), 1); + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL_JSON_STR, 0), 1); + /** + * @tc.steps:step2. Check whether doc has been deleted compeletely + * @tc.expected: step2. GRD_OK + */ + ChkDeleteResWithFilter(NULL_JSON_STR); } /** * @tc.name: DocumentDelete002 - * @tc.desc: Delete with filter which has no _id + * @tc.desc: Delete document for person whose name is David * @tc.type: FUNC * @tc.require: * @tc.author: mazhao @@ -153,16 +160,22 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest001, TestSize.Level1) HWTEST_F(DocumentDBDeleteTest, DeleteDBTest002, TestSize.Level1) { /** - * @tc.steps:step1. Delete with filter which has no _id - * @tc.expected: step1. GRD_INVALID_ARGS + * @tc.steps:step1. Test delete with normal filter + * Delete document for person whose name is David + * @tc.expected: step1. 1, means 1 document has been deleted. */ - const char *filter = "{\"age\" : 15}"; + const char *filter = "{\"name\" : \"David\"}"; EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + /** + * @tc.steps:step2. Check whether doc has been deleted compeletely. + * @tc.expected: step2. GRD_OK + */ + ChkDeleteResWithFilter(filter); } /** * @tc.name: DocumentDelete003 - * @tc.desc: Delete with filter which has more than one fields. + * @tc.desc: Test delete with nest-json-filter * @tc.type: FUNC * @tc.require: * @tc.author: mazhao @@ -170,11 +183,17 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest002, TestSize.Level1) HWTEST_F(DocumentDBDeleteTest, DeleteDBTest003, TestSize.Level1) { /** - * @tc.steps:step1. Delete with filter which has more than one fields. - * @tc.expected: step1. GRD_INVALID_ARGS + * @tc.steps:step1. Test delete with nest-json-filter + * Delete document for person whose friend is David. + * @tc.expected: step1. GRD_OK. */ - const char *filter = "{\"_id\" : \"1\", \"age\" : 15}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + const char *filter = "{\"friend\" : {\"name\": \"David\"}}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), GRD_OK); + /** + * @tc.steps:step2. Check whether doc has been deleted compeletely. + * @tc.expected: step2. GRD_OK + */ + ChkDeleteResWithFilter(filter); } /** @@ -190,20 +209,29 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest004, TestSize.Level1) * @tc.steps:step1. Test delete with un-zero flags * @tc.expected: step1. GRD_INVALID_ARGS */ - const char *filter1 = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter1, 1), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, NULL_JSON_STR, 1), GRD_INVALID_ARGS); /** * @tc.steps:step2. Test delete with NULL collection name * @tc.expected: step2. GRD_INVALID_ARGS */ - const char *filter2 = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, filter2, 0), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, NULL_JSON_STR, 0), GRD_INVALID_ARGS); /** * @tc.steps:step3. Test delete with empty collection name * @tc.expected: step3. GRD_INVALID_ARGS */ - const char *filter3 = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter3, 1), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_DeleteDoc(g_db, "", NULL_JSON_STR, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step4. Test with filter whose json value is over the limit of double. + * @tc.expected: step4. GRD_INVALID_ARGS + */ + const char *filter1 = "{\"age\" : 1.79769313486232e308}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter1, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step5. Test with normal filter + * @tc.expected: step5. 1, meas 1 document has been deleted + */ + const char *filter2 = "{\"age\" : 15}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter2, 0), 1); } /** @@ -219,10 +247,10 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest005, TestSize.Level1) /** * @tc.step1: Test delete with same collection name * but one is uppercase(delete) and the other is lowercase(insert) - * @tc.expected: step1. GRD_INVALID_ARGS + * @tc.expected: step1. GRD_OK */ - const char *filter = "{\"_id\" : \"1\"}"; - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + const char *filter = "{\"friend\" : {\"name\" : \"David\"}}"; + EXPECT_EQ(GRD_DeleteDoc(g_db, "STUDENT", filter, 0), GRD_OK); /** * @tc.step2: Check whether doc has been deleted compeletely * @tc.expected: step2. GRD_OK @@ -241,13 +269,15 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest006, TestSize.Level1) { /** * @tc.step1: Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. GRD_OK + * @tc.expected: step1. succeed to get the record, the matching record is g_document6. */ - const char *filter = "{\"_id\" : \"1\"}"; + const char *collectionName = "stuDENT"; + const char *filter = "{\"name\" : \"xiaoming\"}"; GRD_ResultSet *resultSet = nullptr; Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter, 0), 1); + EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, 0, &resultSet), GRD_OK); + int expectedCount = 1; // return 1 count + EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName, filter, 0), expectedCount); /** * @tc.step2: Invoke GRD_Next to get the next matching value. Release resultSet. * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. @@ -265,7 +295,7 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest006, TestSize.Level1) */ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest007, TestSize.Level1) { - const char *filter = "{\"_id\" : \"1\"}"; + const char *filter = "{\"name\":\"doc1\"}"; string collectionName1(MAX_COLLECTION_LENS, 'a'); EXPECT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName1.c_str(), filter, 0), 0); @@ -273,6 +303,7 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest007, TestSize.Level1) string collectionName2(MAX_COLLECTION_LENS + 1, 'a'); EXPECT_EQ(GRD_DeleteDoc(g_db, collectionName2.c_str(), filter, 0), GRD_OVER_LIMIT); + EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter, 0), GRD_INVALID_ARGS); } /** @@ -288,7 +319,7 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest008, TestSize.Level1) * @tc.steps:step1. Delete with filter which has more than one fields. * @tc.expected: step1. GRD_INVALID_ARGS */ - const char *filter = "{\"_id\" : \"1\"}"; + const char *filter = "{\"name\" : \"doc1\"}"; EXPECT_EQ(GRD_DeleteDoc(NULL, COLLECTION_NAME, filter, 0), GRD_INVALID_ARGS); EXPECT_EQ(GRD_DeleteDoc(g_db, NULL, filter, 0), GRD_INVALID_ARGS); EXPECT_EQ(GRD_DeleteDoc(g_db, "", filter, 0), GRD_INVALID_ARGS); @@ -321,7 +352,7 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest010, TestSize.Level1) /** * @tc.name: DocumentDelete011 - * @tc.desc: + * @tc.desc: Test delete when filter id string len is larger than max. * @tc.type: FUNC * @tc.require: * @tc.author: mazhao @@ -329,21 +360,19 @@ HWTEST_F(DocumentDBDeleteTest, DeleteDBTest010, TestSize.Level1) HWTEST_F(DocumentDBDeleteTest, DeleteDBTest011, TestSize.Level1) { /** - * @tc.step1: Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. GRD_OK - */ - const char *filter = "{\"_id\" : \"1\"}"; - const char *filter2 = "{\"subject.info\" : \"exam\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); - EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter2, 0), 1); + * @tc.step1: delete document whose id string len is 899 + * @tc.expected: step1. GRD_OK + */ + std::string head = "{\"_id\":\""; + std::string end = "\", \"name\": \"mike\"}"; + std::string filter = head + std::string(900 - 1, 'k') + end; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter.c_str(), 0), 0); /** - * @tc.step2: Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + * @tc.steps: step2. delete document whose id string len is 900. + * @tc.expected: step2. GRD_OVER_LIMIT. + */ + std::string filter1 = head + std::string(900, 'k') + end; + EXPECT_EQ(GRD_DeleteDoc(g_db, COLLECTION_NAME, filter1.c_str(), 0), GRD_OVER_LIMIT); } /** diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp index 50ac7575d2c1825077e018d5e6bec7f7c19679b0..80d599fab57629f028007e55de39887f8076b14f 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_find_test.cpp @@ -36,51 +36,57 @@ std::string g_path = "./document.db"; GRD_DB *g_db = nullptr; constexpr const char *COLLECTION_NAME = "student"; const int MAX_COLLECTION_NAME = 511; +constexpr int JSON_LENS_MAX = 1024 * 1024; +constexpr const char *ID_FIELD = "_id"; const int MAX_ID_LENS = 899; -static const char *g_document1 = "{\"_id\" : \"1\", \"name\":\"doc1\",\"item\":\"journal\",\"personInfo\":\ +static const char *g_document1 = "{\"name\":\"doc1\",\"item\":\"journal\",\"personInfo\":\ {\"school\":\"AB\", \"age\" : 51}}"; -static const char *g_document2 = "{\"_id\" : \"2\", \"name\":\"doc2\",\"item\": 1, \"personInfo\":\ +static const char *g_document2 = "{\"name\":\"doc2\",\"_id\" : \"1\", \"item\": 1, \"personInfo\":\ [1, \"my string\", {\"school\":\"AB\", \"age\" : 51}, true, {\"school\":\"CD\", \"age\" : 15}, false]}"; -static const char *g_document3 = "{\"_id\" : \"3\", \"name\":\"doc3\",\"item\":\"notebook\",\"personInfo\":\ +static const char *g_document3 = "{\"name\":\"doc3\", \"_id\" : \"2\", \"item\":\"notebook\",\"personInfo\":\ [{\"school\":\"C\", \"age\" : 5}]}"; -static const char *g_document4 = "{\"_id\" : \"4\", \"name\":\"doc4\",\"item\":\"paper\",\"personInfo\":\ +static const char *g_document4 = "{\"name\":\"doc4\",\"item\":\"paper\",\"personInfo\":\ {\"grade\" : 1, \"school\":\"A\", \"age\" : 18}}"; -static const char *g_document5 = "{\"_id\" : \"5\", \"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ +static const char *g_document5 = "{\"name\":\"doc5\",\"item\":\"journal\",\"personInfo\":\ [{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; -static const char *g_document6 = "{\"_id\" : \"6\", \"name\":\"doc6\",\"item\":false,\"personInfo\":\ - [{\"school\":\"B\", \"teacher\" : \"mike\", \"age\" : 15},\ - {\"school\":\"C\", \"teacher\" : \"moon\", \"age\" : 20}]}"; - -static const char *g_document7 = "{\"_id\" : \"7\", \"name\":\"doc7\",\"item\":\"fruit\",\"other_Info\":\ +static const char *g_document6 = "{\"name\":\"doc6\", \"_id\" : \"3\", \"item\":false,\"personInfo\":\ + [{\"school\":\"B\", \"teacher\" : \"mike\",\"age\" : 15}, {\"school\":\"C\", \"teacher\" : \"moon\",\"age\":20}]}"; +static const char *g_document7 = "{\"name\":\"doc7\", \"_id\" : \"4\", \"item\":\"fruit\",\"other_Info\":\ [{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; -static const char *g_document8 = "{\"_id\" : \"8\", \"name\":\"doc8\",\"item\":true,\"personInfo\":\ +static const char *g_document8 = "{\"name\":\"doc8\",\"item\":true,\"personInfo\":\ [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; -static const char *g_document9 = "{\"_id\" : \"9\", \"name\":\"doc9\",\"item\": true}"; -static const char *g_document10 = "{\"_id\" : \"10\", \"name\":\"doc10\", \"parent\" : \"kate\"}"; -static const char *g_document11 = "{\"_id\" : \"11\", \"name\":\"doc11\", \"other\" : \"null\"}"; -static const char *g_document12 = "{\"_id\" : \"12\", \"name\":\"doc12\",\"other\" : null}"; -static const char *g_document13 = "{\"_id\" : \"13\", \"name\":\"doc13\",\"item\" : \"shoes\",\"personInfo\":\ +static const char *g_document9 = "{\"name\":\"doc9\",\"item\": true}"; +static const char *g_document10 = "{\"name\":\"doc10\", \"parent\" : \"kate\"}"; +static const char *g_document11 = "{\"name\":\"doc11\", \"others\" : \"null\"}"; +static const char *g_document12 = "{\"name\":\"doc12\",\"other\" : null}"; +static const char *g_document13 = "{\"name\":\"doc13\",\"item\" : \"shoes\",\"personInfo\":\ {\"school\":\"AB\", \"age\" : 15}}"; -static const char *g_document14 = "{\"_id\" : \"14\", \"name\":\"doc14\",\"item\" : true,\"personInfo\":\ +static const char *g_document14 = "{\"name\":\"doc14\",\"item\" : true,\"personInfo\":\ [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 85}]}"; -static const char *g_document15 = "{\"_id\" : \"15\", \"name\":\"doc15\",\"personInfo\":[{\"school\":\"C\", \"age\" : " +static const char *g_document15 = "{\"name\":\"doc15\", \"_id\" : \"6\", \"personInfo\":[{\"school\":\"C\", \"age\" : " "5}]}"; -static const char *g_document16 = "{\"_id\" : \"16\", \"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ +static const char *g_document16 = "{\"name\":\"doc16\", \"_id\" : \"7\", \"nested1\":{\"nested2\":{\"nested3\":\ {\"nested4\":\"ABC\", \"field2\":\"CCC\"}}}}"; -static const char *g_document17 = "{\"_id\" : \"17\", \"name\":\"doc17\",\"personInfo\":\"oh,ok\"}"; -static const char *g_document18 = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ +static const char *g_document17 = "{\"name\":\"doc17\", \"_id\" : \"8\", \"personInfo\":\"oh,ok\"}"; +static const char *g_document18 = "{\"name\":\"doc18\", \"_id\" : \"9\", \"item\" : \"mobile phone\",\"personInfo\":\ {\"school\":\"DD\", \"age\":66}, \"color\":\"blue\"}"; -static const char *g_document19 = "{\"_id\" : \"19\", \"name\":\"doc19\",\"ITEM\" : true,\"PERSONINFO\":\ +static const char *g_document19 = "{\"name\":\"doc19\",\"ITEM\" : true,\"PERSONINFO\":\ {\"school\":\"AB\", \"age\":15}}"; -static const char *g_document20 = "{\"_id\" : \"20\", \"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\ +static const char *g_document20 = "{\"name\":\"doc20\",\"ITEM\" : true,\"personInfo\":\ [{\"SCHOOL\":\"B\", \"AGE\":15}, {\"SCHOOL\":\"C\", \"AGE\":35}]}"; -static const char *g_document23 = "{\"_id\" : \"23\", \"name\":\"doc22\",\"ITEM\" : " - "true,\"personInfo\":[{\"school\":\"b\", \"age\":15}, [{\"school\":\"doc23\"}, 10, " - "{\"school\":\"doc23\"}, true, {\"school\":\"y\"}], {\"school\":\"b\"}]}"; +static const char *g_document21 = "{\"name\":\"doc21\",\"ITEM\" : true,\"personInfo\":[{\"name\":\"lili\", \ + \"school\":\"doc21\", \"AGE\":15}, {\"school\":\"C\", \"age\":35}]}"; +static const char *g_document22 = "{\"name\":\"doc22\",\"ITEM\" : true,\"personInfo\":[{\"school\":\"c\", \"age\":15}, \ + {\"school\":\"doc22\", \"age\":35}, {\"school\":\"doc22\",\"age\":30}]}"; +static const char *g_document23 = "{\"name\":\"doc22\",\"ITEM\" : true,\"personInfo\":[{\"school\":\"b\", \"age\":15}, \ + [{\"school\":\"doc23\"}, 10, {\"school\":\"doc23\"}, true, {\"school\":\"y\"}], {\"school\":\"b\"}]}"; +static const char *g_document24 = "{\"name\":\"doc24\", \"t1\":[1, null, \"abc\", [{\"t2\":\"xyz\"},true],\ + false, {\"t2\":{\"t21\":\"zzz\"}}, true, {\"t2\": 123}],\"t21\":true}"; static std::vector g_data = { g_document1, g_document2, g_document3, g_document4, g_document5, g_document6, g_document7, g_document8, g_document9, g_document10, g_document11, g_document12, g_document13, - g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20, g_document23 }; + g_document14, g_document15, g_document16, g_document17, g_document18, g_document19, g_document20, g_document21, + g_document22, g_document23, g_document24 }; static void InsertData(GRD_DB *g_db, const char *collectionName) { @@ -99,6 +105,27 @@ static void CompareValue(const char *value, const char *targetValue) EXPECT_EQ(valueObj.Print(), targetValueObj.Print()); } +static void CompareValueWithAutoGeneratedId(const char *value, const char *targetValue, bool hasId) +{ + int errCode; + DocumentDB::JsonObject valueObj = DocumentDB::JsonObject::Parse(value, errCode); + EXPECT_EQ(errCode, DocumentDB::E_OK); + DocumentDB::JsonObject targetValueObj = DocumentDB::JsonObject::Parse(targetValue, errCode); + EXPECT_EQ(errCode, DocumentDB::E_OK); + if (hasId) { + valueObj.GetObjectItem(ID_FIELD, errCode); + EXPECT_EQ(errCode, DocumentDB::E_OK); + } else { + valueObj.GetObjectItem(ID_FIELD, errCode); + EXPECT_EQ(errCode, -DocumentDB::E_NOT_FOUND); + } + printf("valueObj is ==============>%s\n", valueObj.Print().c_str()); + valueObj.DeleteItemFromObject(ID_FIELD); + printf("valueObj become ==============>%s\n", valueObj.Print().c_str()); + printf("targetValueObj become ==============>%s\n", targetValueObj.Print().c_str()); + EXPECT_EQ(valueObj.Print(), targetValueObj.Print()); +} + class DocumentDBFindTest : public testing::Test { public: static void SetUpTestCase(void); @@ -128,240 +155,841 @@ void DocumentDBFindTest::SetUp(void) InsertData(g_db, "student"); } -void DocumentDBFindTest::TearDown(void) {} +void DocumentDBFindTest::TearDown(void) {} + +/** + * @tc.name: DocumentDBFindTest001 + * @tc.desc: Test Insert document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. + */ + const char *filter = "{\"_id\" : \"3\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest002 + * @tc.desc: Test filter with multiple fields and _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest002, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with multiple and _id. and get the record according to filter condition. + * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS, + * GRD_GetValue return GRD_NOT_AVAILABLE and GRD_Next return GRD_NO_DATA. + */ + const char *filter = "{\"_id\" : \"3\", \"name\":\"doc6\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest003 + * @tc.desc: Test filter with all fields and _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest003, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. + */ + GRD_ResultSet *resultSet = nullptr; + Query query = { g_document6, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest004 + * @tc.desc: test filter with string filter without _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest004, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter without _id and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document1. + */ + const char *filter = "{\"item\":\"journal\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document1, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. + * @tc.expected: step2. succeed to get the next record, the matching record is g_document5. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document5, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step3. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step3. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest005 + * @tc.desc: test filter with object field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest005, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with object field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document4. + */ + const char *filter = "{\"personInfo\": {\"grade\":1, \"school\":\"A\", \"age\":18}}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document4, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest006 + * @tc.desc: test filter with bool field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest006, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with bool field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document6. + */ + const char *filter = "{\"item\":false}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest007 + * @tc.desc: test filter with nested field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest007, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with nested field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document2. + */ + const char *filter = "{\"personInfo.school\":\"AB\"}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document2); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. + * @tc.expected: step2. Succeed to get the next record, the matching record is g_document1 and g_document13. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document1, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document13, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step3. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step3. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest007 + * @tc.desc: test filter with part of array field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest008, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with part of array field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document6. + */ + const char *filter = "{\"personInfo.0.school\":\"B\", \"personInfo.0.age\":15}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document6); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. + * @tc.expected: step2. Succeed to get the next record, the matching record is g_document1 and g_document5. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document5, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step3. Invoke GRD_Next to get the next matching value. + * @tc.expected: step3. Succeed to get the next record, the matching record is g_document1 and g_document8. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document8, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step4. Invoke GRD_Next to get the next matching value. + * @tc.expected: step4. Succeed to get the next record, the matching record is g_document1 and g_document14. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document14, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step5. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step5. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest007 + * @tc.desc: test FindDoc API with Array value. The filter doesn't contain _id and mismatch the value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest009, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with part of array field and get the record according to filter condition. + * @tc.expected: step1. Cannot get the matching data. + */ + const char *filter = "{\"personInfo\": [{\"school\":\"C\"}]}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_TRUE(value == nullptr); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest010 + * @tc.desc: test FindDoc API with Array value. The filter doesn't contain _id and mismatch the value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest010, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with part of array field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document3. + */ + const char *filter = "{\"personInfo\": [{\"school\":\"C\", \"age\":5}]}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document3); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. + * @tc.expected: step2. succeed to get the next record, the matching record is g_document15. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document15); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step3. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step3. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest011 + * @tc.desc: test FindDoc API with Array value. The filter doesn't contain _id and mismatch the multyple value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest011, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with part of array field and get the record according to filter condition. + * @tc.expected: step1. Cannot get matching record, return GRD_NO_DATA. + */ + const char *filter = "{\"tags\": [\"blank\", \"red\"]}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_TRUE(value == nullptr); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest012 + * @tc.desc: test FindDoc API with Array value. The filter contains contain _id and match the value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest012, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with an array field and _id, then get the records according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document3. + */ + const char *filter = "{\"_id\":\"2\", \"personInfo\":[{\"school\":\"C\", \"age\" : 5}]}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document3); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest013 + * @tc.desc: test FindDoc API with Array value. The filter doesn't contain _id and mismatch the value. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest013, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with an array field and get the record according to filter condition. + * @tc.expected: step1. Cannot get matching record, return GRD_NO_DATA. + */ + const char *filter = "{\"_id\":\"0\", \"tags\":[\"blank\"]}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_TRUE(value == nullptr); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest014 + * @tc.desc: test FindDoc API with Array value. The filter doesn't contain _id and mismatch the null or missing field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest014, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with part of array field and a null field, + * then get the records according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document15. + */ + const char *filter = "{\"item\":null, \"personInfo\":[{\"school\":\"C\", \"age\":5}]}"; + GRD_ResultSet *resultSet = nullptr; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, g_document15); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest015 + * @tc.desc: test filter field with invalid filter. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest015, TestSize.Level1) +{ + GRD_ResultSet *resultSet = nullptr; + const char *filter = "{\"_id\":\"1\", \"dataList.test.k.4\":{\"$gt\":1}}"; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter1 = "{\"_id\":\"1\", \"dataList.test.k.j.k\": 1}"; + query = { filter1, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter2 = "{\"_id\":\"1\", \"dataList.test.k.4\":{\"kui\":123, \"$gt\":1}}"; + query = { filter2, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter3 = "{\"_id\":\"1\", \"dataList.test.k.4\":{\"$and\":123, \"$gt\":1}}"; + query = { filter3, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter4 = "{\"_id\":\"1\", \"$and\":[{\"$gt\":1}, 2, 3, {\"t1\":\"xx\"}]}"; + query = { filter4, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter5 = "{\"_id\":\"1\",\"$and\":[{\"t1\":\"xx\"},{\"$gt\":1}]}"; + query = { filter5, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter8 = "{\"_id\":\"1\", \"dataList.test.k.4\":{\">\":1}}"; + query = { filter8, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter9 = "{\"_id\":\"1\", \"dataList.test.k.4\":{\"0t1\":1}}"; + query = { filter9, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter10 = "{\"_id\":\"1\", \"dataList.test.k.4\":{\"gt\":{\"34fdf\":\"1234%&*&%*\"}}}"; + query = { filter10, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter11 = "{\"_id\":\"1\", \"dataList.test.k.4\":{\"gt\": {\"f.df\":\"1234%&*&%*\"}}}"; + query = { filter11, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter12 = "{\"$and\":[{\"name\": \"doc7\"},{\"item\":\"fruit\"}]}"; + query = { filter12, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter13 = "{\"$or\" : [{\"name\":\"doc7\"}, {\"item\":\"fruit\"}]}"; + query = { filter13, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter14 = "{\"name\": \"doc7\", \"$and\" : [{\"item\":\"fruit\"}]}"; + query = { filter14, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + + const char *filter15 = ""; + query = { filter15, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_FORMAT); + + std::vector filterVec = { R"({"name":"123", "_id":1})", R"({"_id":[1, 2]})", R"({"_id": {"t1":1}})", + R"({"_id":null})", R"({"_id":true})", R"({"_id":false})", R"({"_id":null})", R"({"_id":2})", + R"({"_id":1.333})", R"({"_id":-2.0})" }; + for (const auto &item : filterVec) { + query = { item.c_str(), "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + } +} + +/** + * @tc.name: DocumentDBFindTest016 + * @tc.desc: Test filter with collection Name is invalid. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest016, TestSize.Level1) +{ + const char *colName1 = "grd_type"; + const char *colName2 = "gm_sysfff"; + GRD_ResultSet *resultSet = nullptr; + const char *filter = "{\"_id\" : \"1\"}"; + Query query = { filter, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, colName1, query, 1, &resultSet), GRD_INVALID_FORMAT); + EXPECT_EQ(GRD_FindDoc(g_db, colName2, query, 1, &resultSet), GRD_INVALID_FORMAT); +} + +/** + * @tc.name: DocumentDBFindTest017 + * @tc.desc: Test filter field with large filter + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest017, TestSize.Level1) +{ + GRD_ResultSet *resultSet = nullptr; + string documentPart1 = "{ \"textVal\" : \" "; + string documentPart2 = "\" }"; + string jsonVal = string(JSON_LENS_MAX - documentPart1.size() - documentPart2.size() - 1, 'k'); + string document = documentPart1 + jsonVal + documentPart2; + string jsonVal1 = string(JSON_LENS_MAX - documentPart1.size() - documentPart2.size(), 'k'); + string document1 = documentPart1 + jsonVal1 + documentPart2; + + Query query = { document.c_str(), "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + query = { document1.c_str(), "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OVER_LIMIT); +} /** - * @tc.name: DocumentDBFindTest001 - * @tc.desc: Test Insert document db + * @tc.name: DocumentDBFindTest018 + * @tc.desc: test filter with field with object filter. * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest001, TestSize.Level1) +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest018, TestSize.Level1) { /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. + * @tc.steps: step1. Create filter with part of array field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, the first matching record is g_document6. */ - const char *filter = "{\"_id\" : \"6\"}"; + const char *filter = "{\"personInfo\": [{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; GRD_ResultSet *resultSet = nullptr; Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); + CompareValueWithAutoGeneratedId(value, g_document8, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + resultSet = nullptr; + const char *filter2 = "{\"personInfo\":[{\"school\":\"B\", \"age\" : 15}, {\"school\":\"C\", \"age\" : 35}]}"; + query = {filter2, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document8, true); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } /** - * @tc.name: DocumentDBFindTest002 - * @tc.desc: Test filter with multiple fields and _id. + * @tc.name: DocumentDBFindTest019 + * @tc.desc: Test filter field with no result * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest002, TestSize.Level1) +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest019, TestSize.Level1) { - /** - * @tc.steps: step1. Create filter with multiple and _id. and get the record according to filter condition. - * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS, - * GRD_GetValue return GRD_NOT_AVAILABLE and GRD_Next return GRD_NO_DATA. - */ - const char *filter = "{\"_id\" : \"6\", \"name\":\"doc6\"}"; + const char *filter = "{\"_id\" : \"100\"}"; GRD_ResultSet *resultSet = nullptr; Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. - */ - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + + const char *filter1 = "{\"dio\" : 100}"; + resultSet = nullptr; + query = { filter1, "{}" }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } /** - * @tc.name: DocumentDBFindTest004 - * @tc.desc: test filter with string filter without _id. + * @tc.name: DocumentDBFindTest020 + * @tc.desc: Test filter field with null result * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest004, TestSize.Level1) +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest020, TestSize.Level1) { - /** - * @tc.steps: step1. Create filter without _id and get the record according to filter condition. - * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - const char *filter = "{\"name\":\"doc6\"}"; + const char *filter = "{\"others\" : \"null\"}"; GRD_ResultSet *resultSet = nullptr; Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. GRD_GetValue return GRD_INVALID_ARGS and GRD_Next return GRD_INVALID_ARGS. - */ - char *value = nullptr; EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document11, true); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - -/** - * @tc.name: DocumentDBFindTest006 - * @tc.desc: test filter field with id which has different type of value. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest006, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id which value is string - * @tc.expected: step1. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet1 = nullptr; - const char *filter1 = "{\"_id\" : \"valstring\"}"; - Query query1 = { filter1, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query1, 1, &resultSet1), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet1), GRD_OK); - - /** - * @tc.steps: step2. Create filter with _id which value is number - * @tc.expected: step2. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet2 = nullptr; - const char *filter2 = "{\"_id\" : 1}"; - Query query2 = { filter2, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query2, 1, &resultSet2), GRD_INVALID_ARGS); - - /** - * @tc.steps: step3. Create filter with _id which value is array - * @tc.expected: step3. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet3 = nullptr; - const char *filter3 = "{\"_id\" : [\"2\", 1]}"; - Query query3 = { filter3, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query3, 1, &resultSet3), GRD_INVALID_ARGS); - - /** - * @tc.steps: step4. Create filter with _id which value is object - * @tc.expected: step4. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet4 = nullptr; - const char *filter4 = "{\"_id\" : {\"info_val\" : \"1\"}}"; - Query query4 = { filter4, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query4, 1, &resultSet4), GRD_INVALID_ARGS); - /** - * @tc.steps: step5. Create filter with _id which value is bool - * @tc.expected: step5. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet5 = nullptr; - const char *filter5 = "{\"_id\" : true}"; - Query query5 = { filter5, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query5, 1, &resultSet5), GRD_INVALID_ARGS); + const char *filter2 = "{\"others\":null}"; + resultSet = nullptr; + query = {filter2, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + for (unsigned int i = 0; i < g_data.size() - 1; i++) { + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + } + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** - * @tc.steps: step6. Create filter with _id which value is null - * @tc.expected: step6. Failed to get the record, the result is GRD_INVALID_ARGS, - */ - GRD_ResultSet *resultSet6 = nullptr; - const char *filter6 = "{\"_id\" : null}"; - Query query6 = { filter6, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query6, 1, &resultSet6), GRD_INVALID_ARGS); + const char *filter3 = "{\"testtesttest\": null}"; + resultSet = nullptr; + query = {filter3, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + for (unsigned int i = 0; i < g_data.size(); i++) { + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + } + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } /** - * @tc.name: DocumentDBFindTest016 - * @tc.desc: Test filter with collection Name is invalid. + * @tc.name: DocumentDBFindTest020 + * @tc.desc: Test filter field with array range not the same * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest016, TestSize.Level1) +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest021, TestSize.Level1) { - const char *colName1 = "grd_type"; - const char *colName2 = "GM_SYS_sysfff"; + const char *filter = "{\"personInfo\":[{\"sex\" : \"woma\", \"school\" : \"B\", \"age\" : 15},\ + {\"school\":\"C\", \"age\" : 35}]}"; GRD_ResultSet *resultSet = nullptr; - const char *filter = "{\"_id\" : \"1\"}"; Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, colName1, query, 1, &resultSet), GRD_INVALID_FORMAT); - EXPECT_EQ(GRD_FindDoc(g_db, colName2, query, 1, &resultSet), GRD_INVALID_FORMAT); -} + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document5, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -/** - * @tc.name: DocumentDBFindTest019 - * @tc.desc: Test filter field with no result - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest019, TestSize.Level1) -{ - const char *filter = "{\"_id\" : \"100\"}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; + const char *filter1 = "{\"personInfo\":[{\"school\":\"C\",\"age\":35}, \ + {\"sex\":\"woman\", \"school\":\"B\",\"age\":15}]}"; + resultSet = nullptr; + query = { filter1, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); - char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } /** - * @tc.name: DocumentDBFindTest023 - * @tc.desc: Test filter field with double find. + * @tc.name: DocumentDBFindTest022 + * @tc.desc: Test filter field with empty result * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest023, TestSize.Level1) +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest022, TestSize.Level1) { /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. succeed to get the record, the matching record is g_document6. + * @tc.steps: step1. Create filter with empty value to match all records. + * @tc.expected: step1. succeed to get the record. */ - const char *filter = "{\"_id\" : \"6\"}"; + const char *filter = "{}"; GRD_ResultSet *resultSet = nullptr; - GRD_ResultSet *resultSet2 = nullptr; Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet2), GRD_RESOURCE_BUSY); - - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + for (size_t i = 0; i < g_data.size(); i++) { + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + } /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + * @tc.steps: step2. After loop, cannot get more record. + * @tc.expected: step2. return GRD_NO_DATA. */ EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document6); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } +// /** +// * @tc.name: DocumentDBFindTest023 +// * @tc.desc: Test filter field with double find. +// * @tc.type: FUNC +// * @tc.require: +// * @tc.author: mazhao +// */ +// HWTEST_F(DocumentDBFindTest, DocumentDBFindTest023, TestSize.Level1) +// { +// /** +// * @tc.steps: step1. Create filter with _id and get the record according to filter condition. +// * @tc.expected: step1. succeed to get the record, the matching record is g_document6. +// */ +// const char *collectionName = "StuDENT"; +// const char *filter = "{\"_id\" : \"3\"}"; +// GRD_ResultSet *resultSet = nullptr; +// GRD_ResultSet *resultSet2 = nullptr; +// Query query = { filter, "{}" }; +// EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, 1, &resultSet), GRD_OK); +// EXPECT_EQ(GRD_FindDoc(g_db, collectionName, query, 1, &resultSet2), GRD_RESOURCE_BUSY); + +// EXPECT_EQ(GRD_Next(resultSet), GRD_OK); +// char *value = nullptr; +// EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); +// CompareValue(value, g_document6); +// EXPECT_EQ(GRD_FreeValue(value), GRD_OK); +// /** +// * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. +// * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. +// */ +// EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); +// EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); +// EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +// EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); +// EXPECT_EQ(GRD_Next(resultSet), GRD_OK); +// value = nullptr; +// EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); +// CompareValue(value, g_document6); +// EXPECT_EQ(GRD_FreeValue(value), GRD_OK); +// EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +// } + /** * @tc.name: DocumentDBFindTest024 * @tc.desc: Test filter field with multi collections @@ -371,7 +999,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest023, TestSize.Level1) */ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest024, TestSize.Level1) { - const char *filter = "{\"_id\" : \"6\"}"; + const char *filter = "{\"_id\" : \"3\"}"; GRD_ResultSet *resultSet = nullptr; GRD_ResultSet *resultSet2 = nullptr; Query query = { filter, "{}" }; @@ -417,7 +1045,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest025, TestSize.Level1) * Create projection to display name,nested4. * @tc.expected: step1. resultSet init successfuly, the result is GRD_OK, */ - const char *filter = "{\"_id\" : \"16\"}"; + const char *filter = "{\"_id\" : \"7\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4\":true}"; const char *targetDocument = "{\"name\":\"doc16\", \"nested1\":{\"nested2\":{\"nested3\":\ @@ -470,6 +1098,35 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest025, TestSize.Level1) EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } +/** + * @tc.name: DocumentDBFindTest026 + * @tc.desc: Test nested projection, with _id flag equals to 1. Projection is 5 level. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest026, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter to match g_document16, _id flag + * @tc.expected: step1. succeed to get the record, the matching record is g_document6. + */ + const char *filter = "{\"_id\" : \"7\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": true, \"nested1.nested2.nested3.nested4.nested5\":true}"; + Query query = { filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); +} + /** * @tc.name: DocumentDBFindTest027 * @tc.desc: Test projection with invalid field, _id field equals to 1. @@ -484,7 +1141,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest027, TestSize.Level1) * Create projection to display name, other _info and non existing field. * @tc.expected: step1. Match the g_document7 and display name, other_info */ - const char *filter = "{\"_id\" : \"7\"}"; + const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\": true, \"other_Info\":true, \"non_exist_field\":true}"; const char *targetDocument = "{\"name\": \"doc7\", \"other_Info\":[{\"school\":\"BX\", \"age\":15},\ @@ -537,7 +1194,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest028, TestSize.Level1) * Create projection to display name, non existing field in array. * @tc.expected: step1. Match the g_document7 and display name, other_info. */ - const char *filter = "{\"_id\" : \"7\"}"; + const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\": true, \"other_Info.non_exist_field\":true}"; const char *targetDocument = "{\"name\": \"doc7\"}"; @@ -591,10 +1248,10 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest029, TestSize.Level1) * Create projection to display conflict path. * @tc.expected: step1. Return GRD_INVALID_ARGS. */ - const char *filter = "{\"_id\" : \"4\"}"; + const char *filter = "{\"name\": \"doc4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}"; - Query query = { filter, projectionInfo }; + Query query = { filter, projectionInfo}; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); } @@ -611,7 +1268,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest030, TestSize.Level1) * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. * @tc.expected: step1. Match the g_document7 and return empty json. */ - const char *filter = "{\"_id\" : \"7\"}"; + const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"non_exist_field\":true}"; int flag = 0; @@ -631,7 +1288,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest030, TestSize.Level1) */ resultSet = nullptr; flag = 1; - targetDocument = "{\"_id\": \"7\"}"; + targetDocument = "{\"_id\": \"4\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -655,12 +1312,12 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest031, TestSize.Level1) * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. * @tc.expected: step1. Match the g_document7 and return json with name, item. */ - const char *filter = "{\"_id\" : \"7\"}"; + const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\":true, \"item\":true}"; int flag = 0; const char *targetDocument = "{\"name\":\"doc7\", \"item\":\"fruit\"}"; - Query query = { filter, projectionInfo }; + Query query = { filter, projectionInfo}; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; @@ -668,7 +1325,6 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest031, TestSize.Level1) CompareValue(value, targetDocument); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - /** * @tc.steps: step2. Create filter to match g_document7, _id flag is 1. * @tc.expected: step2. Match g_document7, and return a json with _id. @@ -676,7 +1332,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest031, TestSize.Level1) resultSet = nullptr; flag = 1; projectionInfo = "{\"name\": 1, \"item\": 1}"; - targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; + targetDocument = "{\"name\":\"doc7\", \"_id\":\"4\", \"item\":\"fruit\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -700,7 +1356,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest032, TestSize.Level1) * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. * @tc.expected: step1. Match the g_document7 and return json with name, item. */ - const char *filter = "{\"_id\" : \"7\"}"; + const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\":true, \"item\":true}"; int flag = 0; @@ -720,7 +1376,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest032, TestSize.Level1) resultSet = nullptr; flag = 1; projectionInfo = "{\"name\": 1, \"item\": 1}"; - targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; + targetDocument = "{\"name\":\"doc7\", \"_id\":\"4\", \"item\":\"fruit\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -736,7 +1392,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest032, TestSize.Level1) resultSet = nullptr; flag = 1; projectionInfo = "{\"name\": 10, \"item\": 10}"; - targetDocument = "{\"_id\":\"7\", \"name\":\"doc7\", \"item\":\"fruit\"}"; + targetDocument = "{\"name\":\"doc7\", \"_id\":\"4\", \"item\":\"fruit\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -760,9 +1416,9 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest033, TestSize.Level1) * @tc.steps: step1. Create filter to match g_document7, _id flag is 0. * @tc.expected: step1. Match the g_document7 and return json with name, item and _id */ - const char *filter = "{\"_id\" : \"7\"}"; + const char *filter = "{\"_id\" : \"4\"}"; GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":false, \"item\":false}"; + const char *projectionInfo = "{\"name\": false, \"item\": false}"; int flag = 0; const char *targetDocument = "{\"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" : " "35}]}"; @@ -781,7 +1437,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest033, TestSize.Level1) resultSet = nullptr; flag = 1; projectionInfo = "{\"name\": 0, \"item\": 0}"; - targetDocument = "{\"_id\": \"7\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" " + targetDocument = "{\"_id\": \"4\", \"other_Info\":[{\"school\":\"BX\", \"age\" : 15}, {\"school\":\"C\", \"age\" " ": 35}]}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); @@ -806,7 +1462,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest034, TestSize.Level1) * @tc.steps: step1. Create filter to match g_document4, _id flag is 0. * @tc.expected: step1. Match the g_document4 and return json without name */ - const char *filter = "{\"_id\" : \"4\"}"; + const char *filter = "{\"name\" : \"doc4\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\": 1, \"personInfo.grade1\": 1, \ \"personInfo.shool1\": 1, \"personInfo.age1\": 1}"; @@ -817,7 +1473,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest034, TestSize.Level1) EXPECT_EQ(GRD_Next(resultSet), GRD_OK); char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument); + CompareValueWithAutoGeneratedId(value, targetDocument, false); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); @@ -833,7 +1489,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest034, TestSize.Level1) EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument2); + CompareValueWithAutoGeneratedId(value, targetDocument2, false); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); @@ -847,7 +1503,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest034, TestSize.Level1) EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument3); + CompareValueWithAutoGeneratedId(value, targetDocument3, false); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); @@ -861,7 +1517,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest034, TestSize.Level1) EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, targetDocument4); + CompareValueWithAutoGeneratedId(value, targetDocument4, false); EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } @@ -877,9 +1533,9 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest035, TestSize.Level1) { /** * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. succeed to get the record, the matching record is g_document17 + * @tc.expected: step1. succeed to get the record, the matching record is g_document6 */ - const char *filter = "{\"_id\" : \"17\"}"; + const char *filter = "{\"_id\" : \"8\"}"; GRD_ResultSet *resultSet = nullptr; Query query = { filter, "{}" }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, GRD_DOC_ID_DISPLAY, &resultSet), GRD_OK); @@ -910,9 +1566,10 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest036, TestSize.Level1) * @tc.steps: step1. Test with invalid collectionName. * @tc.expected: step1. Return GRD_INVALID_ARGS. */ - const char *filter = "{\"_id\" : \"17\"}"; + const char *filter = "{\"name\" : \"doc4\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; + const char *projectionInfo = "{\"personInfo\": true, \"personInfo.grade\": true}"; + Query query = { filter, projectionInfo}; EXPECT_EQ(GRD_FindDoc(g_db, "", query, 0, &resultSet), GRD_INVALID_ARGS); EXPECT_EQ(GRD_FindDoc(g_db, nullptr, query, 0, &resultSet), GRD_INVALID_ARGS); } @@ -930,9 +1587,9 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step1. Test field with different value.some are 1, other are 0. * @tc.expected: step1. Return GRD_INVALID_ARGS. */ - const char *filter = "{\"_id\" : \"4\"}"; + const char *filter = "{\"name\" : \"doc4\"}"; GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{\"name\":1, \"personInfo\":0, \"item\":1}"; + const char *projectionInfo = "{\"name\":1, \"personInfo\":0, \"personInfo.grade\":1}"; Query query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); @@ -940,7 +1597,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step2. Test field with different value.some are 2, other are 0. * @tc.expected: step2. Return GRD_INVALID_ARGS. */ - projectionInfo = "{\"name\":2, \"personInfo\":0, \"item\":2}"; + projectionInfo = "{\"name\":2, \"personInfo\":0, \"personInfo.grade\":2}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); @@ -948,7 +1605,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step3. Test field with different value.some are 0, other are true. * @tc.expected: step3. Return GRD_INVALID_ARGS. */ - projectionInfo = "{\"name\":true, \"personInfo\":0, \"item\":true}"; + projectionInfo = "{\"name\":true, \"personInfo\":0, \"personInfo.grade\":true}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); @@ -956,7 +1613,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step4. Test field with different value.some are 0, other are "". * @tc.expected: step4. Return GRD_INVALID_ARGS. */ - projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"item\":\"\"}"; + projectionInfo = "{\"name\":\"\", \"personInfo\":0, \"personInfo.grade\":\"\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); @@ -964,7 +1621,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step5. Test field with different value.some are 1, other are false. * @tc.expected: step5. Return GRD_INVALID_ARGS. */ - projectionInfo = "{\"name\":false, \"personInfo\":1, \"item\":false"; + projectionInfo = "{\"name\":false, \"personInfo\":1, \"personInfo.grade\":false"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); @@ -972,7 +1629,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step6. Test field with different value.some are -1.123, other are false. * @tc.expected: step6. Return GRD_INVALID_ARGS. */ - projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"item\":false"; + projectionInfo = "{\"name\":false, \"personInfo\":-1.123, \"personInfo.grade\":false"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); @@ -980,7 +1637,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest037, TestSize.Level1) * @tc.steps: step7. Test field with different value.some are true, other are false. * @tc.expected: step7. Return GRD_INVALID_ARGS. */ - projectionInfo = "{\"name\":false, \"personInfo\":true, \"item\":false"; + projectionInfo = "{\"name\":false, \"personInfo\":true, \"personInfo.grade\":false"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_FORMAT); } @@ -998,7 +1655,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest038, TestSize.Level1) * @tc.steps: step1. Test field with different false value. Some are false, other are 0. flag is 0. * @tc.expected: step1. Match the g_document6 and return empty json. */ - const char *filter = "{\"_id\" : \"6\"}"; + const char *filter = "{\"_id\" : \"3\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\":false, \"personInfo\": 0, \"item\":0}"; int flag = 0; @@ -1016,7 +1673,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest038, TestSize.Level1) * @tc.steps: step2. Test field with different false value.Some are false, others are 0. flag is 1. * @tc.expected: step2. Match g_document6, Return json with _id. */ - targetDocument = "{\"_id\": \"6\"}"; + targetDocument = "{\"_id\": \"3\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); @@ -1036,10 +1693,10 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest038, TestSize.Level1) HWTEST_F(DocumentDBFindTest, DocumentDBFindTest039, TestSize.Level1) { /** - * @tc.steps: step1. Test field with different true value. Some are true, other are 1. flag is 0. + * @tc.steps: step1. Test field with different true value. Some are true, other are "". flag is 0. * @tc.expected: step1. Match the g_document18 and return json with name, item, personInfo.age and color. */ - const char *filter = "{\"_id\" : \"18\"}"; + const char *filter = "{\"_id\" : \"9\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"name\":true, \"personInfo.age\": \"\", \"item\":1, \"color\":10, \"nonExist\" : " "-100}"; @@ -1059,7 +1716,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest039, TestSize.Level1) * @tc.steps: step2. Test field with different true value.Some are false, others are 0. flag is 1. * @tc.expected: step2. Match g_document18, Return json with name, item, personInfo.age, color and _id. */ - targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\",\"item\" : \"mobile phone\",\"personInfo\":\ + targetDocument = "{\"name\":\"doc18\", \"_id\" : \"9\", \"item\" : \"mobile phone\",\"personInfo\":\ {\"age\":66}, \"color\":\"blue\"}"; query = { filter, projectionInfo }; EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); @@ -1083,7 +1740,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest040, TestSize.Level1) * @tc.steps: step1. Test field with invalid value.Value is array. * @tc.expected: step1. Match the g_document18 and return GRD_INVALID_ARGS. */ - const char *filter = "{\"_id\" : \"18\"}"; + const char *filter = "{\"_id\" : \"9\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"personInfo\":[true, 1]}"; int flag = 1; @@ -1091,20 +1748,146 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest040, TestSize.Level1) EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); /** - * @tc.steps: step2. Test field with invalid value.Value is null. - * @tc.expected: step2. Match the g_document18 and return GRD_INVALID_ARGS. + * @tc.steps: step2. Test field with invalid value.Value is null. + * @tc.expected: step2. Match the g_document18 and return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"personInfo\":null}"; + query = { filter, projectionInfo }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); + + /** + * @tc.steps: step3. Test field with invalid value.Value is invalid string. + * @tc.expected: step3. Match the g_document18 and return GRD_INVALID_ARGS. + */ + projectionInfo = "{\"personInfo\":\"invalid string.\"}"; + query = { filter, projectionInfo }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDBFindTest041 + * @tc.desc: Test auto generated _id. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest041, TestSize.Level1) +{ + /** + * @tc.steps: step1. Find the record with field "name" and value "doc9". + * Projection expects to conceal name and _id, flag is 0. + * @tc.expected: step1. Match the g_document9, record doesn't contain name and _id. + */ + const char *filter = "{\"name\": \"doc9\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"name\": 0, \"_id\": 0}"; + const char *targetDocument = "{\"item\":true}"; + Query query = { filter, projectionInfo}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, false); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step2. Find the record with field "name" and value "doc9". + * Projeciton expects to conceal name and _id, flag is 1. + * @tc.expected: step2. Match g_document9, record doesn't contain name and _id. + */ + projectionInfo = "{\"name\":0, \"_id\": 0}"; + query = { filter, projectionInfo}; + targetDocument = "{\"item\":true}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, false); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + // /** + // * @tc.steps: step3. Find the record with field "name" and value "doc9". + // * Projeciton expects to conceal name and _id, flag is 0. + // * @tc.expected: step3. Match g_document9, record doesn't contain name and _id. + // */ + projectionInfo = "{\"name\":0}"; + query = { filter, projectionInfo}; + targetDocument = "{\"item\": true}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, false); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step4. Find the record with field "name" and value "doc9". + * Projeciton expects to conceal name flag is 1. + * @tc.expected: step4. Match g_document9, record doesn't contain name,but contains _id. + */ + projectionInfo = "{\"name\":0}"; + query = { filter, projectionInfo}; + targetDocument = "{\"item\": true}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step5. Find the record with field "name" and value "doc9". + * Projeciton expects to display name and _id, flag is 0. + * @tc.expected: step5. Match g_document9, record doesn't contain name and contains _id. + */ + projectionInfo = "{\"name\": 1, \"_id\": 1}"; + query = { filter, projectionInfo}; + targetDocument = "{\"name\": \"doc9\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step6. Find the record with field "name" and value "doc9". + * Projeciton expects to display name and _id, flag is 1. + * @tc.expected: step6. Match g_document9, record contains name and contains _id. */ - projectionInfo = "{\"personInfo\":null}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); - + projectionInfo = "{\"name\": 1, \"_id\": 1}"; + query = { filter, projectionInfo}; + targetDocument = "{\"name\": \"doc9\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); /** - * @tc.steps: step3. Test field with invalid value.Value is invalid string. - * @tc.expected: step3. Match the g_document18 and return GRD_INVALID_ARGS. + * @tc.steps: step7. Find the record with field "name" and value "doc9". + * Projeciton expects to display name , flag is 0. + * @tc.expected: step7. Match g_document9, record contains name,but doesn't contains _id. */ - projectionInfo = "{\"personInfo\":\"invalid string.\"}"; - query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, flag, &resultSet), GRD_INVALID_ARGS); + projectionInfo = "{\"name\": 1}"; + query = { filter, projectionInfo}; + targetDocument = "{\"name\": \"doc9\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, false); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step8. Find the record with field "name" and value "doc9". + * Projeciton expects to display name , flag is 1. + * @tc.expected: step8. Match g_document9, record contains name and _id. + */ + projectionInfo = "{\"name\": 1}"; + query = { filter, projectionInfo}; + targetDocument = "{\"name\": \"doc9\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, targetDocument, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } /** @@ -1120,7 +1903,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest042, TestSize.Level1) * @tc.steps: step1. Test field with no existed uppercase filter. * @tc.expected: step1. not match any item. */ - const char *filter = "{\"_iD\" : \"18\"}"; + const char *filter = "{\"_iD\" : \"9\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"Name\":true, \"personInfo.age\": \"\", \"item\":1, \"COLOR\":10, \"nonExist\" : " "-100}"; @@ -1133,8 +1916,8 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest042, TestSize.Level1) * @tc.steps: step2. Test field with upper projection. * @tc.expected: step2. Match g_document18, Return json with item, personInfo.age, color and _id. */ - const char *filter1 = "{\"_id\" : \"18\"}"; - const char *targetDocument = "{\"_id\" : \"18\", \"item\" : \"mobile phone\",\"personInfo\":\ + const char *filter1 = "{\"_id\" : \"9\"}"; + const char *targetDocument = "{\"_id\" : \"9\", \"item\" : \"mobile phone\",\"personInfo\":\ {\"age\":66}}"; query = { filter1, projectionInfo }; char *value = nullptr; @@ -1147,6 +1930,44 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest042, TestSize.Level1) EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } +/** + * @tc.name: DocumentDBFindTest043 + * @tc.desc: Test field with existed uppercase filter + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest043, TestSize.Level1) +{ + /** + * @tc.steps: step1. Test field with existed uppercase filter. + * @tc.expected: step1. Match g_document19, Return json with ITEM, personInfo.age. + */ + const char *filter = "{\"ITEM\" : true, \"PERSONINFO.school\": \"AB\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projectionInfo = "{\"ITEM\": true, \"PERSONINFO.age\": 1}"; + Query query = { filter, projectionInfo }; + char *value = nullptr; + const char *target = "{\"ITEM\":true, \"PERSONINFO\": {\"age\":15}}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValue(value, target); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step2. Test field with non existed uppercase filter. + * @tc.expected: step2. not match any item + */ + const char *filter1 = "{\"ITEM\":true, \"personInfo.school\": \"AB\"}"; + query = {filter1, projectionInfo}; + value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + /** * @tc.name: DocumentDBFindTest044 * @tc.desc: Test field with uppercase projection @@ -1160,11 +1981,11 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest044, TestSize.Level1) * @tc.steps: step1. Test with false uppercase projection * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. */ - const char *filter = "{\"_id\" : \"18\"}"; + const char *filter = "{\"_id\" : \"9\"}"; GRD_ResultSet *resultSet = nullptr; const char *projectionInfo = "{\"Name\":0, \"personInfo.age\": false, \"personInfo.SCHOOL\": false, \"item\":\ false, \"COLOR\":false, \"nonExist\" : false}"; - const char *targetDocument = "{\"_id\" : \"18\", \"name\":\"doc18\", \"personInfo\":\ + const char *targetDocument = "{\"name\":\"doc18\", \"_id\" : \"9\", \"personInfo\":\ {\"school\":\"DD\"}, \"color\":\"blue\"}"; Query query = { filter, projectionInfo }; char *value = nullptr; @@ -1190,7 +2011,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest045, TestSize.Level1) * @tc.steps: step1. Test with false uppercase projection * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. */ - const char *filter = "{\"_id\" : \"18\"}"; + const char *filter = "{\"_id\" : \"9\"}"; GRD_ResultSet *resultSet = nullptr; Query query = { filter, "{}" }; string collectionName1(MAX_COLLECTION_NAME, 'a'); @@ -1204,6 +2025,285 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest045, TestSize.Level1) EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS); } +/** + * @tc.name: DocumentDBFindTest046 + * @tc.desc: Test find with various projection value + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest046, TestSize.Level1) +{ + /** + * @tc.steps: step1. Insert data. + * @tc.expected: step1. return GRD_OK. + */ + const char *document1 = + "{\"level1_field0\":\"val0\",\"level1_field1\":[[{\"field\":1}]],\"level1_field2\":{\"level2_field0\":\ + \"val0\",\"level2_field1\":[{\"level4_field30\":1, \"level4_field31\":\"val1\"},\"val1\"],\ + \"level2_field2\":{\"level3_field0\":\"val0\",\"level3_field1\":[\"val31\",\"val32\"],\ + \"level3_field2\":{\"level4_field0\":\"val0\",\"level4_field1\":\"val1\",\"level4_field2\":\"val2\"}}}}"; + EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document1, 0), GRD_OK); + const char *document2 = + "{\"level1_field00\":true,\"level1_field11\":\"\",\"level1_field22\":66,\"level1_field33\":-12.34,\ + \"level1_field44\":1}"; + EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document2, 0), GRD_OK); + /** + * @tc.steps: step2. call GRD_FindDoc with query.projection = {"level1_field0":1} + * @tc.expected: step2. return GRD_INVALID_ARGS + */ + Query query; + query.filter = "{}"; + query.projection = "{\"level1_field0\":\"1\"}"; + int query_flags_conceal = 0; + GRD_ResultSet *resultSet = nullptr; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step3. call GRD_FindDoc with query.projection = {"level1_field0":0} + * @tc.expected: step3. return GRD_INVALID_ARGS + */ + query.projection = "{\"level1_field0\":\"0\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step4. call GRD_FindDoc with query.projection = {"level1_field0":"true"} + * @tc.expected: step4. return GRD_INVALID_ARGS + */ + query.projection = "{\"level1_field0\":\"true\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step5. call GRD_FindDoc with query.projection = {"level1_field0":"false"} + * @tc.expected: step5. return GRD_INVALID_ARGS + */ + query.projection = "{\"level1_field0\":\"false\"}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step6. call GRD_FindDoc with query.projection = {"level1_field0":{}} + * @tc.expected: step6. return GRD_INVALID_ARGS + */ + query.projection = "{\"level1_field0\":{}}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step7. call GRD_FindDoc with query.projection = {"level1_field0":[1]} + * @tc.expected: step7. return GRD_INVALID_ARGS + */ + query.projection = "{\"level1_field0\":[1]}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step8. call GRD_FindDoc with query.projection whose depth is larger than 5 + * @tc.expected: step8. return GRD_INVALID_ARGS + */ + query.projection = "{\"level1_field2\":{\"level2_field2\":{\"level3_field2\":{\"level4_field2\":\ + {\"level5_field2\":1}}}}}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); + /** + * @tc.steps: step9. call GRD_FindDoc with query.projection whose depth is larger than 5 + * @tc.expected: step9. return GRD_INVALID_ARGS + */ + query.projection = "{\"t1.t2.t3.t4.t5\":1}"; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, query_flags_conceal, &resultSet), GRD_INVALID_ARGS); + EXPECT_TRUE(resultSet == nullptr); + EXPECT_EQ(GRD_Next(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_INVALID_ARGS); + EXPECT_EQ(GRD_FreeValue(value), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDBFindTest047 + * @tc.desc: Test find with auto generated _id + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest047, TestSize.Level1) +{ + /** + * @tc.steps: step1. Insert one data without _id + * @tc.expected: step1. return GRD_OK. + */ + const char *document1 = "{\"name\":\"doc100\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, COLLECTION_NAME, document1, 0), GRD_OK); + /** + * @tc.steps: step2. Invoke FindDoc method to query this record. + * @tc.expected: step2. return GRD_OK. + */ + Query query = {"{\"name\":\"doc100\"}", "{}"}; + GRD_ResultSet *resultSet = nullptr; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, document1, true); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step3. Invoke FindDoc method to query record with previous result. + * @tc.expected: step3. return GRD_OK. + */ + query.filter = value; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value1 = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value1), GRD_OK); + CompareValue(value, value1); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeValue(value1), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest048 + * @tc.desc: Test find with array field first match + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest048, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with array field + * @tc.expected: step1. succeed to get the record, the first matching record is g_document21. + */ + const char *filter = "{\"personInfo.school\":\"doc21\"}"; + Query query = {filter, "{}"}; + GRD_ResultSet *resultSet = nullptr; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document21, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest049 + * @tc.desc: Test find doc with nested array field matched. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest049, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with array field + * @tc.expected: step1. succeed to get the record, the first matching record is g_document21. + */ + const char *filter = "{\"t1.t2.t21\":\"zzz\"}"; + Query query = {filter, "{}"}; + GRD_ResultSet *resultSet = nullptr; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + CompareValueWithAutoGeneratedId(value, g_document24, true); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + /** + * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest050 + * @tc.desc: Test find doc with nested array field not matched + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest050, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with array field + * @tc.expected: step1. succeed to get the record, the first matching record is g_document21. + */ + const char *filter = "{\"t1.t2.t21\":\"zzy\"}"; + Query query = {filter, "{}"}; + GRD_ResultSet *resultSet = nullptr; + char *value = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); + /** + * @tc.steps: step2. Create filter with array field + * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. + */ + const char *filter1 = "{\"t1.t2\":\"xyz\"}"; + query = {filter1, "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentDBFindTest051 + * @tc.desc: Test find doc with array but array index is larger than array max. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBFindTest, DocumentDBFindTest051, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with part of array field and get the record according to filter condition. + * @tc.expected: step1. succeed to get the record, no record matched. + */ + const char *filter = "{\"personInfo.10.school\":\"B\"}"; + Query query = {filter, "{}"}; + GRD_ResultSet *resultSet = nullptr; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_NO_DATA); + char *value = nullptr; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_NOT_AVAILABLE); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + /** * @tc.name: DocumentDBFindTest052 * @tc.desc: Test field when id string len is large than max @@ -1214,26 +2314,28 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest045, TestSize.Level1) HWTEST_F(DocumentDBFindTest, DocumentDBFindTest052, TestSize.Level1) { /** - * @tc.steps: step1. Test with false uppercase projection - * @tc.expected: step1. Match g_document18, Return json with item, personInfo.age and _id. + * @tc.steps: step1. find document whose id string len is 899 + * @tc.expected: step1. return GRD_OK */ - const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - string collectionName1(MAX_COLLECTION_NAME, 'a'); - ASSERT_EQ(GRD_CreateCollection(g_db, collectionName1.c_str(), "", 0), GRD_OK); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName1.c_str(), query, 1, &resultSet), GRD_OK); + std::string head = "{\"_id\":\""; + std::string end = "\", \"name\":\"mike\"}"; + std::string filter = head + std::string(900 - 1, 'k') + end; + Query query = {filter.c_str(), "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); - ASSERT_EQ(GRD_DropCollection(g_db, collectionName1.c_str(), 0), GRD_OK); - - string collectionName2(MAX_COLLECTION_NAME + 1, 'a'); - EXPECT_EQ(GRD_FindDoc(g_db, collectionName2.c_str(), query, 1, &resultSet), GRD_OVER_LIMIT); - EXPECT_EQ(GRD_FindDoc(g_db, "", query, 1, &resultSet), GRD_INVALID_ARGS); + /** + * @tc.steps: step2. find document whose id string len is 900 + * @tc.expected: step2. return GRD_OVER_LIMIT + */ + std::string filter1 = head + std::string(900, 'k') + end; + query = {filter1.c_str(), "{}"}; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OVER_LIMIT); } /** * @tc.name: DocumentDBFindTest053 - * @tc.desc: Test with invalid flags + * @tc.desc: Test find when id type is not string * @tc.type: FUNC * @tc.require: * @tc.author: mazhao @@ -1265,7 +2367,7 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest053, TestSize.Level1) /** * @tc.name: DocumentDBFindTest054 - * @tc.desc: Test with null g_db and resultSet, filter. + * @tc.desc: Test find when filter contains compare or element type field. * @tc.type: FUNC * @tc.require: * @tc.author: mazhao @@ -1273,27 +2375,24 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest053, TestSize.Level1) HWTEST_F(DocumentDBFindTest, DocumentDBFindTest054, TestSize.Level1) { /** - * @tc.steps: step1. Test with null g_db. + * @tc.steps: step1. Test find when filter contains compare or element type field. * @tc.expected: step1. Return GRD_INVALID_ARGS. */ - const char *filter = "{\"_id\" : \"18\"}"; GRD_ResultSet *resultSet = nullptr; - const char *projectionInfo = "{}"; - Query query = { filter, projectionInfo }; - EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); - - /** - * @tc.steps: step2. Test with null resultSet. - * @tc.expected: step2. Return GRD_INVALID_ARGS. - */ - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, nullptr), GRD_INVALID_ARGS); - - /** - * @tc.steps: step1. Test with query that has two nullptr data. - * @tc.expected: step1. Return GRD_INVALID_ARGS. - */ - query = { nullptr, nullptr }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 0, &resultSet), GRD_INVALID_ARGS); + Query query; + query.filter = R"({"t1":{"t2":{"$gt":1}}})"; + query.projection = "{}"; + EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + query.filter = R"({"$gt":{"t2":{"t1":1}}})"; + EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + /** + * @tc.steps: step2. filter with element type filed. + * @tc.expected: step2. return GRD_INVALID_ARGS. + */ + query.filter = R"({"$exists":{"t2":{"t1":1}}})"; + EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); + query.filter = R"({"t1":{"t2":{"$exists":"t4"}}})"; + EXPECT_EQ(GRD_FindDoc(nullptr, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); } /** @@ -1331,36 +2430,6 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest055, TestSize.Level1) EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } -/** - * @tc.name: DocumentDBFindTest056 - * @tc.desc: Test findDoc with no _id. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDBFindTest, DocumentDBFindTest056, TestSize.Level1) -{ - /** - * @tc.steps: step1. Create filter with _id and get the record according to filter condition. - * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. - */ - const char *filter = "{\"personInfo\" : {\"school\":\"B\"}}"; - GRD_ResultSet *resultSet = nullptr; - Query query = { filter, "{}" }; - EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - EXPECT_EQ(GRD_Next(resultSet), GRD_OK); - char *value = nullptr; - EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); - CompareValue(value, g_document5); - EXPECT_EQ(GRD_FreeValue(value), GRD_OK); - /** - * @tc.steps: step2. Invoke GRD_Next to get the next matching value. Release resultSet. - * @tc.expected: step2. Cannot get next record, return GRD_NO_DATA. - */ - EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); -} - /** * @tc.name: DocumentDBFindTest056 * @tc.desc: Test findDoc with no _id. @@ -1440,14 +2509,17 @@ HWTEST_F(DocumentDBFindTest, DocumentDBFindTest061, TestSize.Level1) char *value = nullptr; EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); CompareValue(value, document061); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); CompareValue(value, document062); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); CompareValue(value, document063); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); EXPECT_EQ(GRD_Next(resultSet), GRD_OK); EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK);