Add some tests

This commit is contained in:
sunli 2020-04-22 21:31:44 +08:00
parent 44bd920ad2
commit 6da2e3a528
3 changed files with 182 additions and 5 deletions

65
tests/directive.rs Normal file
View File

@ -0,0 +1,65 @@
use async_graphql::*;
#[async_std::test]
pub async fn test_directive_skip() {
struct QueryRoot;
#[Object]
impl QueryRoot {
#[field]
pub async fn value(&self) -> i32 {
10
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let resp = schema
.execute(
r#"
{
value1: value @skip(if: true)
value2: value @skip(if: false)
}
"#,
)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"value2": 10,
})
);
}
#[async_std::test]
pub async fn test_directive_include() {
struct QueryRoot;
#[Object]
impl QueryRoot {
#[field]
pub async fn value(&self) -> i32 {
10
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let resp = schema
.execute(
r#"
{
value1: value @include(if: true)
value2: value @include(if: false)
}
"#,
)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"value1": 10,
})
);
}

View File

@ -4,13 +4,11 @@ use std::sync::Arc;
use std::time::Duration;
#[async_std::test]
pub async fn test_list_type() {
struct QueryRoot;
pub async fn test_mutation_execution_order() {
type List = Arc<Mutex<Vec<i32>>>;
#[Object]
impl QueryRoot {}
#[SimpleObject]
struct QueryRoot;
struct MutationRoot;
@ -42,3 +40,42 @@ pub async fn test_list_type() {
assert_eq!(list.lock().await[0], 1);
assert_eq!(list.lock().await[1], 2);
}
#[async_std::test]
pub async fn test_mutation_fragment() {
#[SimpleObject]
struct QueryRoot;
struct MutationRoot;
#[Object]
impl MutationRoot {
#[field]
async fn action(&self) -> bool {
true
}
}
let schema = Schema::new(QueryRoot, MutationRoot, EmptySubscription);
let resp = schema
.execute(
r#"
mutation {
... {
actionInUnnamedFragment: action
}
... on MutationRoot {
actionInNamedFragment: action
}
}"#,
)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"actionInUnnamedFragment": true,
"actionInNamedFragment": true,
})
);
}

75
tests/variables.rs Normal file
View File

@ -0,0 +1,75 @@
use async_graphql::*;
#[async_std::test]
pub async fn test_variables() {
struct QueryRoot;
#[Object]
impl QueryRoot {
#[field]
pub async fn int_val(&self, value: i32) -> i32 {
value
}
#[field]
pub async fn int_list_val(&self, value: Vec<i32>) -> Vec<i32> {
value
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let query = QueryBuilder::new(
r#"
query QueryWithVariables($intVal: Int!, $intListVal: [Int!]!) {
intVal(value: $intVal)
intListVal(value: $intListVal)
}
"#,
)
.variables(
Variables::parse_from_json(serde_json::json!({
"intVal": 10,
"intListVal": [1, 2, 3, 4, 5],
}))
.unwrap(),
);
let resp = query.execute(&schema).await.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"intVal": 10,
"intListVal": [1, 2, 3, 4, 5],
})
);
}
#[async_std::test]
pub async fn test_variable_default_value() {
struct QueryRoot;
#[Object]
impl QueryRoot {
#[field]
pub async fn int_val(&self, value: i32) -> i32 {
value
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let resp = schema
.execute(
r#"
query QueryWithVariables($intVal: Int = 10) {
intVal(value: $intVal)
}
"#,
)
.await
.unwrap();
assert_eq!(
resp.data,
serde_json::json!({
"intVal": 10,
})
);
}