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>, 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`. /// 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>, pub availability: Option<Availability>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]

View File

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