use std::path::Path;
use crate::{
cdr,
price::{
self,
test::{ExpectFields, UnwrapReport},
},
tariff,
test::{self, IntoFields as _},
timezone, ObjectType, Version,
};
#[test_each::file(
glob = "ocpi-tariffs/test_data/v221/real_world/*/cdr*.json",
name(segments = 2)
)]
fn should_price_cdr(cdr_json: &str, path: &Path) {
const VERSION: Version = Version::V221;
test::setup();
let expect_json = test::read_expect_json(path, "price");
let expect = test::parse_expect_json(expect_json.as_deref());
let ExpectFields {
timezone_find_expect,
tariff_parse_expect,
cdr_parse_expect,
cdr_price_expect,
} = expect.into_fields();
let tariff_json = std::fs::read_to_string(path.parent().unwrap().join("tariff.json")).ok();
let tariff = tariff_json
.as_deref()
.map(|json| tariff::parse_with_version(json, VERSION))
.transpose()
.unwrap();
let tariff = tariff
.map(|report| {
let tariff::ParseReport {
tariff,
unexpected_fields,
} = report;
price::test::assert_parse_report(
ObjectType::Tariff,
unexpected_fields,
tariff_parse_expect,
);
price::TariffSource::Override(vec![tariff])
})
.unwrap_or(price::TariffSource::UseCdr);
let report = cdr::parse_with_version(cdr_json, VERSION).unwrap();
let cdr::ParseReport {
cdr,
unexpected_fields,
} = report;
price::test::assert_parse_report(ObjectType::Cdr, unexpected_fields, cdr_parse_expect);
let (timezone_source, warnings) = timezone::find_or_infer(&cdr).unwrap().into_parts();
timezone::test::assert_find_or_infer_outcome(timezone_source, timezone_find_expect, &warnings);
let report =
cdr::price(&cdr, tariff, timezone_source.into_timezone()).unwrap_report(cdr.as_json_str());
price::test::assert_price_report(report, cdr_price_expect);
}