#![allow(
clippy::panic,
clippy::unwrap_used,
reason = "test code with known-structure parsed data"
)]
use crate::{Version, fix, json, tariff, test};
use super::{Fixed, MAX_PASSES, Options, Outcome, UnexpectedFields};
const TARIFF: &str = r#"{
"country_code": "NL",
"party_id": "TDR",
"id": "TEST",
"currency": "EUR",
"elements": [
{
"restrictions": {
"day_of_week": {DAYS}
},
"price_components": [
{
"type": "ENERGY",
"price": 0.25,
"step_size": 1
}
]
}
],
"last_updated": "2018-12-17T11:36:01Z"
}"#;
const TARIFF_RESTRICTIONS: &str = r#"{
"country_code": "NL",
"party_id": "TDR",
"id": "TEST",
"currency": "EUR",
"elements": [
{
"restrictions": {RESTRICTIONS},
"price_components": [
{
"type": "ENERGY",
"price": 0.25,
"step_size": 1
}
]
}
],
"last_updated": "2018-12-17T11:36:01Z"
}"#;
const MISCASED_AND_UNSORTED: &str = r#"["tuesday", "monday"]"#;
#[test]
fn a_fix_that_another_fix_enables_is_reached_by_a_later_pass() {
test::setup();
let fixed = fix(&TARIFF.replace("{DAYS}", MISCASED_AND_UNSORTED), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.passes, 2);
assert_eq!(fixed.edits, 3);
assert!(
fixed.source.contains(r#"["MONDAY", "TUESDAY"]"#),
"{}",
fixed.source
);
}
#[test]
fn a_tariff_with_nothing_to_fix_is_returned_unchanged() {
test::setup();
let source = TARIFF.replace("{DAYS}", r#"["MONDAY", "TUESDAY"]"#);
let fixed = fix(&source, MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.passes, 0);
assert_eq!(fixed.edits, 0);
assert_eq!(fixed.source, source);
}
#[test]
fn the_pass_limit_hands_back_the_last_pass_that_ran() {
test::setup();
let fixed = fix(&TARIFF.replace("{DAYS}", MISCASED_AND_UNSORTED), 1);
assert_eq!(fixed.outcome, Outcome::FixLimit);
assert_eq!(fixed.passes, 1);
assert_eq!(fixed.edits, 2);
assert!(
fixed.source.contains(r#"["TUESDAY", "MONDAY"]"#),
"{}",
fixed.source
);
}
#[test]
fn one_pass_fixes_the_schema_warnings_and_the_lint_warnings_together() {
test::setup();
let source = TARIFF
.replace("{DAYS}", r#"["TUESDAY", "MONDAY", "TUESDAY"]"#)
.replace(
r#""currency": "EUR","#,
"\"currency\": \"EUR\",\n \"tariff_alt_url\": null,",
);
let fixed = fix(&source, MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.passes, 1);
assert!(!fixed.source.contains("tariff_alt_url"), "{}", fixed.source);
assert!(
fixed.source.contains(r#"["MONDAY", "TUESDAY"]"#),
"{}",
fixed.source
);
}
#[test]
fn an_unexpected_field_is_removed_when_that_is_asked_for() {
test::setup();
let fixed = fix(&unexpected_field_tariff(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.edits, 1);
assert!(!fixed.source.contains("surprise"), "{}", fixed.source);
}
#[test]
fn an_unexpected_field_is_left_in_place_by_default() {
test::setup();
let source = unexpected_field_tariff();
let fixed = fix_with(&source, Options::default(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.edits, 0);
assert_eq!(fixed.source, source);
}
#[test]
fn a_misspelled_field_is_kept_when_unexpected_fields_are_removed() {
test::setup();
let source = TARIFF.replace(r#""day_of_week": {DAYS}"#, r#""dayOfWeek": ["MONDAY"]"#);
let fixed = fix(&source, MAX_PASSES);
assert_eq!(fixed.edits, 0);
assert_eq!(fixed.source, source);
}
#[test]
fn a_misspelled_field_beside_a_null_one_is_kept() {
test::setup();
let source = TARIFF.replace(
r#""day_of_week": {DAYS}"#,
r#""day_of_week": null, "dayOfWeek": ["MONDAY"]"#,
);
let fixed = fix(&source, MAX_PASSES);
assert_eq!(fixed.edits, 1);
assert!(
fixed.source.contains(r#""dayOfWeek": ["MONDAY"]"#),
"{}",
fixed.source
);
assert!(!fixed.source.contains("null"), "{}", fixed.source);
}
#[test]
fn a_field_with_a_malformed_name_is_removed_with_the_unexpected_fields() {
test::setup();
let source = TARIFF
.replace("{DAYS}", r#"["MONDAY", "TUESDAY"]"#)
.replace(r#""currency": "EUR","#, r#""currency": "EUR", "x\q": 1,"#);
let fixed = fix(&source, MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.edits, 1);
assert!(!fixed.source.contains(r"x\q"), "{}", fixed.source);
}
#[test]
fn a_field_name_that_needs_an_escape_is_left_in_place() {
test::setup();
let source = TARIFF
.replace("{DAYS}", r#"["MONDAY", "TUESDAY"]"#)
.replace(
r#""currency": "EUR","#,
r#""currency": "EUR", "x\u0022y": 1,"#,
);
let fixed = fix_with(&source, Options::default(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.edits, 0);
assert_eq!(fixed.source, source);
}
#[test]
fn an_object_written_empty_is_removed_in_one_pass() {
test::setup();
let fixed = fix(
&TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", "{}"),
MAX_PASSES,
);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.passes, 1);
assert_eq!(fixed.edits, 1);
assert!(!fixed.source.contains("restrictions"), "{}", fixed.source);
}
#[test]
fn an_object_emptied_by_an_earlier_fix_is_removed_by_a_later_pass() {
test::setup();
let fixed = fix(
&TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "min_kwh": null }"#),
MAX_PASSES,
);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.passes, 2);
assert_eq!(fixed.edits, 2);
assert!(!fixed.source.contains("restrictions"), "{}", fixed.source);
}
#[test]
fn an_edit_that_changes_the_meaning_is_held_back_by_default() {
test::setup();
let tariff = TARIFF.replace("{DAYS}", r#"["MONDAI", "TUESDAY"]"#);
let fixed = fix(&tariff, MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.source, tariff);
assert_eq!(fixed.edits, 0);
assert_eq!(fixed.changes_held_back, 1);
}
#[test]
fn an_edit_that_changes_the_meaning_is_applied_when_that_is_asked_for() {
test::setup();
let tariff = TARIFF.replace("{DAYS}", r#"["MONDAI", "TUESDAY"]"#);
let options = Options::default()
.unexpected_fields(UnexpectedFields::Remove)
.allow_changes();
let fixed = fix_with(&tariff, options, MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(
fixed.source,
TARIFF.replace("{DAYS}", r#"["MONDAY", "TUESDAY"]"#)
);
assert_eq!(fixed.edits, 1);
assert_eq!(fixed.changes_held_back, 0);
}
#[test]
fn a_field_name_both_escaped_and_misspelled_is_renamed_over_two_passes() {
test::setup();
let tariff =
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "day\u004ffWeek": ["MONDAY"] }"#);
let fixed = fix_with(&tariff, allowing_changes(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(
fixed.source,
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "day_of_week": ["MONDAY"] }"#)
);
assert_eq!(fixed.passes, 2);
}
#[test]
fn a_misspelled_field_is_not_renamed_onto_a_key_the_object_keeps() {
test::setup();
let tariff = TARIFF_RESTRICTIONS.replace(
"{RESTRICTIONS}",
r#"{ "dayOfWeek": ["MONDAY"], "day_of_week": null, "day_of_week": null }"#,
);
let fixed = fix_with(&tariff, allowing_changes(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(
fixed.source,
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "day_of_week": ["MONDAY"] }"#)
);
assert_eq!(fixed.refused, vec![]);
}
#[test]
fn a_misspelled_field_takes_the_key_of_a_removed_field_in_one_pass() {
test::setup();
let tariff = TARIFF_RESTRICTIONS.replace(
"{RESTRICTIONS}",
r#"{ "day\u005fof_week": null, "dayOfWeek": ["MONDAY"] }"#,
);
let fixed = fix_with(&tariff, allowing_changes(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(
fixed.source,
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "day_of_week": ["MONDAY"] }"#)
);
assert_eq!(fixed.passes, 1);
}
#[test]
fn a_misspelled_field_holding_null_is_removed_without_allowing_changes() {
test::setup();
let tariff =
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "dayOfWeek": null, "max_kwh": 5 }"#);
let fixed = fix(&tariff, MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(
fixed.source,
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "max_kwh": 5 }"#)
);
assert_eq!(fixed.changes_held_back, 0);
}
#[test]
fn a_misspelled_field_holding_null_does_not_block_its_sibling_rename() {
test::setup();
let tariff = TARIFF_RESTRICTIONS.replace(
"{RESTRICTIONS}",
r#"{ "dayOfWeek": null, "DayOfWeek": ["MONDAY"] }"#,
);
let fixed = fix_with(&tariff, allowing_changes(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(
fixed.source,
TARIFF_RESTRICTIONS.replace("{RESTRICTIONS}", r#"{ "day_of_week": ["MONDAY"] }"#)
);
assert_eq!(fixed.passes, 1);
}
#[test]
fn two_fields_misspelling_one_name_are_both_refused() {
test::setup();
let tariff = TARIFF_RESTRICTIONS.replace(
"{RESTRICTIONS}",
r#"{ "dayOfWeek": ["MONDAY"], "DayOfWeek": ["SUNDAY"] }"#,
);
let fixed = fix_with(&tariff, allowing_changes(), MAX_PASSES);
assert_eq!(fixed.outcome, Outcome::AllFixesApplied);
assert_eq!(fixed.source, tariff);
assert_eq!(fixed.edits, 0);
assert_eq!(fixed.changes_held_back, 0);
assert_eq!(
refused(&fixed),
vec![
(
"$.elements[0].restrictions.dayOfWeek",
"misspelled_field(day_of_week)",
fix::Refusal::RenameClashes
),
(
"$.elements[0].restrictions.DayOfWeek",
"misspelled_field(day_of_week)",
fix::Refusal::RenameClashes
),
]
);
}
#[test]
fn a_refused_rename_is_not_held_back_by_default() {
test::setup();
let tariff = TARIFF_RESTRICTIONS.replace(
"{RESTRICTIONS}",
r#"{ "dayOfWeek": ["MONDAY"], "DayOfWeek": ["SUNDAY"] }"#,
);
let fixed = fix(&tariff, MAX_PASSES);
assert_eq!(fixed.changes_held_back, 0);
assert_eq!(fixed.refused.len(), 2);
}
fn refused(fixed: &Fixed) -> Vec<(&str, &str, fix::Refusal)> {
fixed
.refused
.iter()
.map(|refused| {
(
refused.path.as_str(),
refused.warning.as_str(),
refused.reason,
)
})
.collect()
}
fn allowing_changes() -> Options {
Options::default()
.unexpected_fields(UnexpectedFields::Remove)
.allow_changes()
}
fn unexpected_field_tariff() -> String {
TARIFF
.replace("{DAYS}", r#"["MONDAY", "TUESDAY"]"#)
.replace(
r#""currency": "EUR","#,
"\"currency\": \"EUR\",\n \"surprise\": \"not in the spec\",",
)
}
#[track_caller]
fn fix(json: &str, max_passes: usize) -> Fixed {
fix_with(
json,
Options::default().unexpected_fields(UnexpectedFields::Remove),
max_passes,
)
}
#[track_caller]
fn fix_with(json: &str, options: Options, max_passes: usize) -> Fixed {
let doc = json::parse_object(json).expect("the tariff parses");
let (tariff, _warnings) = tariff::from_json(doc, Version::V221).into_parts();
super::fix_until_no_edits(tariff.as_json_str(), Version::V221, options, max_passes)
.expect("the fixes apply")
}