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 #define LOG_TAG "CloudDataTest" 17 #include <gtest/gtest.h> 18 #include "log_print.h" 19 #include "value_proxy.h" 20 namespace Test { 21 using namespace testing::ext; 22 using namespace OHOS::DistributedData; 23 class ValueProxyTest : public testing::Test { 24 }; 25 26 /** 27 * @tc.name: GetSchema 28 * @tc.desc: GetSchema from cloud when no schema in meta. 29 * @tc.type: FUNC 30 * @tc.require: 31 * @tc.author: ht 32 */ 33 HWTEST_F(ValueProxyTest, VBucketsNormal2GaussDB, TestSize.Level0) 34 { 35 std::vector<DistributedDB::VBucket> dbVBuckets; 36 OHOS::DistributedData::VBuckets extends = { 37 {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}, 38 {{"#gid", {"0000001"}}} 39 }; 40 dbVBuckets = ValueProxy::Convert(std::move(extends)); 41 ASSERT_EQ(dbVBuckets.size(), 2); 42 } 43 44 /** 45 * @tc.name: GetSchema 46 * @tc.desc: GetSchema from cloud when no schema in meta. 47 * @tc.type: FUNC 48 * @tc.require: 49 * @tc.author: ht 50 */ 51 HWTEST_F(ValueProxyTest, VBucketsGaussDB2Normal, TestSize.Level0) 52 { 53 std::vector<DistributedDB::VBucket> dbVBuckets = { 54 {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}, 55 {{"#gid", {"0000001"}}} 56 }; 57 OHOS::DistributedData::VBuckets extends; 58 extends = ValueProxy::Convert(std::move(dbVBuckets)); 59 ASSERT_EQ(extends.size(), 2); 60 } 61 62 /** 63 * @tc.name: GetSchema 64 * @tc.desc: GetSchema from cloud when no schema in meta. 65 * @tc.type: FUNC 66 * @tc.require: 67 * @tc.author: ht 68 */ 69 HWTEST_F(ValueProxyTest, VBucketsNormal2Rdb, TestSize.Level0) 70 { 71 using RdbBucket = OHOS::NativeRdb::ValuesBucket; 72 std::vector<RdbBucket> rdbVBuckets; 73 OHOS::DistributedData::VBuckets extends = { 74 {{"#gid", {"0000000"}}, {"#flag", {true }}, {"#value", {int64_t(100)}}, {"#float", {double(100)}}}, 75 {{"#gid", {"0000001"}}} 76 }; 77 rdbVBuckets = ValueProxy::Convert(std::move(extends)); 78 ASSERT_EQ(rdbVBuckets.size(), 2); 79 } 80 81 /** 82 * @tc.name: GetSchema 83 * @tc.desc: GetSchema from cloud when no schema in meta. 84 * @tc.type: FUNC 85 * @tc.require: 86 * @tc.author: ht 87 */ 88 HWTEST_F(ValueProxyTest, VBucketsRdb2Normal, TestSize.Level0) 89 { 90 using RdbBucket = OHOS::NativeRdb::ValuesBucket; 91 using RdbValue = OHOS::NativeRdb::ValueObject; 92 std::vector<RdbBucket> rdbVBuckets = { 93 RdbBucket(std::map<std::string, RdbValue> { 94 {"#gid", {"0000000"}}, 95 {"#flag", {true }}, 96 {"#value", {int64_t(100)}}, 97 {"#float", {double(100)}} 98 }), 99 RdbBucket(std::map<std::string, RdbValue> { 100 {"#gid", {"0000001"}} 101 }) 102 }; 103 OHOS::DistributedData::VBuckets extends; 104 extends = ValueProxy::Convert(std::move(rdbVBuckets)); 105 ASSERT_EQ(extends.size(), 2); 106 } 107 108 /** 109 * @tc.name: GetSchema 110 * @tc.desc: GetSchema from cloud when no schema in meta. 111 * @tc.type: FUNC 112 * @tc.require: 113 * @tc.author: ht 114 */ 115 HWTEST_F(ValueProxyTest, ConvertIntMapTest, TestSize.Level0) 116 { 117 std::map<std::string, int64_t> testMap = { { "name", 1 }, { "school", 2 }, { "address", 3 } }; 118 auto res = ValueProxy::Convert<int64_t>(testMap); 119 auto testMap2 = std::map<std::string, int64_t>(res); 120 ASSERT_EQ(testMap2.find("name")->second, 1); 121 122 auto errorMap = std::map<std::string, double>(res); 123 ASSERT_EQ(errorMap.size(), 0); 124 } 125 126 /** 127 * @tc.name: GetSchema 128 * @tc.desc: GetSchema from cloud when no schema in meta. 129 * @tc.type: FUNC 130 * @tc.require: 131 * @tc.author: ht 132 */ 133 HWTEST_F(ValueProxyTest, ConvertAssetMapGaussDB2NormalTest, TestSize.Level0) 134 { 135 DistributedDB::Asset dbAsset0 { .name = "dbname", .uri = "dburi" }; 136 DistributedDB::Asset dbAsset1 { .name = "dbname", .uri = "dburi" }; 137 std::map<std::string, DistributedDB::Asset> dbMap { { "asset0", dbAsset0 }, { "asset1", dbAsset1 } }; 138 OHOS::DistributedData::VBucket transferredAsset = ValueProxy::Convert(dbMap); 139 ASSERT_EQ(transferredAsset.size(), 2); 140 auto asset = std::get<OHOS::DistributedData::Asset>(transferredAsset.find("asset0")->second); 141 ASSERT_EQ(asset.name, "dbname"); 142 143 DistributedDB::Assets dbAssets { dbAsset0, dbAsset1 }; 144 std::map<std::string, DistributedDB::Assets> dbAssetsMap { {"dbAssets", dbAssets} }; 145 OHOS::DistributedData::VBucket transferredAssets = ValueProxy::Convert(dbAssetsMap); 146 ASSERT_EQ(transferredAssets.size(), 1); 147 auto assets = std::get<OHOS::DistributedData::Assets>(transferredAssets.find("dbAssets")->second); 148 ASSERT_EQ(assets.size(), 2); 149 auto dataAsset = assets.begin(); 150 ASSERT_EQ(dataAsset->name, "dbname"); 151 } 152 153 /** 154 * @tc.name: GetSchema 155 * @tc.desc: GetSchema from cloud when no schema in meta. 156 * @tc.type: FUNC 157 * @tc.require: 158 * @tc.author: ht 159 */ 160 HWTEST_F(ValueProxyTest, ConvertAssetMapNormal2GaussDBTest, TestSize.Level0) 161 { 162 using NormalAsset = OHOS::DistributedData::Asset; 163 using NormalAssets = OHOS::DistributedData::Assets; 164 NormalAsset nAsset0 { .name = "name", .uri = "uri" }; 165 NormalAsset nAsset1 { .name = "name", .uri = "uri" }; 166 std::map<std::string, NormalAsset> nMap { { "asset0", nAsset0 }, { "asset1", nAsset1 } }; 167 DistributedDB::VBucket transferredAsset = ValueProxy::Convert(nMap); 168 ASSERT_EQ(transferredAsset.size(), 2); 169 auto asset = std::get<DistributedDB::Asset>(transferredAsset.find("asset0")->second); 170 ASSERT_EQ(asset.name, "name"); 171 172 NormalAssets nAssets { nAsset0, nAsset1 }; 173 std::map<std::string, NormalAssets> nAssetsMap { { "Assets", nAssets } }; 174 DistributedDB::VBucket transferredAssets = ValueProxy::Convert(nAssetsMap); 175 ASSERT_EQ(transferredAssets.size(), 1); 176 auto assets = std::get<DistributedDB::Assets>(transferredAssets.find("Assets")->second); 177 ASSERT_EQ(assets.size(), 2); 178 auto dataAsset = assets.begin(); 179 ASSERT_EQ(dataAsset->name, "name"); 180 } 181 182 /** 183 * @tc.name: GetSchema 184 * @tc.desc: GetSchema from cloud when no schema in meta. 185 * @tc.type: FUNC 186 * @tc.require: 187 * @tc.author: ht 188 */ 189 HWTEST_F(ValueProxyTest, ConvertAssetMapRdb2NormalTest, TestSize.Level0) 190 { 191 using RdbAsset = OHOS::NativeRdb::AssetValue; 192 using RdbAssets = std::vector<RdbAsset>; 193 RdbAsset dbAsset0 { .name = "dbname", .uri = "dburi" }; 194 RdbAsset dbAsset1 { .name = "dbname", .uri = "dburi" }; 195 std::map<std::string, RdbAsset> dbMap { { "asset0", dbAsset0 }, { "asset1", dbAsset1 } }; 196 OHOS::DistributedData::VBucket transferredAsset = ValueProxy::Convert(dbMap); 197 ASSERT_EQ(transferredAsset.size(), 2); 198 auto asset = std::get<OHOS::DistributedData::Asset>(transferredAsset.find("asset0")->second); 199 ASSERT_EQ(asset.name, "dbname"); 200 201 RdbAssets dbAssets { dbAsset0, dbAsset1 }; 202 std::map<std::string, RdbAssets> dbAssetsMap { {"dbAssets", dbAssets} }; 203 OHOS::DistributedData::VBucket transferredAssets = ValueProxy::Convert(dbAssetsMap); 204 ASSERT_EQ(transferredAssets.size(), 1); 205 auto assets = std::get<OHOS::DistributedData::Assets>(transferredAssets.find("dbAssets")->second); 206 ASSERT_EQ(assets.size(), 2); 207 auto dataAsset = assets.begin(); 208 ASSERT_EQ(dataAsset->name, "dbname"); 209 } 210 211 /** 212 * @tc.name: GetSchema 213 * @tc.desc: GetSchema from cloud when no schema in meta. 214 * @tc.type: FUNC 215 * @tc.require: 216 * @tc.author: ht 217 */ 218 HWTEST_F(ValueProxyTest, ConvertAssetMapNormal2RdbTest, TestSize.Level0) 219 { 220 using RdbAsset = OHOS::NativeRdb::AssetValue; 221 using RdbAssets = std::vector<RdbAsset>; 222 using NormalAsset = OHOS::DistributedData::Asset; 223 using NormalAssets = OHOS::DistributedData::Assets; 224 NormalAsset nAsset0 { .name = "name", .uri = "uri" }; 225 NormalAsset nAsset1 { .name = "name", .uri = "uri" }; 226 std::map<std::string, NormalAsset> nMap { { "asset0", nAsset0 }, { "asset1", nAsset1 } }; 227 OHOS::NativeRdb::ValuesBucket transferredAsset = ValueProxy::Convert(nMap); 228 ASSERT_EQ(transferredAsset.Size(), 2); 229 OHOS::NativeRdb::ValueObject rdbObject; 230 transferredAsset.GetObject("asset0", rdbObject); 231 RdbAsset rdbAsset; 232 rdbObject.GetAsset(rdbAsset); 233 ASSERT_EQ(rdbAsset.name, "name"); 234 235 NormalAssets nAssets { nAsset0, nAsset1 }; 236 std::map<std::string, NormalAssets> nAssetsMap { { "Assets", nAssets } }; 237 OHOS::NativeRdb::ValuesBucket transferredAssets = ValueProxy::Convert(nAssetsMap); 238 ASSERT_EQ(transferredAssets.Size(), 1); 239 OHOS::NativeRdb::ValueObject rdbObject2; 240 transferredAssets.GetObject("Assets", rdbObject2); 241 RdbAssets rdbAssets; 242 rdbObject2.GetAssets(rdbAssets); 243 ASSERT_EQ(rdbAssets.size(), 2); 244 auto dataAsset = rdbAssets.begin(); 245 ASSERT_EQ(dataAsset->name, "name"); 246 } 247 } // namespace Test 248