ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
//! Whether `lint --fix` may remove an element whose restrictions exclude every session depends on
//! the other elements, which the warning records as an `OtherElements`. All three values share the
//! id `element_excludes_every_session`, and the fixtures assert only ids, so these tests read the
//! warning itself.

use super::{OtherElements, Warning};
use crate::{Version, json, tariff, test, warning};

#[test]
fn an_unrestricted_element_may_match_a_session() {
    test::setup();

    // Element 0 excludes every session through its `max_duration`. Element 1 is unrestricted, so a
    // session may match it.
    let source = r#"{
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V221).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![(
            "$.elements[0]",
            &Warning::ElementExcludesEverySession(OtherElements::SomeMayMatch),
        )],
    );
}

#[test]
fn an_element_restricted_to_reservation_leaves_no_element_that_may_match() {
    test::setup();

    // Element 0 excludes every session through its `max_duration`. Element 1 is restricted to
    // `reservation`, a kind of session pricing doesn't cover.
    let source = r#"{
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"reservation": "RESERVATION"}
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V221).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![(
            "$.elements[0]",
            &Warning::ElementExcludesEverySession(OtherElements::NoneMayMatch),
        )],
    );
}

#[test]
fn an_element_with_a_restriction_that_does_not_read_gets_the_tariff_rejected() {
    test::setup();

    // Element 0 excludes every session through its `max_duration`. Element 1's `start_date` names a
    // month that doesn't exist, so pricing rejects the tariff.
    let source = r#"{
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"start_date": "2025-13-01"}
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V221).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![(
            "$.elements[0]",
            &Warning::ElementExcludesEverySession(OtherElements::TariffRejected),
        )],
    );
}

#[test]
fn an_element_without_price_components_gets_the_tariff_rejected() {
    test::setup();

    // Element 0 excludes every session through its `max_duration`. Element 1 is an empty object. It
    // lacks the `price_components` OCPI requires, so pricing rejects the tariff, and it lacks
    // `restrictions`, which takes it through the walk's branch for an unrestricted element.
    let source = r#"{
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {}
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V221).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![(
            "$.elements[0]",
            &Warning::ElementExcludesEverySession(OtherElements::TariffRejected),
        )],
    );
}

#[test]
fn a_v211_tariff_pricing_rejects_is_reported_as_rejected() {
    test::setup();

    // `lint_v211` repeats the walk in `lint_v221`, so it gets a test of its own. Element 0 excludes
    // every session through its `max_duration`. Element 1's `start_date` names a month that doesn't
    // exist, so pricing rejects the tariff.
    let source = r#"{
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"start_date": "2025-13-01"}
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V211).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![(
            "$.elements[0]",
            &Warning::ElementExcludesEverySession(OtherElements::TariffRejected),
        )],
    );
}

#[test]
fn an_unrestricted_element_in_a_v211_tariff_may_match_a_session() {
    test::setup();

    // `lint_v211` repeats the walk in `lint_v221`, so it gets a test of its own. Element 0 excludes
    // every session through its `max_duration`. Element 1 is unrestricted, so a session may match
    // it.
    let source = r#"{
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V211).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![(
            "$.elements[0]",
            &Warning::ElementExcludesEverySession(OtherElements::SomeMayMatch),
        )],
    );
}

#[test]
fn every_element_excluding_every_session_leaves_none_that_may_match() {
    test::setup();

    // Element 0 excludes every session through its `max_duration`, and element 1 through a
    // `start_time` equal to its `end_time`.
    let source = r#"{
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"start_time": "10:00", "end_time": "10:00"}
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V221).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![
            (
                "$.elements[0]",
                &Warning::ElementExcludesEverySession(OtherElements::NoneMayMatch),
            ),
            (
                "$.elements[1]",
                &Warning::ElementExcludesEverySession(OtherElements::NoneMayMatch),
            ),
        ],
    );
}

#[test]
fn every_element_excluding_every_session_in_a_v211_tariff_leaves_none_that_may_match() {
    test::setup();

    // `lint_v211` repeats the walk in `lint_v221`, so it gets a test of its own. Element 0 excludes
    // every session through its `max_duration`, and element 1 through a `start_time` equal to its
    // `end_time`.
    let source = r#"{
        "currency": "EUR",
        "id": "ID",
        "last_updated": "2024-01-01T00:00:00Z",
        "elements": [
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"max_duration": 0}
            },
            {
                "price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}],
                "restrictions": {"start_time": "10:00", "end_time": "10:00"}
            }
        ]
    }"#;

    let doc = json::parse_object(source).unwrap();
    let tariff = tariff::from_json(doc, Version::V211).ignore_warnings();
    let warnings = tariff::lint(&tariff).warnings;

    assert_eq!(
        raised(&warnings),
        vec![
            (
                "$.elements[0]",
                &Warning::ElementExcludesEverySession(OtherElements::NoneMayMatch),
            ),
            (
                "$.elements[1]",
                &Warning::ElementExcludesEverySession(OtherElements::NoneMayMatch),
            ),
        ],
    );
}

/// Each `ElementExcludesEverySession` in `warnings`, with the path it sits on.
fn raised(warnings: &warning::Set<Warning>) -> Vec<(&str, &Warning)> {
    let mut raised = Vec::new();

    for group in warnings {
        let (element, group_warnings) = group.to_parts();

        for warning in group_warnings {
            if let Warning::ElementExcludesEverySession(_) = warning {
                raised.push((element.path.as_str(), warning));
            }
        }
    }

    raised
}