Add more rigorous tests

This commit is contained in:
Austen Adler 2024-04-13 11:48:34 -04:00
parent 529610a7ff
commit cb9fe3b9e3
2 changed files with 25 additions and 16 deletions

View File

@ -47,6 +47,7 @@ pub struct Common {
pub availability_mode: Option<AvailabilityMode>,
/// A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with `availability_topic`.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub availability: Option<Availability>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]

View File

@ -4,7 +4,7 @@ use crate::integrations::{
};
use super::*;
use serde_json::from_str;
use serde_json::{from_str, Value};
#[test]
fn device() {
@ -22,6 +22,10 @@ fn device() {
};
assert_eq!(expected, serde_json::from_str::<Device>(test_data).unwrap(),);
assert_eq!(
serde_json::from_str::<Value>(&serde_json::to_string(&expected).unwrap()).unwrap(),
serde_json::from_str::<Value>(test_data).unwrap()
);
}
#[test]
@ -66,18 +70,15 @@ fn light_default() {
expected,
serde_json::from_str::<light::DefaultDiscovery>(test_data).unwrap(),
);
assert_eq!(
serde_json::from_str::<Value>(&serde_json::to_string(&expected).unwrap()).unwrap(),
serde_json::from_str::<Value>(test_data).unwrap()
);
}
#[test]
fn light_json() {
let test_data = r#"{
"schema": "json",
"name": "mqtt_json_light_1",
"state_topic": "home/rgb1",
"command_topic": "home/rgb1/set",
"brightness": true,
"supported_color_modes": ["rgb"]
}"#;
let test_data = r#"{"schema":"json","name":"mqtt_json_light_1","state_topic":"home/rgb1","command_topic":"home/rgb1/set","brightness":true,"supported_color_modes":["rgb"]}"#;
let expected = light::JsonDiscovery {
common: Common {
..Common::default()
@ -93,12 +94,16 @@ fn light_json() {
expected,
serde_json::from_str::<light::JsonDiscovery>(test_data).unwrap(),
);
assert_eq!(
serde_json::from_str::<Value>(&serde_json::to_string(&expected).unwrap()).unwrap(),
serde_json::from_str::<Value>(test_data).unwrap()
);
}
#[test]
fn binary_sensor() {
let test_data = r#"{
"name": null,
"device_class": "motion",
"state_topic": "homeassistant/binary_sensor/garden/state",
"unique_id": "motion01ad",
@ -128,17 +133,20 @@ fn binary_sensor() {
expected,
serde_json::from_str::<binary_sensor::Discovery>(test_data).unwrap(),
);
assert_eq!(
serde_json::from_str::<Value>(&serde_json::to_string(&expected).unwrap()).unwrap(),
serde_json::from_str::<Value>(test_data).unwrap()
);
}
#[test]
fn test_availability() {
assert_eq!(
from_str::<Availability>(
r#"{
let test_data = r#"{
"availability_topic": "asdf"
}"#
)
.unwrap(),
}"#;
assert_eq!(
from_str::<Availability>(test_data).unwrap(),
Availability::AvailabilityTopic(String::from("asdf"))
);