Skip to content

Commit

Permalink
feat(blueprint-metadata): add nested struct and enum tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Jan 29, 2025
1 parent 1a755db commit aec01bd
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
33 changes: 33 additions & 0 deletions crates/blueprint/metadata/tests/assets/job_enum_param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::convert::Infallible;
use blueprint_sdk::event_listeners::tangle::events::TangleEventListener;
use blueprint_sdk::event_listeners::tangle::services::{services_post_processor, services_pre_processor};
use blueprint_sdk::macros::contexts::{ServicesContext, TangleClientContext};
use blueprint_sdk::macros::ext::tangle::tangle_subxt::tangle_testnet_runtime::api::services::events::JobCalled;
use blueprint_sdk::config::GadgetConfiguration;

#[derive(Clone, TangleClientContext, ServicesContext)]
pub struct MyContext {
#[config]
pub env: GadgetConfiguration,
#[call_id]
pub call_id: Option<u64>,
}

#[derive(PartialEq)]
pub enum Status {
Failed,
Success,
}

#[blueprint_sdk::job(
id = 0,
params(status),
event_listener(
listener = TangleEventListener<MyContext, JobCalled>,
pre_processor = services_pre_processor,
post_processor = services_post_processor,
),
)]
pub fn check_status(status: Status, _context: MyContext) -> Result<bool, Infallible> {
Ok(status == Status::Success)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::convert::Infallible;
use blueprint_sdk::event_listeners::tangle::events::TangleEventListener;
use blueprint_sdk::event_listeners::tangle::services::{services_post_processor, services_pre_processor};
use blueprint_sdk::macros::contexts::{ServicesContext, TangleClientContext};
use blueprint_sdk::macros::ext::tangle::tangle_subxt::tangle_testnet_runtime::api::services::events::JobCalled;
use blueprint_sdk::config::GadgetConfiguration;

#[derive(Clone, TangleClientContext, ServicesContext)]
pub struct MyContext {
#[config]
pub env: GadgetConfiguration,
#[call_id]
pub call_id: Option<u64>,
}

pub struct SomeParam {
pub a: u8,
pub nested: SomeNestedParam,
}

pub struct SomeNestedParam {
pub b: u16,
pub c: u32,
pub d: u64,
pub e: u128,
pub f: i8,
pub g: i16,
pub h: i32,
pub i: i64,
pub j: i128,
pub k: f32,
pub l: f64,
pub m: bool,
}

#[blueprint_sdk::job(
id = 0,
params(x),
event_listener(
listener = TangleEventListener<MyContext, JobCalled>,
pre_processor = services_pre_processor,
post_processor = services_post_processor,
),
)]
pub fn xsquare(x: SomeParam, _context: MyContext) -> Result<u64, Infallible> {
Ok(x.nested.d.saturating_pow(2))
}
57 changes: 57 additions & 0 deletions crates/blueprint/metadata/tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,60 @@ fn generate_job_with_primitive_struct_param() {
)
);
}

#[test]
fn generate_job_with_primitive_nested_struct_param() {
let blueprint = do_test("job_primitive_nested_struct_param");
assert_eq!(blueprint.jobs.len(), 1);

let job = &blueprint.jobs[0];
assert_eq!(job.job_id, 0);
assert_eq!(job.metadata.name, "xsquare");

assert_eq!(job.params.len(), 1);
assert_eq!(job.params[0].as_rust_type(), "SomeParam");

let nested_struct = FieldType::Struct(
String::from("SomeNestedParam"),
vec![
(String::from("b"), Box::new(FieldType::Uint16)),
(String::from("c"), Box::new(FieldType::Uint32)),
(String::from("d"), Box::new(FieldType::Uint64)),
(String::from("e"), Box::new(FieldType::Uint128)),
(String::from("f"), Box::new(FieldType::Int8)),
(String::from("g"), Box::new(FieldType::Int16)),
(String::from("h"), Box::new(FieldType::Int32)),
(String::from("i"), Box::new(FieldType::Int64)),
(String::from("j"), Box::new(FieldType::Int128)),
(String::from("k"), Box::new(FieldType::Float64)),
(String::from("l"), Box::new(FieldType::Float64)),
(String::from("m"), Box::new(FieldType::Bool)),
],
);

assert_eq!(
job.params[0],
FieldType::Struct(
"SomeParam".to_string(),
vec![
(String::from("a"), Box::new(FieldType::Uint8),),
(String::from("nested"), Box::new(nested_struct))
]
)
);
}

#[test]
fn generate_job_with_enum_param() {
let blueprint = do_test("job_enum_param");
assert_eq!(blueprint.jobs.len(), 1);

let job = &blueprint.jobs[0];
assert_eq!(job.job_id, 0);
assert_eq!(job.metadata.name, "check_status");

assert_eq!(job.params.len(), 1);

// Enums are represented as strings, since we only support unit variants
assert_eq!(job.params[0].as_rust_type(), "String");
}

0 comments on commit aec01bd

Please sign in to comment.