From 01dfc6b81e0870837856327e84a13e8523e16b22 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Mon, 8 Jul 2024 23:07:45 +0200 Subject: [PATCH] Improved google_home tests --- .../google_home/src/request/execute.rs | 128 +++++++++--------- google_home/google_home/src/request/query.rs | 58 ++++---- google_home/google_home/src/request/sync.rs | 20 +-- .../google_home/src/response/execute.rs | 24 +++- google_home/google_home/src/response/query.rs | 22 ++- google_home/google_home/src/response/sync.rs | 34 ++++- 6 files changed, 177 insertions(+), 109 deletions(-) diff --git a/google_home/google_home/src/request/execute.rs b/google_home/google_home/src/request/execute.rs index 625a9cb..43dac3f 100644 --- a/google_home/google_home/src/request/execute.rs +++ b/google_home/google_home/src/request/execute.rs @@ -24,36 +24,38 @@ pub struct Device { #[cfg(test)] mod tests { + use serde_json::json; + use super::*; use crate::request::{Intent, Request}; #[test] fn deserialize_set_fan_speed() { - let json = r#"{ - "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", - "inputs": [ - { - "intent": "action.devices.EXECUTE", - "payload": { - "commands": [ - { - "devices": [], - "execution": [ - { - "command": "action.devices.commands.SetFanSpeed", - "params": { - "fanSpeed": "Test" - } + let req = json!({ + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", + "inputs": [ + { + "intent": "action.devices.EXECUTE", + "payload": { + "commands": [ + { + "devices": [], + "execution": [ + { + "command": "action.devices.commands.SetFanSpeed", + "params": { + "fanSpeed": "Test" + } + } + ] + } + ] } - ] - } - ] - } - } - ] -}"#; + } + ] + }); - let req: Request = serde_json::from_str(json).unwrap(); + let req: Request = serde_json::from_value(req).unwrap(); assert_eq!(req.inputs.len(), 1); match &req.inputs[0] { @@ -72,48 +74,48 @@ mod tests { #[test] fn deserialize() { - let json = r#"{ - "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", - "inputs": [ - { - "intent": "action.devices.EXECUTE", - "payload": { - "commands": [ - { - "devices": [ - { - "id": "123", - "customData": { - "fooValue": 74, - "barValue": true, - "bazValue": "sheepdip" - } - }, - { - "id": "456", - "customData": { - "fooValue": 36, - "barValue": false, - "bazValue": "moarsheep" - } + let req = json!({ + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", + "inputs": [ + { + "intent": "action.devices.EXECUTE", + "payload": { + "commands": [ + { + "devices": [ + { + "id": "123", + "customData": { + "fooValue": 74, + "barValue": true, + "bazValue": "sheepdip" + } + }, + { + "id": "456", + "customData": { + "fooValue": 36, + "barValue": false, + "bazValue": "moarsheep" + } + } + ], + "execution": [ + { + "command": "action.devices.commands.OnOff", + "params": { + "on": true + } + } + ] + } + ] } - ], - "execution": [ - { - "command": "action.devices.commands.OnOff", - "params": { - "on": true - } - } - ] - } - ] - } - } - ] -}"#; + } + ] + }); - let req: Request = serde_json::from_str(json).unwrap(); + let req: Request = serde_json::from_value(req).unwrap(); println!("{:?}", req); diff --git a/google_home/google_home/src/request/query.rs b/google_home/google_home/src/request/query.rs index 2703490..c4bd0c8 100644 --- a/google_home/google_home/src/request/query.rs +++ b/google_home/google_home/src/request/query.rs @@ -15,40 +15,42 @@ pub struct Device { #[cfg(test)] mod tests { + use serde_json::json; + use crate::request::{Intent, Request}; #[test] fn deserialize() { - let json = r#"{ - "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", - "inputs": [ - { - "intent": "action.devices.QUERY", - "payload": { - "devices": [ - { - "id": "123", - "customData": { - "fooValue": 74, - "barValue": true, - "bazValue": "foo" + let req = json!({ + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", + "inputs": [ + { + "intent": "action.devices.QUERY", + "payload": { + "devices": [ + { + "id": "123", + "customData": { + "fooValue": 74, + "barValue": true, + "bazValue": "foo" + } + }, + { + "id": "456", + "customData": { + "fooValue": 12, + "barValue": false, + "bazValue": "bar" + } + } + ] + } } - }, - { - "id": "456", - "customData": { - "fooValue": 12, - "barValue": false, - "bazValue": "bar" - } - } - ] - } - } - ] -}"#; + ] + }); - let req: Request = serde_json::from_str(json).unwrap(); + let req: Request = serde_json::from_value(req).unwrap(); println!("{:?}", req); diff --git a/google_home/google_home/src/request/sync.rs b/google_home/google_home/src/request/sync.rs index 5d4033d..f7e0862 100644 --- a/google_home/google_home/src/request/sync.rs +++ b/google_home/google_home/src/request/sync.rs @@ -1,19 +1,21 @@ #[cfg(test)] mod tests { + use serde_json::json; + use crate::request::{Intent, Request}; #[test] fn deserialize() { - let json = r#"{ - "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", - "inputs": [ - { - "intent": "action.devices.SYNC" - } - ] - }"#; + let req = json!({ + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", + "inputs": [ + { + "intent": "action.devices.SYNC" + } + ] + }); - let req: Request = serde_json::from_str(json).unwrap(); + let req: Request = serde_json::from_value(req).unwrap(); println!("{:?}", req); diff --git a/google_home/google_home/src/response/execute.rs b/google_home/google_home/src/response/execute.rs index f193fba..14e6968 100644 --- a/google_home/google_home/src/response/execute.rs +++ b/google_home/google_home/src/response/execute.rs @@ -117,10 +117,28 @@ mod tests { ResponsePayload::Execute(execute_resp), ); - let json = serde_json::to_string(&resp).unwrap(); + let resp = serde_json::to_value(resp).unwrap(); - println!("{}", json); + let resp_expected = json!({ + "payload": { + "commands": [ + { + "states": { + "on": true, + "online": true + }, + "ids": ["123"], + "status": "SUCCESS" + }, { + "errorCode": "deviceNotFound", + "ids": ["456"], + "status":"ERROR" + } + ] + }, + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf" + }); - // TODO: Add a known correct output to test against + assert_eq!(resp, resp_expected); } } diff --git a/google_home/google_home/src/response/query.rs b/google_home/google_home/src/response/query.rs index 8e92f1d..eeb37e5 100644 --- a/google_home/google_home/src/response/query.rs +++ b/google_home/google_home/src/response/query.rs @@ -113,10 +113,26 @@ mod tests { ResponsePayload::Query(query_resp), ); - let json = serde_json::to_string(&resp).unwrap(); + let resp = serde_json::to_value(resp).unwrap(); - println!("{}", json); + let resp_expected = json!({ + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", + "payload": { + "devices": { + "123": { + "online": true, + "status": "SUCCESS", + "on": true + }, + "456": { + "online": true, + "status": "SUCCESS", + "on":true + } + } + } + }); - // TODO: Add a known correct output to test against + assert_eq!(resp, resp_expected); } } diff --git a/google_home/google_home/src/response/sync.rs b/google_home/google_home/src/response/sync.rs index c820314..32fd234 100644 --- a/google_home/google_home/src/response/sync.rs +++ b/google_home/google_home/src/response/sync.rs @@ -46,6 +46,7 @@ pub struct Device { pub room_hint: Option, #[serde(skip_serializing_if = "Option::is_none")] pub device_info: Option, + #[serde(skip_serializing_if = "serde_json::Value::is_null")] pub attributes: serde_json::Value, } @@ -67,6 +68,8 @@ impl Device { #[cfg(test)] mod tests { + use serde_json::json; + use super::*; use crate::response::{Response, ResponsePayload}; use crate::traits::Trait; @@ -96,10 +99,35 @@ mod tests { ResponsePayload::Sync(sync_resp), ); - let json = serde_json::to_string(&resp).unwrap(); + let resp = serde_json::to_value(resp).unwrap(); - println!("{}", json); + let resp_expected = json!({ + "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", + "payload": { + "agentUserId": "1836.15267389", + "devices": [ + { + "id": "123", + "type": "action.devices.types.KETTLE", + "traits": ["action.devices.traits.OnOff"], + "name": { + "defaultNames": ["My Outlet 1234"], + "name": "Night light", + "nicknames": ["wall plug"] + }, + "willReportState": false, + "roomHint": "kitchen", + "deviceInfo": { + "manufacturer": "lights-out-inc", + "model": "hs1234", + "hwVersion": "3.2", + "swVersion": "11.4" + } + } + ] + } + }); - // assert_eq!(json, r#"{"requestId":"ff36a3cc-ec34-11e6-b1a0-64510650abcf","payload":{"agentUserId":"1836.15267389","devices":[{"id":"123","type":"action.devices.types.KETTLE","traits":["action.devices.traits.OnOff"],"name":{"defaultNames":["My Outlet 1234"],"name":"Night light","nicknames":["wall plug"]},"willReportState":false,"roomHint":"kitchen","deviceInfo":{"manufacturer":"lights-out-inc","model":"hs1234","hwVersion":"3.2","swVersion":"11.4"}}]}}"#) + assert_eq!(resp, resp_expected); } }