chore: add tests for de/serialization
This commit is contained in:
parent
1a17f8ea99
commit
853b04e46d
@ -103,3 +103,78 @@ pub struct ParameterSet {
|
||||
value_from_pipeline: Option<bool>,
|
||||
value_from_pipeline_by_property_name: Option<bool>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
use super::*;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
fn make_base_parameter(is_array: bool, data_type: DataType) -> Parameter {
|
||||
Parameter {
|
||||
name: Arc::from("testParam"),
|
||||
data_type,
|
||||
is_array,
|
||||
aliases: vec![Arc::from("alias1")],
|
||||
description: Some(Arc::from("desc")),
|
||||
help_message: Some(Arc::from("help")),
|
||||
parameter_sets: vec![],
|
||||
validations: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_with_array_type() {
|
||||
let json_data = json!({
|
||||
"name": "foo",
|
||||
"type": "string[]",
|
||||
"aliases": ["aliasA"],
|
||||
"description": "some description",
|
||||
"helpMessage": "some help",
|
||||
"parameterSets": [],
|
||||
"validations": []
|
||||
});
|
||||
|
||||
let param: Parameter = serde_json::from_value(json_data).unwrap();
|
||||
assert!(param.is_array);
|
||||
assert!(matches!(param.data_type, DataType::String));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_without_array_type() {
|
||||
let json_data = json!({
|
||||
"name": "bar",
|
||||
"type": "boolean",
|
||||
"aliases": [],
|
||||
"description": null,
|
||||
"helpMessage": null,
|
||||
"parameterSets": [],
|
||||
"validations": []
|
||||
});
|
||||
|
||||
let param: Parameter = serde_json::from_value(json_data).unwrap();
|
||||
assert!(!param.is_array);
|
||||
assert!(matches!(param.data_type, DataType::Boolean));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_with_array_type() {
|
||||
let param = make_base_parameter(true, DataType::Int32);
|
||||
let json_str = serde_json::to_string(¶m).unwrap();
|
||||
let value: Value = serde_json::from_str(&json_str).unwrap();
|
||||
|
||||
assert_eq!(value["type"], "Int32[]");
|
||||
assert_eq!(value["name"], "testParam");
|
||||
assert!(value.get("is_array").is_none()); // is_array must not be serialized
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_without_array_type() {
|
||||
let param = make_base_parameter(false, DataType::Int64);
|
||||
let json_str = serde_json::to_string(¶m).unwrap();
|
||||
let value: Value = serde_json::from_str(&json_str).unwrap();
|
||||
|
||||
assert_eq!(value["type"], "Int64");
|
||||
assert!(value.get("is_array").is_none()); // is_array must not be serialized
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user