ocpi-tariffs 0.54.0

OCPI tariff calculations
Documentation
//! Tests for lowering a `schema::Number` into the decimal-backed money types via
//! `FromSchema`.
//!
//! A `schema::Number` is only produced by the builder, so each test drives a real
//! `v2.1.1` tariff through `build_tariff` and lowers a price component's `price` field
//! (a `Number` leaf). The leaf is just a carrier of numeric content here; its origin
//! field is irrelevant to the lowering.

#![allow(
    clippy::indexing_slicing,
    clippy::unwrap_in_result,
    reason = "unwraps and indexing are allowed anywhere in tests"
)]

use std::assert_matches;

use rust_decimal::Decimal;
use rust_decimal_macros::dec;

use super::{Money, Price, Vat, VatOrigin, Warning};
use crate::{
    FromSchema as _, json, number,
    schema::{self, Integrity, v211, v221},
    warning,
};

/// A minimal, fully valid `v2.1.1` tariff. The price component's `price` is the `Number`
/// leaf the tests lower.
const VALID: &str = r#"{
    "currency": "EUR",
    "elements": [
        {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
    ],
    "id": "ID",
    "last_updated": "2024-01-01T00:00:00Z"
}"#;

/// Build a `v2.1.1` tariff, dropping the schema-level warnings.
fn build_tariff<'buf>(doc: &json::Document<'buf>) -> v211::Tariff<'buf> {
    v211::build_tariff(doc).ignore_warnings()
}

/// Borrow the single price component's `price` `Number` leaf out of a tariff.
fn price<'a, 'buf>(tariff: &'a v211::Tariff<'buf>) -> &'a schema::Number<'buf> {
    let Integrity::Ok(elements) = &tariff.elements else {
        panic!("elements should be built: {:?}", tariff.elements);
    };
    let Integrity::Ok(element) = &elements[0] else {
        panic!("the element should be built");
    };
    let Integrity::Ok(components) = &element.price_components else {
        panic!("price_components should be built");
    };
    let Integrity::Ok(component) = &components[0] else {
        panic!("the price component should be built");
    };
    let Integrity::Ok(number) = &component.price else {
        panic!("the price should be built: {:?}", component.price);
    };
    number
}

#[test]
fn money_lowers_from_number() {
    let doc = json::parse(VALID.into()).unwrap();
    let tariff = build_tariff(&doc);

    let (money, warnings) = Money::from_schema(price(&tariff)).unwrap().into_parts();

    assert_eq!(Decimal::from(money), dec!(0.25));
    assert!(warnings.path_map().is_empty());
}

#[test]
fn vat_lowers_from_number() {
    let doc = json::parse(VALID.into()).unwrap();
    let tariff = build_tariff(&doc);

    let (vat, warnings) = Vat::from_schema(price(&tariff)).unwrap().into_parts();

    assert_eq!(Decimal::from(vat), dec!(0.25));
    assert!(warnings.path_map().is_empty());
}

#[test]
fn vat_origin_lowers_from_number() {
    let doc = json::parse(VALID.into()).unwrap();
    let tariff = build_tariff(&doc);

    let (vat_origin, warnings) = VatOrigin::from_schema(price(&tariff)).unwrap().into_parts();

    let VatOrigin::Provided(vat) = vat_origin else {
        panic!("expected Provided, got {vat_origin:?}");
    };
    assert_eq!(Decimal::from(vat), dec!(0.25));
    assert!(warnings.path_map().is_empty());
}

#[test]
fn a_vat_below_zero_is_reported() {
    // A rate less than `0` makes `1 + vat/100` a multiplier less than `1`, which is what
    // `Price::vat_is_consistent` depends on being impossible. The assertion checks the rate is
    // still `Provided` afterwards, because dropping it would price a different tariff.
    //
    // The v2.1.1 fixture states no `vat`, so `price()` hands over the `price` leaf and the
    // lowering is driven through that, as the two tests above it also do.
    let src = VALID.replace(r#""price": 0.25"#, r#""price": -0.25"#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (vat_origin, warnings) = VatOrigin::from_schema(price(&tariff)).unwrap().into_parts();

    let VatOrigin::Provided(vat) = vat_origin else {
        panic!("expected Provided, got {vat_origin:?}");
    };
    assert_eq!(Decimal::from(vat), dec!(-0.25));
    assert_eq!(all_warnings(&warnings), vec![&Warning::NegativeVat]);
}

#[test]
fn excessive_precision_lowers_without_a_lowering_warning() {
    let src = VALID.replace(r#""price": 0.25"#, r#""price": 0.12345"#);
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_tariff(&doc);

    let (money, warnings) = Money::from_schema(price(&tariff)).unwrap().into_parts();

    // The value is kept as written. The precision is the schema walk's finding, so the
    // lowering has nothing of its own to say about it.
    assert_eq!(Decimal::from(money), dec!(0.12345));
    let warnings: Vec<&number::Warning> = warnings.path_map().into_values().flatten().collect();
    assert_eq!(warnings, Vec::<&number::Warning>::new());
}

// -- Price (object-shaped, lowered from `schema::v221::Price`) ----------------

/// A minimal, fully valid `v2.2.1` tariff with a `min_price` object the tests lower.
const VALID_V221: &str = r#"{
    "country_code": "NL",
    "currency": "EUR",
    "elements": [
        {"price_components": [{"price": 0.25, "step_size": 1, "type": "ENERGY"}]}
    ],
    "id": "ID",
    "last_updated": "2024-01-01T00:00:00Z",
    "party_id": "ABC",
    "min_price": {"excl_vat": 1.0, "incl_vat": 1.21}
}"#;

/// Build a `v2.2.1` tariff, dropping the schema-level warnings.
fn build_v221_tariff<'buf>(doc: &json::Document<'buf>) -> v221::Tariff<'buf> {
    v221::build_tariff(doc).ignore_warnings()
}

/// Borrow the built `min_price` `Price` object out of a tariff.
fn min_price<'a, 'buf>(tariff: &'a v221::Tariff<'buf>) -> &'a v221::Price<'buf> {
    let Integrity::Ok(Some(price)) = &tariff.min_price else {
        panic!("min_price should be built: {:?}", tariff.min_price);
    };
    price
}

/// Collect every `money::Warning` across all paths into a flat list.
fn all_warnings(warnings: &warning::Set<Warning>) -> Vec<&Warning> {
    warnings.path_map().into_values().flatten().collect()
}

#[test]
fn price_with_excl_and_incl_lowers() {
    let doc = json::parse(VALID_V221.into()).unwrap();
    let tariff = build_v221_tariff(&doc);

    let (price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_eq!(Decimal::from(price.excl_vat), dec!(1.0));
    assert_eq!(price.incl_vat.map(Decimal::from), Some(dec!(1.21)));
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());
}

#[test]
fn a_credit_price_with_both_values_negative_is_consistent() {
    // `mod_cdrs.asciidoc:39` requires a Credit CDR to contain negative totals, and `-5.50`
    // against `-6.6550` is the correct pair at 21%. Reading the rule as `excl_vat` less than or
    // equal to `incl_vat` reports a defect on exactly the shape the spec mandates.
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": {"excl_vat": -5.50, "incl_vat": -6.6550}"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_v221_tariff(&doc);

    let (price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_eq!(Decimal::from(price.excl_vat), dec!(-5.50));
    assert_eq!(price.incl_vat.map(Decimal::from), Some(dec!(-6.6550)));
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());
}

#[test]
fn a_credit_price_whose_inclusive_value_is_smaller_in_magnitude_is_reported() {
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": {"excl_vat": -5.50, "incl_vat": -4.3450}"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_v221_tariff(&doc);

    let (_price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_eq!(
        all_warnings(&warnings),
        vec![&Warning::ImpliedVatIsNegative]
    );
}

#[test]
fn a_price_whose_values_differ_in_sign_is_reported() {
    // The magnitude test alone would accept this pair, since `6.6550` is greater than
    // `5.50`. The sign test is what rejects it, because applying VAT never changes a sign.
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": {"excl_vat": 5.50, "incl_vat": -6.6550}"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_v221_tariff(&doc);

    let (_price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_eq!(
        all_warnings(&warnings),
        vec![&Warning::ImpliedVatIsNegative]
    );
}

#[test]
fn price_with_only_excl_has_no_incl_vat() {
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": {"excl_vat": 1.0}"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_v221_tariff(&doc);

    let (price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_eq!(Decimal::from(price.excl_vat), dec!(1.0));
    assert_eq!(price.incl_vat, None);
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());
}

#[test]
fn excl_greater_than_incl_warns() {
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": {"excl_vat": 2.0, "incl_vat": 1.0}"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let tariff = build_v221_tariff(&doc);

    let (_price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_matches!(all_warnings(&warnings)[..], [Warning::ImpliedVatIsNegative]);
}

#[test]
fn price_from_bare_number_lowers_to_excl_vat_only() {
    // OCPI 2.1.1 wrote a price as a bare number; the schema accepts that shape and puts
    // the number in `excl_vat`, leaving `incl_vat` absent.
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": 1.5"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let (tariff, schema_warnings) = v221::build_tariff(&doc).into_parts();

    let (price, warnings) = Price::from_schema(min_price(&tariff)).unwrap().into_parts();

    assert_eq!(Decimal::from(price.excl_vat), dec!(1.5));
    assert_eq!(price.incl_vat, None);
    assert_eq!(all_warnings(&warnings), Vec::<&Warning>::new());

    let schema_warnings = schema_warnings.path_map();
    assert_matches!(
        *schema_warnings["$.min_price"],
        [schema::Warning::InvalidType {
            expected: _,
            actual: _
        }]
    );
}

#[test]
fn missing_excl_vat_is_rejected() {
    let src = VALID_V221.replace(
        r#""min_price": {"excl_vat": 1.0, "incl_vat": 1.21}"#,
        r#""min_price": {"incl_vat": 1.0}"#,
    );
    let doc = json::parse(src.as_str().into()).unwrap();
    let (tariff, schema_warnings) = v221::build_tariff(&doc).into_parts();

    let err_set = Price::from_schema(min_price(&tariff)).unwrap_err();
    let error = err_set.unwrap();
    let (error, _element) = error.into_parts();

    assert_matches!(error, Warning::Rejected);

    let schema_warnings = schema_warnings.path_map();
    assert_matches!(
        *schema_warnings["$.min_price"],
        [schema::Warning::MissingField { name: "excl_vat" }]
    );
}