Skip to content

Commit d6e9575

Browse files
committed
Test whether the logical meter returns the correct formulas
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 64c1b5d commit d6e9575

4 files changed

Lines changed: 102 additions & 5 deletions

File tree

src/client/test_utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ impl MockComponent {
128128
}
129129
}
130130

131-
#[allow(dead_code)]
132131
pub fn ev_charger(component_id: u64) -> Self {
133132
Self {
134133
component: ElectricalComponent {
@@ -141,7 +140,6 @@ impl MockComponent {
141140
}
142141
}
143142

144-
#[allow(dead_code)]
145143
pub fn chp(component_id: u64) -> Self {
146144
Self {
147145
component: ElectricalComponent {

src/logical_meter/formula/aggregation_formula.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct AggregationFormula<M: Metric> {
1919

2020
impl<M: Metric> std::fmt::Display for AggregationFormula<M> {
2121
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22-
write!(f, "{}::{}", self.formula, M::METRIC.as_str_name())
22+
write!(f, "{}::({})", M::METRIC.as_str_name(), self.formula)
2323
}
2424
}
2525

src/logical_meter/formula/coalesce_formula.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct CoalesceFormula<M: Metric> {
1919

2020
impl<M: Metric> std::fmt::Display for CoalesceFormula<M> {
2121
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22-
write!(f, "{}::{}", self.formula, M::METRIC.as_str_name())
22+
write!(f, "{}::({})", M::METRIC.as_str_name(), self.formula)
2323
}
2424
}
2525

src/logical_meter/logical_meter_handle.rs

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,28 @@ mod tests {
222222
// Battery
223223
MockComponent::battery(7),
224224
]),
225+
// Battery inverter
226+
MockComponent::battery_inverter(8)
227+
.with_voltage(vec![400.0, 400.0, 398.0, 396.0, 396.0, 396.0])
228+
.with_children(vec![
229+
// Battery
230+
MockComponent::battery(9),
231+
]),
225232
]),
226233
// Consumer meter
227-
MockComponent::meter(8)
234+
MockComponent::meter(10)
228235
.with_current(vec![14.5, 15.0, 16.0, 15.5, 14.0, 13.5]),
236+
// Chp meter
237+
MockComponent::meter(11).with_children(vec![
238+
// Chp
239+
MockComponent::chp(12),
240+
]),
241+
// Ev charger meter
242+
MockComponent::meter(13).with_children(vec![
243+
// Ev chargers
244+
MockComponent::ev_charger(14),
245+
MockComponent::ev_charger(15),
246+
]),
229247
]),
230248
]),
231249
);
@@ -240,6 +258,87 @@ mod tests {
240258
.unwrap()
241259
}
242260

261+
#[tokio::test]
262+
async fn test_formula_display() {
263+
let mut lm = new_logical_meter_handle().await;
264+
265+
let formula = lm.grid(crate::metric::AcPowerActive).unwrap();
266+
assert_eq!(formula.to_string(), "METRIC_AC_POWER_ACTIVE::(#2)");
267+
268+
let formula = lm.battery(None, crate::metric::AcPowerReactive).unwrap();
269+
assert_eq!(
270+
formula.to_string(),
271+
"METRIC_AC_POWER_REACTIVE::(COALESCE(#8 + #6, #5, COALESCE(#8, 0.0) + COALESCE(#6, 0.0)))"
272+
);
273+
274+
let formula = lm
275+
.battery(Some([9].into()), crate::metric::AcPowerActive)
276+
.unwrap();
277+
assert_eq!(
278+
formula.to_string(),
279+
"METRIC_AC_POWER_ACTIVE::(COALESCE(#8, 0.0))"
280+
);
281+
282+
let formula = lm
283+
.battery(Some([7].into()), crate::metric::AcVoltage)
284+
.unwrap();
285+
assert_eq!(formula.to_string(), "METRIC_AC_VOLTAGE::(COALESCE(#5, #6))");
286+
287+
let formula = lm.battery(None, crate::metric::AcFrequency).unwrap();
288+
assert_eq!(
289+
formula.to_string(),
290+
"METRIC_AC_FREQUENCY::(COALESCE(#5, #6, #8))"
291+
);
292+
293+
let formula = lm.pv(None, crate::metric::AcPowerReactive).unwrap();
294+
assert_eq!(
295+
formula.to_string(),
296+
"METRIC_AC_POWER_REACTIVE::(COALESCE(#4, #3, 0.0))"
297+
);
298+
299+
let formula = lm.chp(None, crate::metric::AcPowerActive).unwrap();
300+
assert_eq!(
301+
formula.to_string(),
302+
"METRIC_AC_POWER_ACTIVE::(COALESCE(#12, #11, 0.0))"
303+
);
304+
305+
let formula = lm.ev_charger(None, crate::metric::AcCurrent).unwrap();
306+
assert_eq!(
307+
formula.to_string(),
308+
"METRIC_AC_CURRENT::(COALESCE(#15 + #14, #13, COALESCE(#15, 0.0) + COALESCE(#14, 0.0)))"
309+
);
310+
311+
let formula = lm.consumer(crate::metric::AcCurrent).unwrap();
312+
assert_eq!(
313+
formula.to_string(),
314+
concat!(
315+
"METRIC_AC_CURRENT::(MAX(",
316+
"#2 - COALESCE(#3, #4, 0.0) - COALESCE(#5, COALESCE(#8, 0.0) + COALESCE(#6, 0.0)) ",
317+
"- #10 - COALESCE(#11, #12, 0.0)",
318+
" - COALESCE(#13, COALESCE(#15, 0.0) + COALESCE(#14, 0.0)),",
319+
" 0.0)",
320+
" + COALESCE(MAX(#3 - #4, 0.0), 0.0) + COALESCE(MAX(#5 - #6 - #8, 0.0), 0.0)",
321+
" + MAX(#10, 0.0) + COALESCE(MAX(#11 - #12, 0.0), 0.0)",
322+
" + COALESCE(MAX(#13 - #14 - #15, 0.0), 0.0)",
323+
")"
324+
)
325+
);
326+
327+
let formula = lm.producer(crate::metric::AcPowerActive).unwrap();
328+
assert_eq!(
329+
formula.to_string(),
330+
concat!(
331+
"METRIC_AC_POWER_ACTIVE::(",
332+
"MIN(COALESCE(#4, #3, 0.0), 0.0)",
333+
" + MIN(COALESCE(#12, #11, 0.0), 0.0)",
334+
")"
335+
)
336+
);
337+
338+
let formula = lm.component(10, crate::metric::AcCurrent).unwrap();
339+
assert_eq!(formula.to_string(), "METRIC_AC_CURRENT::(#10)");
340+
}
341+
243342
#[tokio::test(start_paused = true)]
244343
async fn test_grid_power_formula() {
245344
let formula = new_logical_meter_handle()

0 commit comments

Comments
 (0)