use super::{OtherElements, Warning};
use crate::{Version, json, tariff, test, warning};
#[test]
fn an_unrestricted_element_may_match_a_session() {
test::setup();
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();
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();
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();
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();
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();
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();
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();
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),
),
],
);
}
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
}