Integration tests: impl_object refactor

This commit is contained in:
Christoph Herzog 2019-05-07 10:55:46 +02:00
parent a993c16b85
commit bf50c3eb86
No known key found for this signature in database
GPG key ID: DAFF71D48B493238
8 changed files with 109 additions and 64 deletions

View file

@ -4,15 +4,7 @@ version = "0.1.0"
publish = false
edition = "2018"
[dependencies]
juniper = { version = "0.11.0", path = "../../juniper" }
serde_json = { version = "1" }
[dev-dependencies]
juniper = { path = "../../juniper" }
serde_json = { version = "1" }
fnv = "1.0.3"
indexmap = "1.0"
[[test]]
name = "integration_tests"
path = "src/lib.rs"
harness = true

View file

@ -19,7 +19,7 @@ enum DocEnum {
Foo,
}
/// Doc 1.
/// Doc 1.\
/// Doc 2.
///
/// Doc 4.
@ -85,7 +85,7 @@ fn test_multi_doc_comment() {
let meta = MultiDocEnum::meta(&(), &mut registry);
assert_eq!(
meta.description(),
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string())
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
);
}

View file

@ -27,7 +27,7 @@ struct DocComment {
regular_field: bool,
}
/// Doc 1.
/// Doc 1.\
/// Doc 2.
///
/// Doc 4.
@ -151,7 +151,7 @@ fn test_multi_doc_comment() {
let meta = MultiDocComment::meta(&(), &mut registry);
assert_eq!(
meta.description(),
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string())
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
);
}

View file

@ -12,20 +12,20 @@ use juniper::{self, execute, EmptyMutation, GraphQLType, RootNode, Value, Variab
#[graphql(
name = "MyObj",
description = "obj descr",
scalar = "DefaultScalarValue"
scalar = DefaultScalarValue
)]
struct Obj {
regular_field: bool,
#[graphql(
name = "renamedField",
description = "descr",
deprecation = "field descr"
deprecated = "field deprecation"
)]
c: i32,
}
#[derive(GraphQLObject, Debug, PartialEq)]
#[graphql(scalar = "DefaultScalarValue")]
#[graphql(scalar = DefaultScalarValue)]
struct Nested {
obj: Obj,
}
@ -39,7 +39,7 @@ struct DocComment {
regular_field: bool,
}
/// Doc 1.
/// Doc 1.\
/// Doc 2.
///
/// Doc 4.
@ -75,56 +75,57 @@ struct Context;
impl juniper::Context for Context {}
#[derive(GraphQLObject, Debug)]
#[graphql(Context = "Context")]
#[graphql(Context = Context)]
struct WithCustomContext {
a: bool,
}
juniper::graphql_object!(Query: () |&self| {
field obj() -> Obj {
Obj{
regular_field: true,
c: 22,
}
}
field nested() -> Nested {
Nested{
obj: Obj{
regular_field: false,
c: 333,
}
#[juniper::impl_object]
impl Query {
fn obj() -> Obj {
Obj {
regular_field: true,
c: 22,
}
}
field doc() -> DocComment {
DocComment{
regular_field: true,
}
fn nested() -> Nested {
Nested {
obj: Obj {
regular_field: false,
c: 333,
},
}
}
field multi_doc() -> MultiDocComment {
MultiDocComment{
regular_field: true,
}
fn doc() -> DocComment {
DocComment {
regular_field: true,
}
}
field override_doc() -> OverrideDocComment {
OverrideDocComment{
regular_field: true,
}
fn multi_doc() -> MultiDocComment {
MultiDocComment {
regular_field: true,
}
}
field skipped_field_obj() -> SkippedFieldObj {
SkippedFieldObj{
fn override_doc() -> OverrideDocComment {
OverrideDocComment {
regular_field: true,
}
}
fn skipped_field_obj() -> SkippedFieldObj {
SkippedFieldObj {
regular_field: false,
skipped: 42,
}
}
});
}
#[test]
fn test_doc_comment() {
fn test_doc_comment_simple() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = DocComment::meta(&(), &mut registry);
assert_eq!(meta.description(), Some(&"Object comment.".to_string()));
@ -143,14 +144,14 @@ fn test_multi_doc_comment() {
let meta = MultiDocComment::meta(&(), &mut registry);
assert_eq!(
meta.description(),
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string())
Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
);
check_descriptions(
"MultiDocComment",
&Value::scalar("Doc 1. Doc 2.\nDoc 4."),
&Value::scalar("Doc 1. Doc 2.\n\nDoc 4."),
"regularField",
&Value::scalar("Field 1. Field 2."),
&Value::scalar("Field 1.\nField 2."),
);
}

View file

@ -1,3 +1,5 @@
mod util;
mod derive_enum;
mod derive_input_object;
mod derive_object;

View file

@ -0,0 +1,54 @@
use juniper::{DefaultScalarValue, GraphQLType, RootNode, Value, Variables};
use std::default::Default;
pub fn run_query<Query, Mutation, Context>(query: &str) -> Value
where
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Context: Default,
{
let schema = RootNode::new(Query::default(), Mutation::default());
let (result, errs) =
juniper::execute(query, None, &schema, &Variables::new(), &Context::default())
.expect("Execution failed");
assert_eq!(errs, []);
result
}
pub fn run_info_query<Query, Mutation, Context>(type_name: &str) -> Value
where
Query: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Mutation: GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
Context: Default,
{
let query = format!(
r#"
{{
__type(name: "{}") {{
name,
description,
fields {{
name
description
args {{
name
description
type {{
name
}}
}}
}}
}}
}}
"#,
type_name
);
let result = run_query::<Query, Mutation, Context>(&query);
result
.as_object_value()
.expect("Result is not an object")
.get_field_value("__type")
.expect("__type field missing")
.clone()
}

View file

@ -151,15 +151,18 @@ juniper::graphql_scalar!(i64 as "Long" where Scalar = MyScalarValue {
struct TestType;
juniper::graphql_object!(TestType: () where Scalar = MyScalarValue |&self| {
field long_field() -> i64 {
#[juniper::impl_object(
Scalar = MyScalarValue
)]
impl TestType {
fn long_field() -> i64 {
(::std::i32::MAX as i64) + 1
}
field long_with_arg(long_arg: i64) -> i64 {
fn long_with_arg(long_arg: i64) -> i64 {
long_arg
}
});
}
#[cfg(test)]
fn run_variable_query<F>(query: &str, vars: Variables<MyScalarValue>, f: F)

View file

@ -1,11 +1,4 @@
extern crate juniper;
#[cfg(test)]
extern crate serde_json;
#[cfg(test)]
extern crate fnv;
#[cfg(test)]
extern crate indexmap;
mod codegen;
#[cfg(test)]
mod custom_scalar;