add remove test

Change-Id: Ief7f5efc7e76e4fd8070c8cbbe3c35173160c2b7

Match-id-aedf2661a00f269158d8ff5642feada3d0f17af7
This commit is contained in:
authName 2023-10-24 16:41:00 +08:00 committed by userName
parent aa3f808d9b
commit 4595561d93
4 changed files with 91 additions and 25 deletions

View File

@ -17,39 +17,39 @@ use asset_sdk::{
AssetMap, AuthType, ConflictResolution, Insert, Tag, Value, ErrCode,
};
use crate::common::{get_bytes, get_number, delete_by_alias, query_by_alias};
use crate::common::{get_bytes, get_number, remove_by_alias, query_by_alias};
#[test]
fn add_values_match_query() {
let alias = "add_values_match_query".as_bytes().to_vec();
let data_label = "add_values_match_query_data_label".as_bytes().to_vec();
let secret = "add_values_match_query_secret".as_bytes().to_vec();
let alias = "add_values_match_query".as_bytes();
let data_label = "add_values_match_query_data_label".as_bytes();
let secret = "add_values_match_query_secret".as_bytes();
let auth_type = AuthType::None;
let mut add = AssetMap::new();
add.insert_attr(Tag::Alias, alias.clone()).unwrap();
add.insert_attr(Tag::Secret, secret).unwrap();
add.insert_attr(Tag::DataLabelCritical1, data_label.clone()).unwrap();
add.insert_attr(Tag::Alias, alias.to_owned()).unwrap();
add.insert_attr(Tag::Secret, secret.to_owned()).unwrap();
add.insert_attr(Tag::DataLabelCritical1, data_label.to_owned()).unwrap();
add.insert_attr(Tag::AuthType, auth_type).unwrap();
asset_sdk::Manager::build().unwrap().add(&add).unwrap();
let mut query = AssetMap::new();
query.insert_attr(Tag::Alias, alias.clone()).unwrap();
let res = query_by_alias(&alias).unwrap();
query.insert_attr(Tag::Alias, alias.to_owned()).unwrap();
let res = query_by_alias(alias).unwrap();
assert_eq!(1, res.len());
assert_eq!(data_label, *get_bytes(&res[0], Tag::DataLabelCritical1).unwrap());
assert_eq!(auth_type as u32, get_number(&res[0], Tag::AuthType).unwrap());
delete_by_alias(&alias).unwrap();
remove_by_alias(alias).unwrap();
}
#[test]
fn add_conflict_throw_error() {
let alias = "add_conflict_throw_error".as_bytes().to_vec();
let secret = "add_conflict_throw_error_secret".as_bytes().to_vec();
let alias = "add_conflict_throw_error".as_bytes();
let secret = "add_conflict_throw_error_secret".as_bytes();
let mut add_throw_error = AssetMap::from([
(Tag::Alias, Value::Bytes(alias.clone())),
(Tag::Secret, Value::Bytes(secret)),
(Tag::Alias, Value::Bytes(alias.to_owned())),
(Tag::Secret, Value::Bytes(secret.to_owned())),
]);
asset_sdk::Manager::build().unwrap().add(&add_throw_error).unwrap();
@ -58,32 +58,32 @@ fn add_conflict_throw_error() {
add_throw_error.insert_attr(Tag::ConflictResolution, ConflictResolution::ThrowError).unwrap();
assert_eq!(Err(ErrCode::Duplicated), asset_sdk::Manager::build().unwrap().add(&add_throw_error));
delete_by_alias(&alias).unwrap();
remove_by_alias(alias).unwrap();
}
#[test]
fn add_conflict_over_write() {
let alias = "add_conflict_over_write".as_bytes().to_vec();
let secret = "add_conflict_over_write_secret".as_bytes().to_vec();
let label_normal_1 = "add_conflict_over_write_label_normal_1".as_bytes().to_vec();
let alias = "add_conflict_over_write".as_bytes();
let secret = "add_conflict_over_write_secret".as_bytes();
let label_normal_1 = "add_conflict_over_write_label_normal_1".as_bytes();
let mut add_over_write = AssetMap::new();
add_over_write.insert_attr(Tag::Alias, alias.clone()).unwrap();
add_over_write.insert_attr(Tag::Secret, secret).unwrap();
add_over_write.insert_attr(Tag::Alias, alias.to_owned()).unwrap();
add_over_write.insert_attr(Tag::Secret, secret.to_owned()).unwrap();
asset_sdk::Manager::build().unwrap().add(&add_over_write).unwrap();
let res = query_by_alias(&alias).unwrap();
let res = query_by_alias(alias).unwrap();
assert_eq!(1, res.len());
assert!(res[0].get(&Tag::DataLabelNormal1).is_none());
add_over_write.insert_attr(Tag::DataLabelNormal1, label_normal_1.clone()).unwrap();
add_over_write.insert_attr(Tag::DataLabelNormal1, label_normal_1.to_owned()).unwrap();
add_over_write.insert_attr(Tag::ConflictResolution, ConflictResolution::Overwrite).unwrap();
asset_sdk::Manager::build().unwrap().add(&add_over_write).unwrap();
let res = query_by_alias(&alias).unwrap();
let res = query_by_alias(alias).unwrap();
assert_eq!(1, res.len());
assert_eq!(label_normal_1, *get_bytes(&res[0], Tag::DataLabelNormal1).unwrap());
delete_by_alias(&alias).unwrap();
remove_by_alias(alias).unwrap();
}

View File

@ -38,7 +38,7 @@ pub(crate) fn get_number(input: &AssetMap, tag: Tag) -> Result<u32> {
// Err(ErrCode::NotFound)
// }
pub(crate) fn delete_by_alias(alias: &[u8]) -> Result<()> {
pub(crate) fn remove_by_alias(alias: &[u8]) -> Result<()> {
asset_sdk::Manager::build()?.remove(&AssetMap::from([(Tag::Alias, Value::Bytes(alias.to_vec()))]))
}

View File

@ -13,12 +13,15 @@
* limitations under the License.
*/
#![feature(is_some_and)]
use core::panic;
use asset_sdk::{Accessibility, AssetMap, AuthType, ConflictResolution, Insert, ReturnType, SyncType, Tag, Value};
mod add;
mod common;
mod remove;
fn add_asset_inner(alias: &[u8]) {
let mut input = AssetMap::new();

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2023 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.
*/
use asset_sdk::{
AssetMap, Tag, ErrCode, Value
};
use crate::common::remove_by_alias;
#[test]
fn remove_alias_non_exist() {
assert_eq!(Err(ErrCode::NotFound), remove_by_alias("remove_alias_non_exist".as_bytes()));
}
#[test]
fn remove_condition_non_exist() {
let delete_condition = AssetMap::from([
(Tag::DataLabelCritical1, Value::Bytes("remove_condition_non_exist".as_bytes().to_vec())),
]);
assert_eq!(Err(ErrCode::NotFound), asset_sdk::Manager::build().unwrap().remove(&delete_condition));
}
#[test]
fn remove_condition_exist_and_query() {
let alias = "remove_condition_exist_and_query".as_bytes();
let secret = "remove_condition_exist_and_query".as_bytes();
let label_vritial_2 = "remove_condition_exist_and_query".as_bytes();
let mut condition = AssetMap::from([
(Tag::Alias, Value::Bytes(alias.to_owned())),
(Tag::Secret, Value::Bytes(secret.to_owned())),
(Tag::DataLabelCritical2, Value::Bytes(label_vritial_2.to_owned())),
]);
asset_sdk::Manager::build().unwrap().add(&condition).unwrap();
condition.remove(&Tag::Alias);
condition.remove(&Tag::Secret);
asset_sdk::Manager::build().unwrap().remove(&condition).unwrap();
assert!(asset_sdk::Manager::build().unwrap().query(&condition).is_err_and(|e| e == ErrCode::NotFound));
}
#[test]
fn remove_condition_with_secret() {
let alias = "remove_condition_with_secret".as_bytes();
let secret = "remove_condition_with_secret".as_bytes();
let condition = AssetMap::from([
(Tag::Alias, Value::Bytes(alias.to_owned())),
(Tag::Secret, Value::Bytes(secret.to_owned())),
]);
asset_sdk::Manager::build().unwrap().add(&condition).unwrap();
assert!(asset_sdk::Manager::build().unwrap().remove(&condition).is_err());
remove_by_alias(alias).unwrap();
}