From 4afdab4648ad5d64e8414f48f64ca3d2d5edefd9 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Sat, 27 Apr 2024 20:14:00 +0530 Subject: [PATCH] separate tests in `serde_value_ext.rs` --- benches/request_template_bench.rs | 1 - src/grpc/request_template.rs | 1 - src/http/request_template.rs | 1 - src/serde_value_ext.rs | 331 ++++++++++++++++++++---------- 4 files changed, 226 insertions(+), 108 deletions(-) diff --git a/benches/request_template_bench.rs b/benches/request_template_bench.rs index 26cfc3fff0..e6d20b118c 100644 --- a/benches/request_template_bench.rs +++ b/benches/request_template_bench.rs @@ -28,7 +28,6 @@ impl Serialize for Context { S: Serializer, { serializer.serialize_none() - // self.value.serialize(serializer) } } diff --git a/src/grpc/request_template.rs b/src/grpc/request_template.rs index c23518da8f..3c80748a2a 100644 --- a/src/grpc/request_template.rs +++ b/src/grpc/request_template.rs @@ -192,7 +192,6 @@ mod tests { S: Serializer, { serializer.serialize_none() - // self.value.serialize(serializer) } } diff --git a/src/http/request_template.rs b/src/http/request_template.rs index 5360ed4097..cbe73726bc 100644 --- a/src/http/request_template.rs +++ b/src/http/request_template.rs @@ -289,7 +289,6 @@ mod tests { S: Serializer, { serializer.serialize_none() - // self.value.serialize(serializer) } } diff --git a/src/serde_value_ext.rs b/src/serde_value_ext.rs index add24084eb..bf00a01a60 100644 --- a/src/serde_value_ext.rs +++ b/src/serde_value_ext.rs @@ -44,121 +44,242 @@ impl ValueExt for DynamicValue { #[cfg(test)] mod tests { - use serde_json::json; - - use crate::blueprint::DynamicValue; - use crate::serde_value_ext::ValueExt; - - #[test] - fn test_render_value() { - let value = json!({"a": "{{.foo}}"}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": "baz"}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": {"bar": "baz"}})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + mod render_value { + use serde_json::json; - #[test] - fn test_render_value_nested() { - let value = json!({"a": "{{.foo.bar.baz}}"}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": 1}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": 1})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + use crate::blueprint::DynamicValue; + use crate::serde_value_ext::ValueExt; - #[test] - fn test_render_value_nested_str() { - let value = json!({"a": "{{.foo.bar.baz}}"}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": "foo"}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": "foo"})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + #[test] + fn test_render_value() { + let value = json!({"a": "{{foo}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": "baz"}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": {"bar": "baz"}})).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_render_value_null() { - let value = json!("{{.foo.bar.baz}}"); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": null}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!(null)).unwrap(); - assert_eq!(result.unwrap(), expected); - } + #[test] + fn test_render_value_nested() { + let value = json!({"a": "{{foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": 1})).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_render_value_nested_bool() { - let value = json!({"a": "{{.foo.bar.baz}}"}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": true}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": true})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + #[test] + fn test_render_value_nested_str() { + let value = json!({"a": "{{foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": "foo"}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": "foo"})).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_render_value_nested_float() { - let value = json!({"a": "{{.foo.bar.baz}}"}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": 1.1}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": 1.1})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + #[test] + fn test_render_value_null() { + let value = json!("{{foo.bar.baz}}"); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": null}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!(null)).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_render_value_arr() { - let value = json!({"a": "{{.foo.bar.baz}}"}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": [1,2,3]}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": [1, 2, 3]})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + #[test] + fn test_render_value_nested_bool() { + let value = json!({"a": "{{foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": true}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": true})).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_render_value_arr_template() { - let value = json!({"a": ["{{.foo.bar.baz}}", "{{.foo.bar.qux}}"]}); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!({"a": [1, 2]})).unwrap(); - assert_eq!(result.unwrap(), expected); - } + #[test] + fn test_render_value_nested_float() { + let value = json!({"a": "{{foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1.1}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": 1.1})).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_mustache_or_value_is_const() { - let value = json!("{{.foo}}"); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": "bar"}); - let result = value.render_value(&ctx).unwrap(); - let expected = async_graphql::Value::String("bar".to_owned()); - assert_eq!(result, expected); - } + #[test] + fn test_render_value_arr() { + let value = json!({"a": "{{foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": [1,2,3]}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": [1, 2, 3]})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_arr_template() { + let value = json!({"a": ["{{foo.bar.baz}}", "{{foo.bar.qux}}"]}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": [1, 2]})).unwrap(); + assert_eq!(result.unwrap(), expected); + } - #[test] - fn test_mustache_arr_obj() { - let value = json!([{"a": "{{.foo.bar.baz}}"}, {"a": "{{.foo.bar.qux}}"}]); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); - let result = value.render_value(&ctx); - let expected = async_graphql::Value::from_json(json!([{"a": 1}, {"a":2}])).unwrap(); - assert_eq!(result.unwrap(), expected); + #[test] + fn test_mustache_or_value_is_const() { + let value = json!("{{foo}}"); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": "bar"}); + let result = value.render_value(&ctx).unwrap(); + let expected = async_graphql::Value::String("bar".to_owned()); + assert_eq!(result, expected); + } + + #[test] + fn test_mustache_arr_obj() { + let value = json!([{"a": "{{foo.bar.baz}}"}, {"a": "{{foo.bar.qux}}"}]); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!([{"a": 1}, {"a":2}])).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_mustache_arr_obj_arr() { + let value = + json!([{"a": [{"aa": "{{foo.bar.baz}}"}]}, {"a": [{"aa": "{{foo.bar.qux}}"}]}]); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); + let result = value.render_value(&ctx); + let expected = + async_graphql::Value::from_json(json!([{"a": [{"aa": 1}]}, {"a":[{"aa": 2}]}])) + .unwrap(); + assert_eq!(result.unwrap(), expected); + } } + mod evaluate { + use serde_json::json; - #[test] - fn test_mustache_arr_obj_arr() { - let value = - json!([{"a": [{"aa": "{{.foo.bar.baz}}"}]}, {"a": [{"aa": "{{.foo.bar.qux}}"}]}]); - let value = DynamicValue::try_from(&value).unwrap(); - let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); - let result = value.render_value(&ctx); - let expected = - async_graphql::Value::from_json(json!([{"a": [{"aa": 1}]}, {"a":[{"aa": 2}]}])) - .unwrap(); - assert_eq!(result.unwrap(), expected); + use crate::blueprint::DynamicValue; + use crate::serde_value_ext::ValueExt; + + #[test] + fn test_render_value() { + let value = json!({"a": "{{.foo}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": "baz"}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": {"bar": "baz"}})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_nested() { + let value = json!({"a": "{{.foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": 1})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_nested_str() { + let value = json!({"a": "{{.foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": "foo"}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": "foo"})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_null() { + let value = json!("{{.foo.bar.baz}}"); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": null}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!(null)).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_nested_bool() { + let value = json!({"a": "{{.foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": true}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": true})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_nested_float() { + let value = json!({"a": "{{.foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1.1}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": 1.1})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_arr() { + let value = json!({"a": "{{.foo.bar.baz}}"}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": [1,2,3]}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": [1, 2, 3]})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_render_value_arr_template() { + let value = json!({"a": ["{{.foo.bar.baz}}", "{{.foo.bar.qux}}"]}); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!({"a": [1, 2]})).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_mustache_or_value_is_const() { + let value = json!("{{.foo}}"); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": "bar"}); + let result = value.render_value(&ctx).unwrap(); + let expected = async_graphql::Value::String("bar".to_owned()); + assert_eq!(result, expected); + } + + #[test] + fn test_mustache_arr_obj() { + let value = json!([{"a": "{{.foo.bar.baz}}"}, {"a": "{{.foo.bar.qux}}"}]); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); + let result = value.render_value(&ctx); + let expected = async_graphql::Value::from_json(json!([{"a": 1}, {"a":2}])).unwrap(); + assert_eq!(result.unwrap(), expected); + } + + #[test] + fn test_mustache_arr_obj_arr() { + let value = + json!([{"a": [{"aa": "{{.foo.bar.baz}}"}]}, {"a": [{"aa": "{{.foo.bar.qux}}"}]}]); + let value = DynamicValue::try_from(&value).unwrap(); + let ctx = json!({"foo": {"bar": {"baz": 1, "qux": 2}}}); + let result = value.render_value(&ctx); + let expected = + async_graphql::Value::from_json(json!([{"a": [{"aa": 1}]}, {"a":[{"aa": 2}]}])) + .unwrap(); + assert_eq!(result.unwrap(), expected); + } } }