Implement energy engine test logic

This commit is contained in:
Pierre HUBERT 2024-09-20 18:27:20 +02:00
parent 719b0a0c5c
commit b7635935ba
4 changed files with 148 additions and 1 deletions

View File

@ -634,6 +634,7 @@ dependencies = [
"semver",
"serde",
"serde_json",
"serde_yml",
"thiserror",
"tokio",
"tokio_schedule",
@ -1499,6 +1500,16 @@ dependencies = [
"libc",
]
[[package]]
name = "libyml"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980"
dependencies = [
"anyhow",
"version_check",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@ -2197,6 +2208,21 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yml"
version = "0.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd"
dependencies = [
"indexmap",
"itoa",
"libyml",
"memchr",
"ryu",
"serde",
"version_check",
]
[[package]]
name = "sha1"
version = "0.10.6"

View File

@ -36,4 +36,5 @@ mime_guess = "2.0.5"
rust-embed = "8.5.0"
jsonwebtoken = { version = "9.3.0", features = ["use_pem"] }
prettytable-rs = "0.10.0"
chrono = "0.4.38"
chrono = "0.4.38"
serde_yml = "0.0.12"

View File

@ -0,0 +1,30 @@
devices:
- id: dev1
info:
reference: A
version: 0.0.1
max_relays: 1
time_create: 1
time_update: 1
name: Dev1
description: Day1
validated: true
enabled: true
relays:
- id: dcb3fd91-bf9b-4de3-99e5-92c1c7dd72e9
name: R1
enabled: true
priority: 1
consumption: 100
minimal_uptime: 10
minimal_downtime: 10
depends_on: []
conflicts_with: []
on: false
for: 500
should_be_on: true
online: true
curr_consumption: -1000

View File

@ -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
}