async-graphql/tests/enum.rs

95 lines
1.9 KiB
Rust
Raw Normal View History

2020-03-07 04:40:04 +00:00
use async_graphql::*;
#[tokio::test]
2020-03-07 04:40:04 +00:00
pub async fn test_enum_type() {
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
2020-03-07 04:40:04 +00:00
enum MyEnum {
A,
B,
}
#[derive(InputObject)]
2020-03-07 04:40:04 +00:00
struct MyInput {
value: MyEnum,
}
struct Root {
value: MyEnum,
}
#[Object]
2020-03-07 04:40:04 +00:00
impl Root {
async fn value(&self) -> MyEnum {
self.value
}
async fn test_arg(&self, input: MyEnum) -> MyEnum {
input
}
async fn test_input<'a>(&self, input: MyInput) -> MyEnum {
input.value
}
}
2020-03-19 09:20:12 +00:00
let schema = Schema::new(Root { value: MyEnum::A }, EmptyMutation, EmptySubscription);
2020-09-01 01:10:12 +00:00
let query = r#"{
2020-03-07 04:40:04 +00:00
value
2020-03-09 12:00:57 +00:00
testArg(input: A)
2020-09-01 01:10:12 +00:00
testInput(input: {value: B})
}"#
.to_owned();
2020-03-07 04:40:04 +00:00
assert_eq!(
2020-09-10 11:35:48 +00:00
schema.execute(&query).await.data,
value!({
2020-03-07 04:40:04 +00:00
"value": "A",
2020-03-09 12:00:57 +00:00
"testArg": "A",
"testInput": "B",
2020-03-07 04:40:04 +00:00
})
);
}
2020-04-08 01:05:54 +00:00
#[tokio::test]
2020-04-08 01:05:54 +00:00
pub async fn test_enum_derive_and_item_attributes() {
use serde::Deserialize;
2020-04-08 01:05:54 +00:00
#[derive(Deserialize, Debug, Enum, Copy, Clone, Eq, PartialEq)]
2020-04-08 01:05:54 +00:00
enum Test {
#[serde(alias = "Other")]
Real,
}
#[derive(Deserialize, PartialEq, Debug)]
#[allow(dead_code)]
struct TestStruct {
value: Test,
}
assert_eq!(
2020-10-13 02:19:30 +00:00
from_value::<TestStruct>(value!({"value": "Other"})).unwrap(),
2020-04-08 01:05:54 +00:00
TestStruct { value: Test::Real }
);
}
#[tokio::test]
pub async fn test_remote_enum() {
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
#[graphql(remote = "remote::RemoteEnum")]
enum LocalEnum {
A,
B,
C,
}
mod remote {
pub enum RemoteEnum {
A,
B,
C,
}
}
let _: remote::RemoteEnum = LocalEnum::A.into();
let _: LocalEnum = remote::RemoteEnum::A.into();
}