#![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::{
Version, cdr, json, price,
price::test::UnwrapReport as _,
tariff,
test::{self},
};
#[test]
fn should_bill_charging_time_in_blocks_of_the_time_step_size() {
test::setup();
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-13T10:21:00Z",
"currency": "EUR",
"tariffs": [],
"cdr_location": { "country": "NLD" },
"charging_periods": [
{
"start_date_time": "2022-01-13T10:00:00Z",
"dimensions": [{ "type": "TIME", "volume": 0.35 }]
}
],
"total_cost": { "excl_vat": 0.00 },
"total_time": 0.35,
"total_energy": 0,
"last_updated": "2022-01-13T00:00:00Z"
})
.to_string();
let tariff_json = serde_json::json!({
"id": "T1",
"country_code": "NL",
"party_id": "TDR",
"currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "TIME", "price": 2.00, "vat": 0.0, "step_size": 900 }
]
}
],
"last_updated": "2022-01-01T00: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::V221).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();
assert_eq!(
report.total_cost.calculated.unwrap().excl_vat,
dec!(1.00).into()
);
}
#[test]
fn should_bill_a_parking_period_of_40_minutes_stated_as_0_6667_hours_as_40_minutes() {
test::setup();
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-13T10:40:00Z",
"currency": "EUR",
"tariffs": [],
"cdr_location": { "country": "NLD" },
"charging_periods": [
{
"start_date_time": "2022-01-13T10:00:00Z",
"dimensions": [{ "type": "PARKING_TIME", "volume": 0.6667 }]
}
],
"total_cost": { "excl_vat": 0.00 },
"total_time": 0.6667,
"total_parking_time": 0.6667,
"total_energy": 0,
"last_updated": "2022-01-13T00:00:00Z"
})
.to_string();
let tariff_json = serde_json::json!({
"id": "T1",
"country_code": "NL",
"party_id": "TDR",
"currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "PARKING_TIME", "price": 15.00, "vat": 0.0, "step_size": 60 }
]
}
],
"last_updated": "2022-01-01T00: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::V221).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();
assert_eq!(
report.total_cost.calculated.unwrap().excl_vat,
dec!(10.00).into()
);
}
#[test]
fn should_bill_a_charging_time_shorter_than_its_period_as_stated() {
test::setup();
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-13T10:40:00Z",
"currency": "EUR",
"tariffs": [],
"cdr_location": { "country": "NLD" },
"charging_periods": [
{
"start_date_time": "2022-01-13T10:00:00Z",
"dimensions": [{ "type": "TIME", "volume": 0.25 }]
}
],
"total_cost": { "excl_vat": 0.00 },
"total_time": 0.6667,
"total_energy": 0,
"last_updated": "2022-01-13T00:00:00Z"
})
.to_string();
let tariff_json = serde_json::json!({
"id": "T1",
"country_code": "NL",
"party_id": "TDR",
"currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "TIME", "price": 6.00, "vat": 0.0, "step_size": 60 }
]
}
],
"last_updated": "2022-01-01T00: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::V221).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();
assert_eq!(
report.total_cost.calculated.unwrap().excl_vat,
dec!(1.50).into()
);
}
#[test]
fn should_bill_nothing_for_a_charging_time_of_0_in_a_period_shorter_than_0_18_seconds() {
test::setup();
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-13T10:00:00.150Z",
"currency": "EUR",
"tariffs": [],
"cdr_location": { "country": "NLD" },
"charging_periods": [
{
"start_date_time": "2022-01-13T10:00:00Z",
"dimensions": [{ "type": "TIME", "volume": 0 }]
}
],
"total_cost": { "excl_vat": 0.00 },
"total_time": 0,
"total_energy": 0,
"last_updated": "2022-01-13T00:00:00Z"
})
.to_string();
let tariff_json = serde_json::json!({
"id": "T1",
"country_code": "NL",
"party_id": "TDR",
"currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "TIME", "price": 6.00, "vat": 0.0, "step_size": 60 }
]
}
],
"last_updated": "2022-01-01T00: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::V221).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();
assert_eq!(
report.total_cost.calculated.unwrap().excl_vat,
dec!(0.00).into()
);
}