1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <memory>
19
20 #include "dk_database.h"
21
22 namespace OHOS::FileManagement::CloudSync::Test {
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace std;
26 using namespace DriveKit;
27
28 class DKDatabaseTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 };
SetUpTestCase(void)35 void DKDatabaseTest::SetUpTestCase(void)
36 {
37 GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39
TearDownTestCase(void)40 void DKDatabaseTest::TearDownTestCase(void)
41 {
42 GTEST_LOG_(INFO) << "TearDownTestCase";
43 }
44
SetUp(void)45 void DKDatabaseTest::SetUp(void)
46 {
47 GTEST_LOG_(INFO) << "SetUp";
48 }
49
TearDown(void)50 void DKDatabaseTest::TearDown(void)
51 {
52 GTEST_LOG_(INFO) << "TearDown";
53 }
54
55 /**
56 * @tc.name: SaveRecord
57 * @tc.desc: Verify the DKDatabase::SaveRecord function
58 * @tc.type: FUNC
59 * @tc.require: SR000HRKKA
60 */
61 HWTEST_F(DKDatabaseTest, SaveRecord, TestSize.Level1)
62 {
63 std::shared_ptr<DKContainer> container = nullptr;
64 std::shared_ptr<DKContext> context = nullptr;
65 DKRecord record;
66 DKDatabase::SaveRecordCallback callback = nullptr;
67 DKSavePolicy policy = DKSavePolicy::DK_SAVE_IF_UNCHANGED;
68 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
69
70 DKDatabase mdkDatabase(container, scope);
71 DKLocalErrorCode ret = mdkDatabase.SaveRecord(context, move(record), policy, callback);
72 EXPECT_EQ(ret, DKLocalErrorCode::NO_ERROR);
73 }
74
75 /**
76 * @tc.name: FetchRecordWithId
77 * @tc.desc: Verify the DKDatabase::FetchRecordWithId function
78 * @tc.type: FUNC
79 * @tc.require: SR000HRKKA
80 */
81 HWTEST_F(DKDatabaseTest, FetchRecordWithId, TestSize.Level1)
82 {
83 std::shared_ptr<DKContainer> container = nullptr;
84 std::shared_ptr<DKContext> context = nullptr;
85 DKDatabase::FetchRecordCallback callback = nullptr;
86 DKRecordType recordType = "DKDatabaseTest";
87 std::vector<DKFieldKey> desiredKeys;
88 DKRecordId recordId = "FetchRecordWithId";
89 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
90
91 DKDatabase mdkDatabase(container, scope);
92 DKLocalErrorCode ret = mdkDatabase.FetchRecordWithId(context, recordType,
93 recordId, desiredKeys, callback);
94 EXPECT_EQ(ret, DKLocalErrorCode::NO_ERROR);
95 }
96
97 /**
98 * @tc.name: FetchRecordsWithQuery
99 * @tc.desc: Verify the DKDatabase::FetchRecordsWithQuery function
100 * @tc.type: FUNC
101 * @tc.require: SR000HRKKA
102 */
103 HWTEST_F(DKDatabaseTest, FetchRecordsWithQuery, TestSize.Level1)
104 {
105 std::shared_ptr<DKContainer> container = nullptr;
106 std::shared_ptr<DKContext> context = nullptr;
107 DKDatabase::FetchRecordsCallback callback = nullptr;
108 std::vector<DKFieldKey> desiredKeys;
109 DKQuery query;
110 int resultLimit = 1;
111 DKQueryCursor cursor = "FetchRecordsWithQuery";
112 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
113
114 DKDatabase mdkDatabase(container, scope);
115 DKLocalErrorCode ret = mdkDatabase.FetchRecordsWithQuery(context, desiredKeys,
116 query, resultLimit, cursor, callback);
117 EXPECT_EQ(ret, DKLocalErrorCode::NO_ERROR);
118 }
119
120 /**
121 * @tc.name: GetStartCursor
122 * @tc.desc: Verify the DKDatabase::GetStartCursor function
123 * @tc.type: FUNC
124 * @tc.require: SR000HRKKA
125 */
126 HWTEST_F(DKDatabaseTest, GetStartCursor, TestSize.Level1)
127 {
128 std::shared_ptr<DKContainer> container = nullptr;
129 DKRecordType recordType = "DKDatabaseTest";
130 DKQueryCursor cursor = "FetchDatabaseChanges";
131 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
132
133 DKDatabase mdkDatabase(container, scope);
134 DKError ret = mdkDatabase.GetStartCursor(recordType, cursor);
135 EXPECT_EQ(ret.dkErrorCode, DKLocalErrorCode::NO_ERROR);
136 }
137
138 /**
139 * @tc.name: GenerateIds
140 * @tc.desc: Verify the DKDatabase::GenerateIds function
141 * @tc.type: FUNC
142 * @tc.require: SR000HRKKA
143 */
144 HWTEST_F(DKDatabaseTest, GenerateIds, TestSize.Level1)
145 {
146 std::shared_ptr<DKContainer> container = nullptr;
147 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
148 int count = 0;
149 std::vector<DKRecordId> ids;
150
151 DKDatabase mdkDatabase(container, scope);
152 DKError ret = mdkDatabase.GenerateIds(count, ids);
153 EXPECT_EQ(ret.dkErrorCode, DKLocalErrorCode::NO_ERROR);
154 }
155
156 /**
157 * @tc.name: GetLock
158 * @tc.desc: Verify the DKDatabase::GetLock function
159 * @tc.type: FUNC
160 * @tc.require: SR000HRKKA
161 */
162 HWTEST_F(DKDatabaseTest, GetLock, TestSize.Level1)
163 {
164 std::shared_ptr<DKContainer> container = nullptr;
165 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
166 DKLock lock;
167
168 DKDatabase mdkDatabase(container, scope);
169 DKError ret = mdkDatabase.GetLock(lock);
170 EXPECT_EQ(ret.dkErrorCode, DKLocalErrorCode::NO_ERROR);
171 }
172
173 /**
174 * @tc.name: NewAssetReadSession
175 * @tc.desc: Verify the DKDatabase::NewAssetReadSession function
176 * @tc.type: FUNC
177 * @tc.require: SR000HRKKA
178 */
179 HWTEST_F(DKDatabaseTest, NewAssetReadSession, TestSize.Level1)
180 {
181 std::shared_ptr<DKContainer> container = nullptr;
182 DKDatabaseScope scope = DKDatabaseScope::DK_PUBLIC_DATABASE;
183 DKRecordType recordType = "";
184 DKRecordId recordId = "";
185 DKFieldKey assetKey = "";
186 DKAssetPath assetPath = "";
187
188 DKDatabase mdkDatabase(container, scope);
189 EXPECT_NE(nullptr, mdkDatabase.NewAssetReadSession(recordType, recordId, assetKey, assetPath));
190 }
191 } // namespace OHOS::FileManagement::CloudSync::Test
192