Skip to content

Commit bdab48f

Browse files
committed
Add tests for deserializing invalid Resource data
This verifies that the errors from the Serde library are the same as before the custom deserializer was implemented.
1 parent 57eda43 commit bdab48f

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lib/dsc-lib/src/configure/config_doc.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,66 @@ mod test {
527527
assert!(result.is_ok());
528528
}
529529

530+
#[test]
531+
fn test_invalid_resource_field_in_array() {
532+
let config_json = r#"{
533+
"resources": [
534+
{
535+
"invalidField": "someValue"
536+
}
537+
]
538+
}"#;
539+
540+
let result: Result<Configuration, _> = serde_json::from_str(config_json);
541+
assert!(result.is_err());
542+
let err = result.unwrap_err().to_string();
543+
assert!(err.starts_with("unknown field `invalidField`, expected one of `condition`, `type`,"));
544+
}
545+
546+
#[test]
547+
fn test_invalid_resource_field_in_object() {
548+
let config_json = r#"{
549+
"resources": {
550+
"someResource": {
551+
"invalidField": "someValue"
552+
}
553+
}
554+
}"#;
555+
556+
let result: Result<Configuration, _> = serde_json::from_str(config_json);
557+
assert!(result.is_err());
558+
let err = result.unwrap_err().to_string();
559+
assert!(err.starts_with("unknown field `invalidField`, expected one of `condition`, `type`,"));
560+
}
561+
562+
#[test]
563+
fn test_invalid_resource_type_in_array() {
564+
let config_json = r#"{
565+
"resources": [
566+
"invalidType"
567+
]
568+
}"#;
569+
570+
let result: Result<Configuration, _> = serde_json::from_str(config_json);
571+
assert!(result.is_err());
572+
let err = result.unwrap_err().to_string();
573+
assert!(err.contains("expected struct Resource"));
574+
}
575+
576+
#[test]
577+
fn test_invalid_resource_type_in_object() {
578+
let config_json = r#"{
579+
"resources": {
580+
"someResource": "invalidType"
581+
}
582+
}"#;
583+
584+
let result: Result<Configuration, _> = serde_json::from_str(config_json);
585+
assert!(result.is_err());
586+
let err = result.unwrap_err().to_string();
587+
assert!(err.contains("expected struct Resource"));
588+
}
589+
530590
#[test]
531591
fn test_resources_as_array() {
532592
let config_json = r#"{

0 commit comments

Comments
 (0)