Skip to content

Commit

Permalink
[simplify] ValueArg unit tests before migrating to python
Browse files Browse the repository at this point in the history
Signed-off-by: Asem-Abdelhady <[email protected]>
  • Loading branch information
Asem-Abdelhady committed Jan 5, 2024
1 parent dbb52eb commit 6df7d12
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 306 deletions.
358 changes: 52 additions & 306 deletions client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl MetadataArgs {
}
}
/// Wrapper around Value to accept possible values and fallback to json
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValueArg(Value);

impl FromStr for ValueArg {
Expand Down Expand Up @@ -1118,334 +1118,80 @@ mod json {
}
}
}

#[cfg(test)]
mod tests {
use iroha_client::{
config::{Configuration as ClientConfiguration, ConfigurationProxy},
data_model::{
asset::{AssetDefinitionId, AssetId},
domain::DomainId,
name::Name,
},
};

use super::*;
use crate::asset::{RemoveKeyValue, SetKeyValue};

struct TestContext {
config: ClientConfiguration,
skip_mst_check: bool,
}
use std::str::FromStr;

impl RunContext for TestContext {
fn configuration(&self) -> &ClientConfiguration {
&self.config
}
use serde_json::Value as JsonValue;

fn print_data(&mut self, _data: &dyn Serialize) -> Result<()> {
Ok(())
}
use super::*;

fn skip_mst_check(&self) -> bool {
self.skip_mst_check
}
#[test]
fn parse_ipv4_address() {
let actual = ValueArg::from_str("192.168.0.1").expect("Failed to parse valid IPv4 address");
assert_eq!(
actual,
ValueArg(Value::Ipv4Addr(Ipv4Addr::new([192, 168, 0, 1])))
);
}

fn initialize_test_context() -> Result<TestContext> {
let config = ConfigurationProxy::default();
let binding = Path::default(DEFAULT_CONFIG_PATH);
let path = binding;
let path = path
.try_resolve()
.wrap_err("Failed to resolve config file")?;

let config = match path {
Some(p) => config.override_with(ConfigurationProxy::from_path(&*p)),
None => config,
};
// Build the final configuration
let config = config
.build()
.wrap_err("Failed to finalize configuration")?;

Ok(TestContext {
config,
skip_mst_check: false,
})
}

fn initialize_test_store(context: &mut TestContext, def_str: &str) -> Result<()> {
let asset_definition_id = AssetDefinitionId::new(
Name::from_str(def_str).unwrap(),
DomainId::new(Name::from_str("wonderland").unwrap()),
#[test]
fn parse_ipv6_address() {
let actual = ValueArg::from_str("::1").expect("Failed to parse valid IPv6 address");
assert_eq!(
actual,
ValueArg(Value::Ipv6Addr(Ipv6Addr::new([0, 0, 0, 0, 0, 0, 0, 1])))
);

let asset_definition = AssetDefinition::store(asset_definition_id);
// Register the asset definition
let create_asset_definition =
iroha_client::data_model::isi::Register::asset_definition(asset_definition);
submit(
[create_asset_definition],
UnlimitedMetadata::default(),
context,
)
.wrap_err("Failed to create store asset")?;

Ok(())
}

#[test]
fn test_set_key_values_without_errors() {
let mut context = initialize_test_context().expect("Failed to initialize test context");
initialize_test_store(&mut context, "test_store").expect("Failed to register asset");
let asset_id =
AssetId::from_str("test_store##alice@wonderland").expect("Invalid AssetId format");

// Setting a Numeric Value
let key_name1 = Name::from_str("test_key1").expect("Invalid Name format");
let test_value1 = ValueArg(Value::Numeric(NumericValue::from_str("17_u32").unwrap()));

let set_key_value1 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name1,
value: test_value1,
};

set_key_value1
.run(&mut context)
.expect("Failed to run SetKeyValue for Numeric Value");

// Setting an IPv4 Address
let key_name2 = Name::from_str("test_key2").expect("Invalid Name format");
let test_value2 = ValueArg(Value::Ipv4Addr(Ipv4Addr::from_str("127.0.0.1").unwrap()));

let set_key_value2 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name2,
value: test_value2,
};

set_key_value2
.run(&mut context)
.expect("Failed to run SetKeyValue for IPv4 Address");

// Setting a Boolean Value
let key_name3 = Name::from_str("test_key3").expect("Invalid Name format");
let test_value3 = ValueArg(Value::Bool(true));

let set_key_value3 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name3,
value: test_value3,
};

set_key_value3
.run(&mut context)
.expect("Failed to run SetKeyValue for Boolean Value");

let key_name4 = Name::from_str("test_key4").expect("Invalid Name format");
let json_data = r#"{ "Vec": [{"String": "a"}, {"String": "b"}] }"#;
let test_value4 = ValueArg(serde_json::from_str(json_data).unwrap());

let set_key_value4 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name4,
value: test_value4,
};

set_key_value4
.run(&mut context)
.expect("Failed to run SetKeyValue for JSON Value");

// Setting an IPv6 Address
let key_name5 = Name::from_str("test_key5").expect("Invalid Name format");
let ipv6_addr = Ipv6Addr::from_str("::1").expect("Invalid IPv6 format");
let test_value5 = ValueArg(Value::Ipv6Addr(ipv6_addr));

let set_key_value5 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name5,
value: test_value5,
};

set_key_value5
.run(&mut context)
.expect("Failed to run SetKeyValue for IPv6 Address");

let key_name6 = Name::from_str("test_key6").expect("Invalid Name format");
let public_key_str =
"ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0";
let pub_key = PublicKey::from_str(public_key_str).expect("Invalid public key");
let test_value6 = ValueArg(Value::PublicKey(pub_key));
let set_key_value6 = SetKeyValue {
asset_id,
key: key_name6,
value: test_value6,
};

set_key_value6
.run(&mut context)
.expect("Failed to run SetKeyValue for PublicKey");
fn parse_boolean_true() {
let actual = ValueArg::from_str("true").expect("Failed to parse 'true' as boolean");
assert!(matches!(actual, ValueArg(Value::Bool(true))));
}

#[test]
fn test_remove_value_without_errors() {
let mut context = initialize_test_context().expect("Failed to initialize test context");
initialize_test_store(&mut context, "test_store2").expect("Failed to register asset");
let mut context = initialize_test_context().expect("Failed to initialize test context");
let asset_id =
AssetId::from_str("test_store2##alice@wonderland").expect("Invalid AssetId format");

// Setting a Numeric Value
let key_name1 = Name::from_str("test_key1").expect("Invalid Name format");
let test_value1 = ValueArg(Value::Numeric(NumericValue::from_str("17_u32").unwrap()));

let set_key_value1 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name1.clone(),
value: test_value1,
};

set_key_value1
.run(&mut context)
.expect("Failed to run SetKeyValue for Numeric Value");

let remove_key_value = RemoveKeyValue {
asset_id,
key: key_name1.clone(),
};

remove_key_value
.run(&mut context)
.unwrap_or_else(|_| panic!("Failed to run RemoveKeyValue for key {key_name1}"))
fn parse_boolean_false() {
let actual = ValueArg::from_str("false").expect("Failed to parse 'false' as boolean");
assert!(matches!(actual, ValueArg(Value::Bool(false))));
}

#[test]
fn test_set_get_values() {
let mut context = initialize_test_context().expect("Failed to initialize test context");
let client = Client::new(context.configuration()).unwrap();

initialize_test_store(&mut context, "test_store3").expect("Failed to register asset");
let asset_id =
AssetId::from_str("test_store3##alice@wonderland").expect("Invalid AssetId format");

// Setting a Numeric Value
let key_name1 = Name::from_str("test_key1").expect("Invalid Name format");
let test_value1 = ValueArg(Value::Numeric(NumericValue::from_str("17_u32").unwrap()));

let set_key_value1 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name1.clone(),
value: test_value1,
};

set_key_value1
.run(&mut context)
.expect("Failed to run SetKeyValue for Numeric Value");

let find_key_value = FindAssetKeyValueByIdAndKey::new(asset_id.clone(), key_name1);
let asset = client
.request(find_key_value)
.wrap_err("Failed to get key-value")
.unwrap();

assert_eq!(
serde_json::to_string(&asset).expect("Failed to deserialize asset"),
r#""17_u32""#
);

// Setting an IPv4 Address
let key_name2 = Name::from_str("test_key2").expect("Invalid Name format");
let test_value2 = ValueArg(Value::Ipv4Addr(Ipv4Addr::from_str("127.0.0.1").unwrap()));

let set_key_value2 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name2.clone(),
value: test_value2,
};

set_key_value2
.run(&mut context)
.expect("Failed to run SetKeyValue for IPv4 Address");

let find_key_value = FindAssetKeyValueByIdAndKey::new(asset_id.clone(), key_name2);
let asset = client
.request(find_key_value)
.wrap_err("Failed to get key-value")
.unwrap();

assert_eq!(
serde_json::to_string(&asset).expect("Failed to deserialize asset"),
r#"{"Ipv4Addr":"127.0.0.1"}"#
);

// Setting a Boolean Value
let key_name3 = Name::from_str("test_key3").expect("Invalid Name format");
let test_value3 = ValueArg(Value::Bool(true));

let set_key_value3 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name3.clone(),
value: test_value3,
};

set_key_value3
.run(&mut context)
.expect("Failed to run SetKeyValue for Boolean Value");

let find_key_value = FindAssetKeyValueByIdAndKey::new(asset_id.clone(), key_name3);
let asset = client
.request(find_key_value)
.wrap_err("Failed to get key-value")
.unwrap();
fn parse_numeric_value() {
let actual = ValueArg::from_str("123_u32").expect("Failed to parse valid numeric value");
assert!(matches!(
actual,
ValueArg(Value::Numeric(NumericValue::U32(123)))
));
}

#[test]
fn parse_public_key() {
let public_key_str =
"ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0";
let actual = ValueArg::from_str(public_key_str).expect("Failed to parse valid public key");
assert_eq!(
serde_json::to_string(&asset).expect("Failed to deserialize asset"),
r#"{"Bool":true}"#
actual,
ValueArg(Value::PublicKey(
PublicKey::from_str(public_key_str).unwrap()
))
);
}

let key_name4 = Name::from_str("test_key4").expect("Invalid Name format");
let json_data = r#"{"Vec":[{"String":"a"},{"String":"b"}]}"#;
let test_value4 = ValueArg(serde_json::from_str(json_data).unwrap());

let set_key_value4 = SetKeyValue {
asset_id: asset_id.clone(),
key: key_name4.clone(),
value: test_value4,
};

set_key_value4
.run(&mut context)
.expect("Failed to run SetKeyValue for JSON Value");

let find_key_value = FindAssetKeyValueByIdAndKey::new(asset_id, key_name4);
let asset = client
.request(find_key_value)
.wrap_err("Failed to get key-value")
.unwrap();

assert_eq!(
serde_json::to_string(&asset).expect("Failed to deserialize asset"),
r#"{"Vec":[{"String":"a"},{"String":"b"}]}"#
);
#[test]
fn parse_json_value() {
let json_str = r#"{"Vec":[{"String":"a"},{"String":"b"}]}"#;
let actual = ValueArg::from_str(json_str).expect("Failed to parse valid JSON value");
let ValueArg(actual_value) = actual;
let actual_json: JsonValue = serde_json::to_value(actual_value).unwrap();
let expected_json: JsonValue = serde_json::from_str(json_str).unwrap();
assert_eq!(actual_json, expected_json);
}

#[test]
fn test_error_for_asset_id() {
let invalid_asset_id = AssetId::from_str("store#alice@wonderland");

match invalid_asset_id {
Ok(_) => panic!("Expected an error, but the operation succeeded"),
Err(e) => {
let error_msg = e.to_string();
let expected_pattern1 = "name#domain_of_asset#account_name@domain_of_account";
let expected_pattern2 = "name##account_name@domain_of_account";
assert!(
error_msg.contains(expected_pattern1) && error_msg.contains(expected_pattern2),
"Error message did not contain the expected pattern"
);
}
}
fn parse_invalid_value() {
let invalid_str = "not_a_valid_value";
let _invalid_value = ValueArg::from_str(invalid_str)
.expect_err("Should fail invalid type from string but passed");
}
}
Loading

0 comments on commit 6df7d12

Please sign in to comment.