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::{
    Money, Version, cdr, json, price,
    price::test::UnwrapReport as _,
    tariff,
    test::{self},
};

// Two elements: the first at 1.00 EUR/kWh, the second at 0.30 EUR/kWh with no restriction. A 10 kWh
// session therefore totals 10.00 when the restriction is satisfied and 3.00 when it is not, so the
// total names the element that applied. Both components state `vat: 0.0`, so the inclusive total
// equals the exclusive one. Nothing in these tests turns on VAT.
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()
}

// The same two elements, written as v2.1.1. A v2.1.1 tariff reaches the pricer only through
// `From<v211::Restrictions> for v221::Restrictions`, which lowers the midnight `end_time` at a
// second site from the v2.2.1 one. v2.1.1 has no `vat` field, so there is no inclusive total here
// and no `vat: 0.0` to state.
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 {
    // A 10 kWh session over 10:00-11:00, priced in `Tz::UTC` so the local time is the stated one.
    //
    // OCPI requires a CDR to state `total_cost`, so this one states `0`. No test reads it, because
    // every assertion reads `price::Total::calculated`.
    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();

    // A test CDR or tariff states only the fields its test depends on, so missing fields are
    // expected. 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).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();

    // `specs/v221/mod_tariffs.asciidoc:1084`: "To stop at end of the day use: 00:00." A bound that
    // ends at the end of the day bounds nothing, so the element applies to the whole session.
    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();

    // If parsing kept a midnight `end_time`, the pricer would skip this element for every session.
    // A later `start_time` would wrap around midnight, so only a `00:00` start is affected.
    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();

    // The element is restricted from 20:00 to 22:00. The session runs 10:00-11:00 local, outside
    // that restriction. Only a midnight end time bounds nothing, so an ordinary window still
    // excludes a session.
    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();

    // The element is bounded by a lone `end_time` of 09:00, and the session starts at 10:00, so the
    // element is excluded. This is the lone-bound form of the case above, where a `start_time` and
    // `end_time` bracket the window.
    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();

    // The window runs from 09:00 to 02:00 the next day, so the 10:00 session falls in the part
    // before midnight. Of the two conditions `Restriction::WrappingTime` accepts, only
    // `snapshot.local_time() >= start_time` holds, so this test pins that one.
    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();

    // The window runs from 22:00 to 12:00 the next day, so the 10:00 session falls in the part
    // after midnight. Of the two conditions `Restriction::WrappingTime` accepts, only
    // `snapshot.local_time() < end_time` holds, so this test pins that one.
    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());
}