From b7635935ba7e79fcc7e529ae065f93964fbf2c6c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 20 Sep 2024 18:27:20 +0200 Subject: [PATCH] Implement energy engine test logic --- central_backend/Cargo.lock | 26 ++++++ central_backend/Cargo.toml | 3 +- .../engine_test/test_basic_conf.yaml | 30 +++++++ central_backend/src/energy/engine.rs | 90 +++++++++++++++++++ 4 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 central_backend/engine_test/test_basic_conf.yaml diff --git a/central_backend/Cargo.lock b/central_backend/Cargo.lock index 9b7f0a2..994b2bb 100644 --- a/central_backend/Cargo.lock +++ b/central_backend/Cargo.lock @@ -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" diff --git a/central_backend/Cargo.toml b/central_backend/Cargo.toml index 46351b8..9f01b2e 100644 --- a/central_backend/Cargo.toml +++ b/central_backend/Cargo.toml @@ -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" \ No newline at end of file +chrono = "0.4.38" +serde_yml = "0.0.12" \ No newline at end of file diff --git a/central_backend/engine_test/test_basic_conf.yaml b/central_backend/engine_test/test_basic_conf.yaml new file mode 100644 index 0000000..0dac6dc --- /dev/null +++ b/central_backend/engine_test/test_basic_conf.yaml @@ -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 \ No newline at end of file diff --git a/central_backend/src/energy/engine.rs b/central_backend/src/energy/engine.rs index 91035fe..1b4d752 100644 --- a/central_backend/src/energy/engine.rs +++ b/central_backend/src/energy/engine.rs @@ -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, + } + + #[derive(serde::Deserialize)] + struct TestConfig { + curr_consumption: EnergyConsumption, + devices: Vec, + } + + #[derive(serde::Deserialize)] + struct TestConfigState { + devices: Vec, + } + + fn parse_test_config( + conf: &str, + ) -> ( + Vec, + EnergyEngine, + EnergyConsumption, + Vec, + ) { + 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 +}