Add some test

This commit is contained in:
sunli 2020-05-03 09:12:14 +08:00
parent 81e2143015
commit c9fe0e9393
2 changed files with 106 additions and 1 deletions

View File

@ -251,7 +251,7 @@ where
for definition in document.definitions {
match definition {
Definition::Operation(OperationDefinition::Subscription(s)) => {
if s.name.as_deref() == operation_name {
if s.name.as_deref() == operation_name || operation_name.is_none() {
subscription = Some(s);
break;
}

View File

@ -405,3 +405,108 @@ pub async fn test_subscription_ws_transport_with_token() {
);
}
}
#[async_std::test]
pub async fn test_subscription_inline_fragment() {
struct QueryRoot;
#[SimpleObject]
struct Event {
a: i32,
b: i32,
}
#[Object]
impl QueryRoot {}
struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
async fn events(&self, start: i32, end: i32) -> impl Stream<Item = Event> {
futures::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 }))
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
let mut stream = schema
.create_subscription_stream(
r#"
subscription {
events(start: 10, end: 20) {
a
... {
b
}
}
}
"#,
None,
Default::default(),
None,
)
.await
.unwrap();
for i in 10..20 {
assert_eq!(
Some(serde_json::json!({ "events": {"a": i, "b": i * 10} })),
stream.next().await
);
}
assert!(stream.next().await.is_none());
}
#[async_std::test]
pub async fn test_subscription_fragment() {
struct QueryRoot;
#[SimpleObject]
struct Event {
a: i32,
b: i32,
}
#[Interface(field(name = "a", type = "i32"))]
struct MyInterface(Event);
#[Object]
impl QueryRoot {}
struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
async fn events(&self, start: i32, end: i32) -> impl Stream<Item = Event> {
futures::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 }))
}
}
let schema = Schema::build(QueryRoot, EmptyMutation, SubscriptionRoot)
.register_type::<MyInterface>()
.finish();
let mut stream = schema
.create_subscription_stream(
r#"
subscription s {
events(start: 10, end: 20) {
... on MyInterface {
a
}
b
}
}
"#,
None,
Default::default(),
None,
)
.await
.unwrap();
for i in 10..20 {
assert_eq!(
Some(serde_json::json!({ "events": {"a": i, "b": i * 10} })),
stream.next().await
);
}
assert!(stream.next().await.is_none());
}