Skip to content

Commit

Permalink
restructure test json's (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop22 authored Jun 8, 2023
1 parent e701e1e commit 3d90088
Show file tree
Hide file tree
Showing 31 changed files with 68 additions and 22 deletions.
1 change: 1 addition & 0 deletions ocpi-tariffs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ serde.workspace = true

[dev-dependencies]
serde_json.workspace = true
test-each = { version = "0.2.1" }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
89 changes: 67 additions & 22 deletions ocpi-tariffs/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,75 @@
mod common;
use chrono_tz::Tz;
use ocpi_tariffs::{
ocpi::{cdr::Cdr, tariff::OcpiTariff},
pricer::Pricer,
};
use std::path::PathBuf;

#[test]
fn test_json_files() {
let mut should_panic = false;
#[test_each::file(glob = "ocpi-tariffs/test_data/*/cdr*.json", name(segments = 2))]
fn test_json(cdr: &str, path: PathBuf) {
let tariff = std::fs::read_to_string(path.parent().unwrap().join("tariff.json")).unwrap();

for json_test in common::collect_json_tests().unwrap() {
let tariff = json_test.tariff;
let cdr = serde_json::from_str(cdr).unwrap();
let tariff = serde_json::from_str(&tariff).unwrap();

eprintln!("\ntesting directory {:?}", json_test.path);
validate_cdr(cdr, tariff).unwrap();
}

pub fn validate_cdr(cdr: Cdr, tariff: OcpiTariff) -> Result<(), ocpi_tariffs::Error> {
let pricer = Pricer::with_tariffs(&cdr, &[tariff], Tz::UTC);
let report = pricer.build_report()?;

assert_eq!(
cdr.total_cost,
report.total_cost.unwrap_or_default().with_scale(),
"total_cost"
);

assert_eq!(
cdr.total_energy,
report.total_energy.with_scale(),
"total_energy"
);
assert_eq!(
cdr.total_energy_cost.unwrap_or_default(),
report.total_energy_cost.unwrap_or_default().with_scale(),
"total_energy_cost"
);

assert_eq!(cdr.total_time, report.total_time, "total_time");

assert_eq!(
cdr.total_time_cost.unwrap_or_default(),
report.total_time_cost.unwrap_or_default().with_scale(),
"total_time_cost"
);

assert_eq!(
cdr.total_parking_time.unwrap_or_default(),
report.total_parking_time,
"total_parking_time"
);

for (name, cdr) in json_test.cdrs {
eprint!(" testing json cdr `{}`: ", name);
assert_eq!(
cdr.total_parking_cost.unwrap_or_default(),
report.total_parking_cost.unwrap_or_default().with_scale(),
"total_parking_cost"
);

let result = std::panic::catch_unwind(|| {
common::validate_cdr(cdr, tariff.clone()).unwrap();
});
assert_eq!(
cdr.total_reservation_cost.unwrap_or_default(),
report
.total_reservation_cost
.unwrap_or_default()
.with_scale(),
"total_reservation_cost"
);

if result.is_err() {
should_panic = true;
} else {
eprintln!("success");
}
}
}
assert_eq!(
cdr.total_fixed_cost.unwrap_or_default(),
report.total_fixed_cost.unwrap_or_default().with_scale(),
"total_fixed_cost"
);

if should_panic {
panic!("not all json tests succeeded")
}
Ok(())
}

0 comments on commit 3d90088

Please sign in to comment.