1 /* 2 * Copyright (C) 2025 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 <gtest/gtest.h> 17 #include <unistd.h> 18 19 #include "datashare_errno.h" 20 #include "datashare_itypes_utils.h" 21 #include "datashare_log.h" 22 #include "datashare_abs_result_set.h" 23 #include "datashare_result_set.h" 24 #include "ikvstore_data_service.h" 25 #include "ipc_types.h" 26 #include "ishared_result_set_stub.h" 27 #include "itypes_util.h" 28 #include "message_parcel.h" 29 #include "gmock/gmock.h" 30 31 namespace OHOS { 32 namespace DataShare { 33 using namespace testing::ext; 34 class DataShareAbsResultSetTest : public testing::Test { 35 public: SetUpTestCase(void)36 static void SetUpTestCase(void){}; TearDownTestCase(void)37 static void TearDownTestCase(void){}; SetUp()38 void SetUp(){}; TearDown()39 void TearDown(){}; 40 }; 41 42 class MockDataShareAbsResultSet : public DataShareAbsResultSet { 43 public: 44 MOCK_METHOD1(GoToRow, int(int)); 45 MOCK_METHOD1(GetRowCount, int(int &)); 46 MOCK_METHOD1(GetAllColumnNames, int(std::vector<std::string> &)); 47 MOCK_METHOD1(GetColumnCount, int(int &)); 48 }; 49 50 class MockDataShareAbsResultSet2 : public DataShareAbsResultSet { 51 public: 52 MOCK_METHOD1(GetAllColumnNames, int(std::vector<std::string> &)); 53 }; 54 55 /** 56 * @tc.name: GoToTest001 57 * @tc.desc: test GoTo function when GoToRow return E_ERROR 58 * @tc.type: FUNC 59 * @tc.require: issueIC9GIH 60 * @tc.precon: None 61 * @tc.step: 62 1.Creat a MockDataShareAbsResultSet object 63 2.call GoTo function when GoToRow return E_ERROR and check the result 64 * @tc.experct: GoTo failed and return E_ERROR 65 */ 66 HWTEST_F(DataShareAbsResultSetTest, GoToTest001, TestSize.Level0) 67 { 68 LOG_INFO("DataShareAbsResultSetTest GoToTest001::Start"); 69 MockDataShareAbsResultSet mockResultSet; 70 int offset = 1; 71 EXPECT_CALL(mockResultSet, GoToRow(testing::_)) 72 .WillOnce(testing::Return(E_ERROR)); 73 auto result = mockResultSet.GoTo(offset); 74 EXPECT_EQ(result, E_ERROR); 75 LOG_INFO("DataShareAbsResultSetTest GoToTest001::End"); 76 } 77 78 /** 79 * @tc.name: GoToTest002 80 * @tc.desc: test GoTo function 81 * @tc.type: FUNC 82 * @tc.require: issueIC9GIH 83 * @tc.precon: None 84 * @tc.step: 85 1.Creat a MockDataShareAbsResultSet object 86 2.call GoTo function and check the result 87 * @tc.experct: GoTo success and return E_OK 88 */ 89 HWTEST_F(DataShareAbsResultSetTest, GoToTest002, TestSize.Level0) 90 { 91 LOG_INFO("DataShareAbsResultSetTest GoToTest002::Start"); 92 DataShareAbsResultSet dataShareAbsResultSet; 93 int offset = 1; 94 auto result = dataShareAbsResultSet.GoTo(offset); 95 EXPECT_EQ(result, E_OK); 96 LOG_INFO("DataShareAbsResultSetTest GoToTest002::End"); 97 } 98 99 /** 100 * @tc.name: IsEndedTest001 101 * @tc.desc: test IsEnded function when GetRowCount return E_ERROR 102 * @tc.type: FUNC 103 * @tc.require: issueIC9GIH 104 * @tc.precon: None 105 * @tc.step: 106 1.Creat a MockDataShareAbsResultSet object 107 2.call IsEnded function when GetRowCount return E_ERROR and check the result 108 * @tc.experct: IsEnded failed and return E_ERROR 109 */ 110 HWTEST_F(DataShareAbsResultSetTest, IsEndedTest001, TestSize.Level0) 111 { 112 LOG_INFO("DataShareAbsResultSetTest IsEndedTest001::Start"); 113 MockDataShareAbsResultSet mockResultSet; 114 EXPECT_CALL(mockResultSet, GetRowCount(testing::_)) 115 .WillOnce(testing::Return(E_ERROR)); 116 bool test = true; 117 auto result = mockResultSet.IsEnded(test); 118 EXPECT_EQ(result, E_ERROR); 119 LOG_INFO("DataShareAbsResultSetTest IsEndedTest001::End"); 120 } 121 122 /** 123 * @tc.name: IsEndedTest002 124 * @tc.desc: test IsEnded function 125 * @tc.type: FUNC 126 * @tc.require: issueIC9GIH 127 * @tc.precon: None 128 * @tc.step: 129 1.Creat a MockDataShareAbsResultSet object 130 2.call IsEnded function and check the result 131 * @tc.experct: IsEnded success and return E_OK 132 */ 133 HWTEST_F(DataShareAbsResultSetTest, IsEndedTest002, TestSize.Level0) 134 { 135 LOG_INFO("DataShareAbsResultSetTest IsEndedTest002::Start"); 136 DataShareAbsResultSet dataShareAbsResultSet; 137 bool test = true; 138 auto result = dataShareAbsResultSet.IsEnded(test); 139 EXPECT_EQ(result, E_OK); 140 LOG_INFO("DataShareAbsResultSetTest IsEndedTest002::End"); 141 } 142 143 /** 144 * @tc.name: GetColumnCountTest001 145 * @tc.desc: test GetColumnCount function when GetAllColumnNames return E_ERROR 146 * @tc.type: FUNC 147 * @tc.require: issueIC9GIH 148 * @tc.precon: None 149 * @tc.step: 150 1.Creat a MockDataShareAbsResultSet2 object 151 2.call GetColumnCount function when GetAllColumnNames return E_ERROR and check the result 152 * @tc.experct: GetColumnCount failed and return E_ERROR 153 */ 154 HWTEST_F(DataShareAbsResultSetTest, GetColumnCountTest001, TestSize.Level0) 155 { 156 LOG_INFO("DataShareAbsResultSetTest GetColumnCountTest001::Start"); 157 MockDataShareAbsResultSet2 mockResultSet; 158 int offset = -1; 159 mockResultSet.count_ = -1; 160 EXPECT_CALL(mockResultSet, GetAllColumnNames(testing::_)) 161 .WillOnce(testing::Return(E_ERROR)); 162 auto result = mockResultSet.GetColumnCount(offset); 163 EXPECT_EQ(result, E_ERROR); 164 LOG_INFO("DataShareAbsResultSetTest GetColumnCountTest001::End"); 165 } 166 167 /** 168 * @tc.name: GetColumnName001 169 * @tc.desc: test GetColumnName function when GetColumnCount return E_ERROR 170 * @tc.type: FUNC 171 * @tc.require: issueIC9GIH 172 * @tc.precon: None 173 * @tc.step: 174 1.Creat a MockDataShareAbsResultSet object 175 2.call GetColumnName function when GetColumnCount return E_ERROR and check the result 176 * @tc.experct: GetColumnName failed and return E_ERROR 177 */ 178 HWTEST_F(DataShareAbsResultSetTest, GetColumnName001, TestSize.Level0) 179 { 180 LOG_INFO("DataShareAbsResultSetTest GetColumnName001::Start"); 181 MockDataShareAbsResultSet mockResultSet; 182 int columnIndex = 1; 183 std::string columnName = "test"; 184 EXPECT_CALL(mockResultSet, GetColumnCount(testing::_)) 185 .WillOnce(testing::Return(E_ERROR)); 186 auto result = mockResultSet.GetColumnName(columnIndex, columnName); 187 EXPECT_EQ(result, E_ERROR); 188 LOG_INFO("DataShareAbsResultSetTest GetColumnName001::End"); 189 } 190 191 /** 192 * @tc.name: GetColumnName002 193 * @tc.desc: test GetColumnName function when columnIndex >= 0 194 * @tc.type: FUNC 195 * @tc.require: issueIC9GIH 196 * @tc.precon: None 197 * @tc.step: 198 1.Creat a MockDataShareAbsResultSet object 199 2.call GetColumnName function when columnIndex >= 0 and check the result 200 * @tc.experct: GetColumnName failed and return E_INVALID_COLUMN_INDEX 201 */ 202 HWTEST_F(DataShareAbsResultSetTest, GetColumnName002, TestSize.Level0) 203 { 204 LOG_INFO("DataShareAbsResultSetTest MarshallingTest002::Start"); 205 DataShareAbsResultSet dataShareAbsResultSet; 206 int columnIndex = 1; 207 std::string columnName = "test"; 208 auto result = dataShareAbsResultSet.GetColumnName(columnIndex, columnName); 209 EXPECT_EQ(result, E_INVALID_COLUMN_INDEX); 210 LOG_INFO("DataShareAbsResultSetTest MarshallingTest002::End"); 211 } 212 213 /** 214 * @tc.name: GetColumnName003 215 * @tc.desc: test GetColumnName function when columnIndex < 0 216 * @tc.type: FUNC 217 * @tc.require: issueIC9GIH 218 * @tc.precon: None 219 * @tc.step: 220 1.Creat a MockDataShareAbsResultSet object 221 2.call GetColumnName function when columnIndex < 0 and check the result 222 * @tc.experct: GetColumnName failed and return E_INVALID_COLUMN_INDEX 223 */ 224 HWTEST_F(DataShareAbsResultSetTest, GetColumnName003, TestSize.Level0) 225 { 226 LOG_INFO("DataShareAbsResultSetTest MarshallingTest002::Start"); 227 DataShareAbsResultSet dataShareAbsResultSet; 228 int columnIndex = -1; 229 std::string columnName = "test"; 230 auto result = dataShareAbsResultSet.GetColumnName(columnIndex, columnName); 231 EXPECT_EQ(result, E_INVALID_COLUMN_INDEX); 232 LOG_INFO("DataShareAbsResultSetTest MarshallingTest002::End"); 233 } 234 } 235 }