Add simple scalar introspection tests

This commit is contained in:
Magnus Hallin 2016-09-20 22:31:02 +02:00
parent f9383a2a39
commit 1e9c63e085
2 changed files with 78 additions and 0 deletions

View file

@ -329,6 +329,14 @@ impl InputValue {
} }
} }
/// View the underlying int value, if present.
pub fn as_int_value(&self) -> Option<i64> {
match *self {
InputValue::Int(i) => Some(i),
_ => None,
}
}
/// View the underlying string value, if present. /// View the underlying string value, if present.
pub fn as_string_value(&self) -> Option<&str> { pub fn as_string_value(&self) -> Option<&str> {
match *self { match *self {

View file

@ -9,10 +9,22 @@ enum Sample {
Two, Two,
} }
struct Scalar(i64);
struct Interface {} struct Interface {}
struct Root {} struct Root {}
graphql_scalar!(Scalar as "SampleScalar" {
resolve(&self) -> Value {
Value::int(self.0)
}
from_input_value(v: &InputValue) -> Option<Scalar> {
v.as_int_value().map(|i| Scalar(i))
}
});
graphql_enum!(Sample as "SampleEnum" { graphql_enum!(Sample as "SampleEnum" {
Sample::One => "ONE", Sample::One => "ONE",
Sample::Two => "TWO", Sample::Two => "TWO",
@ -36,8 +48,35 @@ graphql_object!(Root: () as "Root" |&self| {
field sample_enum() -> FieldResult<Sample> { field sample_enum() -> FieldResult<Sample> {
Ok(Sample::One) Ok(Sample::One)
} }
field sample_scalar() -> FieldResult<Scalar> {
Ok(Scalar(123))
}
}); });
#[test]
fn test_execution() {
let doc = r#"
{
sampleEnum
sampleScalar
}
"#;
let schema = RootNode::new(Root {}, ());
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &())
.expect("Execution failed");
assert_eq!(errs, []);
println!("Result: {:?}", result);
assert_eq!(result, Value::object(vec![
("sampleEnum", Value::string("ONE")),
("sampleScalar", Value::int(123)),
].into_iter().collect()));
}
#[test] #[test]
fn enum_introspection() { fn enum_introspection() {
let doc = r#" let doc = r#"
@ -175,3 +214,34 @@ fn interface_introspection() {
("deprecationReason", Value::null()), ("deprecationReason", Value::null()),
].into_iter().collect()))); ].into_iter().collect())));
} }
#[test]
fn scalar_introspection() {
let doc = r#"
{
__type(name: "SampleScalar") {
name
kind
description
}
}
"#;
let schema = RootNode::new(Root {}, ());
let (result, errs) = ::execute(doc, None, &schema, &HashMap::new(), &())
.expect("Execution failed");
assert_eq!(errs, []);
println!("Result: {:?}", result);
let type_info = result
.as_object_value().expect("Result is not an object")
.get("__type").expect("__type field missing");
assert_eq!(type_info, &Value::object(vec![
("name", Value::string("SampleScalar")),
("kind", Value::string("SCALAR")),
("description", Value::null()),
].into_iter().collect()));
}