Skip to content

Commit

Permalink
separate tests in serde_value_ext.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Apr 27, 2024
1 parent 3369838 commit 4afdab4
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 108 deletions.
1 change: 0 additions & 1 deletion benches/request_template_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ impl Serialize for Context {
S: Serializer,
{
serializer.serialize_none()
// self.value.serialize(serializer)
}
}

Expand Down
1 change: 0 additions & 1 deletion src/grpc/request_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ mod tests {
S: Serializer,
{
serializer.serialize_none()
// self.value.serialize(serializer)
}

Check warning on line 195 in src/grpc/request_template.rs

View check run for this annotation

Codecov / codecov/patch

src/grpc/request_template.rs#L190-L195

Added lines #L190 - L195 were not covered by tests
}

Expand Down
1 change: 0 additions & 1 deletion src/http/request_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ mod tests {
S: Serializer,
{
serializer.serialize_none()
// self.value.serialize(serializer)
}
}

Expand Down
331 changes: 226 additions & 105 deletions src/serde_value_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

1 comment on commit 4afdab4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.68ms 3.52ms 104.53ms 72.51%
Req/Sec 3.30k 266.29 3.76k 85.67%

393606 requests in 30.01s, 1.97GB read

Requests/sec: 13115.92

Transfer/sec: 67.32MB

Please sign in to comment.