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 publish = false
edition = "2018" edition = "2018"
[dependencies]
juniper = { version = "0.11.0", path = "../../juniper" }
serde_json = { version = "1" }
[dev-dependencies] [dev-dependencies]
juniper = { path = "../../juniper" }
serde_json = { version = "1" }
fnv = "1.0.3" 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, Foo,
} }
/// Doc 1. /// Doc 1.\
/// Doc 2. /// Doc 2.
/// ///
/// Doc 4. /// Doc 4.
@ -85,7 +85,7 @@ fn test_multi_doc_comment() {
let meta = MultiDocEnum::meta(&(), &mut registry); let meta = MultiDocEnum::meta(&(), &mut registry);
assert_eq!( assert_eq!(
meta.description(), 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, regular_field: bool,
} }
/// Doc 1. /// Doc 1.\
/// Doc 2. /// Doc 2.
/// ///
/// Doc 4. /// Doc 4.
@ -151,7 +151,7 @@ fn test_multi_doc_comment() {
let meta = MultiDocComment::meta(&(), &mut registry); let meta = MultiDocComment::meta(&(), &mut registry);
assert_eq!( assert_eq!(
meta.description(), 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( #[graphql(
name = "MyObj", name = "MyObj",
description = "obj descr", description = "obj descr",
scalar = "DefaultScalarValue" scalar = DefaultScalarValue
)] )]
struct Obj { struct Obj {
regular_field: bool, regular_field: bool,
#[graphql( #[graphql(
name = "renamedField", name = "renamedField",
description = "descr", description = "descr",
deprecation = "field descr" deprecated = "field deprecation"
)] )]
c: i32, c: i32,
} }
#[derive(GraphQLObject, Debug, PartialEq)] #[derive(GraphQLObject, Debug, PartialEq)]
#[graphql(scalar = "DefaultScalarValue")] #[graphql(scalar = DefaultScalarValue)]
struct Nested { struct Nested {
obj: Obj, obj: Obj,
} }
@ -39,7 +39,7 @@ struct DocComment {
regular_field: bool, regular_field: bool,
} }
/// Doc 1. /// Doc 1.\
/// Doc 2. /// Doc 2.
/// ///
/// Doc 4. /// Doc 4.
@ -75,56 +75,57 @@ struct Context;
impl juniper::Context for Context {} impl juniper::Context for Context {}
#[derive(GraphQLObject, Debug)] #[derive(GraphQLObject, Debug)]
#[graphql(Context = "Context")] #[graphql(Context = Context)]
struct WithCustomContext { struct WithCustomContext {
a: bool, a: bool,
} }
juniper::graphql_object!(Query: () |&self| { #[juniper::impl_object]
field obj() -> Obj { impl Query {
Obj{ fn obj() -> Obj {
Obj {
regular_field: true, regular_field: true,
c: 22, c: 22,
} }
} }
field nested() -> Nested { fn nested() -> Nested {
Nested{ Nested {
obj: Obj{ obj: Obj {
regular_field: false, regular_field: false,
c: 333, c: 333,
} },
} }
} }
field doc() -> DocComment { fn doc() -> DocComment {
DocComment{ DocComment {
regular_field: true, regular_field: true,
} }
} }
field multi_doc() -> MultiDocComment { fn multi_doc() -> MultiDocComment {
MultiDocComment{ MultiDocComment {
regular_field: true, regular_field: true,
} }
} }
field override_doc() -> OverrideDocComment { fn override_doc() -> OverrideDocComment {
OverrideDocComment{ OverrideDocComment {
regular_field: true, regular_field: true,
} }
} }
field skipped_field_obj() -> SkippedFieldObj { fn skipped_field_obj() -> SkippedFieldObj {
SkippedFieldObj{ SkippedFieldObj {
regular_field: false, regular_field: false,
skipped: 42, skipped: 42,
} }
} }
}); }
#[test] #[test]
fn test_doc_comment() { fn test_doc_comment_simple() {
let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default()); let mut registry: juniper::Registry = juniper::Registry::new(FnvHashMap::default());
let meta = DocComment::meta(&(), &mut registry); let meta = DocComment::meta(&(), &mut registry);
assert_eq!(meta.description(), Some(&"Object comment.".to_string())); assert_eq!(meta.description(), Some(&"Object comment.".to_string()));
@ -143,14 +144,14 @@ fn test_multi_doc_comment() {
let meta = MultiDocComment::meta(&(), &mut registry); let meta = MultiDocComment::meta(&(), &mut registry);
assert_eq!( assert_eq!(
meta.description(), meta.description(),
Some(&"Doc 1. Doc 2.\nDoc 4.".to_string()) Some(&"Doc 1. Doc 2.\n\nDoc 4.".to_string())
); );
check_descriptions( check_descriptions(
"MultiDocComment", "MultiDocComment",
&Value::scalar("Doc 1. Doc 2.\nDoc 4."), &Value::scalar("Doc 1. Doc 2.\n\nDoc 4."),
"regularField", "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_enum;
mod derive_input_object; mod derive_input_object;
mod derive_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; struct TestType;
juniper::graphql_object!(TestType: () where Scalar = MyScalarValue |&self| { #[juniper::impl_object(
field long_field() -> i64 { Scalar = MyScalarValue
)]
impl TestType {
fn long_field() -> i64 {
(::std::i32::MAX as i64) + 1 (::std::i32::MAX as i64) + 1
} }
field long_with_arg(long_arg: i64) -> i64 { fn long_with_arg(long_arg: i64) -> i64 {
long_arg long_arg
} }
}); }
#[cfg(test)] #[cfg(test)]
fn run_variable_query<F>(query: &str, vars: Variables<MyScalarValue>, f: F) fn run_variable_query<F>(query: &str, vars: Variables<MyScalarValue>, f: F)

View file

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