Skip to content

Commit

Permalink
fix metadata and example tests accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
re-gius committed Jan 31, 2025
1 parent 0678c94 commit 04b430e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
42 changes: 15 additions & 27 deletions substrate/frame/examples/view-functions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ use crate::{
pallet2,
};
use codec::{Decode, Encode};
use scale_info::{form::PortableForm, meta_type};
use scale_info::meta_type;

use frame_support::{derive_impl, pallet_prelude::PalletInfoAccess, view_functions::ViewFunction};
use sp_io::hashing::twox_128;
use sp_metadata_ir::{ViewFunctionArgMetadataIR, ViewFunctionGroupIR, ViewFunctionMetadataIR};
use sp_metadata_ir::{
DeprecationStatusIR, PalletViewFunctionMethodMetadataIR,
PalletViewFunctionMethodParamMetadataIR,
};
use sp_runtime::testing::TestXt;

pub type AccountId = u32;
Expand Down Expand Up @@ -111,8 +114,7 @@ fn metadata_ir_definitions() {
new_test_ext().execute_with(|| {
let metadata_ir = Runtime::metadata_ir();
let pallet1 = metadata_ir
.view_functions
.groups
.pallets
.iter()
.find(|pallet| pallet.name == "ViewFunctionsExample")
.unwrap();
Expand All @@ -137,44 +139,30 @@ fn metadata_ir_definitions() {
pretty_assertions::assert_eq!(
pallet1.view_functions,
vec![
ViewFunctionMetadataIR {
PalletViewFunctionMethodMetadataIR {
name: "get_value",
id: get_value_id,
args: vec![],
inputs: vec![],
output: meta_type::<Option<u32>>(),
docs: vec![" Query value no args."],
deprecation_info: DeprecationStatusIR::NotDeprecated,
},
ViewFunctionMetadataIR {
PalletViewFunctionMethodMetadataIR {
name: "get_value_with_arg",
id: get_value_with_arg_id,
args: vec![ViewFunctionArgMetadataIR { name: "key", ty: meta_type::<u32>() },],
inputs: vec![PalletViewFunctionMethodParamMetadataIR {
name: "key",
ty: meta_type::<u32>()
},],
output: meta_type::<Option<u32>>(),
docs: vec![" Query value with args."],
deprecation_info: DeprecationStatusIR::NotDeprecated,
},
]
);
});
}

#[test]
fn metadata_encoded_to_custom_value() {
new_test_ext().execute_with(|| {
let metadata = sp_metadata_ir::into_latest(Runtime::metadata_ir());
// metadata is currently experimental so lives as a custom value.
let frame_metadata::RuntimeMetadata::V15(v15) = metadata.1 else {
panic!("Expected metadata v15")
};
let custom_value = v15
.custom
.map
.get("view_functions_experimental")
.expect("Expected custom value");
let view_function_groups: Vec<ViewFunctionGroupIR<PortableForm>> =
Decode::decode(&mut &custom_value.value[..]).unwrap();
assert_eq!(view_function_groups.len(), 4);
});
}

fn test_dispatch_view_function<Q, V>(query: &Q, expected: V)
where
Q: ViewFunction + Encode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn impl_view_function_metadata(
};

quote::quote! {
#frame_support::__private::metadata_ir::PalletViewFunctionMethodMetadata {
#frame_support::__private::metadata_ir::PalletViewFunctionMethodMetadataIR {
name: ::core::stringify!(#name),
id: <#view_function_struct_ident<#type_use_gen> as #frame_support::view_functions::ViewFunction>::id().into(),
inputs: #frame_support::__private::sp_std::vec![ #( #inputs ),* ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ pub struct ViewFunctionsImplDef {
pub where_clause: Option<syn::WhereClause>,
/// The span of the pallet::view_functions_experimental attribute.
pub attr_span: proc_macro2::Span,
/// Docs, specified on the impl Block.
pub docs: Vec<syn::Expr>,
/// The view function definitions.
pub view_functions: Vec<ViewFunctionDef>,
}
Expand Down Expand Up @@ -67,7 +65,6 @@ impl ViewFunctionsImplDef {
view_functions,
attr_span,
where_clause: item_impl.generics.where_clause.clone(),
docs: get_doc_literals(&item_impl.attrs),
})
}
}
Expand Down

0 comments on commit 04b430e

Please sign in to comment.