Skip to content

Commit a8fa4b4

Browse files
jasonz-dfinityIDX GitHub Automation
andauthored
test(nns): Use SelfDescribingValue::from for primitives in tests (#8365)
In some previous PRs (e.g. [this one](#8297)) we started to use `SelfDescribingValue::from` for simpler types (i.e. not Array/Map) for API types in tests. This PR makes all other similar test cases consistent with this practice --------- Co-authored-by: IDX GitHub Automation <infra+github-automation@dfinity.org>
1 parent 8f51fe4 commit a8fa4b4

11 files changed

+197
-200
lines changed

rs/nns/governance/api/src/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,6 +4600,12 @@ pub enum SelfDescribingValue {
46004600
Map(HashMap<String, SelfDescribingValue>),
46014601
}
46024602

4603+
impl From<String> for SelfDescribingValue {
4604+
fn from(value: String) -> Self {
4605+
SelfDescribingValue::Text(value)
4606+
}
4607+
}
4608+
46034609
impl From<&str> for SelfDescribingValue {
46044610
fn from(value: &str) -> Self {
46054611
SelfDescribingValue::Text(value.to_string())
@@ -4624,6 +4630,12 @@ impl From<Vec<u8>> for SelfDescribingValue {
46244630
}
46254631
}
46264632

4633+
impl From<PrincipalId> for SelfDescribingValue {
4634+
fn from(value: PrincipalId) -> Self {
4635+
SelfDescribingValue::Text(value.to_string())
4636+
}
4637+
}
4638+
46274639
#[derive(candid::CandidType, candid::Deserialize, serde::Serialize, Debug, Clone, PartialEq)]
46284640
pub struct SelfDescribingProposalAction {
46294641
pub type_name: Option<String>,

rs/nns/governance/src/proposals/add_or_remove_node_provider_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ fn test_to_self_describing_value() {
280280
),
281281
SelfDescribingValue::Map(hashmap! {
282282
"ToAdd".to_string() => SelfDescribingValue::Map(hashmap! {
283-
"id".to_string() => SelfDescribingValue::Text("6fyp7-3ibaa-aaaaa-aaaap-4ai".to_string()),
284-
"reward_account".to_string() => SelfDescribingValue::Text(account.to_hex())
283+
"id".to_string() => SelfDescribingValue::from("6fyp7-3ibaa-aaaaa-aaaap-4ai"),
284+
"reward_account".to_string() => SelfDescribingValue::from(account.to_hex())
285285
})
286286
})
287287
);
@@ -301,7 +301,7 @@ fn test_to_self_describing_value() {
301301
),
302302
SelfDescribingValue::Map(hashmap! {
303303
"ToRemove".to_string() => SelfDescribingValue::Map(hashmap! {
304-
"id".to_string() => SelfDescribingValue::Text("6fyp7-3ibaa-aaaaa-aaaap-4ai".to_string())
304+
"id".to_string() => SelfDescribingValue::from("6fyp7-3ibaa-aaaaa-aaaap-4ai")
305305
})
306306
})
307307
);

rs/nns/governance/src/proposals/decode_candid_args_to_self_describing_value_tests.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ fn test_decode_candid_args_to_self_describing_value_simple_record() {
4848
"my_method",
4949
&encoded,
5050
SelfDescribingValue::Map(hashmap! {
51-
"name".to_string() => SelfDescribingValue::Text("Alice".to_string()),
52-
"age".to_string() => SelfDescribingValue::Nat(Nat::from(30u64)),
51+
"name".to_string() => SelfDescribingValue::from("Alice"),
52+
"age".to_string() => SelfDescribingValue::from(30_u64),
5353
}),
5454
);
5555
}
@@ -69,7 +69,7 @@ fn test_decode_candid_args_to_self_describing_value_text() {
6969
schema,
7070
"greet",
7171
&encoded,
72-
SelfDescribingValue::Text("Hello, World!".to_string()),
72+
SelfDescribingValue::from("Hello, World!"),
7373
);
7474
}
7575

@@ -88,7 +88,7 @@ fn test_decode_candid_args_to_self_describing_value_nat() {
8888
schema,
8989
"set_count",
9090
&encoded,
91-
SelfDescribingValue::Nat(Nat::from(42u64)),
91+
SelfDescribingValue::from(42_u64),
9292
);
9393
}
9494

@@ -107,7 +107,7 @@ fn test_decode_candid_args_to_self_describing_value_principal() {
107107
schema,
108108
"set_owner",
109109
&encoded,
110-
SelfDescribingValue::Text(principal.to_string()),
110+
SelfDescribingValue::from(principal.to_string()),
111111
);
112112
}
113113

@@ -126,7 +126,7 @@ fn test_decode_candid_args_to_self_describing_value_opt_some() {
126126
schema,
127127
"set_value",
128128
&encoded,
129-
SelfDescribingValue::Array(vec![SelfDescribingValue::Nat(Nat::from(42u64))]),
129+
SelfDescribingValue::Array(vec![SelfDescribingValue::from(42_u64)]),
130130
);
131131
}
132132

@@ -165,9 +165,9 @@ fn test_decode_candid_args_to_self_describing_value_vec() {
165165
"set_values",
166166
&encoded,
167167
SelfDescribingValue::Array(vec![
168-
SelfDescribingValue::Nat(Nat::from(1u64)),
169-
SelfDescribingValue::Nat(Nat::from(2u64)),
170-
SelfDescribingValue::Nat(Nat::from(3u64)),
168+
SelfDescribingValue::from(1_u64),
169+
SelfDescribingValue::from(2_u64),
170+
SelfDescribingValue::from(3_u64),
171171
]),
172172
);
173173
}
@@ -197,7 +197,7 @@ fn test_decode_candid_args_to_self_describing_value_variant() {
197197
schema,
198198
"set_status",
199199
&encoded,
200-
SelfDescribingValue::Text("Active".to_string()),
200+
SelfDescribingValue::from("Active"),
201201
);
202202

203203
// Test variant with value
@@ -208,7 +208,7 @@ fn test_decode_candid_args_to_self_describing_value_variant() {
208208
"set_status",
209209
&encoded,
210210
SelfDescribingValue::Map(hashmap! {
211-
"Pending".to_string() => SelfDescribingValue::Nat(Nat::from(42u64)),
211+
"Pending".to_string() => SelfDescribingValue::from(42_u64),
212212
}),
213213
);
214214
}
@@ -230,13 +230,13 @@ fn test_decode_candid_args_to_self_describing_value_blob() {
230230
schema,
231231
"set_data_vec_nat8",
232232
&encoded,
233-
SelfDescribingValue::Blob(vec![1u8, 2u8, 3u8, 4u8]),
233+
SelfDescribingValue::from(vec![1_u8, 2, 3, 4]),
234234
);
235235
assert_candid_converts_to(
236236
schema,
237237
"set_data_blob",
238238
&encoded,
239-
SelfDescribingValue::Blob(vec![1u8, 2u8, 3u8, 4u8]),
239+
SelfDescribingValue::from(vec![1_u8, 2, 3, 4]),
240240
);
241241
}
242242

@@ -287,7 +287,7 @@ fn test_decode_candid_args_to_self_describing_value_reserved() {
287287
"set_name",
288288
&encoded,
289289
SelfDescribingValue::Map(hashmap! {
290-
"first".to_string() => SelfDescribingValue::Text("John".to_string()),
290+
"first".to_string() => SelfDescribingValue::from("John"),
291291
"last".to_string() => SelfDescribingValue::Array(vec![]),
292292
}),
293293
);
@@ -409,10 +409,10 @@ fn test_decode_candid_args_to_self_describing_value_nested_record() {
409409
"set_person",
410410
&encoded,
411411
SelfDescribingValue::Map(hashmap! {
412-
"name".to_string() => SelfDescribingValue::Text("Alice".to_string()),
412+
"name".to_string() => SelfDescribingValue::from("Alice"),
413413
"address".to_string() => SelfDescribingValue::Map(hashmap! {
414-
"street".to_string() => SelfDescribingValue::Text("123 Main St".to_string()),
415-
"city".to_string() => SelfDescribingValue::Text("Wonderland".to_string()),
414+
"street".to_string() => SelfDescribingValue::from("123 Main St"),
415+
"city".to_string() => SelfDescribingValue::from("Wonderland"),
416416
}),
417417
}),
418418
);

rs/nns/governance/src/proposals/deregister_known_neuron_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn test_deregister_known_neuron_to_self_describing() {
200200
assert_eq!(
201201
value,
202202
SelfDescribingValue::Map(hashmap! {
203-
"neuron_id".to_string() => SelfDescribingValue::Nat(candid::Nat::from(456u64)),
203+
"neuron_id".to_string() => SelfDescribingValue::from(456_u64),
204204
})
205205
);
206206
}

rs/nns/governance/src/proposals/execute_nns_function_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
},
77
test_utils::{ExpectedCallCanisterMethodCallArguments, MockEnvironment},
88
};
9-
use candid::{Decode, Encode, Nat};
9+
use candid::{Decode, Encode};
1010
use ic_base_types::CanisterId;
1111
use ic_management_canister_types_private::{CanisterMetadataRequest, CanisterMetadataResponse};
1212
use ic_nns_constants::{BITCOIN_MAINNET_CANISTER_ID, CYCLES_MINTING_CANISTER_ID};
@@ -186,7 +186,7 @@ service : {
186186
assert_eq!(
187187
self_describing_value,
188188
SelfDescribingValue::Map(hashmap! {
189-
"Add".to_string() => SelfDescribingValue::Text("application".to_string()),
189+
"Add".to_string() => SelfDescribingValue::from("application"),
190190
})
191191
);
192192
}
@@ -227,9 +227,9 @@ async fn test_to_self_describing_uninstall_code() {
227227
assert_eq!(
228228
SelfDescribingValue::from(result.value.unwrap()),
229229
SelfDescribingValue::Map(hashmap! {
230-
"canister_id".to_string() => SelfDescribingValue::Text(target_canister.to_string()),
230+
"canister_id".to_string() => SelfDescribingValue::from(target_canister.to_string()),
231231
"sender_canister_version".to_string() => SelfDescribingValue::Array(vec![
232-
SelfDescribingValue::Nat(Nat::from(42_u64)),
232+
SelfDescribingValue::from(42_u64),
233233
]),
234234
})
235235
);
@@ -264,9 +264,9 @@ async fn test_to_self_describing_bitcoin_set_config() {
264264
assert_eq!(
265265
SelfDescribingValue::from(result.value.unwrap()),
266266
SelfDescribingValue::Map(hashmap! {
267-
"canister_id".to_string() => SelfDescribingValue::Text(BITCOIN_MAINNET_CANISTER_ID.to_string()),
268-
"method_name".to_string() => SelfDescribingValue::Text("set_config".to_string()),
269-
"payload".to_string() => SelfDescribingValue::Blob(bitcoin_payload),
267+
"canister_id".to_string() => SelfDescribingValue::from(BITCOIN_MAINNET_CANISTER_ID.to_string()),
268+
"method_name".to_string() => SelfDescribingValue::from("set_config"),
269+
"payload".to_string() => SelfDescribingValue::from(bitcoin_payload),
270270
})
271271
);
272272
}

rs/nns/governance/src/proposals/install_code.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,6 @@ mod tests {
453453

454454
#[test]
455455
fn test_install_code_to_self_describing() {
456-
use SelfDescribingValue::*;
457-
458456
let wasm_hash = Sha256::hash(&[1, 2, 3]).to_vec();
459457
let arg_hash = Sha256::hash(&[4, 5, 6]).to_vec();
460458

@@ -474,19 +472,17 @@ mod tests {
474472
assert_eq!(
475473
value,
476474
SelfDescribingValue::Map(hashmap! {
477-
"canister_id".to_string() => Text(REGISTRY_CANISTER_ID.get().to_string()),
478-
"install_mode".to_string() => Text("Upgrade".to_string()),
479-
"wasm_module_hash".to_string() => Blob(wasm_hash),
480-
"arg_hash".to_string() => Blob(arg_hash),
481-
"skip_stopping_before_installing".to_string() => Nat(candid::Nat::from(1_u8)),
475+
"canister_id".to_string() => SelfDescribingValue::from(REGISTRY_CANISTER_ID.get().to_string()),
476+
"install_mode".to_string() => SelfDescribingValue::from("Upgrade"),
477+
"wasm_module_hash".to_string() => SelfDescribingValue::from(wasm_hash),
478+
"arg_hash".to_string() => SelfDescribingValue::from(arg_hash),
479+
"skip_stopping_before_installing".to_string() => SelfDescribingValue::from(1_u64),
482480
})
483481
);
484482
}
485483

486484
#[test]
487485
fn test_install_code_to_self_describing_install_mode() {
488-
use SelfDescribingValue::*;
489-
490486
let install_code = InstallCode {
491487
canister_id: Some(REGISTRY_CANISTER_ID.get()),
492488
wasm_module: Some(vec![1, 2, 3]),
@@ -502,12 +498,12 @@ mod tests {
502498

503499
assert_eq!(
504500
value,
505-
Map(hashmap! {
506-
"canister_id".to_string() => Text(REGISTRY_CANISTER_ID.get().to_string()),
507-
"install_mode".to_string() => Text("Install".to_string()),
508-
"wasm_module_hash".to_string() => Blob(Sha256::hash(&[1, 2, 3]).to_vec()),
509-
"arg_hash".to_string() => Blob(Sha256::hash(&[]).to_vec()),
510-
"skip_stopping_before_installing".to_string() => Nat(candid::Nat::from(0_u8)),
501+
SelfDescribingValue::Map(hashmap! {
502+
"canister_id".to_string() => SelfDescribingValue::from(REGISTRY_CANISTER_ID.get().to_string()),
503+
"install_mode".to_string() => SelfDescribingValue::from("Install"),
504+
"wasm_module_hash".to_string() => SelfDescribingValue::from(Sha256::hash(&[1, 2, 3]).to_vec()),
505+
"arg_hash".to_string() => SelfDescribingValue::from(Sha256::hash(&[]).to_vec()),
506+
"skip_stopping_before_installing".to_string() => SelfDescribingValue::from(0_u64),
511507
})
512508
);
513509
}

0 commit comments

Comments
 (0)