1
0
Fork 0

Start adding some tests for config parsing

gh-action
Bo Jeanes 2022-08-13 15:05:19 +10:00
parent 650e48c70d
commit 01db290c42
1 changed files with 109 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::time::Duration;
#[derive(Clone, Serialize, Deserialize)]
@ -202,3 +203,111 @@ struct Unit(crate::modbus::UnitId);
fn default_address_offset() -> i8 {
0
}
#[test]
fn parse_minimal_tcp_connect_config() {
let result = serde_json::from_value::<Connect>(json!({
"host": "1.1.1.1"
}));
assert!(result.is_ok());
let connect = result.unwrap();
assert!(matches!(
connect.settings,
ModbusProto::Tcp {
ref host,
port: 502
} if host == "1.1.1.1"
))
}
#[test]
fn parse_full_tcp_connect_config() {
let result = serde_json::from_value::<Connect>(json!({
"host": "10.10.10.219",
"unit": 1,
"address_offset": -1,
"input": [
{
"address": 5017,
"type": "u32",
"name": "dc_power",
"swap_words": false,
"period": "3s"
},
{
"address": 5008,
"type": "s16",
"name": "internal_temperature",
"period": "1m"
},
{
"address": 13008,
"type": "s32",
"name": "load_power",
"swap_words": false,
"period": "3s"
},
{
"address": 13010,
"type": "s32",
"name": "export_power",
"swap_words": false,
"period": "3s"
},
{
"address": 13022,
"name": "battery_power",
"period": "3s"
},
{
"address": 13023,
"name": "battery_level",
"period": "1m"
},
{
"address": 13024,
"name": "battery_health",
"period": "10m"
}
],
"hold": [
{
"address": 13058,
"name": "max_soc",
"period": "90s"
},
{
"address": 13059,
"name": "min_soc",
"period": "90s"
}
]
}));
assert!(result.is_ok());
}
#[test]
fn parse_minimal_rtu_connect_config() {
let result = serde_json::from_value::<Connect>(json!({
"tty": "/dev/ttyUSB0",
"baud_rate": 9600,
}));
assert!(result.is_ok());
let connect = result.unwrap();
use tokio_serial::*;
assert!(matches!(
connect.settings,
ModbusProto::Rtu {
ref tty,
baud_rate: 9600,
data_bits: DataBits::Eight,
stop_bits: StopBits::One,
flow_control: FlowControl::None,
parity: Parity::None,
..
} if tty == "/dev/ttyUSB0"
))
}