async-graphql/tests/scalars.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2020-03-07 04:40:04 +00:00
use async_graphql::*;
macro_rules! test_scalars {
($test_name:ident, $ty:ty, $value:expr, $res_value:expr) => {
2020-03-07 04:40:04 +00:00
#[async_std::test]
pub async fn $test_name() {
#[InputObject]
struct MyInput {
value: $ty,
}
struct Root {
value: $ty,
}
#[Object]
impl Root {
#[field]
async fn value(&self) -> $ty {
self.value
}
#[field]
async fn test_arg(&self, input: $ty) -> $ty {
input
}
#[field]
async fn test_input(&self, input: MyInput) -> $ty {
input.value
}
}
2020-03-19 09:20:12 +00:00
let schema = Schema::new(Root { value: $value }, EmptyMutation, EmptySubscription);
2020-03-07 04:40:04 +00:00
let json_value: serde_json::Value = $value.into();
2020-03-09 12:00:57 +00:00
let query = format!("{{ value testArg(input: {0}) testInput(input: {{value: {0}}}) }}", json_value);
2020-03-07 04:40:04 +00:00
assert_eq!(
2020-04-02 04:53:53 +00:00
schema.execute(&query).await.unwrap().data,
serde_json::json!({ "value": $res_value, "testArg": $res_value, "testInput": $res_value })
2020-03-07 04:40:04 +00:00
);
}
};
}
test_scalars!(test_i8_scalar, i8, 10, 10);
test_scalars!(test_i16_scalar, i16, 10, 10);
test_scalars!(test_i32_scalar, i32, 10, 10);
test_scalars!(test_u8_scalar, u8, 10, 10);
test_scalars!(test_u16_scalar, u16, 10, 10);
test_scalars!(test_u32_scalar, u32, 10, 10);
test_scalars!(test_bool_scalar, bool, true, true);
test_scalars!(test_f32_scalar, f32, 10.5, 10.5);
test_scalars!(test_f64_scalar, f32, 10.5, 10.5);
test_scalars!(test_i64_scalar, i64, 10, "10");
test_scalars!(test_u64_scalar, u64, 10, "10");