add DataAbilityUtils TDD

add RdbStoreConfig TDD

Signed-off-by: wuyongning <wuyongning@huawei.com>
This commit is contained in:
wuyongning 2022-11-15 20:28:31 +08:00
parent 48aa46ad48
commit a60c917430
4 changed files with 506 additions and 2 deletions

View File

@ -141,6 +141,7 @@
"//foundation/distributeddatamgr/relational_store/test/native/dataability:unittest",
"//foundation/distributeddatamgr/relational_store/test/native/rdb:unittest",
"//foundation/distributeddatamgr/relational_store/test/native/rdb:fuzztest",
"//foundation/distributeddatamgr/relational_store/test/native/rdb_data_ability_adapter:unittest",
"//foundation/distributeddatamgr/relational_store/test/native/rdb_data_share_adapter:unittest",
"//foundation/distributeddatamgr/relational_store/test/native/rdb:distributedtest"
]

View File

@ -435,7 +435,7 @@ HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_013, TestSize.Level1)
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
config.SetSecurityLevel(SecurityLevel::LAST);
@ -466,7 +466,7 @@ HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_014, TestSize.Level1)
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
createNecessary = false;
@ -477,3 +477,334 @@ HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_014, TestSize.Level1)
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_EQ(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_015
* @tc.desc: test RdbStoreConfig interfaces: SetReadOnly/IsReadOnly
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_015, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
bool readOnly = true;
config.SetReadOnly(readOnly);
bool retReadOnly = config.IsReadOnly();
EXPECT_EQ(readOnly, retReadOnly);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_EQ(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
readOnly = false;
config.SetReadOnly(readOnly);
retReadOnly = config.IsReadOnly();
EXPECT_EQ(readOnly, retReadOnly);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
readOnly = true;
config.SetReadOnly(readOnly);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_016
* @tc.desc: test RdbStoreConfig interfaces: SetStorageMode/GetStorageMode
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_016, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
StorageMode storageMode = StorageMode::MODE_DISK;
config.SetStorageMode(storageMode);
StorageMode retStorageMode = config.GetStorageMode();
EXPECT_EQ(storageMode, retStorageMode);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
storageMode = StorageMode::MODE_MEMORY;
config.SetStorageMode(storageMode);
retStorageMode = config.GetStorageMode();
EXPECT_EQ(storageMode, retStorageMode);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_017
* @tc.desc: test RdbStoreConfig interfaces: SetDatabaseFileType/GetDatabaseFileType
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_017, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
DatabaseFileType databaseFileType = DatabaseFileType::NORMAL;
config.SetDatabaseFileType(databaseFileType);
std::string retDatabaseFileType = config.GetDatabaseFileType();
EXPECT_EQ("db", retDatabaseFileType);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
databaseFileType = DatabaseFileType::BACKUP;
config.SetDatabaseFileType(databaseFileType);
retDatabaseFileType = config.GetDatabaseFileType();
EXPECT_EQ("backup", retDatabaseFileType);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
databaseFileType = DatabaseFileType::CORRUPT;
config.SetDatabaseFileType(databaseFileType);
retDatabaseFileType = config.GetDatabaseFileType();
EXPECT_EQ("corrupt", retDatabaseFileType);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_018
* @tc.desc: test RdbStoreConfig interfaces: SetDistributedType/GetDistributedType
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_018, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
DistributedType distributedType = DistributedType::RDB_DEVICE_COLLABORATION;
config.SetDistributedType(distributedType);
DistributedType retDistributedType = config.GetDistributedType();
EXPECT_EQ(distributedType, retDistributedType);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
distributedType = DistributedType::RDB_DISTRIBUTED_TYPE_MAX;
config.SetDistributedType(distributedType);
retDistributedType = config.GetDistributedType();
EXPECT_NE(distributedType, retDistributedType);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_019
* @tc.desc: test RdbStoreConfig interfaces: SetModuleName/GetModuleName
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_019, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
std::string moduleName = "phone";
config.SetModuleName(moduleName);
std::string retModuleName = config.GetModuleName();
EXPECT_EQ(moduleName, retModuleName);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_020
* @tc.desc: test RdbStoreConfig interfaces: SetModuleName/GetModuleName
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_020, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
std::string serviceName = "com.ohos.config.test";
config.SetServiceName(serviceName);
std::string retServiceName = config.GetBundleName();
EXPECT_EQ(serviceName, retServiceName);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_021
* @tc.desc: test RdbStoreConfig interfaces: GetSyncModeValue
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_021, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
std::string syncMode = config.GetSyncModeValue(SyncMode::MODE_OFF);
EXPECT_EQ(syncMode, "MODE_OFF");
syncMode = OHOS::NativeRdb::RdbStoreConfig::GetSyncModeValue(SyncMode::MODE_NORMAL);
EXPECT_EQ(syncMode, "MODE_NORMAL");
syncMode = config.GetSyncModeValue(SyncMode::MODE_FULL);
EXPECT_EQ(syncMode, "MODE_FULL");
syncMode = config.GetSyncModeValue(SyncMode::MODE_EXTRA);
EXPECT_EQ(syncMode, "MODE_EXTRA");
}
/**
* @tc.name: RdbStoreConfig_022
* @tc.desc: test RdbStoreConfig interfaces: SetAutoCheck/IsAutoCheck
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_022, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
bool autoCheck = true;
config.SetAutoCheck(autoCheck);
bool retAutoCheck = config.IsAutoCheck();
EXPECT_EQ(autoCheck, retAutoCheck);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_023
* @tc.desc: test RdbStoreConfig interfaces: SetJournalSize/GetJournalSize
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_023, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
static constexpr int journalSize = 2 * 1024 * 1024;
config.SetJournalSize(journalSize);
int retJournalSize = config.GetJournalSize();
EXPECT_EQ(journalSize, retJournalSize);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
config.SetJournalSize(0);
retJournalSize = config.GetJournalSize();
EXPECT_EQ(0, retJournalSize);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_024
* @tc.desc: test RdbStoreConfig interfaces: SetJournalSize/GetJournalSize
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_024, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
static constexpr int pageSize = 4 * 1024;
config.SetPageSize(pageSize);
int retPageSize = config.GetPageSize();
EXPECT_EQ(pageSize, retPageSize);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
config.SetPageSize(0);
retPageSize = config.GetPageSize();
EXPECT_EQ(0, retPageSize);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
}
/**
* @tc.name: RdbStoreConfig_025
* @tc.desc: test RdbStoreConfig interfaces: SetEncryptAlgo/GetEncryptAlgo
* @tc.type: FUNC
* @tc.require:
* @tc.author:
*/
HWTEST_F(RdbStoreConfigTest, RdbStoreConfig_025, TestSize.Level1)
{
const std::string dbPath = RDB_TEST_PATH + "config_test.db";
RdbStoreConfig config(dbPath);
std::string encryptAlgo = "sha256";
config.SetEncryptAlgo(encryptAlgo);
std::string retEncryptAlgo = config.GetEncryptAlgo();
EXPECT_EQ(encryptAlgo, retEncryptAlgo);
ConfigTestOpenCallback helper;
int errCode = E_ERROR;
std::shared_ptr<RdbStore> store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_NE(store, nullptr);
store = nullptr;
RdbHelper::DeleteRdbStore(dbPath);
config.SetEncryptAlgo("");
retEncryptAlgo = config.GetEncryptAlgo();
EXPECT_EQ("", retEncryptAlgo);
store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
EXPECT_EQ(store, nullptr);
}

View File

@ -0,0 +1,55 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/test.gni")
import("//foundation/distributeddatamgr/data_share/datashare.gni")
import("//foundation/distributeddatamgr/relational_store/relational_store.gni")
module_output_path = "relational_store/rdb_data_ability_adapter"
###############################################################################
config("module_private_config") {
visibility = [ ":*" ]
include_dirs = [
"${datashare_innerapi_path}/common/include",
"${relational_store_innerapi_path}/dataability/include",
"${relational_store_innerapi_path}/rdb_data_ability_adapter/include",
"//commonlibrary/c_utils/base/include",
]
}
ohos_unittest("NativeDataAbilityAdapterTest") {
module_out_path = module_output_path
sources = [ "unittest/data_ability_utils_test.cpp" ]
configs = [ ":module_private_config" ]
external_deps = [
"c_utils:utils",
"hilog_native:libhilog",
"relational_store:native_dataability",
"relational_store:native_rdb",
"relational_store:rdb_data_ability_adapter",
]
deps = [ "//third_party/googletest:gtest_main" ]
}
###############################################################################
group("unittest") {
testonly = true
deps = [ ":NativeDataAbilityAdapterTest" ]
}
###############################################################################

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <vector>
#include "abs_rdb_predicates.h"
#include "datashare_value_object.h"
#include "datashare_values_bucket.h"
#include "rdb_data_ability_utils.h"
#include "refbase.h"
#include "values_bucket.h"
using namespace testing::ext;
using namespace OHOS::NativeRdb;
using namespace OHOS::RdbDataAbilityAdapter;
using namespace OHOS::DataShare;
class DataAbilityUtilsTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void DataAbilityUtilsTest::SetUpTestCase(void) {}
void DataAbilityUtilsTest::TearDownTestCase(void) {}
void DataAbilityUtilsTest::SetUp(void) {}
void DataAbilityUtilsTest::TearDown(void) {}
/* *
* @tc.name: DataAbilityUtilsTest_001
* @tc.desc: test ToDataShareValuesBucket()
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(DataAbilityUtilsTest, DataAbilityUtilsTest_001, TestSize.Level1)
{
ValuesBucket valuesBucket;
valuesBucket.PutString("name", "Tom");
valuesBucket.PutInt("age", 10);
valuesBucket.PutDouble("weight", 50.5);
valuesBucket.PutBlob("data", { 1, 2, 3 });
auto dataShareValuesBucket = RdbDataAbilityUtils::ToDataShareValuesBucket(valuesBucket);
bool isEmpty = dataShareValuesBucket.IsEmpty();
EXPECT_EQ(isEmpty, false);
auto iter = dataShareValuesBucket.valuesMap.find("name");
if (iter != dataShareValuesBucket.valuesMap.end()) {
std::string strValue = std::get<std::string>(iter->second);
EXPECT_EQ(strValue, "Tom");
} else {
EXPECT_EQ(true, false);
}
if (iter != dataShareValuesBucket.valuesMap.end()) {
iter = dataShareValuesBucket.valuesMap.find("age");
int64_t intValue = std::get<int64_t>(iter->second);
EXPECT_EQ(intValue, 10);
} else {
EXPECT_EQ(true, false);
}
if (iter != dataShareValuesBucket.valuesMap.end()) {
iter = dataShareValuesBucket.valuesMap.find("weight");
double doubleValue = std::get<double>(iter->second);
EXPECT_EQ(doubleValue, 50.5);
} else {
EXPECT_EQ(true, false);
}
if (iter != dataShareValuesBucket.valuesMap.end()) {
iter = dataShareValuesBucket.valuesMap.find("data");
std::vector<uint8_t> uint8Vec = std::get<std::vector<uint8_t>>(iter->second);
EXPECT_EQ(uint8Vec[0], 1);
} else {
EXPECT_EQ(true, false);
}
}
/* *
* @tc.name: DataAbilityUtilsTest_002
* @tc.desc: test ToDataSharePredicates()
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(DataAbilityUtilsTest, DataAbilityUtilsTest_002, TestSize.Level1)
{
std::string order = "age";
auto *predicates = new DataAbilityPredicates();
predicates->EqualTo("name", "Tom")->OrderByDesc("id")->Limit(2);
predicates->SetOrder(order);
predicates->Distinct();
std::string whereClause = "`name` = ? ";
auto dataSharePredicates = RdbDataAbilityUtils::ToDataSharePredicates(*predicates);
std::string dataShareWhereClause = dataSharePredicates.GetWhereClause();
EXPECT_EQ(dataShareWhereClause, whereClause);
std::string dataShareOrder = dataSharePredicates.GetOrder();
EXPECT_EQ(dataShareOrder, order);
}