From 519812a4d82a1b0cf141e11a1f13e077a7ce5757 Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 14:39:49 +0800 Subject: [PATCH 1/2] change insert UT Signed-off-by: Jeremyzz --- .../unittest/api/documentdb_insert_test.cpp | 219 +++++++++++------- 1 file changed, 138 insertions(+), 81 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp index c3ad96cb..793c91a1 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp @@ -15,6 +15,7 @@ #include #include +#include #include "documentdb_test_utils.h" #include "grd_base/grd_db_api.h" @@ -116,23 +117,29 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest002, TestSize.Level1) const char *document2 = "{\"_id\" : true, \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); /** - * @tc.steps:step2.Insert a document whose _id is NULL - * @tc.expected:step2.GRD_INVALID_ARGS + * @tc.steps:step3.Insert a document whose _id is NULL + * @tc.expected:step3.GRD_INVALID_ARGS */ const char *document3 = "{\"_id\" : null, \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); /** - * @tc.steps:step2.Insert a document whose _id is ARRAY - * @tc.expected:step2.GRD_INVALID_ARGS + * @tc.steps:step4.Insert a document whose _id is ARRAY + * @tc.expected:step4.GRD_INVALID_ARGS */ const char *document4 = "{\"_id\" : [\"2\"], \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); /** - * @tc.steps:step2.Insert a document whose _id is OBJECT - * @tc.expected:step2.GRD_INVALID_ARGS + * @tc.steps:step5.Insert a document whose _id is OBJECT + * @tc.expected:step5.GRD_INVALID_ARGS */ const char *document5 = "{\"_id\" : {\"val\" : \"2\"}, \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document5, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step6.Insert a document whose _id is double type + * @tc.expected:step6 .GRD_INVALID_ARGS + */ + const char *document6 = "{\"_id\" : 1.99999, \"name\" : \"Chuan\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document6, 0), GRD_INVALID_ARGS); } /** @@ -323,21 +330,21 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest014, TestSize.Level1) * @tc.expected:step1.GRD_INVALID_ARGS */ const char *document1 = R""({"level1" : {"level2" : {"level3" : {"level4": {"level5" : 1}}, - "level3_2" : "level3_2_val"}},"_id":"14"})""; + "level3_2" : "level3_2_val"}}})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_INVALID_ARGS); /** * @tc.steps:step1.document's JSON depth is larger than 4, which is 5.But with array type. * @tc.expected:step1.GRD_INVALID_ARGS */ const char *document2 = R""({"level1" : {"level2" : {"level3" : [{ "level5" : "level5_1val", - "level5_2":"level5_2_val"}, "level4_val1","level4_val2"], "level3_2" : "level3_2_val"}}, "_id":"14"})""; + "level5_2":"level5_2_val"}, "level4_val1","level4_val2"], "level3_2" : "level3_2_val"}}})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); /** * @tc.steps:step1.document's JSON depth is 4 * @tc.expected:step1.GRD_OK */ const char *document3 = R""({"level1" : {"level2" : {"level3" : { "level4" : "level5_1val"}, - "level3_2" : "level3_2_val"}}, "_id":"14"})""; + "level3_2" : "level3_2_val"}}})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_OK); } @@ -354,7 +361,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest015, TestSize.Level1) * @tc.steps:step1.document's JSON is bigger than 512k - 1 * @tc.expected:step1.GRD_INVALID_ARGS */ - string documentPart1 = "{ \"_id\" : \"15\", \"textVal\" : \" "; + string documentPart1 = "{\"textVal\" : \" "; string documentPart2 = "\" }"; string jsonVal = string(512 * 1024 - documentPart1.size() - documentPart2.size(), 'k'); string document = documentPart1 + jsonVal + documentPart2; @@ -380,7 +387,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest016, TestSize.Level1) * @tc.steps:step1.document JSON string contains irregular char. * @tc.expected:step1.GRD_OK */ - const char *document1 = "{\"_id\" : \"16\", \"name\" : \"!@#Ori\"}"; + const char *document1 = "{\"name\" : \"!@#Ori\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } @@ -397,7 +404,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest017, TestSize.Level1) * @tc.steps:step1.document JSON string contains invalid value type such as BLOB type. * @tc.expected:step1.GRD_INVALID_FORMAT. */ - const char *document = "{\"_id\" : \"17\", \"level1\" : {\"level2\" : {\"level3\" : {\"level4\" : x'1234'\ + const char *document = "{\"level1\" : {\"level2\" : {\"level3\" : {\"level4\" : x'1234'\ } } }, \"level1_2\" : \"level1_2Val\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document, 0), GRD_INVALID_FORMAT); } @@ -429,14 +436,49 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest018, TestSize.Level1) HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest019, TestSize.Level1) { /** - * @tc.steps:step1.Insert a normal documents which _id is in the end of the string + * @tc.steps:step1.Insert a normal documents. * @tc.expected:step1.GRD_OK. */ - const char *document1 = "{\"name\" : \"Jack\", \"age\" : 18, \"friend\" : {\"name\" : \" lucy\"}, \"_id\" : " - "\"19\"}"; + const char *document1 = "{\"name\" : \"Jack\", \"age\" : 18, \"friend\" : {\"name\" : \" lucy\"}}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } +/** + * @tc.name: DocumentDBInsertTest020 + * @tc.desc: Insert a normal documents + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest020, TestSize.Level1) +{ + /** + * @tc.steps:step1.parameter document is NULL + * This will Insert a null document with an auto-generated _id field. + * @tc.expected:step1.GRD_INVALID_ARGS + */ + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, nullptr, 0), GRD_INVALID_ARGS); +} + +/** + * @tc.name: DocumentDBInsertTest021 + * @tc.desc: Insert ten times, with nullptr parameter document + * This will Insert a null document with an auto-generated _id field. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest021, TestSize.Level1) +{ + /** + * @tc.steps:step1.Insert ten times, with nullptr parameter document + * This will Insert a null document with an auto-generated _id field. + * @tc.expected:step1.GRD_INVALID_ARGS + */ + for (int i = 0; i < 10; i++) { + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, NULL, 0), GRD_INVALID_ARGS); + } +} /** * @tc.name: DocumentDBInsertTest022 * @tc.desc: parameter collectionName is equal to 256 charactors @@ -563,14 +605,14 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest025, TestSize.Level1) * @tc.steps:step1.documents JSON depth is 4, which is allowed. * @tc.expected:step1.GRD_OK. */ - const char *document1 = "{\"_id\" : \"25_0\", \"level1\" : {\"level2\" : {\"level3\" :\ + const char *document1 = "{\"level1\" : {\"level2\" : {\"level3\" :\ {\"level4\" : \"level4Val\"}}} , \"level1_2\" : \"level1_2Val\" }"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); /** * @tc.steps:step2.documents JSON depth is exactly 4. * @tc.expected:step2.GRD_OK. */ - const char *document2 = "{\"_id\" : \"25_1\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \ + const char *document2 = "{\"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \ \"student_info\" : [{\"name\":\"张三\", \"age\" : 18, \"sex\" : \"男\"}, \ { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\"] }]}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_OK); @@ -578,7 +620,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest025, TestSize.Level1) * @tc.steps:step3.documents JSON depth is exactly 4, but the last field in array contains leading number * @tc.expected:step3.GRD_INVALID_ARGS. */ - const char *document3 = "{\"_id\" : \"25_2\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \ + const char *document3 = "{\"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \ \"student_info\" : [{\"name\":\"张三\", \"age\" : 18, \"sex\" : \"男\"}, \ [\"qw\", \"dr\", 0, \"ab\", {\"0ab\" : null}]]}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document3, 0), GRD_INVALID_ARGS); @@ -586,7 +628,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest025, TestSize.Level1) * @tc.steps:step4.documents JSON depth is exactly 5. * @tc.expected:step4.GRD_INVALID_ARGS. */ - const char *document4 = "{\"_id\" : \"25_3\", \"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \ + const char *document4 = "{\"class_name\" : \"计算机科学一班\", \"signed_info\" : true, \ \"student_info\" : [{\"name\":\"张三\", \"age\" : 18, \"sex\" : \"男\"}, \ { \"newName1\" : [\"qw\", \"dr\", 0, \"ab\", {\"level5\" : 1}] }]}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document4, 0), GRD_INVALID_ARGS); @@ -605,13 +647,8 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest, TestSize.Level1) * @tc.steps:step1.Insert 100 normal documents continuously * @tc.expected:step1.GRD_OK. */ - string document1 = "{\"_id\" : "; - string document2 = "\""; - string document4 = "\""; - string document5 = ", \"name\" : \"Ori\"}"; + string document = "{\"name\" : \"Ori\"}"; for (int i = 0; i < 5; i++) { - string document_midlle = "26" + std::to_string(i); - string document = document1 + document2 + document_midlle + document4 + document5; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); } } @@ -631,7 +668,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest035, TestSize.Level1) * upper &lower case charactors, numbers and underline. * @tc.expected:step1.GRD_OK. */ - const char *document1 = "{\"_id\" : \"35\", \"A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; + const char *document1 = "{\"A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); /** * @tc.steps:step1.Insert a document whose value contains @@ -639,7 +676,7 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest035, TestSize.Level1) * But the field started with number, which is not allowed. * @tc.expected:step1.GRD_OK. */ - const char *document2 = "{\"_id\" : \"35_2\", \"1A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; + const char *document2 = "{\"1A_aBdk_324_\" : \"value\", \"name\" : \"Chuan\"}"; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document2, 0), GRD_INVALID_ARGS); } @@ -721,90 +758,110 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest038, TestSize.Level1) HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest039, TestSize.Level1) { /** - * @tc.steps:step1.Insert a filter which _id value's lens is larger than MAX_ID_LENS. + * @tc.steps:step1.Insert a filter which _id string len is 899 * @tc.expected:step1.GRD_OVER_LIMIT. */ - string document1 = "{\"_id\" : "; - string document2 = "\""; - string document4 = "\""; - string document5 = ", \"name\" : \"Ori\"}"; - string document_midlle(MAX_ID_LENS + 1, 'k'); - string document = document1 + document2 + document_midlle + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OVER_LIMIT); + string head = "{\"_id\" : \""; + string end = "\", \"name\":\"mike\"}"; + string doc = head + std::string(900 - 1, 'k') + end; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, doc.c_str(), 0), GRD_OK); /** * @tc.steps:step1.Insert a filter which _id value's lens is equal as MAX_ID_LENS. * @tc.expected:step1.GRD_OK. */ - string document_midlle2(MAX_ID_LENS, 'k'); - document = document1 + document2 + document_midlle2 + document4 + document5; - EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document.c_str(), 0), GRD_OK); + string doc1 = head + std::string(900, 'k') + end; + EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, doc1.c_str(), 0), GRD_OVER_LIMIT); } /** * @tc.name: DocumentUpdataApiTest040 - * @tc.desc: Insert a filter which _id value's lens is larger than MAX_ID_LENS + * @tc.desc: Test insert under condition with multiple connection * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest040, TestSize.Level1) { - const char *filter = "{\"_id\" : \"1\"}"; - const char *updata2 = "{\"objectInfo.child.child\" : {\"child\":{\"child\":null}}}"; - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, updata2, 0), GRD_INVALID_ARGS); + /** + * @tc.steps:step1.create multiple collection. + * @tc.expected:step1.GRD_OK. + */ + string head = "{\"_id\" : \""; + string end = "\", \"name\":\"mike\"}"; + string doc = ""; + const int connectCnt = 16; + GRD_DB *dbVec[connectCnt] = { g_db }; + for (int i = 1; i < connectCnt; i++) { + int status = GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &dbVec[i]); + EXPECT_EQ(status, GRD_OK); + } + /** + * @tc.steps:step2. Insert document(with and without _id) using multiple collection + * @tc.expected:step2.GRD_OK. + */ + string doc2 = "{\"name\": \"doc\"}"; + for (int i = 1; i < connectCnt; i++) { + doc = head + std::to_string(i) + 'a' + end; + EXPECT_EQ(GRD_InsertDoc(dbVec[i], RIGHT_COLLECTION_NAME, doc.c_str(), 0), GRD_OK); + EXPECT_EQ(GRD_InsertDoc(dbVec[i], RIGHT_COLLECTION_NAME, doc2.c_str(), 0), GRD_OK); + } + /** + * @tc.steps:step3. Close the connection created before + * @tc.expected:step3.GRD_OK. + */ + for (int i = 1; i < connectCnt; i++) { + EXPECT_EQ(GRD_DBClose(dbVec[i], 0), GRD_OK); + } } -/** - * @tc.name: DocumentUpdataApiTest041 - * @tc.desc: Insert a filter which _id value's lens is larger than MAX_ID_LENS - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest041, TestSize.Level1) +static void CreateDbAndInsert(int i) { - const char *filter = "{\"_id\" : \"1\"}"; - const char *updata1 = "{\"_id\" : \"6\"}"; - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, updata1, 0), GRD_INVALID_ARGS); + GRD_DB *db = nullptr; + string head = "{\"_id\" : \""; + string end = "\", \"name\":\"mike\"}"; + string doc = head + std::to_string(i) + 'b' + end; + string doc2 = "{\"name\": \"doc\"}"; + int status = GRD_DBOpen(g_path.c_str(), nullptr, GRD_DB_OPEN_CREATE, &db); + EXPECT_EQ(status, GRD_OK); + EXPECT_EQ(GRD_InsertDoc(db, RIGHT_COLLECTION_NAME, doc.c_str(), 0), GRD_OK); + EXPECT_EQ(GRD_InsertDoc(db, RIGHT_COLLECTION_NAME, doc2.c_str(), 0), GRD_OK); + EXPECT_EQ(GRD_DBClose(db, 0), GRD_OK); } -/** - * @tc.name: DocumentUpdataApiTest042 - * @tc.desc: Insert a filter which _id value's lens is larger than MAX_ID_LENS - * @tc.type: FUNC - * @tc.require: - * @tc.author: mazhao - */ -HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest042, TestSize.Level1) +static void CreateAndDropCollection(int i) { - const char *filter = "{\"_id\" : \"1\"}"; - const char *updata1 = "{\"age$\" : \"21\"}"; - const char *updata2 = "{\"bonus..traffic\" : 100}"; - const char *updata3 = "{\"0item\" : 100}"; - const char *updata4 = "{\"item\" : 1.79769313486232e308}"; - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, updata1, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, updata2, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, updata3, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, updata4, 0), GRD_INVALID_ARGS); + string collection = "collection" + std::to_string(i); + EXPECT_EQ(GRD_CreateCollection(g_db, collection.c_str(), "", 0), GRD_OK); + EXPECT_EQ(GRD_DropCollection(g_db, collection.c_str(), 0), GRD_OK); } +struct CreateDbAndInsertThread { + CreateDbAndInsertThread(int id) : m_id(id) {} + void operator()() const + { + CreateDbAndInsert(m_id); + } + int m_id; +}; + /** - * @tc.name: DocumentUpdataApiTest043 - * @tc.desc: Insert a filter which _id value's lens is larger than MAX_ID_LENS + * @tc.name: DocumentInsertApiTest041 + * @tc.desc: Test insert under multi-thread condition * @tc.type: FUNC * @tc.require: * @tc.author: mazhao */ -HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest043, TestSize.Level1) -{ - const char *filter = "{\"_id\" : \"1\"}"; - const char *updata1 = "{\"age\" : 21}"; - EXPECT_EQ(GRD_UpdateDoc(g_db, NULL, filter, updata1, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(g_db, "", filter, updata1, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(NULL, RIGHT_COLLECTION_NAME, filter, updata1, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, NULL, updata1, 0), GRD_INVALID_ARGS); - EXPECT_EQ(GRD_UpdateDoc(g_db, RIGHT_COLLECTION_NAME, filter, NULL, 0), GRD_INVALID_ARGS); +HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest041, TestSize.Level1) +{ + vector threads; + const int numThreads = 16; + for (int i = 1; i < numThreads; i++) { + threads.emplace_back(std::thread(CreateDbAndInsertThread(i))); + } + for (auto &thread : threads) { + thread.join(); + } } HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest044, TestSize.Level1) -- Gitee From b21dce2c356524aa2dad1eb08dca8a33389b746a Mon Sep 17 00:00:00 2001 From: Jeremyzz Date: Fri, 26 May 2023 15:07:12 +0800 Subject: [PATCH 2/2] add key ut Signed-off-by: Jeremyzz --- .../unittest/api/documentdb_insert_test.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp index 793c91a1..34d52866 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd/test/unittest/api/documentdb_insert_test.cpp @@ -17,6 +17,7 @@ #include #include +#include "document_key.h" #include "documentdb_test_utils.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" @@ -883,4 +884,42 @@ HWTEST_F(DocumentDBInsertTest, DocumentDBInsertTest046, TestSize.Level1) const char *document1 = R""({})""; EXPECT_EQ(GRD_InsertDoc(g_db, RIGHT_COLLECTION_NAME, document1, 0), GRD_OK); } + +/** + * @tc.name: GetOidKey003 + * @tc.desc: Check the range of key which is not repeated + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentDBInsertTest, GetOidKey003, TestSize.Level1) +{ + std::set keySet; + DocumentDB::DocKey docKey; + uint32_t curTime = time(nullptr); + int num = 0; + for (int i = 0; i < 77777; ++i) { // 77777 is a random num greater than uint16. + /** + * @tc.steps:step1. Get objectId key. + * @tc.expected:step1. Return GRD_OK and key is not NULL. + */ + EXPECT_EQ(DocumentDB::DocumentKey::GetOidDocKey(docKey), GRD_OK); + EXPECT_NE(docKey.key.empty(), true); + /** + * @tc.steps:step2. Cache the hex key + * @tc.expected:step2. Up to 65536 keys are not equal in one second. + */ + uint32_t now = 0; + sscanf(docKey.key.c_str(), "%08x", &now); + if (curTime != now) { + break; + } + if (num > 65535) { + EXPECT_NE(keySet.find(docKey.key), keySet.end()); + } + num++; + EXPECT_EQ(keySet.find(docKey.key), keySet.end()); + keySet.insert(docKey.key); + } +} } // namespace -- Gitee