ocpi-tariffs 0.55.0

OCPI tariff calculations
Documentation
//! Tests for building the tariffs a CDR contains with `cdr::Cdr::tariffs`.

use std::assert_matches;

use crate::{Version, cdr, country, currency, json, schema, tariff, test};

/// A minimal, valid `v2.2.1` CDR carrying a single embedded tariff.
const CDR: &str = r#"{
    "country_code": "NL",
    "party_id": "ENE",
    "start_date_time": "2022-01-13T16:00:00Z",
    "end_date_time": "2022-01-13T19:12:00Z",
    "currency": "EUR",
    "tariffs": [
        {
            "country_code": "NL",
            "party_id": "TNM",
            "currency": "EUR",
            "id": "ID",
            "last_updated": "2024-01-01T00:00:00Z",
            "elements": [
                {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
            ]
        }
    ],
    "cdr_location": {"country": "NLD"},
    "charging_periods": [
        {
            "start_date_time": "2022-01-13T16:00:00Z",
            "dimensions": [{"type": "TIME", "volume": 2.5}]
        }
    ],
    "total_cost": {"excl_vat": 11.25, "incl_vat": 12.75},
    "total_time": 3.2,
    "total_energy": 0,
    "last_updated": "2022-01-13T00:00:00Z"
}"#;

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

    let doc = json::parse_object(CDR).unwrap();
    let cdr = cdr::check(doc, Version::V221).ignore_warnings();
    let cdr = cdr::build(cdr).unwrap().ignore_warnings();

    let (tariffs, warnings) = cdr.tariffs().unwrap().into_parts();
    test::assert_build_warnings(&warnings, &[]);

    let [tariff] = tariffs.as_slice() else {
        panic!(
            "the CDR contains one tariff, and `tariffs` built {}",
            tariffs.len()
        );
    };
    assert_eq!(&*tariff.as_v221().id, "ID");
}

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

    let src = CDR.replace(r#""tariffs": ["#, r#""unused": [1], "not_tariffs": ["#);
    let doc = json::parse_object(&src).unwrap();
    let cdr = cdr::check(doc, Version::V221).ignore_warnings();
    let cdr = cdr::build(cdr).unwrap().ignore_warnings();

    let tariffs = cdr.tariffs().unwrap().ignore_warnings();

    assert!(tariffs.is_empty());
}

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

    // The schema walk locates the cause; the lowering only refuses to carry on.
    let src = CDR.replace(
        r#""tariffs": [
        {
            "country_code": "NL",
            "party_id": "TNM",
            "currency": "EUR",
            "id": "ID",
            "last_updated": "2024-01-01T00:00:00Z",
            "elements": [
                {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
            ]
        }
    ]"#,
        r#""tariffs": 5"#,
    );
    let doc = json::parse_object(&src).unwrap();
    let (cdr, schema_warnings) = cdr::check(doc, Version::V221).into_parts();

    let invalid_types = schema_warnings
        .into_path_as_str_map()
        .remove("$.tariffs")
        .expect("the schema should report the `tariffs` field");
    assert_matches!(
        invalid_types.as_slice(),
        [schema::Warning::InvalidType {
            expected: _,
            actual: _
        }]
    );

    let cdr = cdr::build(cdr).unwrap().ignore_warnings();

    let failure = cdr.tariffs().unwrap_err();

    assert_matches!(
        failure.into_parts().0.into_warning(),
        tariff::Warning::Rejected
    );
}

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

    let src = CDR.replace(
        r#"[
                {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
            ]"#,
        "[]",
    );
    let doc = json::parse_object(&src).unwrap();
    let cdr = cdr::check(doc, Version::V221).ignore_warnings();
    let cdr = cdr::build(cdr).unwrap().ignore_warnings();

    let (error, _warnings) = cdr.tariffs().unwrap_err().into_parts();

    assert_eq!(error.element().path.as_str(), "$.tariffs[0]");
    assert_matches!(error.into_warning(), tariff::Warning::NoElements);
}

/// An embedded tariff the schema couldn't build doesn't discard the warnings of the
/// tariffs built before it.
#[test]
fn an_unbuilt_tariff_keeps_the_earlier_tariffs_warnings() {
    test::setup();

    // Building raises `IncorrectCase` for the first tariff's `country_code`, then
    // rejects the second entry, which the schema couldn't build as a tariff.
    let src = CDR
        .replace(
            r#""country_code": "NL",
            "party_id": "TNM""#,
            r#""country_code": "nl",
            "party_id": "TNM""#,
        )
        .replace(
            r#"        }
    ],
    "cdr_location""#,
            r#"        },
        5
    ],
    "cdr_location""#,
        );
    let doc = json::parse_object(&src).unwrap();
    let cdr = cdr::check(doc, Version::V221).ignore_warnings();
    let cdr = cdr::build(cdr).unwrap().ignore_warnings();

    let (error, warnings) = cdr.tariffs().unwrap_err().into_parts();

    assert_matches!(error.warning(), tariff::Warning::Rejected);
    let (element, _warning) = warnings
        .find(|warning| {
            matches!(
                warning,
                tariff::Warning::Country(country::Warning::IncorrectCase)
            )
        })
        .expect("the rejection should keep the first tariff's warning");
    assert_eq!(element.path.as_str(), "$.tariffs[0].country_code");
}

/// A tariff the CDR contains whose values can't be read doesn't discard the warnings of the
/// tariffs built before it, and the error's path names the tariff that failed.
#[test]
fn a_tariff_with_an_unknown_currency_keeps_the_earlier_tariffs_warnings() {
    test::setup();

    // Building raises `IncorrectCase` for the first tariff's `country_code`, then fails on the
    // second tariff's `currency`.
    let src = CDR
        .replace(
            r#""country_code": "NL",
            "party_id": "TNM""#,
            r#""country_code": "nl",
            "party_id": "TNM""#,
        )
        .replace(
            r#"        }
    ],
    "cdr_location""#,
            r#"        },
        {
            "country_code": "NL",
            "party_id": "TNM",
            "currency": "ZZZ",
            "id": "ID2",
            "last_updated": "2024-01-01T00:00:00Z",
            "elements": [
                {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
            ]
        }
    ],
    "cdr_location""#,
        );
    let doc = json::parse_object(&src).unwrap();
    let cdr = cdr::check(doc, Version::V221).ignore_warnings();
    let cdr = cdr::build(cdr).unwrap().ignore_warnings();

    let (error, warnings) = cdr.tariffs().unwrap_err().into_parts();

    assert_eq!(error.element().path.as_str(), "$.tariffs[1].currency");
    assert_matches!(
        error.warning(),
        tariff::Warning::Currency(currency::Warning::InvalidCode)
    );
    let (element, _warning) = warnings
        .find(|warning| {
            matches!(
                warning,
                tariff::Warning::Country(country::Warning::IncorrectCase)
            )
        })
        .expect("the failure should keep the first tariff's warning");
    assert_eq!(element.path.as_str(), "$.tariffs[0].country_code");
}