#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed panic")]
use jiff::SignedDuration;
use rust_decimal::Decimal;
use serde::Deserialize;
use super::{Report, Total, Warning};
use crate::{
Caveat, Kwh, Price, assert_approx_eq, assert_approx_eq_tolerance,
duration::ToDuration,
json,
number::{IsZero as _, RoundDecimal as _},
test::{self, ApproxEq as _, ExpectFile, Expectation},
timezone,
warning::{self, Gather as _, VerdictExt as _},
};
const PRECISION: u32 = 2;
const CDR_DURATION_TOLERANCE: SignedDuration = SignedDuration::from_secs(10);
#[test]
const fn warning_kind_should_be_send_and_sync() {
const fn f<T: Send + Sync>() {}
f::<Warning>();
}
pub trait UnwrapReport {
#[track_caller]
fn unwrap_report(self, json_source: &str) -> Caveat<Report, Warning>;
}
impl UnwrapReport for super::Verdict<Report> {
fn unwrap_report(self, json_source: &str) -> Caveat<Report, Warning> {
match self.only_error() {
Ok(v) => v,
Err(error) => {
let warning::test::ErrorSourceContext {
context: _,
element_path,
element_position,
error,
} = error.into_context(json_source).unwrap();
let json_source = serde_json::from_str::<serde_json::Value>(json_source).unwrap();
panic!(
"Unable to price the CDR due to an error at path `{element_path}`:\n{error}\n{}",
json::test::LineHighlighter::from_value(&json_source, element_position)
);
}
}
}
}
#[test]
#[should_panic(expected = "Unable to price the CDR due to an error at path `$`")]
fn unwrap_report_reports_the_error_that_stopped_pricing() {
const CDR: &str = r#"{"currency": "EUR"}"#;
test::setup();
let doc = json::parse_object(CDR).unwrap();
let root = doc.root();
let mut warnings = warning::Gatherer::new();
warnings.insert(root, Warning::PeriodsStateNoTime);
let error = warning::Error::new(root, Warning::NoValidTariff);
let verdict: super::Verdict<Report> = warnings.into_verdict(Err(error));
let _report = verdict.unwrap_report(CDR);
}
#[derive(Debug, Default)]
pub(crate) struct HoursDecimal(Decimal);
impl ToDuration for HoursDecimal {
fn to_duration(&self) -> SignedDuration {
self.0.to_duration()
}
}
fn decimal<'de, D>(deserializer: D) -> Result<Decimal, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let d = <Decimal as Deserialize>::deserialize(deserializer)?;
Ok(d.round_to_ocpi_scale())
}
impl<'de> Deserialize<'de> for HoursDecimal {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
decimal(deserializer).map(Self)
}
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct Expect {
pub timezone_find: Option<timezone::test::FindOrInferExpect>,
pub tariff_check: Option<StepExpect>,
pub tariff_build: Option<StepExpect>,
pub cdr_check: Option<StepExpect>,
pub cdr_build: Option<StepExpect>,
pub cdr_tariffs: Option<StepExpect>,
pub cdr_price: Option<PriceExpect>,
}
#[expect(
clippy::struct_field_names,
reason = "When deconstructed these fields will always be called *_expect. This avoids having to rename them in-place."
)]
pub(crate) struct ExpectFields {
pub timezone_find_expect: ExpectFile<timezone::test::FindOrInferExpect>,
pub tariff_check_expect: ExpectFile<StepExpect>,
pub tariff_build_expect: ExpectFile<StepExpect>,
pub cdr_check_expect: ExpectFile<StepExpect>,
pub cdr_build_expect: ExpectFile<StepExpect>,
pub cdr_tariffs_expect: ExpectFile<StepExpect>,
pub cdr_price_expect: ExpectFile<PriceExpect>,
}
impl test::IntoFields<ExpectFields> for ExpectFile<Expect> {
fn into_fields(self) -> ExpectFields {
let ExpectFile {
value,
expect_file_name,
} = self;
match value {
Some(expect) => {
let Expect {
timezone_find,
tariff_check,
tariff_build,
cdr_check,
cdr_build,
cdr_tariffs,
cdr_price,
} = expect;
ExpectFields {
timezone_find_expect: ExpectFile::with_value(timezone_find, &expect_file_name),
tariff_check_expect: ExpectFile::with_value(tariff_check, &expect_file_name),
tariff_build_expect: ExpectFile::with_value(tariff_build, &expect_file_name),
cdr_check_expect: ExpectFile::with_value(cdr_check, &expect_file_name),
cdr_build_expect: ExpectFile::with_value(cdr_build, &expect_file_name),
cdr_tariffs_expect: ExpectFile::with_value(cdr_tariffs, &expect_file_name),
cdr_price_expect: ExpectFile::with_value(cdr_price, &expect_file_name),
}
}
None => ExpectFields {
timezone_find_expect: ExpectFile::only_file_name(&expect_file_name),
tariff_check_expect: ExpectFile::only_file_name(&expect_file_name),
tariff_build_expect: ExpectFile::only_file_name(&expect_file_name),
cdr_check_expect: ExpectFile::only_file_name(&expect_file_name),
cdr_build_expect: ExpectFile::only_file_name(&expect_file_name),
cdr_tariffs_expect: ExpectFile::only_file_name(&expect_file_name),
cdr_price_expect: ExpectFile::only_file_name(&expect_file_name),
},
}
}
}
#[track_caller]
pub(crate) fn assert_step_warnings<W: crate::Warning>(
expect_field: &str,
warnings: &warning::Set<W>,
expect: ExpectFile<StepExpect>,
) {
let ExpectFile {
value,
expect_file_name,
} = expect;
let expected = value
.map(|StepExpect { warnings }| warnings)
.unwrap_or(Expectation::Absent);
warning::test::assert_warnings(
&format!("`{expect_field}` in the `{expect_file_name}` file"),
warnings,
expected,
);
}
#[track_caller]
pub(crate) fn assert_price_report(
report: Caveat<Report, Warning>,
expect: ExpectFile<PriceExpect>,
) {
let (report, warnings) = report.into_parts();
let Report {
periods: _,
tariff_used,
timezone: _,
billed_energy: _,
billed_idle_time: _,
billed_charging_time: _,
total_charging_time: _,
total_cost,
total_fixed_cost,
total_time,
total_charging_time_cost,
total_energy,
total_energy_cost,
total_idle_time,
total_idle_cost,
} = report;
let ExpectFile {
value: expect,
expect_file_name,
} = expect;
let (
warnings_expect,
tariff_index_expect,
tariff_id_expect,
total_cost_expectation,
total_fixed_cost_expectation,
total_time_expectation,
total_charging_time_cost_expectation,
total_energy_expectation,
total_energy_cost_expectation,
total_idle_time_expectation,
total_idle_time_cost_expectation,
) = expect
.map(|exp| {
let PriceExpect {
warnings,
tariff_index,
tariff_id,
total_cost,
total_fixed_cost,
total_time,
total_charging_time_cost,
total_energy,
total_energy_cost,
total_idle_time,
total_idle_time_cost,
} = exp;
(
warnings,
tariff_index,
tariff_id,
total_cost,
total_fixed_cost,
total_time,
total_charging_time_cost,
total_energy,
total_energy_cost,
total_idle_time,
total_idle_time_cost,
)
})
.unwrap_or((
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
Expectation::Absent,
));
warning::test::assert_warnings(
&format!("`cdr_price.warnings` in the `{expect_file_name}` file"),
&warnings,
warnings_expect,
);
if let Expectation::Present(expectation) = tariff_id_expect {
assert_eq!(tariff_used.id, expectation.expect_value());
}
if let Expectation::Present(expectation) = tariff_index_expect {
assert_eq!(tariff_used.index, expectation.expect_value());
}
total_cost_expectation.expect_price("total_cost", &total_cost);
total_fixed_cost_expectation.expect_opt_price("total_fixed_cost", &total_fixed_cost);
total_time_expectation.expect_duration("total_time", &total_time);
total_charging_time_cost_expectation
.expect_opt_price("total_charging_time_cost", &total_charging_time_cost);
total_energy_expectation.expect_opt_kwh("total_energy", &total_energy);
total_energy_cost_expectation.expect_opt_price("total_energy_cost", &total_energy_cost);
total_idle_time_expectation.expect_opt_duration("total_idle_time", &total_idle_time);
total_idle_time_cost_expectation.expect_opt_price("total_idle_cost", &total_idle_cost);
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StepExpect {
#[serde(default)]
warnings: Expectation<test::WarningMap>,
}
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PriceExpect {
#[serde(default)]
warnings: Expectation<test::WarningMap>,
#[serde(default)]
tariff_index: Expectation<usize>,
#[serde(default)]
tariff_id: Expectation<String>,
#[serde(default)]
total_cost: Expectation<Price>,
#[serde(default)]
total_fixed_cost: Expectation<Price>,
#[serde(default)]
total_time: Expectation<HoursDecimal>,
#[serde(default)]
total_charging_time_cost: Expectation<Price>,
#[serde(default)]
total_energy: Expectation<Kwh>,
#[serde(default)]
total_energy_cost: Expectation<Price>,
#[serde(default)]
total_idle_time: Expectation<HoursDecimal>,
#[serde(default)]
total_idle_time_cost: Expectation<Price>,
}
impl Expectation<Price> {
#[track_caller]
fn expect_opt_price(self, field_name: &str, total: &Total<Option<Price>>) {
if let Expectation::Present(expect_value) = self {
match (expect_value.into_option(), total.calculated) {
(Some(a), Some(b)) => assert!(
a.approx_eq(&b),
"Expected `{a}` but `{b}` was calculated for `{field_name}`"
),
(Some(a), None) => {
panic!("Expected `{a}`, but no price was calculated for `{field_name}`")
}
(None, Some(b)) => {
panic!("Expected no value, but `{b}` was calculated for `{field_name}`")
}
(None, None) => (),
}
} else {
match (total.cdr, total.calculated) {
(None, None) => (),
(None, Some(calculated)) => {
assert!(
calculated.is_zero(),
"The CDR field `{field_name}` doesn't have a value but a value was calculated; calculated: {calculated}"
);
}
(Some(cdr), None) => {
panic!(
"The CDR states `{cdr}` for `{field_name}` but no value was calculated. \
State `\"{field_name}\": null` in the expect file if the tariff should \
price nothing"
);
}
(Some(cdr), Some(calculated)) => {
assert!(
cdr.approx_eq(&calculated),
"Comparing `{field_name}` field with CDR"
);
}
}
}
}
#[track_caller]
fn expect_price(self, field_name: &str, total: &Total<Price, Option<Price>>) {
if let Expectation::Present(expect_value) = self {
match (expect_value.into_option(), total.calculated) {
(Some(a), Some(b)) => assert!(
a.approx_eq(&b),
"Expected `{a}` but `{b}` was calculated for `{field_name}`"
),
(Some(a), None) => {
panic!("Expected `{a}`, but no price was calculated for `{field_name}`")
}
(None, Some(b)) => {
panic!("Expected no value, but `{b}` was calculated for `{field_name}`")
}
(None, None) => (),
}
} else if let Some(calculated) = total.calculated {
assert!(
total.cdr.approx_eq(&calculated),
"CDR contains `{}` but `{}` was calculated for `{field_name}`",
total.cdr,
calculated
);
} else {
panic!(
"The CDR states `{}` for `{field_name}` but no value was calculated. State \
`\"{field_name}\": null` in the expect file if the tariff should price nothing",
total.cdr
);
}
}
}
impl Expectation<HoursDecimal> {
#[track_caller]
fn expect_duration(self, field_name: &str, total: &Total<SignedDuration>) {
if let Expectation::Present(expect_value) = self {
let expected = expect_value.expect_value().to_duration();
assert_approx_eq!(
total.calculated,
expected,
"Comparing `{field_name}` field with expectation"
);
} else {
assert_approx_eq_tolerance!(
total.calculated,
total.cdr,
CDR_DURATION_TOLERANCE,
"Comparing `{field_name}` field with CDR"
);
}
}
#[track_caller]
fn expect_opt_duration(
self,
field_name: &str,
total: &Total<Option<SignedDuration>, Option<SignedDuration>>,
) {
if let Expectation::Present(expect_value) = self {
let expected = expect_value.into_option().unwrap_or_default().to_duration();
assert_approx_eq!(
total.calculated.unwrap_or_default(),
expected,
"Comparing `{field_name}` field with expectation"
);
} else {
assert_approx_eq_tolerance!(
total.calculated.unwrap_or_default(),
total.cdr.unwrap_or_default(),
CDR_DURATION_TOLERANCE,
"Comparing `{field_name}` field with CDR"
);
}
}
}
impl Expectation<Kwh> {
#[track_caller]
fn expect_opt_kwh(self, field_name: &str, total: &Total<Kwh, Option<Kwh>>) {
if let Expectation::Present(expect_value) = self {
assert_eq!(
expect_value
.into_option()
.map(|kwh| kwh.round_dp(PRECISION)),
total.calculated.map(|kwh| kwh.round_dp(PRECISION)),
"Comparing `{field_name}` field with expectation"
);
} else {
assert_eq!(
total.cdr.round_dp(PRECISION),
total
.calculated
.map(|kwh| kwh.round_dp(PRECISION))
.unwrap_or_default(),
"Comparing `{field_name}` field with CDR"
);
}
}
}