From 923f726bb4f648ceef3bf3150fc1c4c40497f955 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 20 Sep 2024 22:30:26 +0200 Subject: [PATCH] Add test scenario --- .../engine_test/test_conflict.yaml | 44 +++++++++++++++++++ central_backend/src/energy/engine.rs | 35 +++++++++++---- 2 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 central_backend/engine_test/test_conflict.yaml diff --git a/central_backend/engine_test/test_conflict.yaml b/central_backend/engine_test/test_conflict.yaml new file mode 100644 index 0000000..4763efa --- /dev/null +++ b/central_backend/engine_test/test_conflict.yaml @@ -0,0 +1,44 @@ +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: true + for: 5 + should_be_on: true + + - id: dcb3fd91-bf9b-4de3-99e5-92c1c7dd72e8 + name: R2 + enabled: true + priority: 1 + consumption: 100 + minimal_uptime: 10 + minimal_downtime: 10 + depends_on: [ ] + conflicts_with: [ dcb3fd91-bf9b-4de3-99e5-92c1c7dd72e9 ] + + on: false + for: 500 + should_be_on: false + + online: true + +curr_consumption: -10000 \ No newline at end of file diff --git a/central_backend/src/energy/engine.rs b/central_backend/src/energy/engine.rs index 1b4d752..d583830 100644 --- a/central_backend/src/energy/engine.rs +++ b/central_backend/src/energy/engine.rs @@ -378,6 +378,7 @@ mod test { use crate::energy::consumption::EnergyConsumption; use crate::energy::engine::EnergyEngine; use crate::utils::time_utils::time_secs; + use rust_embed::Embed; #[derive(serde::Deserialize)] struct TestRelayState { @@ -442,22 +443,38 @@ mod test { ) } - fn run_test(conf: &str) { + fn run_test(name: &str, 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); + for (device_s, device) in states.iter().zip(&devices) { + for (relay_s, relay) in device_s.relays.iter().zip(&device.relays) { + let is_on = energy_engine.relay_state(relay_s.id).on; + + assert_eq!( + energy_engine.relay_state(relay_s.id).on, + relay_s.should_be_on, + "For test {name} on relay {} got state {is_on} instead of {}", + relay.name, + relay_s.should_be_on + ); } } } - #[test] - fn test_basic_conf() { - run_test(include_str!("../../engine_test/test_basic_conf.yaml")) - } + #[derive(Embed)] + #[folder = "engine_test/"] + struct Asset; - // TODO : test more scenarios + #[test] + fn test_confs() { + for file in Asset::iter() { + let content = Asset::get(&file).unwrap(); + + log::info!("Testing {file}"); + + run_test(&file, &String::from_utf8(content.data.to_vec()).unwrap()) + } + } }