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_block_writer_impl.h" 20 #include "datashare_log.h" 21 22 namespace OHOS { 23 namespace DataShare { 24 using namespace testing::ext; 25 class DataShareBlockWriterImplTest : public testing::Test { 26 public: SetUpTestCase(void)27 static void SetUpTestCase(void){}; TearDownTestCase(void)28 static void TearDownTestCase(void){}; SetUp()29 void SetUp(){}; TearDown()30 void TearDown(){}; 31 }; 32 33 /** 34 * @tc.name: AllocRowTest001 35 * @tc.desc: test AllocRow function when shareBlock_ = nullptr 36 * @tc.type: FUNC 37 * @tc.require:issueIC413F 38 * @tc.precon: None 39 * @tc.step: 40 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 41 2.call AllocRow function and check the result 42 * @tc.experct: AllocRow failed and reutrn E_ERROR 43 */ 44 HWTEST_F(DataShareBlockWriterImplTest, AllocRowTest001, TestSize.Level0) 45 { 46 LOG_INFO("DataShareBlockWriterImplTest AllocRowTest001::Start"); 47 DataShareBlockWriterImpl dataShareBlockWriterImpl; 48 dataShareBlockWriterImpl.shareBlock_ = nullptr; 49 auto result = dataShareBlockWriterImpl.AllocRow(); 50 EXPECT_EQ(result, E_ERROR); 51 LOG_INFO("DataShareBlockWriterImplTest AllocRowTest001::End"); 52 } 53 54 /** 55 * @tc.name: WriteTest001 56 * @tc.desc: test Write function when shareBlock_ = nullptr 57 * @tc.type: FUNC 58 * @tc.require:issueIC413F 59 * @tc.precon: None 60 * @tc.step: 61 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 62 2.call Write function and check the result 63 * @tc.experct: Write failed and reutrn E_ERROR 64 */ 65 HWTEST_F(DataShareBlockWriterImplTest, WriteTest001, TestSize.Level0) 66 { 67 LOG_INFO("DataShareBlockWriterImplTest WriteTest001::Start"); 68 DataShareBlockWriterImpl dataShareBlockWriterImpl; 69 dataShareBlockWriterImpl.shareBlock_ = nullptr; 70 uint32_t column = 1; 71 auto result = dataShareBlockWriterImpl.Write(column); 72 EXPECT_EQ(result, E_ERROR); 73 LOG_INFO("DataShareBlockWriterImplTest WriteTest001::End"); 74 } 75 76 /** 77 * @tc.name: WriteTest002 78 * @tc.desc: test Write function when shareBlock_ = nullptr 79 * @tc.type: FUNC 80 * @tc.require:issueIC413F 81 * @tc.precon: None 82 * @tc.step: 83 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 84 2.call Write function and check the result 85 * @tc.experct: Write failed and reutrn E_ERROR 86 */ 87 HWTEST_F(DataShareBlockWriterImplTest, WriteTest002, TestSize.Level0) 88 { 89 LOG_INFO("DataShareBlockWriterImplTest WriteTest002::Start"); 90 DataShareBlockWriterImpl dataShareBlockWriterImpl; 91 dataShareBlockWriterImpl.shareBlock_ = nullptr; 92 uint32_t column = 1; 93 int64_t value = 1; 94 auto result = dataShareBlockWriterImpl.Write(column, value); 95 EXPECT_EQ(result, E_ERROR); 96 LOG_INFO("DataShareBlockWriterImplTest WriteTest002::End"); 97 } 98 99 /** 100 * @tc.name: WriteTest003 101 * @tc.desc: test Write function when shareBlock_ = nullptr 102 * @tc.type: FUNC 103 * @tc.require:issueIC413F 104 * @tc.precon: None 105 * @tc.step: 106 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 107 2.call Write function and check the result 108 * @tc.experct: Write failed and reutrn E_ERROR 109 */ 110 HWTEST_F(DataShareBlockWriterImplTest, WriteTest003, TestSize.Level0) 111 { 112 LOG_INFO("DataShareBlockWriterImplTest WriteTest003::Start"); 113 DataShareBlockWriterImpl dataShareBlockWriterImpl; 114 dataShareBlockWriterImpl.shareBlock_ = nullptr; 115 uint32_t column = 1; 116 double value = 1; 117 auto result = dataShareBlockWriterImpl.Write(column, value); 118 EXPECT_EQ(result, E_ERROR); 119 LOG_INFO("DataShareBlockWriterImplTest WriteTest003::End"); 120 } 121 122 /** 123 * @tc.name: WriteTest004 124 * @tc.desc: test Write function when shareBlock_ = nullptr 125 * @tc.type: FUNC 126 * @tc.require:issueIC413F 127 * @tc.precon: None 128 * @tc.step: 129 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 130 2.call Write function and check the result 131 * @tc.experct: Write failed and reutrn E_ERROR 132 */ 133 HWTEST_F(DataShareBlockWriterImplTest, WriteTest004, TestSize.Level0) 134 { 135 LOG_INFO("DataShareBlockWriterImplTest WriteTest004::Start"); 136 DataShareBlockWriterImpl dataShareBlockWriterImpl; 137 dataShareBlockWriterImpl.shareBlock_ = nullptr; 138 uint32_t column = 1; 139 uint8_t value[3] = {1}; 140 size_t size = 1; 141 auto result = dataShareBlockWriterImpl.Write(column, value, size); 142 EXPECT_EQ(result, E_ERROR); 143 LOG_INFO("DataShareBlockWriterImplTest WriteTest004::End"); 144 } 145 146 /** 147 * @tc.name: WriteTest005 148 * @tc.desc: test Write function when shareBlock_ = nullptr 149 * @tc.type: FUNC 150 * @tc.require:issueIC413F 151 * @tc.precon: None 152 * @tc.step: 153 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 154 2.call Write function and check the result 155 * @tc.experct: Write failed and reutrn E_ERROR 156 */ 157 HWTEST_F(DataShareBlockWriterImplTest, WriteTest005, TestSize.Level0) 158 { 159 LOG_INFO("DataShareBlockWriterImplTest WriteTest005::Start"); 160 DataShareBlockWriterImpl dataShareBlockWriterImpl; 161 dataShareBlockWriterImpl.shareBlock_ = nullptr; 162 uint32_t column = 1; 163 const char* value = "test"; 164 size_t sizeIncludingNull = 1; 165 auto result = dataShareBlockWriterImpl.Write(column, value, sizeIncludingNull); 166 EXPECT_EQ(result, E_ERROR); 167 LOG_INFO("DataShareBlockWriterImplTest WriteTest005::End"); 168 } 169 170 /** 171 * @tc.name: GetCurrentRowIndexTest001 172 * @tc.desc: test GetCurrentRowIndex function when shareBlock_ = nullptr 173 * @tc.type: FUNC 174 * @tc.require:issueIC413F 175 * @tc.precon: None 176 * @tc.step: 177 1.Create a DataShareBlockWriterImpl object and shareBlock_ = nullptr 178 2.call GetCurrentRowIndex function and check the result 179 * @tc.experct: GetCurrentRowIndex failed and reutrn false 180 */ 181 HWTEST_F(DataShareBlockWriterImplTest, GetCurrentRowIndexTest001, TestSize.Level0) 182 { 183 LOG_INFO("DataShareBlockWriterImplTest GetCurrentRowIndexTest001::Start"); 184 DataShareBlockWriterImpl dataShareBlockWriterImpl; 185 dataShareBlockWriterImpl.shareBlock_ = nullptr; 186 uint32_t rowIndex = 1; 187 auto result = dataShareBlockWriterImpl.GetCurrentRowIndex(rowIndex); 188 EXPECT_EQ(result, false); 189 LOG_INFO("DataShareBlockWriterImplTest GetCurrentRowIndexTest001::End"); 190 } 191 192 } // namespace DataShare 193 } // namespace OHOS