#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed to panic")]
use rust_decimal_macros::dec;
use crate::{
Money, Version, cdr, json, price,
price::test::UnwrapReport as _,
tariff,
test::{self},
};
fn tariff_json(restrictions: &serde_json::Value) -> String {
serde_json::json!({
"id": "T1",
"country_code": "NL",
"party_id": "TDR",
"currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "ENERGY", "price": 1.00, "vat": 0.0, "step_size": 1 }
],
"restrictions": restrictions
},
{
"price_components": [
{ "type": "ENERGY", "price": 0.30, "vat": 0.0, "step_size": 1 }
]
}
],
"last_updated": "2022-01-01T00:00:00Z"
})
.to_string()
}
fn tariff_v211_json(restrictions: &serde_json::Value) -> String {
serde_json::json!({
"id": "T1",
"currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "ENERGY", "price": 1.00, "step_size": 1 }
],
"restrictions": restrictions
},
{
"price_components": [
{ "type": "ENERGY", "price": 0.30, "step_size": 1 }
]
}
]
})
.to_string()
}
#[track_caller]
fn total_excl_vat(tariff_json: &str, version: Version) -> Money {
let cdr_json = serde_json::json!({
"country_code": "NL",
"party_id": "TDR",
"start_date_time": "2022-01-13T10:00:00Z",
"end_date_time": "2022-01-13T11:00:00Z",
"currency": "EUR",
"tariffs": [],
"cdr_location": { "country": "NLD" },
"charging_periods": [
{
"start_date_time": "2022-01-13T10:00:00Z",
"dimensions": [
{ "type": "ENERGY", "volume": 10.0 }
]
}
],
"total_cost": { "excl_vat": 0.00 },
"total_time": 1.0,
"total_energy": 10.0,
"last_updated": "2022-01-13T00:00:00Z"
})
.to_string();
let (cdr, mut cdr_warnings) =
cdr::from_json(json::parse_object(&cdr_json).unwrap(), Version::V221).into_parts();
cdr_warnings.remove_missing_fields();
test::assert_schema_warnings(&cdr_warnings, &[]);
let (tariff, mut tariff_warnings) =
tariff::from_json(json::parse_object(tariff_json).unwrap(), version).into_parts();
tariff_warnings.remove_missing_fields();
test::assert_schema_warnings(&tariff_warnings, &[]);
let report = cdr::price(
&cdr,
price::TariffSource::Override(vec![tariff]),
chrono_tz::Tz::UTC,
)
.unwrap_report(cdr.as_json_str());
let (report, _) = report.into_parts();
report.total_cost.calculated.unwrap().excl_vat
}
#[test]
fn should_price_the_element_when_a_lone_end_time_is_midnight() {
test::setup();
let tariff = tariff_json(&serde_json::json!({ "end_time": "00:00" }));
assert_eq!(total_excl_vat(&tariff, Version::V221), dec!(10.00).into());
}
#[test]
fn should_price_the_element_when_start_and_end_time_are_both_midnight() {
test::setup();
let tariff = tariff_json(&serde_json::json!({
"start_time": "00:00",
"end_time": "00:00"
}));
assert_eq!(total_excl_vat(&tariff, Version::V221), dec!(10.00).into());
}
#[test]
fn should_not_price_the_element_when_the_session_falls_outside_the_window() {
test::setup();
let tariff = tariff_json(&serde_json::json!({
"start_time": "20:00",
"end_time": "22:00"
}));
assert_eq!(total_excl_vat(&tariff, Version::V221), dec!(3.00).into());
}
#[test]
fn should_not_price_the_element_when_an_end_time_precedes_the_session() {
test::setup();
let tariff = tariff_json(&serde_json::json!({ "end_time": "09:00" }));
assert_eq!(total_excl_vat(&tariff, Version::V221), dec!(3.00).into());
}
#[test]
fn should_price_the_element_when_the_session_falls_after_the_start_of_an_overnight_window() {
test::setup();
let tariff = tariff_json(&serde_json::json!({
"start_time": "09:00",
"end_time": "02:00"
}));
assert_eq!(total_excl_vat(&tariff, Version::V221), dec!(10.00).into());
}
#[test]
fn should_price_the_element_when_the_session_falls_before_the_end_of_an_overnight_window() {
test::setup();
let tariff = tariff_json(&serde_json::json!({
"start_time": "22:00",
"end_time": "12:00"
}));
assert_eq!(total_excl_vat(&tariff, Version::V221), dec!(10.00).into());
}
#[test]
fn should_price_the_element_when_a_v211_tariff_ends_at_midnight() {
test::setup();
let tariff = tariff_v211_json(&serde_json::json!({ "end_time": "00:00" }));
assert_eq!(total_excl_vat(&tariff, Version::V211), dec!(10.00).into());
}
#[test]
fn should_not_price_the_element_when_a_v211_end_time_precedes_the_session() {
test::setup();
let tariff = tariff_v211_json(&serde_json::json!({ "end_time": "09:00" }));
assert_eq!(total_excl_vat(&tariff, Version::V211), dec!(3.00).into());
}