diff --git a/juniper/src/tests/introspection_tests.rs b/juniper/src/tests/introspection_tests.rs index af46affb..1a9bc3d6 100644 --- a/juniper/src/tests/introspection_tests.rs +++ b/juniper/src/tests/introspection_tests.rs @@ -157,13 +157,14 @@ fn test_introspection_directives() { let database = Database::new(); let schema = RootNode::new(&database, EmptyMutation::::new()); - let result = ::execute(q, None, &schema, &Variables::new(), &database).unwrap(); + let mut result = crate::execute(q, None, &schema, &Variables::new(), &database).unwrap(); + sort_schema_value(&mut result.0); - let expected = graphql_value!({ + let mut expected = graphql_value!({ "__schema": { "directives": [ { - "name": "skip", + "name": "include", "locations": [ "FIELD", "FRAGMENT_SPREAD", @@ -171,7 +172,7 @@ fn test_introspection_directives() { ], }, { - "name": "include", + "name": "skip", "locations": [ "FIELD", "FRAGMENT_SPREAD", @@ -181,6 +182,7 @@ fn test_introspection_directives() { ], }, }); + sort_schema_value(&mut expected); assert_eq!(result, (expected, vec![])); } @@ -201,8 +203,6 @@ fn test_introspection_possible_types() { let result = ::execute(doc, None, &schema, &Variables::new(), &database); - println!("Result: {:#?}", result); - let (result, errors) = result.ok().expect("Query returned error"); assert_eq!(errors, vec![]); @@ -237,9 +237,10 @@ fn test_builtin_introspection_query() { let database = Database::new(); let schema = RootNode::new(&database, EmptyMutation::::new()); - let result = ::introspect(&schema, &database, IntrospectionFormat::default()); + let mut result = crate::introspect(&schema, &database, IntrospectionFormat::default()).unwrap(); + sort_schema_value(&mut result.0); let expected = schema_introspection_result(); - assert_eq!(result, Ok((expected, vec![]))); + assert_eq!(result, (expected, vec![])); } #[test] @@ -247,7 +248,10 @@ fn test_builtin_introspection_query_without_descriptions() { let database = Database::new(); let schema = RootNode::new(&database, EmptyMutation::::new()); - let result = ::introspect(&schema, &database, IntrospectionFormat::WithoutDescriptions); + let mut result = + crate::introspect(&schema, &database, IntrospectionFormat::WithoutDescriptions).unwrap(); + sort_schema_value(&mut result.0); let expected = schema_introspection_result_without_descriptions(); - assert_eq!(result, Ok((expected, vec![]))); + + assert_eq!(result, (expected, vec![])); } diff --git a/juniper/src/tests/schema_introspection.rs b/juniper/src/tests/schema_introspection.rs index 3c56f6ce..42d24a3b 100644 --- a/juniper/src/tests/schema_introspection.rs +++ b/juniper/src/tests/schema_introspection.rs @@ -1,7 +1,40 @@ -use value::{self, Value::Null}; +use crate::value::{self, Value, Value::Null}; + +// Sort a nested schema Value. +// In particular, lists are sorted by the "name" key of children, if present. +// Only needed for comparisons. +pub(super) fn sort_schema_value(value: &mut Value) { + match value { + Value::Null | Value::Scalar(_) => {} + Value::List(ref mut items) => { + items.sort_by(|a, b| { + let name_a = a + .as_object_value() + .and_then(|v| v.get_field_value("name")) + .and_then(|v| v.as_scalar_value::()) + .map(|x| x.as_str()) + .unwrap_or(""); + let name_b = b + .as_object_value() + .and_then(|v| v.get_field_value("name")) + .and_then(|v| v.as_scalar_value::()) + .map(|x| x.as_str()) + .unwrap_or(""); + name_a.cmp(name_b) + }); + for item in items.iter_mut() { + sort_schema_value(item); + } + } + Value::Object(ref mut obj) => { + obj.iter_mut() + .for_each(|(_key, item)| sort_schema_value(item)); + } + } +} pub(crate) fn schema_introspection_result() -> value::Value { - graphql_value!({ + let mut v = graphql_value!({ "__schema": { "queryType": { "name": "Query" @@ -1274,11 +1307,13 @@ pub(crate) fn schema_introspection_result() -> value::Value { } ] } - }) + }); + sort_schema_value(&mut v); + v } pub(crate) fn schema_introspection_result_without_descriptions() -> value::Value { - graphql_value!({ + let mut v = graphql_value!({ "__schema": { "queryType": { "name": "Query" @@ -2458,5 +2493,7 @@ pub(crate) fn schema_introspection_result_without_descriptions() -> value::Value } ] } - }) + }); + sort_schema_value(&mut v); + v }