Implement energy engine test logic
This commit is contained in:
@ -371,3 +371,93 @@ impl EnergyEngine {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::devices::device::{Device, DeviceId, DeviceRelayID};
|
||||
use crate::energy::consumption::EnergyConsumption;
|
||||
use crate::energy::engine::EnergyEngine;
|
||||
use crate::utils::time_utils::time_secs;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct TestRelayState {
|
||||
id: DeviceRelayID,
|
||||
on: bool,
|
||||
r#for: usize,
|
||||
should_be_on: bool,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct TestDeviceState {
|
||||
id: DeviceId,
|
||||
online: bool,
|
||||
relays: Vec<TestRelayState>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct TestConfig {
|
||||
curr_consumption: EnergyConsumption,
|
||||
devices: Vec<Device>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct TestConfigState {
|
||||
devices: Vec<TestDeviceState>,
|
||||
}
|
||||
|
||||
fn parse_test_config(
|
||||
conf: &str,
|
||||
) -> (
|
||||
Vec<Device>,
|
||||
EnergyEngine,
|
||||
EnergyConsumption,
|
||||
Vec<TestDeviceState>,
|
||||
) {
|
||||
let config: TestConfig = serde_yml::from_str(conf).unwrap();
|
||||
let test_config: TestConfigState = serde_yml::from_str(conf).unwrap();
|
||||
|
||||
let mut engine = EnergyEngine {
|
||||
devices_state: Default::default(),
|
||||
relays_state: Default::default(),
|
||||
};
|
||||
|
||||
for d in &test_config.devices {
|
||||
engine.device_state(&d.id).last_ping = match d.online {
|
||||
true => time_secs() - 1,
|
||||
false => 10,
|
||||
};
|
||||
|
||||
for r in &d.relays {
|
||||
let s = engine.relay_state(r.id);
|
||||
s.on = r.on;
|
||||
s.since = time_secs() as usize - r.r#for;
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
config.devices,
|
||||
engine,
|
||||
config.curr_consumption,
|
||||
test_config.devices,
|
||||
)
|
||||
}
|
||||
|
||||
fn run_test(conf: &str) {
|
||||
let (devices, mut energy_engine, consumption, states) = parse_test_config(conf);
|
||||
|
||||
energy_engine.refresh(consumption, &devices);
|
||||
|
||||
for d in states {
|
||||
for r in d.relays {
|
||||
assert_eq!(energy_engine.relay_state(r.id).on, r.should_be_on);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_conf() {
|
||||
run_test(include_str!("../../engine_test/test_basic_conf.yaml"))
|
||||
}
|
||||
|
||||
// TODO : test more scenarios
|
||||
}
|
||||
|
Reference in New Issue
Block a user