ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
#![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();

    // 21 minutes of charging against a `step_size` of 900 seconds is billed as two blocks of 15
    // minutes. At 2.00 per hour that is 1.00, where the 21 minutes alone cost 0.70. The CDR's own
    // `total_cost` of `0` is never compared, because the assertion reads the calculated total.
    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();

    // The CDR and the tariff leave out most of the fields the spec requires, so the schema walk
    // reports them as missing. Any other warning, such as a misspelled key reported as an
    // unexpected field, means the input is not what the test says it is.
    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();

    // OCPI states a volume in hours to 4 decimals, so 40 minutes of parking is written as `0.6667`,
    // which is 2400.12 seconds. A pricer that billed the volume as stated would round it up to 41
    // blocks of 60 seconds, 10.25 at 15.00 per hour, where 40 minutes cost 10.00.
    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();

    // The period lasts 40 minutes and states 15 minutes of charging. A pricer that read every
    // volume as its period's length would bill 40 minutes, 4.00 at 6.00 per hour, where the 15
    // minutes stated cost 1.50.
    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();

    // The period lasts 150 milliseconds and states a charging time of `0`. 150 milliseconds is
    // within the 0.18 seconds the pricer allows between a volume and its period's length. A pricer
    // that read that `0` as the period's length would bill one 60-second block, 0.10 at 6.00 per
    // hour, for charging the session never did.
    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()
    );
}