From bf50c3eb86a56de24fbdc7c36b83a9508697444f Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Tue, 7 May 2019 10:55:46 +0200 Subject: [PATCH] Integration tests: impl_object refactor --- integration_tests/juniper_tests/Cargo.toml | 12 +-- .../juniper_tests/src/codegen/derive_enum.rs | 4 +- .../src/codegen/derive_input_object.rs | 4 +- .../src/codegen/derive_object.rs | 77 ++++++++++--------- .../juniper_tests/src/codegen/mod.rs | 2 + .../juniper_tests/src/codegen/util.rs | 54 +++++++++++++ .../juniper_tests/src/custom_scalar.rs | 11 ++- integration_tests/juniper_tests/src/lib.rs | 9 +-- 8 files changed, 109 insertions(+), 64 deletions(-) create mode 100644 integration_tests/juniper_tests/src/codegen/util.rs diff --git a/integration_tests/juniper_tests/Cargo.toml b/integration_tests/juniper_tests/Cargo.toml index bedc80d1..7b72245c 100644 --- a/integration_tests/juniper_tests/Cargo.toml +++ b/integration_tests/juniper_tests/Cargo.toml @@ -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 diff --git a/integration_tests/juniper_tests/src/codegen/derive_enum.rs b/integration_tests/juniper_tests/src/codegen/derive_enum.rs index fd5dbd3c..27c5e8dd 100644 --- a/integration_tests/juniper_tests/src/codegen/derive_enum.rs +++ b/integration_tests/juniper_tests/src/codegen/derive_enum.rs @@ -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()) ); } diff --git a/integration_tests/juniper_tests/src/codegen/derive_input_object.rs b/integration_tests/juniper_tests/src/codegen/derive_input_object.rs index 16173000..938446ef 100644 --- a/integration_tests/juniper_tests/src/codegen/derive_input_object.rs +++ b/integration_tests/juniper_tests/src/codegen/derive_input_object.rs @@ -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()) ); } diff --git a/integration_tests/juniper_tests/src/codegen/derive_object.rs b/integration_tests/juniper_tests/src/codegen/derive_object.rs index 1a2d9936..ac78b521 100644 --- a/integration_tests/juniper_tests/src/codegen/derive_object.rs +++ b/integration_tests/juniper_tests/src/codegen/derive_object.rs @@ -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."), ); } diff --git a/integration_tests/juniper_tests/src/codegen/mod.rs b/integration_tests/juniper_tests/src/codegen/mod.rs index 596af99f..ac20e205 100644 --- a/integration_tests/juniper_tests/src/codegen/mod.rs +++ b/integration_tests/juniper_tests/src/codegen/mod.rs @@ -1,3 +1,5 @@ +mod util; + mod derive_enum; mod derive_input_object; mod derive_object; diff --git a/integration_tests/juniper_tests/src/codegen/util.rs b/integration_tests/juniper_tests/src/codegen/util.rs new file mode 100644 index 00000000..59112ea0 --- /dev/null +++ b/integration_tests/juniper_tests/src/codegen/util.rs @@ -0,0 +1,54 @@ +use juniper::{DefaultScalarValue, GraphQLType, RootNode, Value, Variables}; +use std::default::Default; + +pub fn run_query(query: &str) -> Value +where + Query: GraphQLType + Default, + Mutation: GraphQLType + 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(type_name: &str) -> Value +where + Query: GraphQLType + Default, + Mutation: GraphQLType + 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); + result + .as_object_value() + .expect("Result is not an object") + .get_field_value("__type") + .expect("__type field missing") + .clone() +} diff --git a/integration_tests/juniper_tests/src/custom_scalar.rs b/integration_tests/juniper_tests/src/custom_scalar.rs index e3d3732f..07c37f72 100644 --- a/integration_tests/juniper_tests/src/custom_scalar.rs +++ b/integration_tests/juniper_tests/src/custom_scalar.rs @@ -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(query: &str, vars: Variables, f: F) diff --git a/integration_tests/juniper_tests/src/lib.rs b/integration_tests/juniper_tests/src/lib.rs index fa3742a3..022c238a 100644 --- a/integration_tests/juniper_tests/src/lib.rs +++ b/integration_tests/juniper_tests/src/lib.rs @@ -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;